22
33import android .annotation .SuppressLint ;
44import android .content .Context ;
5+ import android .util .Pair ;
6+
7+ import androidx .annotation .NonNull ;
8+ import androidx .annotation .Nullable ;
59
610import com .liskovsoft .sharedutils .helpers .Helpers ;
711import com .liskovsoft .smartyoutubetv2 .common .prefs .AppPrefs .ProfileChangeListener ;
812import com .liskovsoft .smartyoutubetv2 .common .utils .Utils ;
913
1014import java .util .ArrayList ;
11- import java .util .Collections ;
12- import java .util .HashSet ;
1315import java .util .List ;
1416import java .util .Map ;
15- import java .util .Set ;
17+ import java .util .Map . Entry ;
1618
1719public 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