Skip to content

Commit

Permalink
Merge pull request #6 from DanicMa/product_adding
Browse files Browse the repository at this point in the history
Product adding
  • Loading branch information
DanicMa authored Nov 13, 2023
2 parents 48479b5 + 798e346 commit 4966e80
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 139 deletions.
1 change: 1 addition & 0 deletions commonUI/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="name">Name</string>
<string name="beer">Beer</string>
<string name="clear_all_confirm_message">Do you really want to delete all items?</string>
<string name="add_product">Add product</string>
<string name="edit_product">Edit product</string>
<string name="error_product_not_found">Product not found</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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


/**
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface ProductRepository : KoinComponent {
//todo - use for allowing the user to delete currently not shown products
fun getNotShownProductsFlow(): Flow<List<Product>>

suspend fun saveProductAndIncrementCount(product: Product)
suspend fun saveProduct(product: Product)

suspend fun incrementProductCount(id: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ val featureCounterKoinModule = module {
CounterScreenViewModel(get(), androidApplication().resources)
}

viewModel { (productId: Int) ->
viewModel { (productId: Int?) ->
EditScreenViewModel(productId, get())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,10 +79,6 @@ private fun CommandCollector(
mutableStateOf(false)
}

val showAddNewDialog = remember {
mutableStateOf(false)
}

val view = LocalView.current

viewModel.collectCommand {
Expand All @@ -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}")
}
Expand All @@ -110,13 +109,6 @@ private fun CommandCollector(
showDialog = showClearAllConfirmDialog,
onEvent = onEvent
)

AddNewProductDialog(
showDialog = showAddNewDialog,
onNewProductCreated = {
onEvent(CounterEvent.OnNewProductAdded(it))
}
)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class CounterScreenViewModel(
CounterEvent.OnClearAllClicked -> onClearAllClicked()
CounterEvent.OnClearAllConfirmed -> onClearAllConfirmed()
CounterEvent.OnAddNewClicked -> onAddNewClicked()
is CounterEvent.OnNewProductAdded -> onNewProductAdded(event.name)
}
}

Expand Down Expand Up @@ -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)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,12 +28,19 @@ 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
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
Expand All @@ -55,7 +63,7 @@ import org.koin.core.parameter.parametersOf
@Composable
fun EditScreen(
navController: NavController,
productId: Int,
productId: Int?,
) {
val viewModel: EditScreenViewModel = getViewModel() {
parametersOf(productId)
Expand All @@ -70,7 +78,7 @@ fun EditScreen(
@Previews
@Composable
private fun Preview() {
EditScreenContent(viewState = EditViewState(), onEvent = {})
EditScreenContent(viewState = EditViewState(isForAdding = false), onEvent = {})
}

@Composable
Expand All @@ -96,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) }) {
Expand All @@ -115,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
)
}
Expand All @@ -125,8 +135,10 @@ private fun EditScreenContent(

@Composable
fun EditForm(
isForAdding: Boolean,
productName: String,
productCount: String,
isSaveButtonEnabled: Boolean,
onEvent: OnEvent
) {
Column(
Expand All @@ -138,18 +150,37 @@ 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,
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
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)
Expand All @@ -167,7 +198,7 @@ fun EditForm(
TBCButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.ok),
enabled = productName.isNotBlank(),
enabled = isSaveButtonEnabled,
onClick = { onEvent(EditEvent.OnSaveClick) }
)
}
Expand Down
Loading

0 comments on commit 4966e80

Please sign in to comment.