Skip to content

Commit

Permalink
Fix unsubscribe to delete old videos
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed Aug 18, 2024
1 parent 1dd9565 commit 10fead2
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,39 +286,6 @@ private Single<Boolean> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ public static Single<Integer> refreshAllSubscriptions(Context context, @Nullable
});
}

public static Single<Integer> refreshSubscribedChannel(ChannelId channelId, @Nullable Consumer<Integer> 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<Integer> refreshSubscriptions(@NonNull List<ChannelId> channelIds, @Nullable Consumer<Integer> newVideosFound) {
if (SkyTubeApp.getSettings().isUseNewPipe() || !YouTubeAPIKey.get().isUserApiKeySet()) {
return YouTubeTasks.getBulkSubscriptionVideos(channelIds, newVideosFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ public static Single<Pair<PersistentChannel, DatabaseResult>> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 10fead2

Please sign in to comment.