Skip to content

Commit

Permalink
refactor(Notifications): remove duplicated subscription block
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Aug 24, 2024
1 parent 1870620 commit 4ebf690
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/shirabox/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -163,7 +160,6 @@ class ResourceActivity : ComponentActivity() {
@Composable
fun Resource(
id: Int,
type: ContentType,
context: Context,
model: ResourceViewModel = hiltViewModel(),
colorScheme: ColorScheme = MaterialTheme.colorScheme
Expand Down Expand Up @@ -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))
}

/**
Expand Down Expand Up @@ -459,7 +448,7 @@ fun Resource(
}

val firstTimestamp = remember {
releaseRange.first().getDuration() ?: "..."
releaseRange.firstOrNull()?.getDuration() ?: "..."
}
val secondTimestamp = remember {
releaseRange.getOrNull(1)?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 4ebf690

Please sign in to comment.