feat: guard automation scheduling by profile

This commit is contained in:
2026-08-26 20:29:13 +08:00
parent fd67820c63
commit 5a25c31b39
2 changed files with 11 additions and 1 deletions
@@ -4,13 +4,22 @@ import com.flagship.abox.manager.domain.ProviderProfileStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import com.flagship.abox.manager.domain.ProviderProfileId
class AutomationBootstrapper(
private val profileStore: ProviderProfileStore,
private val scheduler: AutomationScheduler,
private val scope: CoroutineScope,
) {
private var previousProfileId: ProviderProfileId? = null
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()
}
}
}
}
@@ -13,6 +13,7 @@ class WorkManagerAutomationScheduleStore(context: Context) : AutomationScheduleS
private val workManager = WorkManager.getInstance(context)
override suspend fun replace(profileId: ProviderProfileId, scenes: List<Scene>): DomainResult<Unit> {
if (scenes.isEmpty()) return remove(profileId)
val request = PeriodicWorkRequestBuilder<AutomationPollWorker>(15, TimeUnit.MINUTES)
.setInputData(androidx.work.Data.Builder().putString(KEY_PROFILE_ID, profileId.value).build())
.build()