Merge pull request #7307 from Bnyro/master

fix: subscription button state not updated after starting next video
This commit is contained in:
Bnyro 2025-04-15 15:08:51 +02:00 committed by GitHub
commit 8388ad659a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 52 deletions

View File

@ -61,31 +61,4 @@ object SubscriptionHelper {
suspend fun submitFeedItemChange(feedItem: SubscriptionsFeedItem) =
feedRepository.submitFeedItemChange(feedItem)
fun handleUnsubscribe(
context: Context,
channelId: String,
channelName: String?,
onUnsubscribe: () -> Unit
) {
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
runBlocking {
unsubscribe(channelId)
onUnsubscribe()
}
return
}
MaterialAlertDialogBuilder(context)
.setTitle(R.string.unsubscribe)
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
.setPositiveButton(R.string.unsubscribe) { _, _ ->
runBlocking {
unsubscribe(channelId)
onUnsubscribe()
}
}
.setNegativeButton(R.string.cancel, null)
.show()
}
}

View File

@ -1,13 +1,14 @@
package com.github.libretube.ui.extensions
import android.content.Context
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.helpers.PreferenceHelper
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -24,47 +25,78 @@ fun TextView.setupSubscriptionButton(
) {
if (channelId == null) return
val notificationsEnabled = PreferenceHelper
.getBoolean(PreferenceKeys.NOTIFICATION_ENABLED, true)
var subscribed: Boolean? = false
fun updateUIStateAndNotifyObservers() {
subscribed?.let { subscribed -> onIsSubscribedChange(subscribed) }
this@setupSubscriptionButton.text =
if (subscribed == true) context.getString(R.string.unsubscribe)
else context.getString(R.string.subscribe)
notificationBell?.isVisible = subscribed == true && notificationsEnabled
this@setupSubscriptionButton.isVisible = true
}
CoroutineScope(Dispatchers.IO).launch {
subscribed = isSubscribed ?: SubscriptionHelper.isSubscribed(channelId)
withContext(Dispatchers.Main) {
subscribed?.let { subscribed -> onIsSubscribedChange(subscribed) }
if (subscribed == true) {
this@setupSubscriptionButton.text = context.getString(R.string.unsubscribe)
} else {
notificationBell?.isGone = true
}
this@setupSubscriptionButton.isVisible = true
updateUIStateAndNotifyObservers()
}
}
notificationBell?.setupNotificationBell(channelId)
setOnClickListener {
if (subscribed == true) {
SubscriptionHelper.handleUnsubscribe(context, channelId, channelName) {
text = context.getString(R.string.subscribe)
notificationBell?.isGone = true
CoroutineScope(Dispatchers.Main).launch {
if (subscribed == true) {
val unsubscribeAction: (suspend () -> Unit) = {
withContext(Dispatchers.IO) {
SubscriptionHelper.unsubscribe(channelId)
}
subscribed = false
subscribed = false
onIsSubscribedChange(false)
}
} else {
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
updateUIStateAndNotifyObservers()
}
text = context.getString(R.string.unsubscribe)
notificationBell?.isVisible = PreferenceHelper
.getBoolean(PreferenceKeys.NOTIFICATION_ENABLED, true)
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
showUnsubscribeDialog(context, channelName) {
CoroutineScope(Dispatchers.Main).launch { unsubscribeAction() }
}
} else {
unsubscribeAction()
}
} else {
withContext(Dispatchers.IO) {
SubscriptionHelper.subscribe(
channelId,
channelName,
channelAvatar,
channelVerified
)
}
subscribed = true
onIsSubscribedChange(true)
updateUIStateAndNotifyObservers()
}
}
}
}
fun showUnsubscribeDialog(
context: Context,
channelName: String?,
onUnsubscribe: () -> Unit
) {
MaterialAlertDialogBuilder(context)
.setTitle(R.string.unsubscribe)
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
.setPositiveButton(R.string.unsubscribe) { _, _ ->
onUnsubscribe()
}
.setNegativeButton(R.string.cancel, null)
.show()
}