fix(scene): address automation review findings
This commit is contained in:
+22
-6
@@ -1,14 +1,18 @@
|
||||
package com.flagship.abox.manager.automation.runtime
|
||||
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||
import com.flagship.abox.manager.domain.ProviderProfileStore
|
||||
import com.flagship.abox.manager.domain.SessionCoordinator
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.delay
|
||||
import com.flagship.abox.manager.domain.ProviderProfileId
|
||||
import kotlinx.coroutines.CancellationException
|
||||
|
||||
class AutomationBootstrapper(
|
||||
private val profileStore: ProviderProfileStore,
|
||||
@@ -16,20 +20,24 @@ class AutomationBootstrapper(
|
||||
private val scheduler: AutomationScheduler,
|
||||
private val engine: AutomationEngine,
|
||||
private val scope: CoroutineScope,
|
||||
) {
|
||||
) : DefaultLifecycleObserver {
|
||||
private var previousProfileId: ProviderProfileId? = null
|
||||
private val isForeground = MutableStateFlow(false)
|
||||
|
||||
fun start() {
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
scope.launch {
|
||||
combine(
|
||||
profileStore.observeActiveProfile(),
|
||||
sessionCoordinator.observeActiveSession(),
|
||||
) { active, session -> active to session }.collectLatest { (active, session) ->
|
||||
isForeground,
|
||||
) { active, session, foreground -> Triple(active, session, foreground) }
|
||||
.collectLatest { (active, session, foreground) ->
|
||||
previousProfileId?.takeIf { it != active?.id }?.let { scheduler.cancel(it) }
|
||||
previousProfileId = active?.id
|
||||
if (active != null && session?.profileId == active.id) {
|
||||
scheduler.reconcile(active.id)
|
||||
while (true) {
|
||||
while (foreground) {
|
||||
try {
|
||||
engine.evaluate(active.id)
|
||||
} catch (cancelled: CancellationException) {
|
||||
@@ -43,4 +51,12 @@ class AutomationBootstrapper(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
isForeground.value = true
|
||||
}
|
||||
|
||||
override fun onStop(owner: LifecycleOwner) {
|
||||
isForeground.value = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user