Skip to content

Commit

Permalink
exceptions update without a button
Browse files Browse the repository at this point in the history
  • Loading branch information
Sal7one committed Mar 14, 2022
1 parent 0b66cd9 commit bae1d8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.sal7one.musicswitcher.compose

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
Expand Down Expand Up @@ -36,7 +35,7 @@ fun OptionScreen() {
val dataStoreProvider = DataStoreProvider(context.applicationContext).getInstance()
val viewModel: OptionsViewModel =
viewModel(factory = OptionsViewModelFactory(dataStoreProvider))
val theScreenUiState = viewModel.OptionScreenState.collectAsState()
val theScreenUiState = viewModel.optionScreenState.collectAsState()

val appleMusicChoice = theScreenUiState.value.appleMusic
val spotifyChoice = theScreenUiState.value.spotify
Expand Down Expand Up @@ -98,42 +97,30 @@ fun OptionScreen() {
}
Spacer(modifier = Modifier.height(10.dp))
Divider()
Spacer(modifier = Modifier.height(5.dp))
OptionList(musicProviders[0], appleMusicChoice) {
viewModel.changeValue(appleMusic = appleMusicChoice)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}
OptionList(musicProviders[1], spotifyChoice) {
viewModel.changeValue(spotify = spotifyChoice)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}
OptionList(musicProviders[2], anghamiChoice) {
viewModel.changeValue(anghami = anghamiChoice)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}
OptionList(musicProviders[3], ytMusicChoice) {
viewModel.changeValue(ytMusic = ytMusicChoice)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}
OptionList(musicProviders[4], deezerChoice) {
viewModel.changeValue(deezer = deezerChoice)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}

Spacer(modifier = Modifier.height(10.dp))
Spacer(modifier = Modifier.height(5.dp))
Divider()
Spacer(modifier = Modifier.height(40.dp))
Box(modifier = Modifier.clickable {

}) {
UpdateButton(
stringResource(R.string.optionScreen_update_Exceptions)
) {
viewModel.saveExceptions(
appleMusic = appleMusicChoice,
spotify = spotifyChoice,
anghami = anghamiChoice,
ytMusic = ytMusicChoice,
deezer = deezerChoice
)
Toast.makeText(context, "Updated Exceptions", Toast.LENGTH_LONG).show()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sal7one.musicswitcher.viewmodels;
package com.sal7one.musicswitcher.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand All @@ -16,8 +16,8 @@ class OptionsViewModel(
private val dataStoreManager: DataStoreProvider
) : ViewModel() {

private val _OptionsViewModel = MutableStateFlow(TheScreenUiData())
val OptionScreenState: StateFlow<TheScreenUiData> = _OptionsViewModel
private val _optionsViewModelStateFlow = MutableStateFlow(TheScreenUiData())
val optionScreenState: StateFlow<TheScreenUiData> = _optionsViewModelStateFlow


init {
Expand Down Expand Up @@ -47,7 +47,7 @@ class OptionsViewModel(
ytMusic: Boolean? = null,
deezer: Boolean? = null,
) {
_OptionsViewModel.update {
_optionsViewModelStateFlow.update {
when {
appleMusic != null -> it.copy(appleMusic = !appleMusic)
spotify != null -> it.copy(spotify = !spotify)
Expand All @@ -57,8 +57,17 @@ class OptionsViewModel(
else -> it.copy()
}
}
// After changing ui state - Save values into Data store Directly
saveExceptions(
_optionsViewModelStateFlow.value.appleMusic,
_optionsViewModelStateFlow.value.spotify,
_optionsViewModelStateFlow.value.anghami,
_optionsViewModelStateFlow.value.ytMusic,
_optionsViewModelStateFlow.value.deezer,
)
}


private fun getData() = viewModelScope.launch(Dispatchers.IO) { // TODO Find Solution to this
dataStoreManager.getFromDataStore().collect {
val appleMusic = it[DataStoreProvider.StoredKeys.appleMusicException] ?: false
Expand All @@ -67,7 +76,7 @@ class OptionsViewModel(
val ytMusic = it[DataStoreProvider.StoredKeys.ytMusicException] ?: false
val deezer = it[DataStoreProvider.StoredKeys.deezerException] ?: false

_OptionsViewModel.value = TheScreenUiData(
_optionsViewModelStateFlow.value = TheScreenUiData(
appleMusic = appleMusic,
spotify = spotify,
anghami = anghami,
Expand All @@ -76,4 +85,4 @@ class OptionsViewModel(
)
}
}
}
}

0 comments on commit bae1d8a

Please sign in to comment.