feat: guard automation scheduling by profile
This commit is contained in:
+10
-1
@@ -4,13 +4,22 @@ import com.flagship.abox.manager.domain.ProviderProfileStore
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||||
|
|
||||||
class AutomationBootstrapper(
|
class AutomationBootstrapper(
|
||||||
private val profileStore: ProviderProfileStore,
|
private val profileStore: ProviderProfileStore,
|
||||||
private val scheduler: AutomationScheduler,
|
private val scheduler: AutomationScheduler,
|
||||||
private val scope: CoroutineScope,
|
private val scope: CoroutineScope,
|
||||||
) {
|
) {
|
||||||
|
private var previousProfileId: ProviderProfileId? = null
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
scope.launch { profileStore.observeActiveProfile().collectLatest { scheduler.reconcileActiveProfile() } }
|
scope.launch {
|
||||||
|
profileStore.observeActiveProfile().collectLatest { active ->
|
||||||
|
previousProfileId?.takeIf { it != active?.id }?.let { scheduler.cancel(it) }
|
||||||
|
previousProfileId = active?.id
|
||||||
|
scheduler.reconcileActiveProfile()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -13,6 +13,7 @@ class WorkManagerAutomationScheduleStore(context: Context) : AutomationScheduleS
|
|||||||
private val workManager = WorkManager.getInstance(context)
|
private val workManager = WorkManager.getInstance(context)
|
||||||
|
|
||||||
override suspend fun replace(profileId: ProviderProfileId, scenes: List<Scene>): DomainResult<Unit> {
|
override suspend fun replace(profileId: ProviderProfileId, scenes: List<Scene>): DomainResult<Unit> {
|
||||||
|
if (scenes.isEmpty()) return remove(profileId)
|
||||||
val request = PeriodicWorkRequestBuilder<AutomationPollWorker>(15, TimeUnit.MINUTES)
|
val request = PeriodicWorkRequestBuilder<AutomationPollWorker>(15, TimeUnit.MINUTES)
|
||||||
.setInputData(androidx.work.Data.Builder().putString(KEY_PROFILE_ID, profileId.value).build())
|
.setInputData(androidx.work.Data.Builder().putString(KEY_PROFILE_ID, profileId.value).build())
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user