From c8f670bbe6a65120163cf09b11c9311d584b9d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Dan=C3=AD=C4=8Dek?= Date: Mon, 13 Nov 2023 14:04:09 +0100 Subject: [PATCH 1/2] Using productEdit screen for adding products --- .../thebeercounter/featureCounter/Koin.kt | 2 +- .../scene/counter/CounterCommand.kt | 2 +- .../scene/counter/CounterEvent.kt | 1 - .../scene/counter/CounterScreen.kt | 18 ++--- .../scene/counter/CounterViewModel.kt | 15 +--- .../counter/dialog/AddNewProductDialog.kt | 78 ------------------- .../scene/dashboard/DashboardNavigation.kt | 5 +- .../featureCounter/scene/edit/EditScreen.kt | 5 +- .../scene/edit/EditScreenViewModel.kt | 44 ++++++++--- 9 files changed, 47 insertions(+), 123 deletions(-) delete mode 100644 featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/dialog/AddNewProductDialog.kt diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/Koin.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/Koin.kt index 7ab9b49..df8117b 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/Koin.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/Koin.kt @@ -16,7 +16,7 @@ val featureCounterKoinModule = module { CounterScreenViewModel(get(), androidApplication().resources) } - viewModel { (productId: Int) -> + viewModel { (productId: Int?) -> EditScreenViewModel(productId, get()) } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterCommand.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterCommand.kt index 0d7bbe5..2504df1 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterCommand.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterCommand.kt @@ -10,7 +10,7 @@ import cz.damat.thebeercounter.commonlib.room.entity.Product sealed class CounterCommand : ViewCommand { object ShowClearAllConfirmDialog : CounterCommand() - object ShowAddNewDialog : CounterCommand() + object OpenAdd : CounterCommand() object PerformHapticFeedback : CounterCommand() data class ShowSetCountDialog(val product: Product) : CounterCommand() data class OpenEdit(val productId: Int) : CounterCommand() diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterEvent.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterEvent.kt index 149590b..11989ad 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterEvent.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterEvent.kt @@ -12,7 +12,6 @@ sealed class CounterEvent : ViewEvent { object OnClearAllClicked : CounterEvent() object OnClearAllConfirmed : CounterEvent() object OnAddNewClicked : CounterEvent() - data class OnNewProductAdded(val name : String) : CounterEvent() data class OnProductClicked(val id: Int) : CounterEvent() data class OnMenuItemClicked(val menuItem: MenuItem, val id: Int) : CounterEvent() data class OnCountSet(val id: Int, val count : Int) : CounterEvent() diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterScreen.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterScreen.kt index 30e6a41..c5bfa05 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterScreen.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterScreen.kt @@ -29,10 +29,10 @@ import cz.damat.thebeercounter.commonUI.utils.collectCommand import cz.damat.thebeercounter.commonUI.utils.collectStateWithLifecycle import cz.damat.thebeercounter.commonUI.utils.getOnEvent import cz.damat.thebeercounter.commonlib.room.entity.Product -import cz.damat.thebeercounter.featureCounter.scene.counter.dialog.AddNewProductDialog import cz.damat.thebeercounter.featureCounter.scene.counter.dialog.SetCountDialog import cz.damat.thebeercounter.commonUI.compose.theme.disabled import cz.damat.thebeercounter.commonUI.compose.utils.vibrateStrong +import cz.damat.thebeercounter.featureCounter.scene.dashboard.RouteArgId import cz.damat.thebeercounter.featureCounter.scene.dashboard.RouteEdit import org.koin.androidx.compose.get import java.text.NumberFormat @@ -79,10 +79,6 @@ private fun CommandCollector( mutableStateOf(false) } - val showAddNewDialog = remember { - mutableStateOf(false) - } - val view = LocalView.current viewModel.collectCommand { @@ -91,10 +87,13 @@ private fun CommandCollector( showSetCountDialogForProduct.value = it.product } CounterCommand.ShowClearAllConfirmDialog -> showClearAllConfirmDialog.value = true - CounterCommand.ShowAddNewDialog -> showAddNewDialog.value = true CounterCommand.PerformHapticFeedback -> { view.vibrateStrong() } + CounterCommand.OpenAdd -> { + navController.navigate("$RouteEdit/-1") + + } is CounterCommand.OpenEdit -> { navController.navigate("$RouteEdit/${it.productId}") } @@ -110,13 +109,6 @@ private fun CommandCollector( showDialog = showClearAllConfirmDialog, onEvent = onEvent ) - - AddNewProductDialog( - showDialog = showAddNewDialog, - onNewProductCreated = { - onEvent(CounterEvent.OnNewProductAdded(it)) - } - ) } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterViewModel.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterViewModel.kt index edc42dd..fc4ece1 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterViewModel.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/CounterViewModel.kt @@ -47,7 +47,6 @@ class CounterScreenViewModel( CounterEvent.OnClearAllClicked -> onClearAllClicked() CounterEvent.OnClearAllConfirmed -> onClearAllConfirmed() CounterEvent.OnAddNewClicked -> onAddNewClicked() - is CounterEvent.OnNewProductAdded -> onNewProductAdded(event.name) } } @@ -98,19 +97,7 @@ class CounterScreenViewModel( private fun onAddNewClicked() { ioScope.launch { productRepository.getShownProductsFlow() - sendCommand(CounterCommand.ShowAddNewDialog) - } - } - - private fun onNewProductAdded(name: String) { - ioScope.launch { - val product = Product( - name = name, - count = 0, - shown = true, - price = null, - ) - productRepository.saveProductAndIncrementCount(product) + sendCommand(CounterCommand.OpenAdd) } } } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/dialog/AddNewProductDialog.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/dialog/AddNewProductDialog.kt deleted file mode 100644 index 9ac90c6..0000000 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/counter/dialog/AddNewProductDialog.kt +++ /dev/null @@ -1,78 +0,0 @@ -package cz.damat.thebeercounter.featureCounter.scene.counter.dialog - -import androidx.compose.foundation.text.KeyboardActions -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.material.OutlinedTextField -import androidx.compose.material.Text -import androidx.compose.runtime.* -import androidx.compose.ui.Modifier -import androidx.compose.ui.focus.FocusRequester -import androidx.compose.ui.focus.focusRequester -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.text.input.KeyboardCapitalization -import androidx.compose.ui.text.input.TextFieldValue -import cz.damat.thebeercounter.commonUI.compose.component.DialogThemed -import cz.damat.thebeercounter.commonUI.compose.utils.shake -import cz.damat.thebeercounter.commonUI.R - -@Composable -fun AddNewProductDialog(showDialog: MutableState, onNewProductCreated: (String) -> Unit) { - if (!showDialog.value) return - - //todo - selection of existing products (autocomplete?) so that we don't have duplicates and history of products is consistent and usable for more advanced statistics in the future - //todo - way of setting price of the product - val textFieldValue = remember { - mutableStateOf(TextFieldValue()) - } - - val shake = remember { - mutableStateOf(false) - } - - val focusRequester = remember { FocusRequester() } - - val onConfirmClick: () -> Unit = { - val name = textFieldValue.value.text - if (name.isEmpty()) { - shake.value = true - } else { - onNewProductCreated(name.trim()) - showDialog.value = false - } - } - - DialogThemed( - showDialog = null, - confirmString = stringResource(id = R.string.action_add), - dismissString = stringResource(id = R.string.cancel), - onConfirmClick = onConfirmClick, - onDismissClick = { - showDialog.value = false - }, - content = { - OutlinedTextField( - label = { - Text(stringResource(id = R.string.name)) - }, - modifier = Modifier - .shake(shake) - .focusRequester(focusRequester), - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done, capitalization = KeyboardCapitalization.Sentences), - keyboardActions = KeyboardActions( - onDone = { - onConfirmClick() - } - ), - value = textFieldValue.value, - onValueChange = { tfv -> - textFieldValue.value = tfv - } - ) - - LaunchedEffect(Unit) { - focusRequester.requestFocus() - } - } - ) -} \ No newline at end of file diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/dashboard/DashboardNavigation.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/dashboard/DashboardNavigation.kt index 1eb65a9..18f9cf7 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/dashboard/DashboardNavigation.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/dashboard/DashboardNavigation.kt @@ -85,9 +85,8 @@ private fun NavigationHost(navController: NavHostController, paddingValues: Padd }, ) ) { entry -> - entry.arguments?.getInt(RouteArgId)?.let { - EditScreen(navController = navController, productId = it) - } + val productId = entry.arguments?.getInt(RouteArgId)?.takeIf { it > 0 } + EditScreen(navController = navController, productId = productId) } } } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt index ebae56b..7eb9c58 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt @@ -33,6 +33,7 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.vectorResource import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.navigation.NavController @@ -55,7 +56,7 @@ import org.koin.core.parameter.parametersOf @Composable fun EditScreen( navController: NavController, - productId: Int, + productId: Int?, ) { val viewModel: EditScreenViewModel = getViewModel() { parametersOf(productId) @@ -142,6 +143,8 @@ fun EditForm( modifier = Modifier.fillMaxWidth(), label = stringResource(id = R.string.name), value = productName, + hasNextDownImeAction = true, + keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences), onValueChanged = { name -> onEvent(EditEvent.OnProductNameChange(name)) } ) diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt index 6db69e5..b059297 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt @@ -3,6 +3,7 @@ package cz.damat.thebeercounter.featureCounter.scene.edit import cz.damat.thebeercounter.commonUI.R import cz.damat.thebeercounter.commonUI.base.BaseViewModel import cz.damat.thebeercounter.commonUI.base.State +import cz.damat.thebeercounter.commonlib.room.entity.Product import cz.damat.thebeercounter.componentCounter.domain.repository.ProductRepository import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -11,21 +12,30 @@ import kotlinx.coroutines.launch * Created by MD on 07.11.22. */ class EditScreenViewModel( - private val productId: Int, + private val productId: Int?, private val productRepository: ProductRepository, ) : BaseViewModel(EditViewState()) { init { ioScope.launch { - val product = productRepository.getProduct(productId) + if (productId != null) { + val product = productRepository.getProduct(productId) - if (product == null) { - updateState { copy(state = State.Error(R.string.error_product_not_found)) } + if (product == null) { + updateState { copy(state = State.Error(R.string.error_product_not_found)) } + } else { + updateState { + copy( + productName = product.name, + productCount = product.count.toString(), + state = State.Content + ) + } + } } else { updateState { copy( - productName = product.name, - productCount = product.count.toString(), + productCount = "1", state = State.Content ) } @@ -47,11 +57,23 @@ class EditScreenViewModel( ioScope.launch { updateState { copy(state = State.Loading) } - productRepository.updateProductNameAndSetCount( - productId, - it.productName, - it.productCount.toIntOrNull() ?: 0 //todo validations - ) + val count = it.productCount.toIntOrNull() ?: 0 // todo validations + + if (productId != null) { + productRepository.updateProductNameAndSetCount( + productId, + it.productName, + count + ) + } else { + val product = Product( + name = it.productName, + count = count, + shown = true, + price = null, + ) + productRepository.saveProductAndIncrementCount(product) + } updateState { copy(state = State.Content) } sendCommand(EditCommand.NavigateBack) From 798e346311018d37b27861f451fc76b27a6caf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Dan=C3=AD=C4=8Dek?= Date: Mon, 13 Nov 2023 14:38:17 +0100 Subject: [PATCH 2/2] ProductEdit/Add improvements --- commonUI/src/main/res/values/strings.xml | 1 + .../repositoryImpl/ProductRepositoryImpl.kt | 9 ++--- .../domain/repository/ProductRepository.kt | 2 +- .../featureCounter/scene/edit/EditScreen.kt | 36 ++++++++++++++++--- .../scene/edit/EditScreenViewModel.kt | 20 +++++------ .../scene/edit/EditViewState.kt | 6 +++- 6 files changed, 51 insertions(+), 23 deletions(-) diff --git a/commonUI/src/main/res/values/strings.xml b/commonUI/src/main/res/values/strings.xml index 75d5d44..5624ce2 100644 --- a/commonUI/src/main/res/values/strings.xml +++ b/commonUI/src/main/res/values/strings.xml @@ -27,6 +27,7 @@ Name Beer Do you really want to delete all items? + Add product Edit product Product not found \ No newline at end of file diff --git a/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/data/repositoryImpl/ProductRepositoryImpl.kt b/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/data/repositoryImpl/ProductRepositoryImpl.kt index 8456777..31424a4 100644 --- a/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/data/repositoryImpl/ProductRepositoryImpl.kt +++ b/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/data/repositoryImpl/ProductRepositoryImpl.kt @@ -9,9 +9,7 @@ import cz.damat.thebeercounter.commonlib.room.entity.HistoryItemType import cz.damat.thebeercounter.commonlib.room.entity.INITIAL_ITEM_ID import cz.damat.thebeercounter.commonlib.room.entity.Product import cz.damat.thebeercounter.componentCounter.domain.repository.ProductRepository -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first -import kotlinx.coroutines.withContext /** @@ -29,11 +27,8 @@ class ProductRepositoryImpl(private val db: AppDatabase, private val productDao: //todo - use for allowing the user to delete currently not shown products override fun getNotShownProductsFlow() = productDao.getProductsFlow(false) - override suspend fun saveProductAndIncrementCount(product: Product) { - db.withTransaction { - val newProductId = productDao.upsert(product) - incrementProductCount(newProductId.toInt()) - } + override suspend fun saveProduct(product: Product) { + productDao.upsert(product) } override suspend fun incrementProductCount(id: Int) { diff --git a/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/domain/repository/ProductRepository.kt b/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/domain/repository/ProductRepository.kt index 4139204..9bba512 100644 --- a/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/domain/repository/ProductRepository.kt +++ b/componentCounter/src/main/java/cz/damat/thebeercounter/componentCounter/domain/repository/ProductRepository.kt @@ -19,7 +19,7 @@ interface ProductRepository : KoinComponent { //todo - use for allowing the user to delete currently not shown products fun getNotShownProductsFlow(): Flow> - suspend fun saveProductAndIncrementCount(product: Product) + suspend fun saveProduct(product: Product) suspend fun incrementProductCount(id: Int) diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt index 7eb9c58..c6d86a8 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreen.kt @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeContentPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.material.Button @@ -27,9 +28,15 @@ import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.Delete import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusDirection +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.vectorResource import androidx.compose.ui.text.input.ImeAction @@ -71,7 +78,7 @@ fun EditScreen( @Previews @Composable private fun Preview() { - EditScreenContent(viewState = EditViewState(), onEvent = {}) + EditScreenContent(viewState = EditViewState(isForAdding = false), onEvent = {}) } @Composable @@ -97,7 +104,7 @@ private fun EditScreenContent( TopAppBar( backgroundColor = MaterialTheme.colors.background, title = { - Text(text = stringResource(id = R.string.edit_product)) + Text(text = stringResource(id = if (viewState.isForAdding) R.string.add_product else R.string.edit_product)) }, navigationIcon = { IconButton(onClick = { onEvent(EditEvent.OnBackClick) }) { @@ -116,8 +123,10 @@ private fun EditScreenContent( .fillMaxSize(), state = viewState.state ) { EditForm( + isForAdding = viewState.isForAdding, productName = viewState.productName, productCount = viewState.productCount, + isSaveButtonEnabled = viewState.isSaveButtonEnabled, onEvent = onEvent ) } @@ -126,8 +135,10 @@ private fun EditScreenContent( @Composable fun EditForm( + isForAdding: Boolean, productName: String, productCount: String, + isSaveButtonEnabled: Boolean, onEvent: OnEvent ) { Column( @@ -139,8 +150,13 @@ fun EditForm( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(16.dp) ) { + val focusRequester = remember { FocusRequester() } + val focusManager = LocalFocusManager.current + TBCTextField( - modifier = Modifier.fillMaxWidth(), + modifier = Modifier + .fillMaxWidth() + .focusRequester(focusRequester), label = stringResource(id = R.string.name), value = productName, hasNextDownImeAction = true, @@ -148,11 +164,23 @@ fun EditForm( onValueChanged = { name -> onEvent(EditEvent.OnProductNameChange(name)) } ) + LaunchedEffect(Unit) { + if (isForAdding) { + focusRequester.requestFocus() + } + } + TBCTextField( modifier = Modifier.fillMaxWidth(), label = stringResource(id = R.string.count), value = productCount, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), + keyboardActions = KeyboardActions( + onDone = { + focusManager.clearFocus() + onEvent(EditEvent.OnSaveClick) + } + ), trailingIcon = { Row { RemoveAddButton(add = false, productCount = productCount, onEvent = onEvent) @@ -170,7 +198,7 @@ fun EditForm( TBCButton( modifier = Modifier.fillMaxWidth(), text = stringResource(id = R.string.ok), - enabled = productName.isNotBlank(), + enabled = isSaveButtonEnabled, onClick = { onEvent(EditEvent.OnSaveClick) } ) } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt index b059297..8179101 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditScreenViewModel.kt @@ -5,7 +5,6 @@ import cz.damat.thebeercounter.commonUI.base.BaseViewModel import cz.damat.thebeercounter.commonUI.base.State import cz.damat.thebeercounter.commonlib.room.entity.Product import cz.damat.thebeercounter.componentCounter.domain.repository.ProductRepository -import kotlinx.coroutines.delay import kotlinx.coroutines.launch /** @@ -14,7 +13,7 @@ import kotlinx.coroutines.launch class EditScreenViewModel( private val productId: Int?, private val productRepository: ProductRepository, -) : BaseViewModel(EditViewState()) { +) : BaseViewModel(EditViewState(isForAdding = productId == null)) { init { ioScope.launch { @@ -53,26 +52,27 @@ class EditScreenViewModel( } private fun save() { - currentState().let { + currentState().let { state -> + if (!state.isSaveButtonEnabled) { + return + } ioScope.launch { updateState { copy(state = State.Loading) } - val count = it.productCount.toIntOrNull() ?: 0 // todo validations - if (productId != null) { productRepository.updateProductNameAndSetCount( productId, - it.productName, - count + state.productName, + state.productCount.toIntOrNull() ?: 0 ) } else { val product = Product( - name = it.productName, - count = count, + name = state.productName, + count = state.productCount.toIntOrNull() ?: 1, shown = true, price = null, ) - productRepository.saveProductAndIncrementCount(product) + productRepository.saveProduct(product) } updateState { copy(state = State.Content) } diff --git a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditViewState.kt b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditViewState.kt index 327f61f..d3fa8ee 100644 --- a/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditViewState.kt +++ b/featureCounter/src/main/java/cz/damat/thebeercounter/featureCounter/scene/edit/EditViewState.kt @@ -9,6 +9,10 @@ import cz.damat.thebeercounter.commonUI.base.State */ data class EditViewState( val state : State = State.Loading, + val isForAdding : Boolean, val productName: String = "", val productCount: String = "", -) : ViewStateDTO +) : ViewStateDTO { + val isSaveButtonEnabled: Boolean + get() = productName.isNotBlank() +}