EARHORNMUSIC UPnP · Android TV · Phones · Android Auto · GNU/Linux
Release 0.5.0

SettingsViewModel.kt

package dev.stave.ui.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.stave.data.LyricsHighlightColor
import dev.stave.data.ReplayGainMode
import dev.stave.data.SettingsStore
import dev.stave.domain.model.MetadataField
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SettingsViewModel @Inject constructor(
    private val store: SettingsStore,
    private val upnpHolder: dev.stave.upnp.UpnpServiceHolder,
) : ViewModel() {

    val enabled: StateFlow<Set<MetadataField>> = store.enabledFields.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = MetadataField.defaults
    )

    val marqueeEnabled: StateFlow<Boolean> = store.marqueeEnabled.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = false,
    )

    val kioskFlipEnabled: StateFlow<Boolean> = store.kioskFlipEnabled.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = true,
    )

    val domEthaUrl: StateFlow<String?> = store.domEthaUrl.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = null,
    )

    val cornerButtonDomEtha: StateFlow<Boolean> = store.cornerButtonDomEtha.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = false,
    )

    val useFraunces: StateFlow<Boolean> = store.useFraunces.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = true,
    )

    val replayGainMode: StateFlow<ReplayGainMode> = store.replayGainMode.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = ReplayGainMode.OFF,
    )

    val replayGainPreAmpDb: StateFlow<Float> = store.replayGainPreAmpDb.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = 3f,
    )

    val randomExcludedGenres: StateFlow<Set<String>> = store.randomExcludedGenres.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = emptySet(),
    )

    val lyricsHighlightSize: StateFlow<Float> = store.lyricsHighlightSize.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = 1.5f,
    )

    val lyricsHighlightBold: StateFlow<Boolean> = store.lyricsHighlightBold.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = false,
    )

    val lyricsHighlightColor: StateFlow<LyricsHighlightColor> = store.lyricsHighlightColor.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = LyricsHighlightColor.AMBER,
    )

    val allowExternalCalls: StateFlow<Boolean> = store.allowExternalCalls.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = true,
    )

    val lyricsMetadataEnabled: StateFlow<Boolean> = store.lyricsMetadataEnabled.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = true,
    )

    val rendererBroadcastEnabled: StateFlow<Boolean> = store.rendererBroadcastEnabled.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = false,
    )

    val activeRendererUdn: StateFlow<String?> = store.activeRendererUdn.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = null,
    )

    val renderers: StateFlow<List<dev.stave.domain.model.RemoteRenderer>> =
        upnpHolder.renderers

    val leftyMode: StateFlow<Boolean> = store.leftyMode.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = false,
    )

    val rendererFriendlyName: StateFlow<String?> = store.rendererFriendlyName.stateIn(
        scope = viewModelScope,
        started = SharingStarted.Eagerly,
        initialValue = null,
    )

    fun toggle(field: MetadataField) {
        viewModelScope.launch { store.toggle(field) }
    }

    fun setMarquee(enabled: Boolean) {
        viewModelScope.launch { store.setMarqueeEnabled(enabled) }
    }

    fun setKioskFlipEnabled(enabled: Boolean) {
        viewModelScope.launch { store.setKioskFlipEnabled(enabled) }
    }

    fun setDomEthaUrl(url: String?) {
        viewModelScope.launch { store.setDomEthaUrl(url) }
    }

    fun setCornerButtonDomEtha(enabled: Boolean) {
        viewModelScope.launch { store.setCornerButtonDomEtha(enabled) }
    }

    fun setUseFraunces(enabled: Boolean) {
        viewModelScope.launch { store.setUseFraunces(enabled) }
    }

    fun setReplayGainMode(mode: ReplayGainMode) {
        viewModelScope.launch { store.setReplayGainMode(mode) }
    }

    fun setReplayGainPreAmpDb(db: Float) {
        viewModelScope.launch { store.setReplayGainPreAmpDb(db) }
    }

    fun addExcludedGenre(genre: String) {
        viewModelScope.launch { store.addExcludedGenre(genre) }
    }

    fun removeExcludedGenre(genre: String) {
        viewModelScope.launch { store.removeExcludedGenre(genre) }
    }

    fun setLyricsHighlightSize(multiplier: Float) {
        viewModelScope.launch { store.setLyricsHighlightSize(multiplier) }
    }

    fun setLyricsHighlightBold(bold: Boolean) {
        viewModelScope.launch { store.setLyricsHighlightBold(bold) }
    }

    fun setLyricsHighlightColor(color: LyricsHighlightColor) {
        viewModelScope.launch { store.setLyricsHighlightColor(color) }
    }

    fun setLyricsMetadata(enabled: Boolean) {
        viewModelScope.launch { store.setLyricsMetadataEnabled(enabled) }
    }

    fun setAllowExternalCalls(allow: Boolean) {
        viewModelScope.launch { store.setAllowExternalCalls(allow) }
    }

    fun setRendererBroadcast(enabled: Boolean) {
        viewModelScope.launch { store.setRendererBroadcastEnabled(enabled) }
    }

    fun setActiveRenderer(udn: String?) {
        viewModelScope.launch { store.setActiveRendererUdn(udn) }
    }
    fun setLeftyMode(enabled: Boolean) {
        viewModelScope.launch { store.setLeftyMode(enabled) }
    }
    fun setRendererFriendlyName(name: String?) {
        viewModelScope.launch { store.setRendererFriendlyName(name?.takeIf { it.isNotBlank() }) }
    }
}