deleted cart fields for coffee

This commit is contained in:
Zyzf 2023-12-12 20:03:24 +04:00
parent fab50baea5
commit bb21aa8cac
17 changed files with 100 additions and 62 deletions

View File

@ -50,8 +50,8 @@ class RestCoffeeRepository(
return service.createCoffee(Coffee(name, cost, ingredients, null, 0).toCoffeeRemote()).toCoffee().uid.toLong() return service.createCoffee(Coffee(name, cost, ingredients, null, 0).toCoffeeRemote()).toCoffee().uid.toLong()
} }
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? { override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int? {
return service.updateCoffee(uid, Coffee(uid, name, cost, ingredients, cartId, count).toCoffeeRemote()).toCoffee().uid return service.updateCoffee(uid, Coffee(uid, name, cost, ingredients).toCoffeeRemote()).toCoffee().uid
} }
override suspend fun delete(coffee: Coffee) { override suspend fun delete(coffee: Coffee) {

View File

@ -8,25 +8,19 @@ data class CoffeeRemote(
val id: Int = 0, val id: Int = 0,
val name: String = "", val name: String = "",
val cost: Double = 0.0, val cost: Double = 0.0,
val ingredients: String = "", val ingredients: String = ""
val cartId: Int? = 0,
val count: Int = 0
) )
fun CoffeeRemote.toCoffee(): Coffee = Coffee( fun CoffeeRemote.toCoffee(): Coffee = Coffee(
id, id,
name, name,
cost, cost,
ingredients, ingredients
cartId,
count
) )
fun Coffee.toCoffeeRemote(): CoffeeRemote = CoffeeRemote( fun Coffee.toCoffeeRemote(): CoffeeRemote = CoffeeRemote(
uid, uid,
name, name,
cost, cost,
ingredients, ingredients
cartId,
count
) )

View File

@ -59,7 +59,7 @@ abstract class AppDatabase : RoomDatabase() {
coffeeDao.insert(coffee11.name, coffee11.cost, coffee11.ingredients) coffeeDao.insert(coffee11.name, coffee11.cost, coffee11.ingredients)
// Cart // Cart
val cartDao = database.cartDao() val cartDao = database.cartDao()
val cart = Cart() val cart = Cart(2, 1)
cartDao.insert(cart) cartDao.insert(cart)
} }
} }

View File

@ -4,28 +4,45 @@ import androidx.paging.PagingSource
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update import androidx.room.Update
import com.zyzf.coffeepreorder.database.model.Cart import com.zyzf.coffeepreorder.database.model.Cart
import com.zyzf.coffeepreorder.database.model.Coffee import com.zyzf.coffeepreorder.database.model.Coffee
@Dao @Dao
interface CartDao { interface CartDao {
@Query("select * from cart limit 1") @Query("select * from cart")
suspend fun get(): Cart suspend fun getAll(): Cart
@Query("select * from coffee where cart_id is not null and count > 0 order by name collate nocase asc") @Query("select coffee.uid, coffee.name, coffee.cost, coffee.ingredients from cart join coffee on coffee.uid = cart.coffee_id and cart.count > 0 collate nocase")
fun getAllInCart(): PagingSource<Int, Coffee> fun getAllInCart(): PagingSource<Int, Coffee>
@Query("select sum(cost) from coffee where cart_id is not null and count > 0") @Query("select sum(coffee.cost) from cart JOIN coffee on coffee.uid = cart.coffee_id and cart.count > 0")
fun getSumInCart(): Double fun getSumInCart(): Double
@Query("select coffee.cost from cart JOIN coffee on coffee.uid = cart.coffee_id where coffee.uid = :coffeeId")
fun getCountForCoffee(coffeeId: Int): Double
@Insert @Insert
suspend fun insert(cart: Cart) suspend fun insert(cart: Cart)
@Query("update coffee set cart_id = :cartId, count = count + :count where uid = :coffeeId") @Query("select * from cart where coffee_id = :coffeeId limit 1")
suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int) suspend fun findCoffee(coffeeId: Int): Cart?
@Query("insert into cart (coffee_id, count) values (:coffeeId, :count)")
suspend fun insertNewCoffee(coffeeId: Int, count: Int)
@Query("update cart set count = count + :count where coffee_id = :coffeeId")
suspend fun updateExistingCoffee(coffeeId: Int, count: Int)
@Transaction
suspend fun insertCoffee(coffeeId: Int, count: Int) {
val cart: Cart? = findCoffee(coffeeId)
if (cart != null) {
updateExistingCoffee(coffeeId, count)
} else {
insertNewCoffee(coffeeId, count)
}
}
@Query("update coffee set count = count - :count where uid = :coffeeId") @Query("update cart set count = count - :count where coffee_id = :coffeeId")
suspend fun deleteCoffee(coffeeId: Int, count: Int) suspend fun deleteCoffee(coffeeId: Int, count: Int)
@Update @Update

View File

@ -11,14 +11,14 @@ interface CoffeeDao {
@Query("select * from coffee order by name collate nocase asc") @Query("select * from coffee order by name collate nocase asc")
fun getAll(): PagingSource<Int, Coffee> fun getAll(): PagingSource<Int, Coffee>
@Query("select coffee.uid, name, cost, ingredients, cart_id, count from coffee where coffee.uid = :uid") @Query("select coffee.uid, name, cost, ingredients from coffee where coffee.uid = :uid")
suspend fun getByUid(uid: Int): Coffee? suspend fun getByUid(uid: Int): Coffee?
@Query("insert into coffee (name, cost, ingredients, count) values (:name, :cost, :ingredients, 0)") @Query("insert into coffee (name, cost, ingredients) values (:name, :cost, :ingredients)")
suspend fun insert(name: String, cost: Double, ingredients: String): Long suspend fun insert(name: String, cost: Double, ingredients: String): Long
@Query("update coffee set name = :name, cost = :cost, ingredients = :ingredients, cart_id = :cartId, count = :count where uid = :uid") @Query("update coffee set name = :name, cost = :cost, ingredients = :ingredients where uid = :uid")
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int?
@Delete @Delete
suspend fun delete(coffee: Coffee) suspend fun delete(coffee: Coffee)

View File

@ -1,16 +1,33 @@
package com.zyzf.coffeepreorder.database.model package com.zyzf.coffeepreorder.database.model
import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore import androidx.room.Ignore
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity(tableName = "cart") @Entity(tableName = "cart", foreignKeys = [
ForeignKey(
entity = Cart::class,
parentColumns = ["uid"],
childColumns = ["coffee_id"],
onDelete = ForeignKey.RESTRICT,
onUpdate = ForeignKey.RESTRICT
)
])
data class Cart( data class Cart(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val uid: Int?, val uid: Int?,
@ColumnInfo(name = "coffee_id", index = true)
val coffeeId: Int,
@ColumnInfo(name = "count", index = true)
val count: Int = 0
) { ) {
@Ignore @Ignore
constructor() : this(null) constructor(
coffeeId: Int,
count: Int
) : this(null, coffeeId, 0)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true

View File

@ -6,15 +6,7 @@ import androidx.room.ForeignKey
import androidx.room.Ignore import androidx.room.Ignore
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity(tableName = "coffee", foreignKeys = [ @Entity(tableName = "coffee")
ForeignKey(
entity = Cart::class,
parentColumns = ["uid"],
childColumns = ["cart_id"],
onDelete = ForeignKey.RESTRICT,
onUpdate = ForeignKey.RESTRICT
)
])
data class Coffee( data class Coffee(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val uid: Int = 0, val uid: Int = 0,
@ -23,11 +15,7 @@ data class Coffee(
@ColumnInfo(name = "cost") @ColumnInfo(name = "cost")
var cost: Double, var cost: Double,
@ColumnInfo(name = "ingredients") @ColumnInfo(name = "ingredients")
var ingredients: String, var ingredients: String
@ColumnInfo(name = "cart_id", index = true)
val cartId: Int?,
@ColumnInfo(name = "count")
val count: Int = 0
) { ) {
@Ignore @Ignore
constructor( constructor(
@ -36,7 +24,7 @@ data class Coffee(
ingredients: String, ingredients: String,
cartId: Int?, cartId: Int?,
count: Int? count: Int?
) : this(0, name, cost, ingredients, null, 0) ) : this(0, name, cost, ingredients)
companion object { companion object {
fun getCoffee(index: Int = 0): Coffee { fun getCoffee(index: Int = 0): Coffee {
@ -44,9 +32,7 @@ data class Coffee(
index, index,
"", "",
0.0, 0.0,
"", ""
null,
0
) )
} }
} }

View File

@ -6,12 +6,13 @@ import com.zyzf.coffeepreorder.database.model.Coffee
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface CartRepository { interface CartRepository {
suspend fun get(): Cart suspend fun getAll(): Cart
suspend fun insert(cart: Cart) suspend fun insert(cart: Cart)
fun getAllInCart(): Flow<PagingData<Coffee>> fun getAllInCart(): Flow<PagingData<Coffee>>
fun getSumInCart(): Double fun getSumInCart(): Double
suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int) suspend fun insertCoffee(coffeeId: Int, count: Int)
suspend fun deleteCoffee(coffeeId: Int, count: Int) suspend fun deleteCoffee(coffeeId: Int, count: Int)
fun getCountForCoffee(coffeeId: Int): Double
suspend fun update(cart: Cart) suspend fun update(cart: Cart)
suspend fun deleteAll() suspend fun deleteAll()
} }

View File

@ -8,6 +8,6 @@ interface CoffeeRepository {
fun getAll(): Flow<PagingData<Coffee>> fun getAll(): Flow<PagingData<Coffee>>
suspend fun getByUid(uid: Int): Coffee? suspend fun getByUid(uid: Int): Coffee?
suspend fun insert(name: String, cost: Double, ingredients: String): Long suspend fun insert(name: String, cost: Double, ingredients: String): Long
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int?
suspend fun delete(coffee: Coffee) suspend fun delete(coffee: Coffee)
} }

View File

@ -18,10 +18,11 @@ class OfflineCartRepository(private val cartDao: CartDao) : CartRepository {
pagingSourceFactory = cartDao::getAllInCart pagingSourceFactory = cartDao::getAllInCart
).flow ).flow
override fun getSumInCart(): Double = cartDao.getSumInCart() override fun getSumInCart(): Double = cartDao.getSumInCart()
override suspend fun get(): Cart = cartDao.get() override suspend fun getAll(): Cart = cartDao.getAll()
override suspend fun insert(cart: Cart) = cartDao.insert(cart) override suspend fun insert(cart: Cart) = cartDao.insert(cart)
override suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int) = cartDao.insertCoffee(cartId, coffeeId, count) override suspend fun insertCoffee(coffeeId: Int, count: Int) = cartDao.insertCoffee(coffeeId, count)
override suspend fun deleteCoffee(coffeeId: Int, count: Int) = cartDao.deleteCoffee(coffeeId, count) override suspend fun deleteCoffee(coffeeId: Int, count: Int) = cartDao.deleteCoffee(coffeeId, count)
override fun getCountForCoffee(coffeeId: Int): Double = cartDao.getCountForCoffee(coffeeId)
override suspend fun update(cart: Cart) = cartDao.update(cart) override suspend fun update(cart: Cart) = cartDao.update(cart)
override suspend fun deleteAll() = cartDao.deleteAll() override suspend fun deleteAll() = cartDao.deleteAll()
} }

View File

@ -22,6 +22,6 @@ class OfflineCoffeeRepository(private val coffeeDao: CoffeeDao) : CoffeeReposito
suspend fun clearCoffees() = coffeeDao.deleteAll() suspend fun clearCoffees() = coffeeDao.deleteAll()
override suspend fun getByUid(uid: Int): Coffee? = coffeeDao.getByUid(uid) override suspend fun getByUid(uid: Int): Coffee? = coffeeDao.getByUid(uid)
override suspend fun insert(name: String, cost: Double, ingredients: String): Long = coffeeDao.insert(name, cost, ingredients) override suspend fun insert(name: String, cost: Double, ingredients: String): Long = coffeeDao.insert(name, cost, ingredients)
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? = coffeeDao.update(uid, name, cost, ingredients, cartId, count) override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int? = coffeeDao.update(uid, name, cost, ingredients)
override suspend fun delete(coffee: Coffee) = coffeeDao.delete(coffee) override suspend fun delete(coffee: Coffee) = coffeeDao.delete(coffee)
} }

View File

@ -95,18 +95,26 @@ fun Cart(
) )
} }
) { innerPadding -> ) { innerPadding ->
Box (modifier = Modifier.padding(0.dp).pullRefresh(state)) { Box (modifier = Modifier
.padding(0.dp)
.pullRefresh(state)) {
PullRefreshIndicator(refreshing = refreshing, state = state, PullRefreshIndicator(refreshing = refreshing, state = state,
modifier = Modifier.zIndex(100f).align(Alignment.TopCenter) modifier = Modifier
.zIndex(100f)
.align(Alignment.TopCenter)
) )
CartList( CartList(
modifier = Modifier modifier = Modifier
.padding(innerPadding).pullRefresh(state) .padding(innerPadding)
.pullRefresh(state)
.fillMaxSize(), .fillMaxSize(),
coffeeList = coffeeListUiState, coffeeList = coffeeListUiState,
onDeleteFromCartClick = {currentCoffee: Coffee -> onDeleteFromCartClick = {currentCoffee: Coffee ->
coffee.value = currentCoffee coffee.value = currentCoffee
openDialog.value = true openDialog.value = true
},
getCoffeeCount = {coffeeId: Int ->
viewModel.getCountForCoffee(coffeeId)
} }
) )
} }
@ -125,7 +133,8 @@ fun Cart(
private fun CartList( private fun CartList(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
coffeeList: LazyPagingItems<Coffee>, coffeeList: LazyPagingItems<Coffee>,
onDeleteFromCartClick: (coffee: Coffee) -> Unit onDeleteFromCartClick: (coffee: Coffee) -> Unit,
getCoffeeCount: (coffeeId: Int) -> Double
) { ) {
LazyColumn( LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally) { horizontalAlignment = Alignment.CenterHorizontally) {
@ -138,7 +147,8 @@ private fun CartList(
coffee?.let { coffee?.let {
CartListItem( CartListItem(
coffee = coffee, coffee = coffee,
onDeleteFromCartClick = onDeleteFromCartClick onDeleteFromCartClick = onDeleteFromCartClick,
getCoffeeCount = getCoffeeCount
) )
} }
} }
@ -149,7 +159,8 @@ private fun CartList(
private fun CartListItem ( private fun CartListItem (
coffee: Coffee, coffee: Coffee,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onDeleteFromCartClick: (coffee: Coffee) -> Unit onDeleteFromCartClick: (coffee: Coffee) -> Unit,
getCoffeeCount: (coffeeId: Int) -> Double
) { ) {
Row(modifier = Modifier Row(modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -171,8 +182,9 @@ private fun CartListItem (
Modifier Modifier
.weight(2f) .weight(2f)
.padding(start = 20.dp)) { .padding(start = 20.dp)) {
val currentCoffeeName: String = if (coffee.count > 1) { val coffeeCount: Double = getCoffeeCount(coffee.uid)
coffee.name + " x" + coffee.count.toString() val currentCoffeeName: String = if (coffeeCount > 1) {
coffee.name + " x" + coffeeCount.toString()
} else { } else {
coffee.name coffee.name
} }

View File

@ -12,6 +12,10 @@ class CartViewModel(
) : ViewModel() { ) : ViewModel() {
val coffeeListUiState: Flow<PagingData<Coffee>> = cartRepository.getAllInCart() val coffeeListUiState: Flow<PagingData<Coffee>> = cartRepository.getAllInCart()
fun getCountForCoffee(coffeeId: Int): Double {
return cartRepository.getCountForCoffee(coffeeId)
}
suspend fun deleteCoffeeFromCart(coffee: Coffee) { suspend fun deleteCoffeeFromCart(coffee: Coffee) {
cartRepository.deleteCoffee(coffee.uid, 1) cartRepository.deleteCoffee(coffee.uid, 1)
} }

View File

@ -259,14 +259,14 @@ private fun AddEditModalBottomSheet(
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
if (coffee.value.uid == 0) { if (coffee.value.uid == 0) {
Button(onClick = { Button(onClick = {
onAddClick(Coffee(coffee.value.uid, name, cost, ingredients, coffee.value.cartId, coffee.value.count), context) onAddClick(Coffee(coffee.value.uid, name, cost, ingredients), context)
openDialog.value = false openDialog.value = false
}, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) { }, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) {
Text("Добавить") Text("Добавить")
} }
} else { } else {
Button(onClick = { Button(onClick = {
onEditClick(Coffee(coffee.value.uid, name, cost, ingredients, coffee.value.cartId, coffee.value.count), context) onEditClick(Coffee(coffee.value.uid, name, cost, ingredients), context)
openDialog.value = false openDialog.value = false
}, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) { }, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) {
Text("Изменить") Text("Изменить")

View File

@ -32,8 +32,7 @@ class CoffeeListViewModel(
val coffeeListUiState: Flow<PagingData<Coffee>> = coffeeRepository.getAll() val coffeeListUiState: Flow<PagingData<Coffee>> = coffeeRepository.getAll()
suspend fun addCoffeeToCart(coffeeUid: Int) { suspend fun addCoffeeToCart(coffeeUid: Int) {
val cart: Cart = cartRepository.get() cartRepository.insertCoffee(coffeeUid, 1)
cart.uid?.let { cartRepository.insertCoffee(it, coffeeUid, 1) }
} }
suspend fun createCoffee(coffee: Coffee, imageUri: Uri, context: Context) { suspend fun createCoffee(coffee: Coffee, imageUri: Uri, context: Context) {
val newCoffee: Long = coffeeRepository.insert(coffee.name, coffee.cost, coffee.ingredients) val newCoffee: Long = coffeeRepository.insert(coffee.name, coffee.cost, coffee.ingredients)
@ -55,7 +54,7 @@ class CoffeeListViewModel(
copyFileToSftp(f, "/mnt/nextcloud/data/Zyzf/files/Images") copyFileToSftp(f, "/mnt/nextcloud/data/Zyzf/files/Images")
} }
suspend fun editCoffee(coffee: Coffee, imageUri: Uri, context: Context) { suspend fun editCoffee(coffee: Coffee, imageUri: Uri, context: Context) {
val editedCoffee: Int = coffeeRepository.update(coffee.uid, coffee.name, coffee.cost, coffee.ingredients, coffee.cartId, coffee.count)!! val editedCoffee: Int = coffeeRepository.update(coffee.uid, coffee.name, coffee.cost, coffee.ingredients)!!
val inputStream = context.contentResolver.openInputStream(imageUri) val inputStream = context.contentResolver.openInputStream(imageUri)
val bitmap = BitmapFactory.decodeStream(inputStream) val bitmap = BitmapFactory.decodeStream(inputStream)

Binary file not shown.

View File

@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists