mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Unsubscription confirmation option
This commit is contained in:
parent
74c0fe85e2
commit
aa9f9852d9
@ -1,12 +1,16 @@
|
||||
package com.github.libretube.api
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
||||
import com.github.libretube.db.obj.LocalSubscription
|
||||
import com.github.libretube.extensions.TAG
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
import com.github.libretube.extensions.query
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@ -55,6 +59,24 @@ object SubscriptionHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun handleUnsubscribe(context: Context, channelId: String, channelName: String?, onUnsubscribe: () -> Unit) {
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
||||
unsubscribe(channelId)
|
||||
onUnsubscribe.invoke()
|
||||
return
|
||||
}
|
||||
|
||||
MaterialAlertDialogBuilder(context)
|
||||
.setTitle(R.string.unsubscribe)
|
||||
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
|
||||
.setPositiveButton(R.string.unsubscribe) { _, _ ->
|
||||
unsubscribe(channelId)
|
||||
onUnsubscribe.invoke()
|
||||
}
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.show()
|
||||
}
|
||||
|
||||
suspend fun isSubscribed(channelId: String): Boolean? {
|
||||
if (PreferenceHelper.getToken() != "") {
|
||||
val isSubscribed = try {
|
||||
@ -99,7 +121,7 @@ object SubscriptionHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun getLocalSubscriptions(): List<LocalSubscription> {
|
||||
private fun getLocalSubscriptions(): List<LocalSubscription> {
|
||||
return awaitQuery {
|
||||
Database.localSubscriptionDao().getAll()
|
||||
}
|
||||
@ -107,6 +129,6 @@ object SubscriptionHelper {
|
||||
|
||||
fun getFormattedLocalSubscriptions(): String {
|
||||
val localSubscriptions = getLocalSubscriptions()
|
||||
return localSubscriptions.map { it.channelId }.joinToString(",")
|
||||
return localSubscriptions.joinToString(",") { it.channelId }
|
||||
}
|
||||
}
|
||||
|
@ -104,6 +104,7 @@ object PreferenceKeys {
|
||||
const val CLEAR_WATCH_HISTORY = "clear_watch_history"
|
||||
const val CLEAR_WATCH_POSITIONS = "clear_watch_positions"
|
||||
const val SHARE_WITH_TIME_CODE = "share_with_time_code"
|
||||
const val CONFIRM_UNSUBSCRIBE = "confirm_unsubscribing"
|
||||
|
||||
/**
|
||||
* History
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.libretube.ui.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -128,13 +129,13 @@ class SearchAdapter(
|
||||
root.setOnClickListener {
|
||||
NavigationHelper.navigateChannel(root.context, item.url)
|
||||
}
|
||||
val channelId = item.url!!.toID()
|
||||
|
||||
isSubscribed(channelId, binding)
|
||||
isSubscribed(root.context, item, binding)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isSubscribed(channelId: String, binding: ChannelRowBinding) {
|
||||
private fun isSubscribed(context: Context, streamItem: ContentItem, binding: ChannelRowBinding) {
|
||||
val channelId = streamItem.url!!.toID()
|
||||
// check whether the user subscribed to the channel
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
var isSubscribed = SubscriptionHelper.isSubscribed(channelId)
|
||||
@ -151,14 +152,13 @@ class SearchAdapter(
|
||||
binding.searchSubButton.setOnClickListener {
|
||||
if (isSubscribed == false) {
|
||||
SubscriptionHelper.subscribe(channelId)
|
||||
binding.searchSubButton.text =
|
||||
binding.root.context.getString(R.string.unsubscribe)
|
||||
binding.searchSubButton.text = binding.root.context.getString(R.string.unsubscribe)
|
||||
isSubscribed = true
|
||||
} else {
|
||||
SubscriptionHelper.unsubscribe(channelId)
|
||||
binding.searchSubButton.text =
|
||||
binding.root.context.getString(R.string.subscribe)
|
||||
isSubscribed = false
|
||||
SubscriptionHelper.handleUnsubscribe(context, channelId, streamItem.uploaderName) {
|
||||
binding.searchSubButton.text = binding.root.context.getString(R.string.subscribe)
|
||||
isSubscribed = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,13 +38,14 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<com.gith
|
||||
subscriptionSubscribe.setOnClickListener {
|
||||
val channelId = subscription.url!!.toID()
|
||||
if (subscribed) {
|
||||
subscriptionSubscribe.text = root.context.getString(R.string.subscribe)
|
||||
SubscriptionHelper.unsubscribe(channelId)
|
||||
subscribed = false
|
||||
|
||||
SubscriptionHelper.handleUnsubscribe(root.context, channelId, subscription.name ?: "") {
|
||||
subscriptionSubscribe.text = root.context.getString(R.string.subscribe)
|
||||
subscribed = false
|
||||
}
|
||||
} else {
|
||||
subscriptionSubscribe.text =
|
||||
root.context.getString(R.string.unsubscribe)
|
||||
SubscriptionHelper.subscribe(channelId)
|
||||
subscriptionSubscribe.text = root.context.getString(R.string.unsubscribe)
|
||||
subscribed = true
|
||||
}
|
||||
}
|
||||
|
@ -112,6 +112,7 @@ class ChannelFragment : BaseFragment() {
|
||||
}
|
||||
// needed if the channel gets loaded by the ID
|
||||
channelId = response.id
|
||||
channelName = response.name
|
||||
val shareData = ShareData(currentChannel = response.name)
|
||||
|
||||
onScrollEnd = {
|
||||
@ -128,14 +129,15 @@ class ChannelFragment : BaseFragment() {
|
||||
}
|
||||
|
||||
binding.channelSubscribe.setOnClickListener {
|
||||
binding.channelSubscribe.text = if (isSubscribed == true) {
|
||||
SubscriptionHelper.unsubscribe(channelId!!)
|
||||
isSubscribed = false
|
||||
getString(R.string.subscribe)
|
||||
if (isSubscribed == true) {
|
||||
SubscriptionHelper.handleUnsubscribe(requireContext(), channelId!!, channelName) {
|
||||
isSubscribed = false
|
||||
binding.channelSubscribe.text = getString(R.string.subscribe)
|
||||
}
|
||||
} else {
|
||||
SubscriptionHelper.subscribe(channelId!!)
|
||||
isSubscribed = true
|
||||
getString(R.string.unsubscribe)
|
||||
binding.channelSubscribe.text = getString(R.string.unsubscribe)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1267,9 +1267,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
}
|
||||
binding.playerSubscribe.setOnClickListener {
|
||||
if (isSubscribed == true) {
|
||||
SubscriptionHelper.unsubscribe(channelId)
|
||||
binding.playerSubscribe.text = getString(R.string.subscribe)
|
||||
isSubscribed = false
|
||||
SubscriptionHelper.handleUnsubscribe(requireContext(), channelId, streams.uploader) {
|
||||
binding.playerSubscribe.text = getString(R.string.subscribe)
|
||||
isSubscribed = false
|
||||
}
|
||||
} else {
|
||||
SubscriptionHelper.subscribe(channelId)
|
||||
binding.playerSubscribe.text = getString(R.string.unsubscribe)
|
||||
|
10
app/src/main/res/drawable/ic_check.xml
Normal file
10
app/src/main/res/drawable/ic_check.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z" />
|
||||
</vector>
|
@ -356,6 +356,9 @@
|
||||
<string name="alternative_videos_layout">Alternative videos layout</string>
|
||||
<string name="defaultIconLight">Default light</string>
|
||||
<string name="playlistCloned">Playlist cloned</string>
|
||||
<string name="confirm_unsubscribe">Are you sure you want to unsubscribe %1$s?</string>
|
||||
<string name="confirm_unsubscribing">Confirm unsubscribing</string>
|
||||
<string name="confirm_unsubscribing_summary">Show a confirmation dialog before unsubscribing.</string>
|
||||
|
||||
<!-- Notification channel strings -->
|
||||
<string name="download_channel_name">Download Service</string>
|
||||
|
@ -36,6 +36,12 @@
|
||||
app:key="save_feed"
|
||||
app:title="@string/save_feed" />
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
android:icon="@drawable/ic_check"
|
||||
android:summary="@string/confirm_unsubscribing_summary"
|
||||
app:key="confirm_unsubscribing"
|
||||
app:title="@string/confirm_unsubscribing" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory app:title="@string/backup_restore">
|
||||
|
Loading…
Reference in New Issue
Block a user