Skip to content

Commit

Permalink
Don't load exclusions on startup
Browse files Browse the repository at this point in the history
The flow in `init` caused the exclusions to be automatically loaded everytime the class is initialized.
  • Loading branch information
d4rken committed Aug 1, 2024
1 parent f24cfce commit e37be8b
Showing 1 changed file with 6 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.plus
import javax.inject.Inject
Expand Down Expand Up @@ -70,15 +66,6 @@ class ExclusionManager @Inject constructor(
replay = 1
)


init {
userExclusions.flow
.drop(1)
.distinctUntilChanged()
.onEach { exclusionStorage.save(it) }
.launchIn(appScope + dispatcherProvider.IO)
}

suspend fun save(toSave: Set<Exclusion>): Collection<Exclusion> {
log(TAG) { "save(): $toSave" }
val newOrUpdated = mutableSetOf<Exclusion>()
Expand All @@ -92,6 +79,7 @@ class ExclusionManager @Inject constructor(
this
.filter { old -> newExclusions.none { old.id == it.id } }
.plus(newExclusions).toSet()
.also { exclusionStorage.save(it) }
}
return newOrUpdated
}
Expand All @@ -102,7 +90,11 @@ class ExclusionManager @Inject constructor(
val userTargets = userExclusions.flow.first().filter { ids.contains(it.id) }.toSet()
if (userTargets.isNotEmpty()) {
log(TAG) { "remove(): Removing user exclusions: $userTargets" }
userExclusions.updateBlocking { this - userTargets }
userExclusions.updateBlocking {
(this - userTargets).also {
exclusionStorage.save(it)
}
}
}

val defaultTargets = defaultExclusions.exclusions.first()
Expand Down

0 comments on commit e37be8b

Please sign in to comment.