Skip to content

Commit

Permalink
Add unsubscribe menuitem to the Channel submenu, and fix unsubscripti…
Browse files Browse the repository at this point in the history
…on problems
  • Loading branch information
gzsombor committed Apr 28, 2024
1 parent 71141ac commit a60aa63
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 45 deletions.
3 changes: 3 additions & 0 deletions app/src/extra/res/menu/menu_youtube_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<item android:id="@+id/subscribe_channel"
android:visible="false"
android:title="@string/subscribe"/>
<item android:id="@+id/unsubscribe_channel"
android:visible="false"
android:title="@string/unsubscribe"/>
<item android:id="@+id/block_channel"
android:title="@string/block_channel"
app:showAsAction="never"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(id);
}

@Override
public String toString() {
return "ChannelId{'" + id + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.jsoup.safety.Safelist;
import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.extractor.stream.Description;

import java.util.Comparator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static Single<DatabaseResult> subscribeToChannel(boolean subscribeToChann
if (subscribeToChannel) {
return SubscriptionsDb.getSubscriptionsDb().subscribe(channel);
} else {
return SubscriptionsDb.getSubscriptionsDb().unsubscribe(channel.getId());
return SubscriptionsDb.getSubscriptionsDb().unsubscribe(channel.getChannelId());
}
})
.subscribeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializer;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -279,18 +275,18 @@ private DatabaseResult saveSubscription(YouTubeChannel channel) {
*
* @return True if the operation was successful; false otherwise.
*/
public DatabaseResult unsubscribe(String channelId) {
public DatabaseResult unsubscribe(ChannelId channelId) {
SkyTubeApp.nonUiThread();

// delete any feed videos pertaining to this channel
getWritableDatabase().delete(SubscriptionsVideosTable.TABLE_NAME_V2,
SubscriptionsVideosTable.COL_CHANNEL_ID + " = ?",
new String[]{channelId});
new String[]{channelId.getRawId()});

// remove this channel from the subscriptions DB
int rowsDeleted = getWritableDatabase().delete(SubscriptionsTable.TABLE_NAME,
SubscriptionsTable.COL_CHANNEL_ID + " = ?",
new String[]{channelId});
new String[]{channelId.getRawId()});

// Need to make sure when we come back to MainActivity, that we refresh the Feed tab so it hides videos from the newly unsubscribed
SkyTubeApp.getSettings().setRefreshSubsFeedFromCache(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,13 @@ private void onOptionsButtonClick(final View view) {
private void onOptionsButtonClick(final View view, YouTubeChannel channel) {
final PopupMenu popupMenu = createPopup(R.menu.channel_options_menu, view);
Menu menu = popupMenu.getMenu();
updateSubscribeMenuItem(channel.getChannelId(), menu);
popupMenu.setOnMenuItemClickListener(item -> {
if (actionHandler.handleChannelActions(context, channel, item.getItemId())) {
return true;
}
switch (item.getItemId()) {
case R.id.share:
SkyTubeApp.shareUrl(context, channel.getChannelUrl());
return true;
case R.id.copyurl:
SkyTubeApp.copyUrl(context, "Channel URL", channel.getChannelUrl());
return true;
}
return false;
});
actionHandler.updateSubscribeMenuItem(channel.getChannelId(), menu);
popupMenu.setOnMenuItemClickListener(item ->
actionHandler.handleChannelActions(context, channel, item.getItemId())
);
popupMenu.show();
}

private void updateSubscribeMenuItem(ChannelId channelId, Menu menu) {
compositeDisposable.add(SubscriptionsDb.getSubscriptionsDb().getUserSubscribedToChannel(channelId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe((subscribed) -> {
if (!subscribed) {
menu.findItem(R.id.subscribe_channel).setVisible(true);
}
}));
}

private void onOptionsButtonClick(final View view, YouTubeVideo youTubeVideo) {
final PopupMenu popupMenu = createPopup(R.menu.video_options_menu, view);
final Menu menu = popupMenu.getMenu();
Expand All @@ -273,17 +252,10 @@ private void onOptionsButtonClick(final View view, YouTubeVideo youTubeVideo) {
menu.findItem(R.id.download_video).setVisible(online);
}
}));
if (SkyTubeApp.getSettings().isEnableVideoBlocker()) {
menu.findItem(R.id.block_channel).setVisible(true);
menu.findItem(R.id.unblock_channel).setVisible(true);
} else {
menu.findItem(R.id.block_channel).setVisible(false);
menu.findItem(R.id.unblock_channel).setVisible(false);
}
if (youTubeVideo.getChannelId() != null) {
menu.findItem(R.id.open_channel).setVisible(true);
updateSubscribeMenuItem(youTubeVideo.getChannelId(), menu);
}

actionHandler.updateBlockingMenuItem(menu);
actionHandler.updateSubscribeMenuItem(youTubeVideo.getChannelId(), menu);

popupMenu.setOnMenuItemClickListener(item -> {
if (actionHandler.handleChannelActions(context, youTubeVideo.getChannel(), item.getItemId())) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
package free.rm.skytube.gui.businessobjects.views;

import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.Nullable;

import free.rm.skytube.R;
import free.rm.skytube.app.SkyTubeApp;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeChannel;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.db.DatabaseTasks;
import free.rm.skytube.businessobjects.db.SubscriptionsDb;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;

public class ChannelActionHandler {
Expand All @@ -35,6 +43,10 @@ public boolean handleChannelActions(Context context, YouTubeChannel channel, int
case R.id.subscribe_channel:
compositeDisposable.add(YouTubeChannel.subscribeChannel(context, channel.getChannelId()));
return true;
case R.id.unsubscribe_channel:
compositeDisposable.add(DatabaseTasks.subscribeToChannel(false,
null, context, channel, true).subscribe());
return true;
case R.id.open_channel:
SkyTubeApp.launchChannel(channel.getChannelId(), context);
return true;
Expand All @@ -44,7 +56,40 @@ public boolean handleChannelActions(Context context, YouTubeChannel channel, int
case R.id.unblock_channel:
compositeDisposable.add(channel.unblockChannel().subscribe());
return true;
case R.id.share:
SkyTubeApp.shareUrl(context, channel.getChannelUrl());
return true;
case R.id.copyurl:
SkyTubeApp.copyUrl(context, "Channel URL", channel.getChannelUrl());
return true;

}
return false;
}

public void updateSubscribeMenuItem(@Nullable ChannelId channelId, Menu menu) {
setVisible(menu, R.id.open_channel, channelId != null);
if (channelId != null) {
compositeDisposable.add(SubscriptionsDb.getSubscriptionsDb().getUserSubscribedToChannel(channelId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe((subscribed) -> {
setVisible(menu, R.id.subscribe_channel, !subscribed);
setVisible(menu, R.id.unsubscribe_channel, subscribed);
}));
}
}

public void updateBlockingMenuItem(Menu menu) {
boolean visibility = SkyTubeApp.getSettings().isEnableVideoBlocker();
setVisible(menu, R.id.block_channel, visibility);
setVisible(menu, R.id.unblock_channel, visibility);
}

private void setVisible(Menu menu, int id, boolean visible) {
MenuItem item = menu.findItem(id);
if (item != null) {
item.setVisible(visible);
}
}

}
3 changes: 3 additions & 0 deletions app/src/main/res/menu/channel_options_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<item
android:id="@+id/subscribe_channel" android:visible="false"
android:title="@string/subscribe"/>
<item
android:id="@+id/unsubscribe_channel" android:visible="false"
android:title="@string/unsubscribe"/>
<item
android:id="@+id/block_channel"
android:title="@string/block_channel"/>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/menu/menu_youtube_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<item android:id="@+id/subscribe_channel"
android:visible="false"
android:title="@string/subscribe"/>
<item android:id="@+id/unsubscribe_channel"
android:visible="false"
android:title="@string/unsubscribe"/>
<item android:id="@+id/block_channel"
android:title="@string/block_channel"
app:showAsAction="never"/>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/menu/video_options_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<item
android:id="@+id/subscribe_channel" android:visible="false"
android:title="@string/subscribe"/>
<item
android:id="@+id/unsubscribe_channel" android:visible="false"
android:title="@string/unsubscribe"/>
<item
android:id="@+id/block_channel"
android:title="@string/block_channel"/>
Expand Down

0 comments on commit a60aa63

Please sign in to comment.