d6220b7bd7
- Add Edit icon to PairDetailScreen top bar - Wire onEdit callback through NavGraph to AddPairScreen with pairId - Manual "Sync now" (home card + detail screen) now ignores wifiOnly and chargingOnly constraints so it runs immediately on tap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
51 lines
1.8 KiB
Kotlin
51 lines
1.8 KiB
Kotlin
package com.syncflow.ui.home
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
import androidx.lifecycle.viewModelScope
|
|
import androidx.work.WorkManager
|
|
import com.syncflow.data.db.SyncPairDao
|
|
import com.syncflow.data.db.entities.SyncPairEntity
|
|
import com.syncflow.domain.model.ScheduleType
|
|
import com.syncflow.worker.SyncWorker
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
import kotlinx.coroutines.flow.SharingStarted
|
|
import kotlinx.coroutines.flow.stateIn
|
|
import kotlinx.coroutines.launch
|
|
import javax.inject.Inject
|
|
|
|
@HiltViewModel
|
|
class HomeViewModel @Inject constructor(
|
|
private val syncPairDao: SyncPairDao,
|
|
private val workManager: WorkManager,
|
|
) : ViewModel() {
|
|
|
|
val syncPairs = syncPairDao.observeAll()
|
|
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
|
|
|
fun triggerSync(pair: SyncPairEntity) {
|
|
val req = SyncWorker.buildOneTimeRequest(pair.id, wifiOnly = false, chargingOnly = false)
|
|
workManager.enqueue(req)
|
|
}
|
|
|
|
fun toggleEnabled(pair: SyncPairEntity) {
|
|
viewModelScope.launch {
|
|
syncPairDao.update(pair.copy(isEnabled = !pair.isEnabled))
|
|
if (!pair.isEnabled && pair.scheduleType != ScheduleType.MANUAL) {
|
|
val req = SyncWorker.buildPeriodicRequest(
|
|
pair.id,
|
|
pair.scheduleIntervalMinutes.toLong().coerceAtLeast(15),
|
|
pair.wifiOnly,
|
|
pair.chargingOnly,
|
|
)
|
|
workManager.enqueueUniquePeriodicWork(
|
|
"periodic_${pair.id}",
|
|
androidx.work.ExistingPeriodicWorkPolicy.UPDATE,
|
|
req,
|
|
)
|
|
} else {
|
|
workManager.cancelAllWorkByTag("sync_${pair.id}")
|
|
}
|
|
}
|
|
}
|
|
}
|