diff --git a/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java index 5b452430b..df529fee0 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java @@ -64,7 +64,7 @@ public class SelectChannelFragment extends DialogFragment { private final ImageLoader imageLoader = ImageLoader.getInstance(); - private OnSelectedLisener onSelectedLisener = null; + private OnSelectedListener onSelectedListener = null; private OnCancelListener onCancelListener = null; private ProgressBar progressBar; @@ -73,8 +73,8 @@ public class SelectChannelFragment extends DialogFragment { private List subscriptions = new Vector<>(); - public void setOnSelectedLisener(final OnSelectedLisener listener) { - onSelectedLisener = listener; + public void setOnSelectedListener(final OnSelectedListener listener) { + onSelectedListener = listener; } public void setOnCancelListener(final OnCancelListener listener) { @@ -129,9 +129,9 @@ public class SelectChannelFragment extends DialogFragment { } private void clickedItem(final int position) { - if (onSelectedLisener != null) { + if (onSelectedListener != null) { SubscriptionEntity entry = subscriptions.get(position); - onSelectedLisener + onSelectedListener .onChannelSelected(entry.getServiceId(), entry.getUrl(), entry.getName()); } dismiss(); @@ -186,7 +186,7 @@ public class SelectChannelFragment extends DialogFragment { // Interfaces //////////////////////////////////////////////////////////////////////////*/ - public interface OnSelectedLisener { + public interface OnSelectedListener { void onChannelSelected(int serviceId, String url, String name); } diff --git a/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java index 4df70ccec..13d34dec8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java @@ -52,11 +52,11 @@ public class SelectKioskFragment extends DialogFragment { private RecyclerView recyclerView = null; private SelectKioskAdapter selectKioskAdapter = null; - private OnSelectedLisener onSelectedLisener = null; + private OnSelectedListener onSelectedListener = null; private OnCancelListener onCancelListener = null; - public void setOnSelectedLisener(final OnSelectedLisener listener) { - onSelectedLisener = listener; + public void setOnSelectedListener(final OnSelectedListener listener) { + onSelectedListener = listener; } public void setOnCancelListener(final OnCancelListener listener) { @@ -102,8 +102,8 @@ public class SelectKioskFragment extends DialogFragment { } private void clickedItem(final SelectKioskAdapter.Entry entry) { - if (onSelectedLisener != null) { - onSelectedLisener.onKioskSelected(entry.serviceId, entry.kioskId, entry.kioskName); + if (onSelectedListener != null) { + onSelectedListener.onKioskSelected(entry.serviceId, entry.kioskId, entry.kioskName); } dismiss(); } @@ -122,7 +122,7 @@ public class SelectKioskFragment extends DialogFragment { // Interfaces //////////////////////////////////////////////////////////////////////////*/ - public interface OnSelectedLisener { + public interface OnSelectedListener { void onKioskSelected(int serviceId, String kioskId, String kioskName); } diff --git a/app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java index dc0b173ed..795ced5fa 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java @@ -35,10 +35,7 @@ import java.util.List; import java.util.Vector; import io.reactivex.Flowable; -import io.reactivex.Observer; -import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; -import io.reactivex.schedulers.Schedulers; public class SelectPlaylistFragment extends DialogFragment { /** @@ -49,17 +46,18 @@ public class SelectPlaylistFragment extends DialogFragment { private final ImageLoader imageLoader = ImageLoader.getInstance(); - private OnSelectedLisener onSelectedLisener = null; + private OnSelectedListener onSelectedListener = null; private OnCancelListener onCancelListener = null; private ProgressBar progressBar; private TextView emptyView; private RecyclerView recyclerView; + private Disposable playlistsSubscriber; private List playlists = new Vector<>(); - public void setOnSelectedLisener(final OnSelectedLisener listener) { - onSelectedLisener = listener; + public void setOnSelectedListener(final OnSelectedListener listener) { + onSelectedListener = listener; } public void setOnCancelListener(final OnCancelListener listener) { @@ -67,13 +65,14 @@ public class SelectPlaylistFragment extends DialogFragment { } /*////////////////////////////////////////////////////////////////////////// - // Init + // Fragment's Lifecycle //////////////////////////////////////////////////////////////////////////*/ @Override public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.select_playlist_fragment, container, false); + final View v = + inflater.inflate(R.layout.select_playlist_fragment, container, false); recyclerView = v.findViewById(R.id.items_list); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter(); @@ -86,19 +85,26 @@ public class SelectPlaylistFragment extends DialogFragment { emptyView.setVisibility(View.GONE); final AppDatabase database = NewPipeDatabase.getInstance(this.getContext()); - LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database); - RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database); + final LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database); + final RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database); - Flowable.combineLatest(localPlaylistManager.getPlaylists(), + playlistsSubscriber = Flowable.combineLatest(localPlaylistManager.getPlaylists(), remotePlaylistManager.getPlaylists(), PlaylistItemsUtils::merge) - .toObservable() - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(getPlaylistsObserver()); + .subscribe(this::displayPlaylists, this::onError); return v; } + @Override + public void onDestroy() { + super.onDestroy(); + + if (playlistsSubscriber != null) { + playlistsSubscriber.dispose(); + playlistsSubscriber = null; + } + } + /*////////////////////////////////////////////////////////////////////////// // Handle actions //////////////////////////////////////////////////////////////////////////*/ @@ -112,17 +118,17 @@ public class SelectPlaylistFragment extends DialogFragment { } private void clickedItem(final int position) { - if (onSelectedLisener != null) { - LocalItem selectedItem = playlists.get(position); + if (onSelectedListener != null) { + final LocalItem selectedItem = playlists.get(position); if (selectedItem instanceof PlaylistMetadataEntry) { final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem); - onSelectedLisener + onSelectedListener .onLocalPlaylistSelected(entry.uid, entry.name); } else if (selectedItem instanceof PlaylistRemoteEntity) { final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem); - onSelectedLisener.onRemotePlaylistSelected( + onSelectedListener.onRemotePlaylistSelected( entry.getServiceId(), entry.getUrl(), entry.getName()); } } @@ -144,26 +150,6 @@ public class SelectPlaylistFragment extends DialogFragment { } - private Observer> getPlaylistsObserver() { - return new Observer>() { - @Override - public void onSubscribe(final Disposable d) { } - - @Override - public void onNext(final List newPlaylists) { - displayPlaylists(newPlaylists); - } - - @Override - public void onError(final Throwable exception) { - SelectPlaylistFragment.this.onError(exception); - } - - @Override - public void onComplete() { } - }; - } - /*////////////////////////////////////////////////////////////////////////// // Error //////////////////////////////////////////////////////////////////////////*/ @@ -178,7 +164,7 @@ public class SelectPlaylistFragment extends DialogFragment { // Interfaces //////////////////////////////////////////////////////////////////////////*/ - public interface OnSelectedLisener { + public interface OnSelectedListener { void onLocalPlaylistSelected(long id, String name); void onRemotePlaylistSelected(int serviceId, String url, String name); } @@ -192,14 +178,14 @@ public class SelectPlaylistFragment extends DialogFragment { @Override public SelectPlaylistItemHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { - View item = LayoutInflater.from(parent.getContext()) + final View item = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_playlist_mini_item, parent, false); return new SelectPlaylistItemHolder(item); } @Override public void onBindViewHolder(final SelectPlaylistItemHolder holder, final int position) { - PlaylistLocalItem selectedItem = playlists.get(position); + final PlaylistLocalItem selectedItem = playlists.get(position); if (selectedItem instanceof PlaylistMetadataEntry) { final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem); diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java index ad8ee043b..1b26cd529 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java @@ -49,7 +49,7 @@ public class ChooseTabsFragment extends Fragment { private TabsManager tabsManager; - private List tabList = new ArrayList<>(); + private final List tabList = new ArrayList<>(); private ChooseTabsFragment.SelectedTabsAdapter selectedTabsAdapter; /*////////////////////////////////////////////////////////////////////////// @@ -79,10 +79,10 @@ public class ChooseTabsFragment extends Fragment { initButton(rootView); - RecyclerView listSelectedTabs = rootView.findViewById(R.id.selectedTabs); + final RecyclerView listSelectedTabs = rootView.findViewById(R.id.selectedTabs); listSelectedTabs.setLayoutManager(new LinearLayoutManager(requireContext())); - ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback()); + final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback()); itemTouchHelper.attachToRecyclerView(listSelectedTabs); selectedTabsAdapter = new SelectedTabsAdapter(requireContext(), itemTouchHelper); @@ -139,7 +139,7 @@ public class ChooseTabsFragment extends Fragment { private void updateTitle() { if (getActivity() instanceof AppCompatActivity) { - ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); + final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); if (actionBar != null) { actionBar.setTitle(R.string.main_page_content); } @@ -202,20 +202,20 @@ public class ChooseTabsFragment extends Fragment { switch (type) { case KIOSK: SelectKioskFragment selectKioskFragment = new SelectKioskFragment(); - selectKioskFragment.setOnSelectedLisener((serviceId, kioskId, kioskName) -> + selectKioskFragment.setOnSelectedListener((serviceId, kioskId, kioskName) -> addTab(new Tab.KioskTab(serviceId, kioskId))); selectKioskFragment.show(requireFragmentManager(), "select_kiosk"); return; case CHANNEL: SelectChannelFragment selectChannelFragment = new SelectChannelFragment(); - selectChannelFragment.setOnSelectedLisener((serviceId, url, name) -> + selectChannelFragment.setOnSelectedListener((serviceId, url, name) -> addTab(new Tab.ChannelTab(serviceId, url, name))); selectChannelFragment.show(requireFragmentManager(), "select_channel"); return; case PLAYLIST: SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment(); - selectPlaylistFragment.setOnSelectedLisener( - new SelectPlaylistFragment.OnSelectedLisener() { + selectPlaylistFragment.setOnSelectedListener( + new SelectPlaylistFragment.OnSelectedListener() { @Override public void onLocalPlaylistSelected(final long id, final String name) { addTab(new Tab.PlaylistTab(id, name)); @@ -360,7 +360,7 @@ public class ChooseTabsFragment extends Fragment { @Override public ChooseTabsFragment.SelectedTabsAdapter.TabViewHolder onCreateViewHolder( @NonNull final ViewGroup parent, final int viewType) { - View view = inflater.inflate(R.layout.list_choose_tabs, parent, false); + final View view = inflater.inflate(R.layout.list_choose_tabs, parent, false); return new ChooseTabsFragment.SelectedTabsAdapter.TabViewHolder(view); } @@ -417,8 +417,8 @@ public class ChooseTabsFragment extends Fragment { .getChannelServiceId()) + "/" + tab.getTabName(requireContext()); break; case PLAYLIST: - int serviceId = ((Tab.PlaylistTab) tab).getPlaylistServiceId(); - String serviceName = serviceId == -1 + final int serviceId = ((Tab.PlaylistTab) tab).getPlaylistServiceId(); + final String serviceName = serviceId == -1 ? getString(R.string.local) : NewPipe.getNameOfService(serviceId); tabName = serviceName + "/" + tab.getTabName(requireContext()); diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java index 28a4e2723..85de8462d 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java @@ -504,11 +504,7 @@ public abstract class Tab { private LocalItemType playlistType; private PlaylistTab() { - this.playlistName = ""; - this.playlistId = -1; - this.playlistType = LocalItemType.PLAYLIST_LOCAL_ITEM; - this.playlistServiceId = -1; - this.playlistUrl = ""; + this(-1, ""); } public PlaylistTab(final long playlistId, final String playlistName) { @@ -545,18 +541,16 @@ public abstract class Tab { @DrawableRes @Override public int getTabIconRes(final Context context) { - return ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_list); + return ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_bookmark); } @Override public Fragment getFragment(final Context context) { if (playlistType == LocalItemType.PLAYLIST_LOCAL_ITEM) { - return LocalPlaylistFragment.getInstance(playlistId, - playlistName == null ? "" : playlistName); + return LocalPlaylistFragment.getInstance(playlistId, playlistName); } else { // playlistType == LocalItemType.PLAYLIST_REMOTE_ITEM - return PlaylistFragment.getInstance(playlistServiceId, playlistUrl, - playlistName == null ? "" : playlistName); + return PlaylistFragment.getInstance(playlistServiceId, playlistUrl, playlistName); } } @@ -583,16 +577,16 @@ public abstract class Tab { @Override public boolean equals(final Object obj) { - boolean baseEqual = super.equals(obj) + final boolean baseEquals = super.equals(obj) && Objects.equals(playlistType, ((PlaylistTab) obj).playlistType) && Objects.equals(playlistName, ((PlaylistTab) obj).playlistName); - if (!baseEqual) { + if (!baseEquals) { return false; } - boolean localPlaylistEquals = playlistId == ((PlaylistTab) obj).playlistId; - boolean remotePlaylistEquals = + final boolean localPlaylistEquals = playlistId == ((PlaylistTab) obj).playlistId; + final boolean remotePlaylistEquals = playlistServiceId == ((PlaylistTab) obj).playlistServiceId && Objects.equals(playlistUrl, ((PlaylistTab) obj).playlistUrl); diff --git a/app/src/main/java/org/schabi/newpipe/util/PlaylistItemsUtils.java b/app/src/main/java/org/schabi/newpipe/util/PlaylistItemsUtils.java index 230be4d28..e5309210c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/PlaylistItemsUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/PlaylistItemsUtils.java @@ -14,7 +14,7 @@ public final class PlaylistItemsUtils { public static List merge( final List localPlaylists, final List remotePlaylists) { - List items = new ArrayList<>( + final List items = new ArrayList<>( localPlaylists.size() + remotePlaylists.size()); items.addAll(localPlaylists); items.addAll(remotePlaylists); diff --git a/app/src/main/res/layout/select_playlist_fragment.xml b/app/src/main/res/layout/select_playlist_fragment.xml index 14462662a..ca0d49e32 100644 --- a/app/src/main/res/layout/select_playlist_fragment.xml +++ b/app/src/main/res/layout/select_playlist_fragment.xml @@ -23,7 +23,9 @@ android:id="@+id/items_list" android:layout_width="match_parent" android:layout_height="wrap_content" - tools:listitem="@layout/list_playlist_mini_item"> + tools:listitem="@layout/list_playlist_mini_item"> + + - \ No newline at end of file +