mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
fix: subscription button state not updated after starting next video
This commit is contained in:
parent
60c6e9348a
commit
9a537eb0ec
@ -61,31 +61,4 @@ object SubscriptionHelper {
|
|||||||
|
|
||||||
suspend fun submitFeedItemChange(feedItem: SubscriptionsFeedItem) =
|
suspend fun submitFeedItemChange(feedItem: SubscriptionsFeedItem) =
|
||||||
feedRepository.submitFeedItemChange(feedItem)
|
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package com.github.libretube.ui.extensions
|
package com.github.libretube.ui.extensions
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.core.view.isGone
|
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import com.google.android.material.button.MaterialButton
|
import com.google.android.material.button.MaterialButton
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -24,47 +25,78 @@ fun TextView.setupSubscriptionButton(
|
|||||||
) {
|
) {
|
||||||
if (channelId == null) return
|
if (channelId == null) return
|
||||||
|
|
||||||
|
val notificationsEnabled = PreferenceHelper
|
||||||
|
.getBoolean(PreferenceKeys.NOTIFICATION_ENABLED, true)
|
||||||
var subscribed: Boolean? = false
|
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 {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
subscribed = isSubscribed ?: SubscriptionHelper.isSubscribed(channelId)
|
subscribed = isSubscribed ?: SubscriptionHelper.isSubscribed(channelId)
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
subscribed?.let { subscribed -> onIsSubscribedChange(subscribed) }
|
updateUIStateAndNotifyObservers()
|
||||||
|
|
||||||
if (subscribed == true) {
|
|
||||||
this@setupSubscriptionButton.text = context.getString(R.string.unsubscribe)
|
|
||||||
} else {
|
|
||||||
notificationBell?.isGone = true
|
|
||||||
}
|
|
||||||
this@setupSubscriptionButton.isVisible = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationBell?.setupNotificationBell(channelId)
|
notificationBell?.setupNotificationBell(channelId)
|
||||||
|
|
||||||
setOnClickListener {
|
setOnClickListener {
|
||||||
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
if (subscribed == true) {
|
if (subscribed == true) {
|
||||||
SubscriptionHelper.handleUnsubscribe(context, channelId, channelName) {
|
val unsubscribeAction: (suspend () -> Unit) = {
|
||||||
text = context.getString(R.string.subscribe)
|
withContext(Dispatchers.IO) {
|
||||||
notificationBell?.isGone = true
|
SubscriptionHelper.unsubscribe(channelId)
|
||||||
|
}
|
||||||
subscribed = false
|
subscribed = false
|
||||||
onIsSubscribedChange(false)
|
|
||||||
|
updateUIStateAndNotifyObservers()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
||||||
|
showUnsubscribeDialog(context, channelName) {
|
||||||
|
CoroutineScope(Dispatchers.Main).launch { unsubscribeAction() }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
unsubscribeAction()
|
||||||
withContext(Dispatchers.IO) {
|
}
|
||||||
SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
|
} else {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
SubscriptionHelper.subscribe(
|
||||||
|
channelId,
|
||||||
|
channelName,
|
||||||
|
channelAvatar,
|
||||||
|
channelVerified
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
text = context.getString(R.string.unsubscribe)
|
|
||||||
notificationBell?.isVisible = PreferenceHelper
|
|
||||||
.getBoolean(PreferenceKeys.NOTIFICATION_ENABLED, true)
|
|
||||||
|
|
||||||
subscribed = true
|
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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user