Skip to content

Commit

Permalink
ChannelId refactor
Browse files Browse the repository at this point in the history
Make it more obvious, if it's a properly formatted channel
  • Loading branch information
gzsombor committed Apr 6, 2024
1 parent d59f98e commit b83621a
Show file tree
Hide file tree
Showing 28 changed files with 246 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.mediarouter.media.MediaRouteSelector;
Expand Down Expand Up @@ -63,6 +62,7 @@
import free.rm.skytube.businessobjects.ChromecastListener;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeVideo;
import free.rm.skytube.businessobjects.YouTube.YouTubeTasks;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.interfaces.GetDesiredStreamListener;
import free.rm.skytube.databinding.ActivityMainBinding;
import free.rm.skytube.gui.businessobjects.MainActivityListener;
Expand Down Expand Up @@ -462,7 +462,7 @@ private void hidePanel() {
}

@Override
public void onChannelClick(String channelId) {
public void onChannelClick(ChannelId channelId) {
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/free/rm/skytube/app/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import free.rm.skytube.app.utils.WeakList;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeVideo;
import free.rm.skytube.businessobjects.YouTube.Tasks.GetSubscriptionVideosTaskListener;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.gui.businessobjects.MainActivityListener;
import free.rm.skytube.gui.fragments.MainFragment;

Expand Down Expand Up @@ -53,11 +54,11 @@ public void notifyMainTabChanged(SettingChange settingChange) {
this.mainFragments.forEach(main -> main.refreshTabs(settingChange));
}

public void notifyChannelNewVideosStatus(String channelId, boolean newVideos) {
public void notifyChannelNewVideosStatus(ChannelId channelId, boolean newVideos) {
this.mainFragments.forEach(main -> main.notifyChangeChannelNewVideosStatus(channelId, newVideos));
}

public void notifyChannelNewVideos(String channelId, int newVideos) {
public void notifyChannelNewVideos(ChannelId channelId, int newVideos) {
if (newVideos > 0) {
this.mainFragments.forEach(main -> main.notifyChangeChannelNewVideosStatus(channelId, true));
}
Expand All @@ -71,7 +72,7 @@ public void registerVideoDetailFetcher(Consumer<YouTubeVideo> videoListener) {
this.videoDetailListeners.add(videoListener);
}

public void notifyChannelRemoved(String channelId) {
public void notifyChannelRemoved(ChannelId channelId) {
this.mainFragments.forEach(main -> main.notifyChannelRemoved(channelId));
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/free/rm/skytube/app/FeedUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import free.rm.skytube.R;
import free.rm.skytube.businessobjects.YouTube.YouTubeTasks;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;

Expand Down Expand Up @@ -100,7 +101,7 @@ public synchronized boolean start(Context context) {
return true;
}

private synchronized void processChannelIds(List<String> channelIds) {
private synchronized void processChannelIds(List<ChannelId> channelIds) {
SkyTubeApp.uiThread();
numVideosFetched = 0;
numChannelsFetched = 0;
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/free/rm/skytube/app/SkyTubeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeChannel;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubePlaylist;
import free.rm.skytube.businessobjects.YouTube.YouTubeTasks;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.YouTube.newpipe.ContentId;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeService;
import free.rm.skytube.businessobjects.db.DatabaseTasks;
Expand Down Expand Up @@ -492,7 +493,7 @@ public static boolean openContent(Context ctx, ContentId content) {
break;
}
case CHANNEL: {
SkyTubeApp.launchChannel(content.getId(), ctx);
SkyTubeApp.launchChannel(new ChannelId(content.getId()), ctx);
break;
}
case PLAYLIST: {
Expand All @@ -512,7 +513,7 @@ public static boolean openContent(Context ctx, ContentId content) {
*
* @param channelId the channel to be displayed.
*/
public static void launchChannel(String channelId, Context context) {
public static void launchChannel(ChannelId channelId, Context context) {
if (channelId != null) {
DatabaseTasks.getChannelInfo(context, channelId, true)
.subscribe(youTubeChannel -> launchChannel(youTubeChannel, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeAPI;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeAPIKey;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeVideo;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeService;
import free.rm.skytube.businessobjects.db.SubscriptionsDb;

Expand All @@ -47,7 +48,7 @@
*/
public class GetChannelVideosFull extends GetYouTubeVideos {

private String channelId;
private ChannelId channelId;
private boolean filterSubscribedVideos;

private YouTube.Search.List videosList = null;
Expand Down Expand Up @@ -137,17 +138,17 @@ private List<CardData> getVideosList(List<SearchResult> searchResultList) throws
* @param channelId Channel ID.
* @param filterSubscribedVideos to filter out the subscribed videos.
*/
public void setChannelQuery(String channelId, boolean filterSubscribedVideos) {
public void setChannelQuery(ChannelId channelId, boolean filterSubscribedVideos) {
this.channelId = channelId;
this.filterSubscribedVideos = filterSubscribedVideos;
if (videosList != null) {
videosList.setChannelId(channelId);
videosList.setChannelId(channelId.getRawId());
}
}

@Override
public void setQuery(String query) {
setChannelQuery(query, false);
setChannelQuery(new ChannelId(query), false);
}

public void setPublishedAfter(long timeInMs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
*/
package free.rm.skytube.businessobjects.YouTube.POJOs;

import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;

public class ChannelView {
private final String id;
private final ChannelId id;
private final String title;
private final String thumbnailUrl;
private boolean newVideosSinceLastVisit;

public ChannelView(String id, String title, String thumbnailUrl, boolean newVideosSinceLastVisit) {
public ChannelView(ChannelId id, String title, String thumbnailUrl, boolean newVideosSinceLastVisit) {
this.id = id;
this.title = title;
this.thumbnailUrl = thumbnailUrl;
this.newVideosSinceLastVisit = newVideosSinceLastVisit;
}

public String getId() {
public ChannelId getId() {
return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import free.rm.skytube.app.SkyTubeApp;
import free.rm.skytube.businessobjects.Logger;
import free.rm.skytube.businessobjects.YouTube.VideoBlocker;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.db.ChannelFilteringDb;
import free.rm.skytube.businessobjects.db.DatabaseTasks;
import free.rm.skytube.businessobjects.db.SubscriptionsDb;
Expand Down Expand Up @@ -91,6 +92,9 @@ private static String getFormattedSubscribers(long subscriberCount) {
return String.format(SkyTubeApp.getStr(R.string.total_subscribers),subscriberCount);
}

public ChannelId getChannelId() {
return new ChannelId(id);
}
public String getBannerUrl() {
return bannerUrl;
}
Expand Down Expand Up @@ -169,7 +173,7 @@ public Single<Boolean> blockChannel() {
* out.
*/
public Single<Boolean> blockChannel(boolean displayToastMessage) {
return SubscriptionsDb.getSubscriptionsDb().getUserSubscribedToChannel(getId())
return SubscriptionsDb.getSubscriptionsDb().getUserSubscribedToChannel(getChannelId())
.flatMap(isSubscribed -> DatabaseTasks.subscribeToChannel(false,
null, SkyTubeApp.getContext(), this, false))
.map(result -> SkyTubeApp.getSettings().isChannelDenyListEnabled())
Expand Down Expand Up @@ -287,7 +291,7 @@ private Single<Boolean> removeDeniedChannel(boolean displayToastMessage) {
});
}

public static Disposable subscribeChannel(final Context context, final String channelId) {
public static Disposable subscribeChannel(final Context context, final ChannelId channelId) {
if (channelId != null) {
return DatabaseTasks.getChannelInfo(context, channelId, false)
.observeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.google.api.services.youtube.model.Channel;
import com.google.api.services.youtube.model.Thumbnail;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoSnippet;
Expand All @@ -54,6 +55,7 @@
import free.rm.skytube.businessobjects.FileDownloader;
import free.rm.skytube.businessobjects.Logger;
import free.rm.skytube.businessobjects.YouTube.YouTubeTasks;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeUtils;
import free.rm.skytube.businessobjects.YouTube.newpipe.VideoId;
import free.rm.skytube.businessobjects.db.BookmarksDb;
Expand Down Expand Up @@ -279,15 +281,15 @@ public void setChannel(YouTubeChannel channel) {
}

public String getSafeChannelId() {
return channel != null ? channel.getId() : null;
return channel != null ? channel.getChannelId().getRawId() : null;
}

public String getSafeChannelName() {
return channel != null ? channel.getTitle() : null;
}

public String getChannelId() {
return channel.getId();
public ChannelId getChannelId() {
return channel.getChannelId();
}

public String getChannelName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import free.rm.skytube.businessobjects.YouTube.POJOs.CardData;
import free.rm.skytube.businessobjects.YouTube.POJOs.ChannelView;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeVideo;
import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.db.ChannelFilteringDb;

import static free.rm.skytube.app.SkyTubeApp.getStr;
Expand Down Expand Up @@ -88,8 +89,8 @@ public List<CardData> filter(List<CardData> videosList) {

List<CardData> filteredVideosList = new ArrayList<>();
final boolean isChannelBlacklistEnabled = settings.isChannelDenyListEnabled();
final List<String> blacklistedChannelIds = isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getDeniedChannelsIdsList() : null;
final List<String> whitelistedChannelIds = !isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getAllowedChannelsIdsList() : null;
final List<ChannelId> blacklistedChannelIds = isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getDeniedChannelsIdsList() : null;
final List<ChannelId> whitelistedChannelIds = !isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getAllowedChannelsIdsList() : null;
// set of user's preferred ISO 639 language codes (regex)
final Set<String> preferredLanguages = SkyTubeApp.getPreferenceManager().getStringSet(getStr(R.string.pref_key_preferred_languages), defaultPrefLanguages);
final BigInteger minimumVideoViews = getViewsFilteringValue();
Expand Down Expand Up @@ -135,8 +136,8 @@ public List<ChannelView> filterChannels(List<ChannelView> channels) {
if (!isChannelBlacklistEnabled) {
return channels;
}
final List<String> blacklistedChannelIds = isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getDeniedChannelsIdsList() : null;
final List<String> whitelistedChannelIds = !isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getAllowedChannelsIdsList() : null;
final List<ChannelId> blacklistedChannelIds = isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getDeniedChannelsIdsList() : null;
final List<ChannelId> whitelistedChannelIds = !isChannelBlacklistEnabled ? ChannelFilteringDb.getChannelFilteringDb().getAllowedChannelsIdsList() : null;

for (ChannelView channel : channels) {
if ( !(isChannelBlacklistEnabled ? filterByBlacklistedChannels(channel.getId(), blacklistedChannelIds)
Expand Down Expand Up @@ -173,8 +174,8 @@ private void log(YouTubeVideo video, FilterType filteringType, String reason) {
*
* @return True if the video is to be filtered; false otherwise.
*/
private boolean filterByBlacklistedChannels(YouTubeVideo video, List<String> blacklistedChannelIds) {
if (filterByBlacklistedChannels(video.getChannel().getId(), blacklistedChannelIds)) {
private boolean filterByBlacklistedChannels(YouTubeVideo video, List<ChannelId> blacklistedChannelIds) {
if (filterByBlacklistedChannels(video.getChannelId(), blacklistedChannelIds)) {
log(video, FilterType.CHANNEL_BLACKLIST, video.getChannelName());
return true;
} else {
Expand All @@ -191,7 +192,7 @@ private boolean filterByBlacklistedChannels(YouTubeVideo video, List<String> bla
*
* @return True if the channel is to be filtered; false otherwise.
*/
private boolean filterByBlacklistedChannels(String channelId, List<String> blacklistedChannelIds) {
private boolean filterByBlacklistedChannels(ChannelId channelId, List<ChannelId> blacklistedChannelIds) {
return blacklistedChannelIds.contains(channelId);
}

Expand All @@ -204,8 +205,8 @@ private boolean filterByBlacklistedChannels(String channelId, List<String> black
*
* @return True if the video is to be filtered; false otherwise.
*/
private boolean filterByWhitelistedChannels(YouTubeVideo video, List<String> whitelistedChannelIds) {
if (filterByWhitelistedChannels(video.getChannel().getId(), whitelistedChannelIds)) {
private boolean filterByWhitelistedChannels(YouTubeVideo video, List<ChannelId> whitelistedChannelIds) {
if (filterByWhitelistedChannels(video.getChannelId(), whitelistedChannelIds)) {
log(video, FilterType.CHANNEL_WHITELIST, video.getChannelName());
return true;
} else {
Expand All @@ -222,7 +223,7 @@ private boolean filterByWhitelistedChannels(YouTubeVideo video, List<String> whi
*
* @return True if the channel is to be filtered; false otherwise.
*/
private boolean filterByWhitelistedChannels(String channelId, List<String> whitelistedChannelIds) {
private boolean filterByWhitelistedChannels(ChannelId channelId, List<ChannelId> whitelistedChannelIds) {
return !whitelistedChannelIds.contains(channelId);
}

Expand Down
Loading

0 comments on commit b83621a

Please sign in to comment.