From 4ebf6907863f1725ff52153d0fd9df7d3fed2346 Mon Sep 17 00:00:00 2001 From: urFate Date: Sat, 24 Aug 2024 11:05:01 +0300 Subject: [PATCH] refactor(Notifications): remove duplicated subscription block --- app/src/main/java/org/shirabox/app/App.kt | 3 +- .../ui/activity/resource/ResourceActivity.kt | 17 ++--------- .../ui/activity/resource/ResourceViewModel.kt | 28 ++++++++----------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/shirabox/app/App.kt b/app/src/main/java/org/shirabox/app/App.kt index 459f74b..37fee3d 100644 --- a/app/src/main/java/org/shirabox/app/App.kt +++ b/app/src/main/java/org/shirabox/app/App.kt @@ -12,6 +12,7 @@ import dagger.hilt.android.HiltAndroidApp import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import org.shirabox.core.db.AppDatabase @@ -40,7 +41,7 @@ class App : Application(), ImageLoaderFactory { // Subscribe to notifications scope.launch { - db.contentDao().getFavourites().collect { list -> + db.contentDao().getFavourites().collectLatest { list -> list.forEach { favouriteAnime -> if (favouriteAnime.shiraboxId != null) { val topic = "id-${favouriteAnime.shiraboxId}" diff --git a/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceActivity.kt b/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceActivity.kt index 7adf31b..16e3aed 100644 --- a/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceActivity.kt +++ b/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceActivity.kt @@ -136,21 +136,18 @@ class ResourceActivity : ComponentActivity() { val activity = context as Activity? var resourceId: Int = -1 - lateinit var type: ContentType try { val arguments = intent.extras!! resourceId = arguments.getInt("id") - type = arguments.getString("type").toString() - .let { ContentType.fromString(it) } } catch (ex: Exception) { ex.printStackTrace() activity?.finish() Toast.makeText(context, ex.localizedMessage, Toast.LENGTH_LONG).show() } - Resource(resourceId, type, LocalContext.current) + Resource(resourceId, LocalContext.current) } } } @@ -163,7 +160,6 @@ class ResourceActivity : ComponentActivity() { @Composable fun Resource( id: Int, - type: ContentType, context: Context, model: ResourceViewModel = hiltViewModel(), colorScheme: ColorScheme = MaterialTheme.colorScheme @@ -384,14 +380,7 @@ fun Resource( modifier = Modifier.size(24.dp) ) Spacer(Modifier.size(ButtonDefaults.IconSpacing)) - Text( - stringResource( - id = when (type) { - ContentType.ANIME -> R.string.watch - else -> R.string.read - } - ) - ) + Text(stringResource(id = R.string.watch)) } /** @@ -459,7 +448,7 @@ fun Resource( } val firstTimestamp = remember { - releaseRange.first().getDuration() ?: "..." + releaseRange.firstOrNull()?.getDuration() ?: "..." } val secondTimestamp = remember { releaseRange.getOrNull(1)?.let { diff --git a/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceViewModel.kt b/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceViewModel.kt index fe7c137..2246d17 100644 --- a/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceViewModel.kt +++ b/app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceViewModel.kt @@ -6,8 +6,6 @@ import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.google.firebase.ktx.Firebase -import com.google.firebase.messaging.ktx.messaging import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.Dispatchers @@ -200,38 +198,34 @@ class ResourceViewModel @Inject constructor(@ApplicationContext context: Context fun switchFavouriteStatus(context: Context) { viewModelScope.launch(Dispatchers.IO) { - isFavourite.value = !isFavourite.value + isFavourite.value = isFavourite.value.not() - - val subscriptionAllowed = - AppDataStore.read(context, DataStoreScheme.FIELD_SUBSCRIPTION.key).firstOrNull() - ?: DataStoreScheme.FIELD_SUBSCRIPTION.defaultValue - - if(subscriptionAllowed) switchNotificationsStatus(forcedValue = true) - - launch { + launch(Dispatchers.IO) { + println("INTERNAL CONTENT UID: ${internalContentUid.longValue}") if(internalContentUid.longValue > -1) { val content = db.contentDao().getContentByUid(internalContentUid.longValue) db.contentDao().updateContents(content.copy(isFavourite = isFavourite.value)) } } + + launch(Dispatchers.IO) { + val subscriptionAllowed = + AppDataStore.read(context, DataStoreScheme.FIELD_SUBSCRIPTION.key).firstOrNull() + ?: DataStoreScheme.FIELD_SUBSCRIPTION.defaultValue + + if(subscriptionAllowed) switchNotificationsStatus(forcedValue = true) + } } } fun switchNotificationsStatus(forcedValue: Boolean? = null) { viewModelScope.launch(Dispatchers.IO) { fun switchValue(id: Int, value: Boolean) { - val topic = "id-${id}" - db.contentDao().updateContents( db.contentDao().getContentByShiraboxId(id) .copy(episodesNotifications = value) ) - if (value) { - Firebase.messaging.subscribeToTopic(topic) - } else Firebase.messaging.unsubscribeFromTopic(topic) - episodesNotifications.value = value }