Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palette detail screen #8

Merged
merged 7 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Delete Palette color code refactoring.
  • Loading branch information
Akashkamble committed Mar 5, 2022
commit fb1a4e0d4c3f02b635062e8c42d4a6c58065a3cb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.akashk.palette.core.ui.components.PaletteCircularProgressIndicator
import com.akashk.palette.domain.data.Palette
import com.akashk.palette.palettedetail.components.ColorBox
import com.akashk.palette.palettedetail.components.ColorGrid
Expand All @@ -36,7 +37,8 @@ fun PaletteDetailScreen(
onSelectedColorIndex = { selectedIndex ->
viewModel.updateSelectedIndex(selectedIndex)
},
onAddColor = {},
onAddColor = { _ ->
},
onDeleteColor = {
viewModel.deleteSelectedColor()
},
Expand All @@ -52,50 +54,59 @@ fun PaletteDetailsContent(
modifier: Modifier = Modifier,
onDeletePalette: () -> Unit,
onSelectedColorIndex: (index: Int) -> Unit,
onAddColor: () -> Unit,
onAddColor: (palette: Palette) -> Unit,
onDeleteColor: () -> Unit,
onRenamePalette: () -> Unit
) {
/*LaunchedEffect(key1 = viewState){
Log.d("Test", viewState.toString())
}*/
Scaffold(
modifier = modifier
.navigationBarsPadding()
.statusBarsPadding(),
topBar = {
PaletteDetailsToolBar(
name = viewState.paletteName,
modifier = modifier,
onDeletePalette = onDeletePalette
)
},
bottomBar = {
PaletteDetailsBottomNavigationBar(
modifier = modifier,
onDeleteColor = onDeleteColor,
onAddColors = onAddColor,
onRenamePalette = onRenamePalette
)
},
) {
Column(
modifier = modifier
.fillMaxSize()
.padding(16.dp)
) {
ColorBox(
modifier = modifier,
color = viewState.paletteColorList[viewState.selectedIndex]
)
Spacer(modifier = Modifier.height(20.dp))
ColorGrid(
colorList = viewState.paletteColorList,
selectedIndex = viewState.selectedIndex,
modifier = modifier,
) { selectedIndex ->
onSelectedColorIndex.invoke(selectedIndex)
when (viewState) {
is PaletteDetailState.CurrentPalette -> {
Scaffold(
modifier = modifier
.navigationBarsPadding()
.statusBarsPadding(),
topBar = {
PaletteDetailsToolBar(
name = viewState.paletteName,
modifier = modifier,
onDeletePalette = onDeletePalette
)
},
bottomBar = {
PaletteDetailsBottomNavigationBar(
modifier = modifier,
onDeleteColor = onDeleteColor,
onAddColors = { onAddColor(viewState.palette) },
onRenamePalette = onRenamePalette
)
},
) {
val selectedColor = viewState.palette.colorList[viewState.selectedIndex]
Column(
modifier = modifier
.fillMaxSize()
.padding(16.dp)
) {
ColorBox(
modifier = modifier,
color = selectedColor
)
Spacer(modifier = Modifier.height(20.dp))
ColorGrid(
colorList = viewState.palette.colorList,
selectedIndex = viewState.selectedIndex,
modifier = modifier,
) { selectedIndex ->
onSelectedColorIndex.invoke(selectedIndex)
}
}
}
}
is PaletteDetailState.ErrorState -> {

}
is PaletteDetailState.IsLoading -> {
PaletteCircularProgressIndicator()
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.akashk.palette.palettedetail

import com.akashk.palette.core.ui.UIText
import com.akashk.palette.domain.data.Palette

data class PaletteDetailState(
val isLoading: Boolean = true,
val paletteColorList: MutableList<String> = mutableListOf(),
val paletteName: String = "",
val selectedIndex: Int = 0,
val errorMessage: UIText? = null
)

sealed class PaletteDetailState(open val paletteName: String) {
data class IsLoading(val flag: Boolean) : PaletteDetailState("")
data class CurrentPalette(
val palette: Palette,
val selectedIndex : Int = 0,
) : PaletteDetailState(paletteName = palette.name)
data class ErrorState(val error: UIText) : PaletteDetailState("")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ package com.akashk.palette.palettedetail

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.akashk.palette.core.Result
import com.akashk.palette.core.ui.UIText
import com.akashk.palette.destinations.PaletteDetailScreenDestination
import com.akashk.palette.domain.data.Palette
import com.akashk.palette.domain.data.PaletteRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlinx.coroutines.flow.update

@HiltViewModel
class PaletteDetailsViewModel @Inject constructor(
Expand All @@ -29,17 +23,11 @@ class PaletteDetailsViewModel @Inject constructor(

private val _viewState: MutableStateFlow<PaletteDetailState> =
MutableStateFlow(
PaletteDetailState()
PaletteDetailState.CurrentPalette(selectedPalette)
)
val viewState: StateFlow<PaletteDetailState> = _viewState

init {
val randomIndex = (0 until selectedPalette.colorList.size).random()
updateSelectedIndex(randomIndex)
fetchPaletteById(selectedPalette.id)
}

private fun fetchPaletteById(id: String) {
/*private fun fetchPaletteById(id: String) {
paletteRepository.fetchPaletteById(id)
.onEach { result ->
_viewState.value = getViewStateForDetails(result)
Expand All @@ -64,46 +52,32 @@ class PaletteDetailsViewModel @Inject constructor(
)
}
}
}
}*/

fun updateSelectedIndex(index: Int) {
_viewState.value = viewState.value.copy(selectedIndex = index)
val palette = (viewState.value as PaletteDetailState.CurrentPalette).palette
_viewState.value =
PaletteDetailState.CurrentPalette(palette = palette, selectedIndex = index)
}

// TODO : This function is changing state but in UI it is not reflecting.
fun deleteSelectedColor() {
viewModelScope.launch {
val index = viewState.value.selectedIndex
val list = viewState.value.paletteColorList
list.removeAt(index = index)
/*
* Without changing selected index viewstate is not updating
* hence I have to create new state with index 0 (i.e default value)
* and then again update the state with selected state.
* If user has selected 0th index then I have to update selected index as random index
* and after delay again update selected index as 0.
* Also, without delay updateSelectedIndex does not update viewstate.
*
* If you're reading this please fix this and create pull request 😂😂😂😂.
*/
/*------------Bad Code starts here-----------------*/
val newState = PaletteDetailState(
isLoading = false,
paletteColorList = list,
paletteName = selectedPalette.name
)
_viewState.value = newState
delay(10)
if (index == 0) {
val randomIndex = (0 until list.size).random()
updateSelectedIndex(index = randomIndex)
delay(10)
}
updateSelectedIndex(index = index)
/*------------Bad Code ends here-----------------*/

val palette = selectedPalette.copy(colorList = list)
paletteRepository.updatePalette(newPalette = palette)
var selectedIndex = (viewState.value as PaletteDetailState.CurrentPalette).selectedIndex
val currentPalette = (viewState.value as PaletteDetailState.CurrentPalette).palette
val list = currentPalette.colorList
list.removeAt(index = selectedIndex)
if (selectedIndex == currentPalette.colorList.size) {
selectedIndex -= 1
}
val newPalette = Palette(
id = selectedPalette.id,
name = selectedPalette.name,
colorList = list,
modifiedAt = System.currentTimeMillis()
)
_viewState.update {
PaletteDetailState.CurrentPalette(newPalette, selectedIndex)
}
paletteRepository.updatePalette(newPalette = newPalette)
}
}