-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: Implement channel blocking feature (#12647) #12787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mstudio005
wants to merge
1
commit into
TeamNewPipe:dev
Choose a base branch
from
mstudio005:feat/block-channels
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
app/src/main/java/org/schabi/newpipe/settings/BlockedChannelsFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package org.schabi.newpipe.settings | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.google.android.material.snackbar.Snackbar | ||
| import org.schabi.newpipe.R | ||
| import org.schabi.newpipe.databinding.FragmentBlockedChannelsBinding | ||
| import org.schabi.newpipe.util.BlockedChannelsManager | ||
|
|
||
| /** | ||
| * Fragment to display and manage blocked channels | ||
| */ | ||
| class BlockedChannelsFragment : Fragment() { | ||
| private var _binding: FragmentBlockedChannelsBinding? = null | ||
| private val binding get() = _binding!! | ||
|
|
||
| private lateinit var adapter: BlockedChannelsAdapter | ||
| private val blockedChannels = mutableListOf<String>() | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View { | ||
| _binding = FragmentBlockedChannelsBinding.inflate(inflater, container, false) | ||
| return binding.root | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
|
|
||
| setupRecyclerView() | ||
| loadBlockedChannels() | ||
| } | ||
|
|
||
| private fun setupRecyclerView() { | ||
| adapter = BlockedChannelsAdapter(blockedChannels) { channelUrl -> | ||
| showUnblockDialog(channelUrl) | ||
| } | ||
|
|
||
| binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) | ||
| binding.recyclerView.adapter = adapter | ||
| } | ||
|
|
||
| private fun loadBlockedChannels() { | ||
| blockedChannels.clear() | ||
| blockedChannels.addAll(BlockedChannelsManager.getBlockedChannelsList(requireContext())) | ||
| adapter.notifyDataSetChanged() | ||
|
|
||
| updateEmptyView() | ||
| } | ||
|
|
||
| private fun updateEmptyView() { | ||
| if (blockedChannels.isEmpty()) { | ||
| binding.emptyView.visibility = View.VISIBLE | ||
| binding.recyclerView.visibility = View.GONE | ||
| } else { | ||
| binding.emptyView.visibility = View.GONE | ||
| binding.recyclerView.visibility = View.VISIBLE | ||
| } | ||
| } | ||
|
|
||
| private fun showUnblockDialog(channelUrl: String) { | ||
| AlertDialog.Builder(requireContext()) | ||
| .setTitle(R.string.unblock_channel) | ||
| .setMessage(R.string.unblock_channel_confirmation) | ||
| .setPositiveButton(R.string.unblock_channel) { _, _ -> | ||
| unblockChannel(channelUrl) | ||
| } | ||
| .setNegativeButton(R.string.cancel, null) | ||
| .show() | ||
| } | ||
|
|
||
| private fun unblockChannel(channelUrl: String) { | ||
| BlockedChannelsManager.unblockChannel(requireContext(), channelUrl) | ||
| loadBlockedChannels() | ||
|
|
||
| Snackbar.make( | ||
| binding.root, | ||
| R.string.channel_unblocked, | ||
| Snackbar.LENGTH_SHORT | ||
| ).show() | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
| /** | ||
| * RecyclerView adapter for blocked channels | ||
| */ | ||
| private class BlockedChannelsAdapter( | ||
| private val channels: List<String>, | ||
| private val onUnblockClick: (String) -> Unit | ||
| ) : RecyclerView.Adapter<BlockedChannelsAdapter.ViewHolder>() { | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.item_blocked_channel, parent, false) | ||
| return ViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
| val channelUrl = channels[position] | ||
| holder.bind(channelUrl, onUnblockClick) | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = channels.size | ||
|
|
||
| class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| private val channelUrlText: android.widget.TextView = itemView.findViewById(R.id.channel_url) | ||
| private val unblockButton: android.widget.Button = itemView.findViewById(R.id.unblock_button) | ||
|
|
||
| fun bind(channelUrl: String, onUnblockClick: (String) -> Unit) { | ||
| channelUrlText.text = channelUrl | ||
| unblockButton.setOnClickListener { | ||
| onUnblockClick(channelUrl) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
104 changes: 104 additions & 0 deletions
104
app/src/main/java/org/schabi/newpipe/util/BlockedChannelsManager.kt
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocked channels should be stored in their own DB table with url and name so that the name can be displayed to the user. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent"> | ||
|
|
||
| <androidx.recyclerview.widget.RecyclerView | ||
| android:id="@+id/recyclerView" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:clipToPadding="false" | ||
| android:paddingTop="8dp" | ||
| android:paddingBottom="8dp" /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/emptyView" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_gravity="center" | ||
| android:text="@string/no_blocked_channels" | ||
| android:textAppearance="?android:attr/textAppearanceMedium" | ||
| android:visibility="gone" /> | ||
|
|
||
| </androidx.coordinatorlayout.widget.CoordinatorLayout> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.setActionTextColor(Color.YELLOW)