-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathBlockedChannelsManager.kt
More file actions
104 lines (94 loc) · 3.18 KB
/
BlockedChannelsManager.kt
File metadata and controls
104 lines (94 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.schabi.newpipe.util
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
/**
* Manager for handling blocked channels.
* Stores blocked channel IDs in SharedPreferences and provides methods to add, remove, and check blocked channels.
*/
object BlockedChannelsManager {
private const val BLOCKED_CHANNELS_KEY = "blocked_channels"
private const val DELIMITER = ","
/**
* Get the SharedPreferences instance
*/
private fun getPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
/**
* Get the set of blocked channel IDs
*/
fun getBlockedChannelIds(context: Context): Set<String> {
val prefs = getPreferences(context)
val blockedString = prefs.getString(BLOCKED_CHANNELS_KEY, "") ?: ""
return if (blockedString.isEmpty()) {
emptySet()
} else {
blockedString.split(DELIMITER).toSet()
}
}
/**
* Check if a channel is blocked
*
* @param context Application context
* @param channelUrl The channel URL to check
* @return true if the channel is blocked, false otherwise
*/
fun isChannelBlocked(context: Context, channelUrl: String?): Boolean {
if (channelUrl.isNullOrEmpty()) {
return false
}
return getBlockedChannelIds(context).contains(channelUrl)
}
/**
* Block a channel
*
* @param context Application context
* @param channelUrl The channel URL to block
* @param channelName The channel name (for logging/debugging)
*/
fun blockChannel(context: Context, channelUrl: String, channelName: String? = null) {
val blockedChannels = getBlockedChannelIds(context).toMutableSet()
blockedChannels.add(channelUrl)
saveBlockedChannels(context, blockedChannels)
}
/**
* Unblock a channel
*
* @param context Application context
* @param channelUrl The channel URL to unblock
*/
fun unblockChannel(context: Context, channelUrl: String) {
val blockedChannels = getBlockedChannelIds(context).toMutableSet()
blockedChannels.remove(channelUrl)
saveBlockedChannels(context, blockedChannels)
}
/**
* Get all blocked channels as a list of channel URLs
*
* @param context Application context
* @return List of blocked channel URLs
*/
fun getBlockedChannelsList(context: Context): List<String> {
return getBlockedChannelIds(context).toList()
}
/**
* Clear all blocked channels
*
* @param context Application context
*/
fun clearAllBlockedChannels(context: Context) {
getPreferences(context).edit()
.remove(BLOCKED_CHANNELS_KEY)
.apply()
}
/**
* Save the blocked channels set to SharedPreferences
*/
private fun saveBlockedChannels(context: Context, blockedChannels: Set<String>) {
val blockedString = blockedChannels.joinToString(DELIMITER)
getPreferences(context).edit()
.putString(BLOCKED_CHANNELS_KEY, blockedString)
.apply()
}
}