diff --git a/app/src/main/java/free/rm/skytube/gui/fragments/ChannelPlaylistsFragment.java b/app/src/main/java/free/rm/skytube/gui/fragments/ChannelPlaylistsFragment.java index 67bd4bd86..87902c309 100644 --- a/app/src/main/java/free/rm/skytube/gui/fragments/ChannelPlaylistsFragment.java +++ b/app/src/main/java/free/rm/skytube/gui/fragments/ChannelPlaylistsFragment.java @@ -26,73 +26,72 @@ * A fragment that displays the Playlists belonging to a Channel */ public class ChannelPlaylistsFragment extends VideosGridFragment implements PlaylistClickListener, SwipeRefreshLayout.OnRefreshListener { - private PlaylistsGridAdapter playlistsGridAdapter; - private MainActivityListener mainActivityListener; - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = super.onCreateView(inflater, container, savedInstanceState); - - - if (playlistsGridAdapter == null) { - playlistsGridAdapter = new PlaylistsGridAdapter(getActivity(), this); - } else { - playlistsGridAdapter.setContext(getActivity()); - } - - YouTubeChannel channel = (YouTubeChannel) requireArguments() - .getSerializable(ChannelBrowserFragment.CHANNEL_OBJ); - playlistsGridAdapter.setFetcher(createFetcher(channel)); - - gridviewBinding.swipeRefreshLayout.setOnRefreshListener(this); - - gridviewBinding.gridView.setAdapter(playlistsGridAdapter); - - return view; - } - - private YouTubeTasks.ChannelPlaylistFetcher createFetcher(YouTubeChannel channel) { - if (SkyTubeApp.getSettings().isUseNewPipe()) { - return new GetPlaylistsForChannel(channel); - } else { - return new LegacyGetChannelPlaylists(channel); - } - } - - @Override - public void onDestroy() { - playlistsGridAdapter.clearBackgroundTasks(); - super.onDestroy(); - } - - @Override - public String getFragmentName() { - return SkyTubeApp.getStr(R.string.playlists); - } - - @Override - public void onClickPlaylist(YouTubePlaylist playlist) { - if(mainActivityListener != null) - mainActivityListener.onPlaylistClick(playlist); - } - - public void setMainActivityListener(MainActivityListener mainActivityListener) { - this.mainActivityListener = mainActivityListener; - } - - @Override - public void onRefresh() { - playlistsGridAdapter.refresh(youTubePlaylists -> gridviewBinding.swipeRefreshLayout.setRefreshing(false)); - } - - @Override - protected VideoCategory getVideoCategory() { - return null; - } - - @Override - public int getPriority() { - return 6; - } + private PlaylistsGridAdapter playlistsGridAdapter; + private MainActivityListener mainActivityListener; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = super.onCreateView(inflater, container, savedInstanceState); + + + if (playlistsGridAdapter == null) { + playlistsGridAdapter = new PlaylistsGridAdapter(getActivity(), this); + } else { + playlistsGridAdapter.setContext(getActivity()); + } + + YouTubeChannel channel = (YouTubeChannel) requireArguments() + .getSerializable(ChannelBrowserFragment.CHANNEL_OBJ); + playlistsGridAdapter.setFetcher(createFetcher(channel)); + + gridviewBinding.gridView.setAdapter(playlistsGridAdapter); + + return view; + } + + private YouTubeTasks.ChannelPlaylistFetcher createFetcher(YouTubeChannel channel) { + if (SkyTubeApp.getSettings().isUseNewPipe()) { + return new GetPlaylistsForChannel(channel); + } else { + return new LegacyGetChannelPlaylists(channel); + } + } + + @Override + public void onDestroy() { + playlistsGridAdapter.clearBackgroundTasks(); + super.onDestroy(); + } + + @Override + public String getFragmentName() { + return SkyTubeApp.getStr(R.string.playlists); + } + + @Override + public void onClickPlaylist(YouTubePlaylist playlist) { + if(mainActivityListener != null) { + mainActivityListener.onPlaylistClick(playlist); + } + } + + public void setMainActivityListener(MainActivityListener mainActivityListener) { + this.mainActivityListener = mainActivityListener; + } + + @Override + public void onRefresh() { + playlistsGridAdapter.refresh(youTubePlaylists -> gridviewBinding.swipeRefreshLayout.setRefreshing(false)); + } + + @Override + protected VideoCategory getVideoCategory() { + return null; + } + + @Override + public int getPriority() { + return 6; + } } diff --git a/app/src/main/java/free/rm/skytube/gui/fragments/SubscriptionsFeedFragment.java b/app/src/main/java/free/rm/skytube/gui/fragments/SubscriptionsFeedFragment.java index 43090d3c1..cfa0273df 100644 --- a/app/src/main/java/free/rm/skytube/gui/fragments/SubscriptionsFeedFragment.java +++ b/app/src/main/java/free/rm/skytube/gui/fragments/SubscriptionsFeedFragment.java @@ -49,32 +49,32 @@ * Fragment that displays subscriptions videos feed from all channels the user is subscribed to. */ public class SubscriptionsFeedFragment extends VideosGridFragment implements GetSubscriptionVideosTaskListener { - private SubscriptionsBackupsManager subscriptionsBackupsManager; + private SubscriptionsBackupsManager subscriptionsBackupsManager; - /** - * BroadcastReceiver that will receive a message that new subscription videos have been found by the - * {@link FeedUpdaterService}. The video grid will be updated when this happens. - */ - private BroadcastReceiver feedUpdaterReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - refreshFeedFromCache(); - } - }; + /** + * BroadcastReceiver that will receive a message that new subscription videos have been found by the + * {@link FeedUpdaterService}. The video grid will be updated when this happens. + */ + private BroadcastReceiver feedUpdaterReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + refreshFeedFromCache(); + } + }; - private FragmentSubsFeedBinding binding; + private FragmentSubsFeedBinding binding; - private final CompositeDisposable compositeDisposable = new CompositeDisposable(); - private MaterialDialog fetchingChannelInfoDialog; + private final CompositeDisposable compositeDisposable = new CompositeDisposable(); + private MaterialDialog fetchingChannelInfoDialog; - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - subscriptionsBackupsManager = new SubscriptionsBackupsManager(getActivity(), SubscriptionsFeedFragment.this); + subscriptionsBackupsManager = new SubscriptionsBackupsManager(getActivity(), SubscriptionsFeedFragment.this); - EventBus.getInstance().registerSubscriptionListener(this); - } + EventBus.getInstance().registerSubscriptionListener(this); + } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -91,45 +91,44 @@ private void init(Context context, FragmentSubsFeedBinding bindingParam) { videoGridAdapter.setVideoGridUpdated(this::setupUiAccordingToNumOfSubbedChannels); // get the previously published videos currently cached in the database videoGridAdapter.setVideoCategory(VideoCategory.SUBSCRIPTIONS_FEED_VIDEOS); - binding.videosGridview.swipeRefreshLayout.setOnRefreshListener(this); } - @Override - public void onResume() { - requireActivity().registerReceiver(feedUpdaterReceiver, new IntentFilter(FeedUpdaterService.NEW_SUBSCRIPTION_VIDEOS_FOUND)); - - super.onResume(); - - // setup the UI and refresh the feed (if applicable) - Settings settings = SkyTubeApp.getSettings(); - startRefreshTask(isFragmentSelected(), settings.isFullRefreshTimely() || settings.isRefreshSubsFeedFull()); - - // this will detect whether we have previous instructed the app (via refreshSubsFeedFromCache()) - // to refresh the subs feed - if (settings.isRefreshSubsFeedFromCache()) { - // unset the flag - settings.setRefreshSubsFeedFromCache(false); - - // refresh the subs feed by reading from the cache (i.e. local DB) - refreshFeedFromCache(); - } - } - - @Override - public synchronized void onPause() { - hideFetchingVideosDialog(); - super.onPause(); - requireActivity().unregisterReceiver(feedUpdaterReceiver); - } - - @Override - public void onDestroy() { - subscriptionsBackupsManager.clearBackgroundTasks(); - compositeDisposable.clear(); - subscriptionsBackupsManager = null; - EventBus.getInstance().unregisterSubscriptionListener(this); - super.onDestroy(); - } + @Override + public void onResume() { + requireActivity().registerReceiver(feedUpdaterReceiver, new IntentFilter(FeedUpdaterService.NEW_SUBSCRIPTION_VIDEOS_FOUND)); + + super.onResume(); + + // setup the UI and refresh the feed (if applicable) + Settings settings = SkyTubeApp.getSettings(); + startRefreshTask(isFragmentSelected(), settings.isFullRefreshTimely() || settings.isRefreshSubsFeedFull()); + + // this will detect whether we have previous instructed the app (via refreshSubsFeedFromCache()) + // to refresh the subs feed + if (settings.isRefreshSubsFeedFromCache()) { + // unset the flag + settings.setRefreshSubsFeedFromCache(false); + + // refresh the subs feed by reading from the cache (i.e. local DB) + refreshFeedFromCache(); + } + } + + @Override + public synchronized void onPause() { + hideFetchingVideosDialog(); + super.onPause(); + requireActivity().unregisterReceiver(feedUpdaterReceiver); + } + + @Override + public void onDestroy() { + subscriptionsBackupsManager.clearBackgroundTasks(); + compositeDisposable.clear(); + subscriptionsBackupsManager = null; + EventBus.getInstance().unregisterSubscriptionListener(this); + super.onDestroy(); + } @Override public void onDestroyView() { @@ -137,123 +136,123 @@ public void onDestroyView() { super.onDestroyView(); } - @Override - public void onRefresh() { - startRefreshTask(false, true); - } - - - private synchronized void startRefreshTask(boolean isShowFetchingVideosDialog, boolean forcedFullRefresh) { - FeedUpdateTask fut = FeedUpdateTask.getInstance(); - if (fut.isRefreshInProgress()) { - if (isShowFetchingVideosDialog) { - showFetchingVideosDialog(); - } - return; - } - if (forcedFullRefresh && SkyTubeApp.isConnected(requireContext())) { - if (isShowFetchingVideosDialog) { - showFetchingVideosDialog(); - } - - fut.start(requireContext()); - - } else { - videoGridAdapter.refresh(true); - } - } - - @Override - public void onSubscriptionRefreshStarted() { - if (gridviewBinding.swipeRefreshLayout != null) { - gridviewBinding.swipeRefreshLayout.setRefreshing(true); - } - } - - @Override - public void onChannelsFound(boolean hasChannels) { - setupUiAccordingToNumOfSubbedChannels(hasChannels); - } - - @Override - public void onSubscriptionRefreshFinished() { - // Remove the progress bar(s) - if (gridviewBinding.swipeRefreshLayout != null) { - gridviewBinding.swipeRefreshLayout.setRefreshing(false); - } - hideFetchingVideosDialog(); - } - - @Override - public void onChannelVideoFetchFinish(boolean changes) { - // refresh the subs feed by reading from the cache (i.e. local DB) - if (changes) { - refreshFeedFromCache(); - } - } - - @Override - protected VideoCategory getVideoCategory() { - return VideoCategory.SUBSCRIPTIONS_FEED_VIDEOS; - } - - @Override - public String getFragmentName() { - return SkyTubeApp.getStr(R.string.feed); - } - - @Override - public int getPriority() { - return 2; - } - - @Override - public String getBundleKey() { - return MainFragment.SUBSCRIPTIONS_FEED_FRAGMENT; - } - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - subscriptionsBackupsManager.onRequestPermissionsResult(requestCode, permissions, grantResults); - } - - /** - * Set up the UI depending to the total number of channel the user is subscribed to. - * - * @param hasChannels If the user has already subscribed to at least one channel. - */ - private void setupUiAccordingToNumOfSubbedChannels(boolean hasChannels) { - final SwipeRefreshLayout swipeRefreshLayout = gridviewBinding.swipeRefreshLayout; - if (hasChannels) { - if (swipeRefreshLayout.getVisibility() != View.VISIBLE) { - swipeRefreshLayout.setVisibility(View.VISIBLE); - binding.noSubscriptionsText.setVisibility(View.GONE); - } - } else { - swipeRefreshLayout.setVisibility(View.GONE); - binding.noSubscriptionsText.setVisibility(View.VISIBLE); - } - } - - private synchronized void hideFetchingVideosDialog() { - if (fetchingChannelInfoDialog != null) { - fetchingChannelInfoDialog.dismiss(); - fetchingChannelInfoDialog = null; - } - } - - private synchronized void showFetchingVideosDialog() { - hideFetchingVideosDialog(); - fetchingChannelInfoDialog = new MaterialDialog.Builder(getActivity()) - .content(R.string.fetching_subbed_channels_info) - .progress(true, 0) - .build(); - fetchingChannelInfoDialog.show(); - } - - public void refreshFeedFromCache() { - if (videoGridAdapter != null) { - videoGridAdapter.refresh(true); - } - } + @Override + public void onRefresh() { + startRefreshTask(false, true); + } + + + private synchronized void startRefreshTask(boolean isShowFetchingVideosDialog, boolean forcedFullRefresh) { + FeedUpdateTask fut = FeedUpdateTask.getInstance(); + if (fut.isRefreshInProgress()) { + if (isShowFetchingVideosDialog) { + showFetchingVideosDialog(); + } + return; + } + if (forcedFullRefresh && SkyTubeApp.isConnected(requireContext())) { + if (isShowFetchingVideosDialog) { + showFetchingVideosDialog(); + } + + fut.start(requireContext()); + + } else { + videoGridAdapter.refresh(true); + } + } + + @Override + public void onSubscriptionRefreshStarted() { + if (gridviewBinding.swipeRefreshLayout != null) { + gridviewBinding.swipeRefreshLayout.setRefreshing(true); + } + } + + @Override + public void onChannelsFound(boolean hasChannels) { + setupUiAccordingToNumOfSubbedChannels(hasChannels); + } + + @Override + public void onSubscriptionRefreshFinished() { + // Remove the progress bar(s) + if (gridviewBinding.swipeRefreshLayout != null) { + gridviewBinding.swipeRefreshLayout.setRefreshing(false); + } + hideFetchingVideosDialog(); + } + + @Override + public void onChannelVideoFetchFinish(boolean changes) { + // refresh the subs feed by reading from the cache (i.e. local DB) + if (changes) { + refreshFeedFromCache(); + } + } + + @Override + protected VideoCategory getVideoCategory() { + return VideoCategory.SUBSCRIPTIONS_FEED_VIDEOS; + } + + @Override + public String getFragmentName() { + return SkyTubeApp.getStr(R.string.feed); + } + + @Override + public int getPriority() { + return 2; + } + + @Override + public String getBundleKey() { + return MainFragment.SUBSCRIPTIONS_FEED_FRAGMENT; + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + subscriptionsBackupsManager.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + + /** + * Set up the UI depending to the total number of channel the user is subscribed to. + * + * @param hasChannels If the user has already subscribed to at least one channel. + */ + private void setupUiAccordingToNumOfSubbedChannels(boolean hasChannels) { + final SwipeRefreshLayout swipeRefreshLayout = gridviewBinding.swipeRefreshLayout; + if (hasChannels) { + if (swipeRefreshLayout.getVisibility() != View.VISIBLE) { + swipeRefreshLayout.setVisibility(View.VISIBLE); + binding.noSubscriptionsText.setVisibility(View.GONE); + } + } else { + swipeRefreshLayout.setVisibility(View.GONE); + binding.noSubscriptionsText.setVisibility(View.VISIBLE); + } + } + + private synchronized void hideFetchingVideosDialog() { + if (fetchingChannelInfoDialog != null) { + fetchingChannelInfoDialog.dismiss(); + fetchingChannelInfoDialog = null; + } + } + + private synchronized void showFetchingVideosDialog() { + hideFetchingVideosDialog(); + fetchingChannelInfoDialog = new MaterialDialog.Builder(getActivity()) + .content(R.string.fetching_subbed_channels_info) + .progress(true, 0) + .build(); + fetchingChannelInfoDialog.show(); + } + + public void refreshFeedFromCache() { + if (videoGridAdapter != null) { + videoGridAdapter.refresh(true); + } + } } diff --git a/app/src/main/java/free/rm/skytube/gui/fragments/VideosGridFragment.java b/app/src/main/java/free/rm/skytube/gui/fragments/VideosGridFragment.java index e675d7e6e..2164188b9 100644 --- a/app/src/main/java/free/rm/skytube/gui/fragments/VideosGridFragment.java +++ b/app/src/main/java/free/rm/skytube/gui/fragments/VideosGridFragment.java @@ -60,50 +60,51 @@ protected void initVideos(@NonNull Context context, VideoGridAdapter videoGridAd videoGridAdapter.setSwipeRefreshLayout(gridviewBindingParam.swipeRefreshLayout); VideoCategory category = getVideoCategory(); - if (category != null) + if (category != null) { videoGridAdapter.setVideoCategory(category, getSearchString()); - + } videoGridAdapter.setListener((MainActivityListener)getActivity()); gridviewBinding.gridView.setHasFixedSize(true); gridviewBinding.gridView.setLayoutManager(new GridLayoutManager(getActivity(), getResources().getInteger(R.integer.video_grid_num_columns))); gridviewBinding.gridView.setAdapter(videoGridAdapter); + gridviewBinding.swipeRefreshLayout.setOnRefreshListener(this); } - @Override - public void onDestroyView() { - gridviewBinding.gridView.setAdapter(null); - gridviewBinding = null; - super.onDestroyView(); - Glide.get(requireContext()).clearMemory(); - } + @Override + public void onDestroyView() { + gridviewBinding.gridView.setAdapter(null); + gridviewBinding = null; + super.onDestroyView(); + Glide.get(requireContext()).clearMemory(); + } void scrollToTop() { gridviewBinding.gridView.smoothScrollToPosition(0); } - /** - * @return Returns the category of videos being displayed by this fragment. - */ - protected abstract VideoCategory getVideoCategory(); - - /** - * @return Returns the search string used when setting the video category. (Can be used to - * set the channel ID in case of VideoCategory.CHANNEL_VIDEOS). - */ - protected String getSearchString() { - return null; - } - - /** - * @return The fragment/tab name/title. - */ - public abstract String getFragmentName(); - - public abstract int getPriority(); - - public String getBundleKey() { - return null; - } + /** + * @return Returns the category of videos being displayed by this fragment. + */ + protected abstract VideoCategory getVideoCategory(); + + /** + * @return Returns the search string used when setting the video category. (Can be used to + * set the channel ID in case of VideoCategory.CHANNEL_VIDEOS). + */ + protected String getSearchString() { + return null; + } + + /** + * @return The fragment/tab name/title. + */ + public abstract String getFragmentName(); + + public abstract int getPriority(); + + public String getBundleKey() { + return null; + } }