Refactoring - Removed most of the click listeners from the adapters and moved them into the appropriate fragments

This commit is contained in:
antonio 2023-01-04 09:14:15 +01:00
parent 29f56945c2
commit 754fc69eab
54 changed files with 1143 additions and 1137 deletions

View file

@ -2,17 +2,16 @@ package com.cappielloantonio.play.adapter;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.session.MediaBrowser;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
@ -22,42 +21,36 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.glide.CustomGlideRequest;
import com.cappielloantonio.play.interfaces.MediaCallback;
import com.cappielloantonio.play.interfaces.ClickCallback;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.service.MediaManager;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.util.MusicUtil;
import com.google.android.material.snackbar.Snackbar;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@UnstableApi
public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.ViewHolder> {
private static final String TAG = "ArtistAdapter";
private final LayoutInflater inflater;
private final MainActivity mainActivity;
private final Context context;
private final ClickCallback click;
private final boolean mix;
private List<Artist> artists;
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
public ArtistAdapter(MainActivity mainActivity, Context context) {
this.mainActivity = mainActivity;
public ArtistAdapter(Context context, ClickCallback click, Boolean mix) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.artists = new ArrayList<>();
this.click = click;
this.mix = mix;
this.artists = Collections.emptyList();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_library_artist, parent, false);
View view = LayoutInflater.from(context).inflate(R.layout.item_library_artist, parent, false);
return new ViewHolder(view);
}
@ -94,10 +87,6 @@ public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.ViewHolder
return position;
}
public void setMediaBrowserListenableFuture(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture) {
this.mediaBrowserListenableFuture = mediaBrowserListenableFuture;
}
private void setArtistCover(Artist artist, ImageView cover) {
ArtistRepository artistRepository = new ArtistRepository(App.getInstance());
LiveData<Artist> livedata = artistRepository.getArtistFullInfo(artist.getId());
@ -115,7 +104,7 @@ public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.ViewHolder
});
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textArtistName;
ImageView cover;
@ -125,56 +114,27 @@ public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.ViewHolder
textArtistName = itemView.findViewById(R.id.artist_name_label);
cover = itemView.findViewById(R.id.artist_cover_image_view);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
textArtistName.setSelected(true);
itemView.setOnClickListener(v -> onClick());
itemView.setOnLongClickListener(v -> onLongClick());
}
@Override
public void onClick(View view) {
public void onClick() {
Bundle bundle = new Bundle();
bundle.putParcelable("artist_object", artists.get(getBindingAdapterPosition()));
bundle.putBoolean("is_mix", mix);
click.onArtistClick(bundle);
}
public boolean onLongClick() {
Bundle bundle = new Bundle();
bundle.putParcelable("artist_object", artists.get(getBindingAdapterPosition()));
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.libraryFragment) {
Navigation.findNavController(view).navigate(R.id.action_libraryFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.artistCatalogueFragment) {
Navigation.findNavController(view).navigate(R.id.action_artistCatalogueFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.homeFragment) {
Snackbar.make(mainActivity.bind.getRoot(), R.string.artist_adapter_radio_station_starting, Snackbar.LENGTH_LONG)
.setAnchorView(mainActivity.bind.playerBottomSheet)
.show();
click.onArtistLongClick(bundle);
if (mediaBrowserListenableFuture != null) {
ArtistRepository artistRepository = new ArtistRepository(App.getInstance());
artistRepository.getInstantMix(artists.get(getBindingAdapterPosition()), 20, new MediaCallback() {
@Override
public void onError(Exception exception) {
Log.e(TAG, "onError() " + exception.getMessage());
}
@Override
public void onLoadMedia(List<?> media) {
if (media.size() > 0) {
MediaManager.startQueue(mediaBrowserListenableFuture, context, (ArrayList<Media>) media, 0);
mainActivity.setBottomSheetInPeek(true);
} else {
Toast.makeText(context, context.getString(R.string.artist_error_retrieving_radio), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
@Override
public boolean onLongClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("artist_object", artists.get(getBindingAdapterPosition()));
Navigation.findNavController(v).navigate(R.id.artistBottomSheetDialog, bundle);
return true;
return false;
}
}
}