Skip to content

Commit f982a93

Browse files
committed
blocked channels: improve channel blocking by a name only
1 parent 361186c commit f982a93

File tree

5 files changed

+112
-77
lines changed

5 files changed

+112
-77
lines changed

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/models/data/Video.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,14 @@ public String getCardImageUrl() {
311311
}
312312

313313
public String getAuthor() {
314-
if (author != null) {
315-
return author;
314+
if (author != null && !Helpers.startsWith(author, "@")) {
315+
return extractAuthor(author);
316316
}
317317

318318
String mainTitle = metadataTitle != null ? metadataTitle : title;
319319
CharSequence subtitle = metadataSecondTitle != null ? metadataSecondTitle : secondTitle;
320-
return hasVideo() ? extractAuthor(subtitle) : Helpers.toString(YouTubeHelper.createInfo(mainTitle, subtitle)); // BAD idea
320+
//return hasVideo() ? extractAuthor(subtitle) : Helpers.toString(YouTubeHelper.createInfo(mainTitle, subtitle)); // BAD idea
321+
return hasVideo() ? extractAuthor(subtitle) : extractAuthor(YouTubeHelper.createInfo(mainTitle, subtitle));
321322
}
322323

323324
public VideoGroup getGroup() {
@@ -348,7 +349,8 @@ private static String extractAuthor(String secondTitle) {
348349
} else {
349350
// First part may be a special label (4K, Stream, New etc)
350351
// Two cases to detect label: 1) Description is long (4 items); 2) First item of info is too short (2 letters)
351-
result = split.length < 4 && split[0].trim().length() > 2 ? split[0] : split[1];
352+
//result = split.length < 4 && split[0].trim().length() > 2 ? split[0] : split[1];
353+
result = split.length < 4 ? split[0] : split[1];
352354
}
353355
}
354356

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/models/data/VideoGroup.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,12 @@ public void add(int idx, Video video) {
473473

474474
private boolean isChannelBlocked(Video video) {
475475
// Filter out videos from blacklisted channels
476-
String channelId = video.getChannelIdOrName();
477-
if (channelId != null) {
476+
String channelId = video.channelId;
477+
String channelName = video.getAuthor();
478+
if (!Helpers.allNulls(channelId, channelName)) {
478479
try {
479480
BlockedChannelData blockedChannelData = BlockedChannelData.instance(GlobalPreferences.context());
480-
return blockedChannelData != null && blockedChannelData.containsChannel(channelId);
481+
return blockedChannelData != null && blockedChannelData.containsChannel(channelId, channelName);
481482
} catch (Exception e) {
482483
// If BlockedChannelData isn't initialized yet, allow the video through
483484
// This can happen during early app startup

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/presenters/BrowsePresenter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.text.TextUtils;
6+
import android.util.Pair;
67

78
import androidx.annotation.Nullable;
89

@@ -274,10 +275,10 @@ private List<Video> getBlockedChannels() {
274275
BlockedChannelData blockedChannelData = BlockedChannelData.instance(getContext());
275276
List<Video> videos = new ArrayList<>();
276277

277-
for (Map.Entry<String, String> entry : blockedChannelData.getChannelIdsWithNames().entrySet()) {
278+
for (Pair<String, String> entry : blockedChannelData.getChannelIdsWithNames()) {
278279
Video video = new Video();
279-
video.channelId = entry.getKey();
280-
video.title = entry.getValue() != null ? entry.getValue() : entry.getKey();
280+
video.channelId = entry.first;
281+
video.title = entry.second;
281282
videos.add(video);
282283
}
283284

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/presenters/dialogs/menu/VideoMenuPresenter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,31 +404,32 @@ private void appendBlockChannelButton() {
404404
return;
405405
}
406406

407-
String channelId = mVideo.getChannelIdOrName();
408-
if (channelId == null || channelId.isEmpty()) {
407+
String channelId = mVideo.channelId;
408+
String channelName = mVideo.getAuthor();
409+
410+
if (Helpers.allNulls(channelId, channelName)) {
409411
return;
410412
}
411413

412414
BlockedChannelData blockedChannelData = BlockedChannelData.instance(getContext());
413-
boolean isBlacklisted = blockedChannelData.containsChannel(channelId);
415+
boolean isBlocked = blockedChannelData.containsChannel(channelId, channelName);
414416

415-
String buttonText = isBlacklisted
417+
String buttonText = isBlocked
416418
? getContext().getString(R.string.dialog_unblock_channel)
417419
: getContext().getString(R.string.dialog_block_channel);
418420

419421
mDialogPresenter.appendSingleButton(
420422
UiOptionItem.from(buttonText, optionItem -> {
421-
if (isBlacklisted) {
423+
if (isBlocked) {
422424
// Remove from blacklist
423-
blockedChannelData.removeChannel(channelId);
425+
blockedChannelData.removeChannel(channelId, channelName);
424426
MessageHelpers.showMessage(getContext(), R.string.channel_unblocked);
425427
if (mCallback != null) {
426428
mCallback.onItemAction(mVideo, VideoMenuCallback.ACTION_REMOVE);
427429
}
428430
mDialogPresenter.closeDialog();
429431
} else {
430432
// Show confirmation dialog before blocking
431-
String channelName = mVideo.getAuthor();
432433
String confirmMessage = getContext().getString(R.string.confirm_block_channel, channelName);
433434

434435
AppDialogUtil.showConfirmationDialog(

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/prefs/BlockedChannelData.java

Lines changed: 90 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,72 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5+
import android.util.Pair;
6+
7+
import androidx.annotation.NonNull;
8+
import androidx.annotation.Nullable;
59

610
import com.liskovsoft.sharedutils.helpers.Helpers;
711
import com.liskovsoft.smartyoutubetv2.common.prefs.AppPrefs.ProfileChangeListener;
812
import com.liskovsoft.smartyoutubetv2.common.utils.Utils;
913

1014
import java.util.ArrayList;
11-
import java.util.Collections;
12-
import java.util.HashSet;
1315
import java.util.List;
1416
import java.util.Map;
15-
import java.util.Set;
17+
import java.util.Map.Entry;
1618

1719
public class BlockedChannelData implements ProfileChangeListener {
1820
private static final String BLOCKED_CHANNEL_DATA = "blocked_channel_data";
1921
@SuppressLint("StaticFieldLeak")
2022
private static BlockedChannelData sInstance;
2123
private final AppPrefs mPrefs;
22-
private Set<String> mChannelIds;
23-
private Map<String, String> mChannelIdsWithNames;
24+
private List<Channel> mChannels;
2425
private final Runnable mPersistStateInt = this::persistStateInt;
2526
private final List<BlockedChannelListener> mListeners = new ArrayList<>();
2627

28+
private static class Channel {
29+
public String channelId;
30+
public String channelName;
31+
32+
public Channel(String channelId, String channelName) {
33+
this.channelId = channelId;
34+
this.channelName = channelName;
35+
}
36+
37+
public static Channel fromString(String specs) {
38+
String[] split = Helpers.splitObj(specs);
39+
40+
if (split == null || split.length != 2) {
41+
return null;
42+
}
43+
44+
return new Channel(Helpers.parseStr(split[0]), Helpers.parseStr(split[1]));
45+
}
46+
47+
@Override
48+
public boolean equals(@Nullable Object obj) {
49+
if (obj instanceof Channel) {
50+
Channel channel = (Channel) obj;
51+
52+
if (channelName != null && channel.channelName != null) {
53+
return Helpers.contains(channelName, channel.channelName) || Helpers.contains(channel.channelName, channelName);
54+
}
55+
56+
if (channelId != null && channel.channelId != null) {
57+
return Helpers.equals(channel, channel.channelId);
58+
}
59+
}
60+
61+
return super.equals(obj);
62+
}
63+
64+
@NonNull
65+
@Override
66+
public String toString() {
67+
return Helpers.mergeObj(channelId, channelName);
68+
}
69+
}
70+
2771
public interface BlockedChannelListener {
2872
void onChanged();
2973
}
@@ -43,80 +87,55 @@ public static BlockedChannelData instance(Context context) {
4387
}
4488

4589
/**
46-
* Add a channel to the blacklist
47-
*
48-
* @param channelId The channel ID to blacklist
49-
* @param channelName The channel name (optional, for display purposes)
90+
* Add a channel to the block list
5091
*/
5192
public void addChannel(String channelId, String channelName) {
52-
if (channelId == null || channelId.isEmpty()) {
93+
if (channelId == null && channelName == null) {
5394
return;
5495
}
5596

56-
mChannelIds.add(channelId);
57-
if (channelName != null && !channelName.isEmpty()) {
58-
mChannelIdsWithNames.put(channelId, channelName);
59-
}
97+
Channel channel = new Channel(channelId, channelName);
98+
mChannels.remove(channel);
99+
mChannels.add(channel);
100+
60101
persistState();
61102
notifyListeners();
62103
}
63104

64105
/**
65-
* Remove a channel from the blacklist
66-
*
67-
* @param channelId The channel ID to remove
106+
* Remove a channel from the list
68107
*/
69-
public void removeChannel(String channelId) {
70-
if (channelId == null || channelId.isEmpty()) {
108+
public void removeChannel(String channelId, String channelName) {
109+
if (channelId == null && channelName == null) {
71110
return;
72111
}
73112

74-
mChannelIds.remove(channelId);
75-
mChannelIdsWithNames.remove(channelId);
113+
mChannels.remove(new Channel(channelId, channelName));
114+
76115
persistState();
77116
notifyListeners();
78117
}
79118

80119
/**
81-
* Check if a channel is blacklisted
82-
*
83-
* @param channelId The channel ID to check
84-
* @return true if the channel is blacklisted
120+
* Check if a channel is blocked
85121
*/
86-
public boolean containsChannel(String channelId) {
87-
if (channelId == null || channelId.isEmpty()) {
88-
return false;
89-
}
90-
91-
return mChannelIds.contains(channelId);
122+
public boolean containsChannel(String channelId, String channelName) {
123+
return mChannels.contains(new Channel(channelId, channelName));
92124
}
93125

94126
/**
95-
* Get all blacklisted channel IDs
127+
* Get all blocked channels with their names
96128
*
97-
* @return Unmodifiable set of blacklisted channel IDs
129+
* @return List of channelId -> channel name
98130
*/
99-
public Set<String> getChannelIds() {
100-
return Collections.unmodifiableSet(mChannelIds);
101-
}
131+
public List<Pair<String, String>> getChannelIdsWithNames() {
132+
List<Pair<String, String>> result = new ArrayList<>();
102133

103-
/**
104-
* Get the name of a blacklisted channel
105-
*
106-
* @param channelId The channel ID
107-
* @return The channel name, or null if not available
108-
*/
109-
public String getChannelName(String channelId) {
110-
return mChannelIdsWithNames.get(channelId);
111-
}
134+
for (Channel item : mChannels) {
135+
result.add(new Pair<>(item.channelId, item.channelName));
136+
}
112137

113-
/**
114-
* Get all blacklisted channels with their names
115-
*
116-
* @return Unmodifiable map of channelId -> channel name
117-
*/
118-
public Map<String, String> getChannelIdsWithNames() {
119-
return Collections.unmodifiableMap(mChannelIdsWithNames);
138+
return result;
120139
}
121140

122141
/**
@@ -125,15 +144,14 @@ public Map<String, String> getChannelIdsWithNames() {
125144
* @return Number of blacklisted channels
126145
*/
127146
public int getChannelCount() {
128-
return mChannelIds.size();
147+
return mChannels.size();
129148
}
130149

131150
/**
132151
* Clear all blacklisted channels
133152
*/
134153
public void clear() {
135-
mChannelIds.clear();
136-
mChannelIdsWithNames.clear();
154+
mChannels.clear();
137155
persistState();
138156
}
139157

@@ -142,10 +160,23 @@ private synchronized void restoreState() {
142160

143161
String[] split = Helpers.splitData(data);
144162

145-
List<String> channelIdList = Helpers.parseStrList(split, 0);
146-
mChannelIdsWithNames = Helpers.parseMap(split, 1, Helpers::parseStr, Helpers::parseStr);
163+
mChannels = Helpers.parseList(split, 0, Channel::fromString);
164+
// null
147165

148-
mChannelIds = new HashSet<>(channelIdList);
166+
restoreOldData(split);
167+
}
168+
169+
private void restoreOldData(String[] split) {
170+
Map<String, String> channelIdsWithNames = Helpers.parseMap(split, 1, Helpers::parseStr, Helpers::parseStr);
171+
172+
if (!channelIdsWithNames.isEmpty()) {
173+
for (Entry<String, String> entry : channelIdsWithNames.entrySet()) {
174+
Channel channel = new Channel(entry.getKey(), entry.getValue());
175+
if (!mChannels.contains(channel)) {
176+
mChannels.add(channel);
177+
}
178+
}
179+
}
149180
}
150181

151182
public void persistNow() {
@@ -158,9 +189,8 @@ private void persistState() {
158189

159190
private void persistStateInt() {
160191
// Convert Set to List for persistence
161-
List<String> channelIdList = new ArrayList<>(mChannelIds);
162192
mPrefs.setProfileData(BLOCKED_CHANNEL_DATA, Helpers.mergeData(
163-
channelIdList, mChannelIdsWithNames));
193+
mChannels, null));
164194
}
165195

166196
public void addListener(BlockedChannelListener listener) {

0 commit comments

Comments
 (0)