fix: notification bell shown even though notifications disabled

This commit is contained in:
Bnyro 2024-06-14 18:50:38 +02:00
parent a692f8d8f1
commit cf4363d703
2 changed files with 19 additions and 8 deletions

View File

@ -13,11 +13,14 @@ fun MaterialButton.setupNotificationBell(channelId: String) {
} }
var isIgnorable = PreferenceHelper.isChannelNotificationIgnorable(channelId) var isIgnorable = PreferenceHelper.isChannelNotificationIgnorable(channelId)
setIconResource(if (isIgnorable) R.drawable.ic_bell else R.drawable.ic_notification) setIconResource(iconResource(isIgnorable))
setOnClickListener { setOnClickListener {
isIgnorable = !isIgnorable isIgnorable = !isIgnorable
PreferenceHelper.toggleIgnorableNotificationChannel(channelId) PreferenceHelper.toggleIgnorableNotificationChannel(channelId)
setIconResource(if (isIgnorable) R.drawable.ic_bell else R.drawable.ic_notification) setIconResource(iconResource(isIgnorable))
} }
} }
private fun iconResource(isIgnorable: Boolean) =
if (isIgnorable) R.drawable.ic_bell else R.drawable.ic_notification

View File

@ -5,11 +5,12 @@ 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.helpers.PreferenceHelper
import com.google.android.material.button.MaterialButton import com.google.android.material.button.MaterialButton
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
fun TextView.setupSubscriptionButton( fun TextView.setupSubscriptionButton(
@ -38,19 +39,26 @@ fun TextView.setupSubscriptionButton(
} }
notificationBell?.setupNotificationBell(channelId) notificationBell?.setupNotificationBell(channelId)
this.setOnClickListener {
setOnClickListener {
if (subscribed == true) { if (subscribed == true) {
SubscriptionHelper.handleUnsubscribe(context, channelId, channelName) { SubscriptionHelper.handleUnsubscribe(context, channelId, channelName) {
this.text = context.getString(R.string.subscribe) text = context.getString(R.string.subscribe)
notificationBell?.isGone = true notificationBell?.isGone = true
subscribed = false subscribed = false
onIsSubscribedChange(false) onIsSubscribedChange(false)
} }
} else { } else {
runBlocking { CoroutineScope(Dispatchers.Main).launch {
SubscriptionHelper.subscribe(channelId) withContext(Dispatchers.IO) {
SubscriptionHelper.subscribe(channelId)
}
text = context.getString(R.string.unsubscribe) text = context.getString(R.string.unsubscribe)
notificationBell?.isVisible = true notificationBell?.isVisible = PreferenceHelper
.getBoolean(PreferenceKeys.NOTIFICATION_ENABLED, true)
subscribed = true subscribed = true
onIsSubscribedChange(true) onIsSubscribedChange(true)
} }