From 10fead27749657b883c1ab8c362343e774fc2083 Mon Sep 17 00:00:00 2001 From: Zsombor Gegesy Date: Sat, 10 Aug 2024 00:00:27 +0200 Subject: [PATCH] Fix unsubscribe to delete old videos --- .../YouTube/POJOs/YouTubeChannel.java | 33 ------------------- .../businessobjects/YouTube/YouTubeTasks.java | 9 ----- .../YouTube/newpipe/NewPipeService.java | 1 - .../businessobjects/db/DatabaseTasks.java | 7 +++- .../businessobjects/db/SubscriptionsDb.java | 4 +-- .../views/ChannelActionHandler.java | 2 +- .../views/SubscribeButton.java | 4 --- 7 files changed, 9 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/POJOs/YouTubeChannel.java b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/POJOs/YouTubeChannel.java index 3edc70184..67651aac9 100644 --- a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/POJOs/YouTubeChannel.java +++ b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/POJOs/YouTubeChannel.java @@ -286,39 +286,6 @@ private Single removeDeniedChannel(boolean displayToastMessage) { }); } - public static Disposable subscribeChannel(final Context context, final ChannelId channelId) { - if (channelId != null) { - return DatabaseTasks.getChannelInfo(context, channelId, false) - .observeOn(Schedulers.io()) - .map(persistentChannel -> - new Pair<>(persistentChannel, SubscriptionsDb.getSubscriptionsDb().subscribe(persistentChannel, Collections.emptyList())) - ) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(youTubeChannelWithResult -> { - switch(youTubeChannelWithResult.second) { - case SUCCESS: { - youTubeChannelWithResult.first.channel.setUserSubscribed(true); - EventBus.getInstance().notifyMainTabChanged(EventBus.SettingChange.SUBSCRIPTION_LIST_CHANGED); - SkyTubeApp.getSettings().setRefreshSubsFeedFromCache(true); - Toast.makeText(context, R.string.channel_subscribed, Toast.LENGTH_LONG).show(); - break; - } - case NOT_MODIFIED: { - Toast.makeText(context, R.string.channel_already_subscribed, Toast.LENGTH_LONG).show(); - break; - } - default: { - Toast.makeText(context, R.string.channel_subscribe_failed, Toast.LENGTH_LONG).show(); - break; - } - } - }); - } else { - Toast.makeText(context, "Channel is not specified", Toast.LENGTH_LONG).show(); - return Disposable.empty(); - } - } - public String getChannelUrl() { return getChannelId().toURL(); } diff --git a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/YouTubeTasks.java b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/YouTubeTasks.java index a9b2b3d85..fb1f4db49 100644 --- a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/YouTubeTasks.java +++ b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/YouTubeTasks.java @@ -89,15 +89,6 @@ public static Single refreshAllSubscriptions(Context context, @Nullable }); } - public static Single refreshSubscribedChannel(ChannelId channelId, @Nullable Consumer newVideosFound) { - if (SkyTubeApp.getSettings().isUseNewPipe() || !YouTubeAPIKey.get().isUserApiKeySet()) { - return YouTubeTasks.getBulkSubscriptionVideos(Collections.singletonList(channelId), newVideosFound); - } else { - return YouTubeTasks.getChannelVideos(channelId, null, false, newVideosFound) - .map(items -> items.size()); - } - } - private static Single refreshSubscriptions(@NonNull List channelIds, @Nullable Consumer newVideosFound) { if (SkyTubeApp.getSettings().isUseNewPipe() || !YouTubeAPIKey.get().isUserApiKeySet()) { return YouTubeTasks.getBulkSubscriptionVideos(channelIds, newVideosFound); diff --git a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/newpipe/NewPipeService.java b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/newpipe/NewPipeService.java index 8c73bd0f3..69c137983 100644 --- a/app/src/main/java/free/rm/skytube/businessobjects/YouTube/newpipe/NewPipeService.java +++ b/app/src/main/java/free/rm/skytube/businessobjects/YouTube/newpipe/NewPipeService.java @@ -322,7 +322,6 @@ public PersistentChannel getChannelDetails(ChannelId channelId, PersistentChanne Logger.i(this, "Fetching channel details for " + channelId); VideoPagerWithChannel pager = getChannelPager(channelId.getRawId()); // get the channel, and add all the videos from the first page - YouTubeChannel channel = pager.getChannel(); try { return pager.getNextPageAsVideosAndUpdateChannel(persistentChannel); } catch (NewPipeException e) { diff --git a/app/src/main/java/free/rm/skytube/businessobjects/db/DatabaseTasks.java b/app/src/main/java/free/rm/skytube/businessobjects/db/DatabaseTasks.java index ed0ee533e..c951a325e 100644 --- a/app/src/main/java/free/rm/skytube/businessobjects/db/DatabaseTasks.java +++ b/app/src/main/java/free/rm/skytube/businessobjects/db/DatabaseTasks.java @@ -166,7 +166,12 @@ public static Single> subscribeToChannel return Single.fromCallable(() -> { PersistentChannel channel = DatabaseTasks.getChannelOrRefresh(context, channelId, true); SubscriptionsDb db = SubscriptionsDb.getSubscriptionsDb(); - DatabaseResult result = subscribeToChannel ? db.subscribe(channel, Collections.emptyList()) : db.unsubscribe(channel); + final DatabaseResult result; + if (subscribeToChannel) { + result = db.subscribe(channel, channel.channel().getYouTubeVideos()); + } else { + result = db.unsubscribe(channel); + } return Pair.create(channel, result); }) .subscribeOn(Schedulers.io()) diff --git a/app/src/main/java/free/rm/skytube/businessobjects/db/SubscriptionsDb.java b/app/src/main/java/free/rm/skytube/businessobjects/db/SubscriptionsDb.java index d4c040a17..2f07e9e1f 100644 --- a/app/src/main/java/free/rm/skytube/businessobjects/db/SubscriptionsDb.java +++ b/app/src/main/java/free/rm/skytube/businessobjects/db/SubscriptionsDb.java @@ -344,11 +344,11 @@ private DatabaseResult saveSubscription(ChannelId channelId) { */ public DatabaseResult unsubscribe(PersistentChannel channel) { SkyTubeApp.nonUiThread(); - + Logger.i(this, "unsubscribing subs_id= %s, channel_id = %s, channel_pk = %s", channel.subscriptionPk(), channel.channel().getChannelId(), channel.channelPk()); // delete any feed videos pertaining to this channel getWritableDatabase().delete(SubscriptionsVideosTable.TABLE_NAME_V2, SubscriptionsVideosTable.COL_SUBS_ID.name() + " = ?", - toArray(channel.channelPk())); + toArray(channel.subscriptionPk())); // remove this channel from the subscriptions DB int rowsDeleted = getWritableDatabase().delete(SubscriptionsTable.TABLE_NAME, diff --git a/app/src/main/java/free/rm/skytube/gui/businessobjects/views/ChannelActionHandler.java b/app/src/main/java/free/rm/skytube/gui/businessobjects/views/ChannelActionHandler.java index 939d8cb49..14539f283 100644 --- a/app/src/main/java/free/rm/skytube/gui/businessobjects/views/ChannelActionHandler.java +++ b/app/src/main/java/free/rm/skytube/gui/businessobjects/views/ChannelActionHandler.java @@ -41,7 +41,7 @@ public ChannelActionHandler(CompositeDisposable compositeDisposable) { public boolean handleChannelActions(Context context, YouTubeChannel channel, int itemId) { switch (itemId) { case R.id.subscribe_channel: - compositeDisposable.add(YouTubeChannel.subscribeChannel(context, channel.getChannelId())); + compositeDisposable.add(DatabaseTasks.subscribeToChannel(true, null, context, channel.getChannelId(), true).subscribe()); return true; case R.id.unsubscribe_channel: compositeDisposable.add(DatabaseTasks.subscribeToChannel(false, diff --git a/app/src/main/java/free/rm/skytube/gui/businessobjects/views/SubscribeButton.java b/app/src/main/java/free/rm/skytube/gui/businessobjects/views/SubscribeButton.java index f642eb095..cb0528951 100644 --- a/app/src/main/java/free/rm/skytube/gui/businessobjects/views/SubscribeButton.java +++ b/app/src/main/java/free/rm/skytube/gui/businessobjects/views/SubscribeButton.java @@ -59,10 +59,6 @@ public void onClick(View view) { externalClickListener.onClick(SubscribeButton.this); } if(channel != null) { - // Only fetch videos for this channel if fetchChannelVideosOnSubscribe is true AND the channel is not subscribed to yet. - if (!isUserSubscribed) { - compositeDisposable.add(YouTubeTasks.refreshSubscribedChannel(channel.getChannelId(), null).subscribe()); - } compositeDisposable.add(DatabaseTasks.subscribeToChannel(!isUserSubscribed, this, getContext(), channel.getChannelId(), true).subscribe()); }