package eu.friedcutlet.yokan.ui.screens.settings.player import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import eu.friedcutlet.yokan.services.files.LocalFileService import eu.friedcutlet.yokan.services.media.player.DefaultMpvConf import eu.friedcutlet.yokan.services.media.player.DefaultMpvInputConf import kotlinx.coroutines.CoroutineDispatcher import eu.friedcutlet.yokan.services.blockingDispatcher import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext internal class PlayerAdvancedSettingsViewModel( private val files: LocalFileService, private val workDispatcher: CoroutineDispatcher = blockingDispatcher, ) : ViewModel() { private val _redownloadStatus = MutableStateFlow(null) val redownloadStatus: StateFlow = _redownloadStatus.asStateFlow() fun redownloadMpvConfig() { viewModelScope.launch { withContext(workDispatcher) { files.writeBytesAsync("${files.cacheRoot}/mpv/mpv.conf", DefaultMpvConf.encodeToByteArray()) files.writeBytesAsync("${files.cacheRoot}/mpv/input.conf", DefaultMpvInputConf.encodeToByteArray()) } val message = "Wrote default mpv.conf and input.conf to ${files.cacheRoot}/mpv/" _redownloadStatus.update { message } } } }