Fix playlist select dialog and do some refactoring

This commit is contained in:
Stypox 2020-11-02 14:24:39 +01:00
parent e97d0b9a69
commit 1f73572dd3
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.settings; package org.schabi.newpipe.settings;
import android.app.Activity; import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -34,6 +33,7 @@ import java.util.List;
import java.util.Vector; import java.util.Vector;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
public class SelectPlaylistFragment extends DialogFragment { public class SelectPlaylistFragment extends DialogFragment {
@ -46,12 +46,11 @@ public class SelectPlaylistFragment extends DialogFragment {
private final ImageLoader imageLoader = ImageLoader.getInstance(); private final ImageLoader imageLoader = ImageLoader.getInstance();
private OnSelectedListener onSelectedListener = null; private OnSelectedListener onSelectedListener = null;
private OnCancelListener onCancelListener = null;
private ProgressBar progressBar; private ProgressBar progressBar;
private TextView emptyView; private TextView emptyView;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private Disposable playlistsSubscriber; private Disposable disposable = null;
private List<PlaylistLocalItem> playlists = new Vector<>(); private List<PlaylistLocalItem> playlists = new Vector<>();
@ -59,10 +58,6 @@ public class SelectPlaylistFragment extends DialogFragment {
onSelectedListener = listener; onSelectedListener = listener;
} }
public void setOnCancelListener(final OnCancelListener listener) {
onCancelListener = listener;
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Fragment's Lifecycle // Fragment's Lifecycle
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -70,15 +65,32 @@ public class SelectPlaylistFragment extends DialogFragment {
@Override @Override
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container, public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) { final Bundle savedInstanceState) {
final View v = final View v = inflater.inflate(R.layout.select_playlist_fragment, container, false);
inflater.inflate(R.layout.select_playlist_fragment, container, false); progressBar = v.findViewById(R.id.progressBar);
recyclerView = v.findViewById(R.id.items_list); recyclerView = v.findViewById(R.id.items_list);
emptyView = v.findViewById(R.id.empty_state_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
final SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter(); final SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter();
recyclerView.setAdapter(playlistAdapter); recyclerView.setAdapter(playlistAdapter);
progressBar = v.findViewById(R.id.progressBar); loadPlaylists();
emptyView = v.findViewById(R.id.empty_state_view); return v;
}
@Override
public void onDestroy() {
super.onDestroy();
if (disposable != null) {
disposable.dispose();
}
}
/*//////////////////////////////////////////////////////////////////////////
// Load and display playlists
//////////////////////////////////////////////////////////////////////////*/
private void loadPlaylists() {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE); recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.GONE); emptyView.setVisibility(View.GONE);
@ -87,43 +99,36 @@ public class SelectPlaylistFragment extends DialogFragment {
final LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database); final LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database);
final RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database); final RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database);
playlistsSubscriber = Flowable.combineLatest(localPlaylistManager.getPlaylists(), disposable = Flowable.combineLatest(localPlaylistManager.getPlaylists(),
remotePlaylistManager.getPlaylists(), PlaylistLocalItem::merge) remotePlaylistManager.getPlaylists(), PlaylistLocalItem::merge)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::displayPlaylists, this::onError); .subscribe(this::displayPlaylists, this::onError);
return v;
} }
@Override private void displayPlaylists(final List<PlaylistLocalItem> newPlaylists) {
public void onDestroy() { playlists = newPlaylists;
super.onDestroy(); progressBar.setVisibility(View.GONE);
emptyView.setVisibility(newPlaylists.isEmpty() ? View.VISIBLE : View.GONE);
if (playlistsSubscriber != null) { recyclerView.setVisibility(newPlaylists.isEmpty() ? View.GONE : View.VISIBLE);
playlistsSubscriber.dispose();
playlistsSubscriber = null;
} }
protected void onError(final Throwable e) {
final Activity activity = requireActivity();
ErrorActivity.reportError(activity, e, activity.getClass(), null, ErrorActivity.ErrorInfo
.make(UserAction.UI_ERROR, "none", "load_playlists", R.string.app_ui_crash));
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Handle actions // Handle actions
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Override
public void onCancel(final DialogInterface dialogInterface) {
super.onCancel(dialogInterface);
if (onCancelListener != null) {
onCancelListener.onCancel();
}
}
private void clickedItem(final int position) { private void clickedItem(final int position) {
if (onSelectedListener != null) { if (onSelectedListener != null) {
final LocalItem selectedItem = playlists.get(position); final LocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) { if (selectedItem instanceof PlaylistMetadataEntry) {
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem); final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);
onSelectedListener onSelectedListener.onLocalPlaylistSelected(entry.uid, entry.name);
.onLocalPlaylistSelected(entry.uid, entry.name);
} else if (selectedItem instanceof PlaylistRemoteEntity) { } else if (selectedItem instanceof PlaylistRemoteEntity) {
final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem); final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem);
@ -134,31 +139,6 @@ public class SelectPlaylistFragment extends DialogFragment {
dismiss(); dismiss();
} }
/*//////////////////////////////////////////////////////////////////////////
// Item handling
//////////////////////////////////////////////////////////////////////////*/
private void displayPlaylists(final List<PlaylistLocalItem> newPlaylists) {
this.playlists = newPlaylists;
progressBar.setVisibility(View.GONE);
if (newPlaylists.isEmpty()) {
emptyView.setVisibility(View.VISIBLE);
return;
}
recyclerView.setVisibility(View.VISIBLE);
}
/*//////////////////////////////////////////////////////////////////////////
// Error
//////////////////////////////////////////////////////////////////////////*/
protected void onError(final Throwable e) {
final Activity activity = getActivity();
ErrorActivity.reportError(activity, e, activity.getClass(), null, ErrorActivity.ErrorInfo
.make(UserAction.UI_ERROR, "none", "", R.string.app_ui_crash));
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Interfaces // Interfaces
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -168,12 +148,9 @@ public class SelectPlaylistFragment extends DialogFragment {
void onRemotePlaylistSelected(int serviceId, String url, String name); void onRemotePlaylistSelected(int serviceId, String url, String name);
} }
public interface OnCancelListener {
void onCancel();
}
private class SelectPlaylistAdapter private class SelectPlaylistAdapter
extends RecyclerView.Adapter<SelectPlaylistAdapter.SelectPlaylistItemHolder> { extends RecyclerView.Adapter<SelectPlaylistAdapter.SelectPlaylistItemHolder> {
@NonNull
@Override @Override
public SelectPlaylistItemHolder onCreateViewHolder(final ViewGroup parent, public SelectPlaylistItemHolder onCreateViewHolder(final ViewGroup parent,
final int viewType) { final int viewType) {
@ -183,7 +160,8 @@ public class SelectPlaylistFragment extends DialogFragment {
} }
@Override @Override
public void onBindViewHolder(final SelectPlaylistItemHolder holder, final int position) { public void onBindViewHolder(@NonNull final SelectPlaylistItemHolder holder,
final int position) {
final PlaylistLocalItem selectedItem = playlists.get(position); final PlaylistLocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) { if (selectedItem instanceof PlaylistMetadataEntry) {