DeveloperSettingsViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/developer/DeveloperSettingsViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.developer23import androidx.compose.runtime.Immutable4import androidx.lifecycle.ViewModel5import androidx.lifecycle.viewModelScope6import eu.friedcutlet.yokan.services.logging.Diagnostics7import eu.friedcutlet.yokan.services.logging.ImageStats8import eu.friedcutlet.yokan.services.logging.LogEntry9import eu.friedcutlet.yokan.services.logging.LogSink10import eu.friedcutlet.yokan.services.logging.NetworkStats11import eu.friedcutlet.yokan.services.system.ProcessMemoryUsage12import eu.friedcutlet.yokan.services.system.processMemoryUsage13import eu.friedcutlet.yokan.ui.image.ImageCacheUsage14import eu.friedcutlet.yokan.ui.image.imageCacheUsage15import eu.friedcutlet.yokan.util.nowMillis16import kotlinx.coroutines.CoroutineDispatcher17import kotlinx.coroutines.Dispatchers18import kotlinx.coroutines.delay19import kotlinx.coroutines.flow.SharingStarted20import kotlinx.coroutines.flow.StateFlow21import kotlinx.coroutines.flow.flow22import kotlinx.coroutines.flow.flowOn23import kotlinx.coroutines.flow.scan24import kotlinx.coroutines.flow.stateIn2526@Immutable27internal data class DeveloperDiagnosticsState(28 /** Oldest first; empty where the platform reports no heap numbers. */29 val memory: List<ProcessMemoryUsage> = emptyList(),30 val memorySupported: Boolean = true,31 val network: NetworkStats = NetworkStats(),32 val images: ImageStats = ImageStats(),33 val imageCache: ImageCacheUsage? = null,34 /** Newest first. */35 val logs: List<LogEntry> = emptyList(),36 /** Warnings and errors, newest first. */37 val problems: List<LogEntry> = emptyList(),38 val logFilePath: String? = null,39 val logFileBytes: Long = 0,40 val uptimeMillis: Long = 0,41)4243/** Samples process diagnostics once a second, only while the Developer screen is collecting. */44internal class DeveloperSettingsViewModel(45 workDispatcher: CoroutineDispatcher = Dispatchers.Default,46) : ViewModel() {47 val state: StateFlow<DeveloperDiagnosticsState> = flow {48 while (true) {49 emit(Unit)50 delay(SampleIntervalMillis)51 }52 }53 .scan(DeveloperDiagnosticsState()) { previous, _ -> sample(previous) }54 .flowOn(workDispatcher)55 .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), DeveloperDiagnosticsState())5657 fun clearLogs() = LogSink.clearMemory()5859 private fun sample(previous: DeveloperDiagnosticsState): DeveloperDiagnosticsState {60 val memory = processMemoryUsage()61 return DeveloperDiagnosticsState(62 memory = if (memory == null) emptyList() else (previous.memory + memory).takeLast(MemorySamples),63 memorySupported = memory != null,64 network = Diagnostics.network.value,65 images = Diagnostics.images.value,66 imageCache = imageCacheUsage(),67 logs = LogSink.recentEntries().asReversed(),68 problems = LogSink.problemEntries().asReversed(),69 logFilePath = LogSink.logFilePath(),70 logFileBytes = LogSink.logFileBytes(),71 uptimeMillis = nowMillis() - Diagnostics.startedAtMillis,72 )73 }7475 companion object {76 const val SampleIntervalMillis = 1_000L77 /** Two minutes of history at one sample per second. */78 const val MemorySamples = 12079 }80}81