Compare commits

...

3 Commits

23 changed files with 513 additions and 148 deletions

View File

@ -1,6 +1,8 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.android'
//lab 3
id 'kotlin-kapt'
} }
android { android {
@ -18,6 +20,10 @@ android {
vectorDrawables { vectorDrawables {
useSupportLibrary true useSupportLibrary true
} }
kapt {
arguments {arg("room.schemaLocation", "$projectDir/schemas")}
}
} }
buildTypes { buildTypes {
@ -62,4 +68,9 @@ dependencies {
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version" androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
// lab 3
implementation 'androidx.room:room-runtime:2.5.0' // Библиотека "Room"
kapt "androidx.room:room-compiler:2.5.0" // Кодогенератор
implementation 'androidx.room:room-ktx:2.5.0' // Дополнительно для Kotlin Coroutines, Kotlin Flows
} }

View File

@ -0,0 +1,30 @@
package com.example.shawarma.data.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderWithProducts
import kotlinx.coroutines.flow.Flow
@Dao
interface OrderDao {
@Insert
suspend fun insert(order: OrderModel)
@Update
suspend fun update(order: OrderModel)
@Delete
suspend fun delete(order: OrderModel)
@Query("select * from orders where orders.user_id is not null")
fun getAll() : Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.id =:id")
fun getById(id: Int): Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.user_id =:userId")
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>>
}

View File

@ -0,0 +1,11 @@
package com.example.shawarma.data.dao
import androidx.room.Dao
import androidx.room.Insert
import com.example.shawarma.data.models.OrderProductModel
@Dao
interface OrderProductDao {
@Insert
suspend fun insert(order: OrderProductModel)
}

View File

@ -0,0 +1,30 @@
package com.example.shawarma.data.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
@Dao
interface ProductDao {
@Insert
suspend fun insert(product: ProductModel)
@Update
suspend fun update(product: ProductModel)
@Delete
suspend fun delete(product: ProductModel)
@Query("select * from products where products.product_old_price is null")
fun getAll() : Flow<List<ProductModel>>
@Query("select * from products where products.product_old_price is not null")
fun getDiscounts() : Flow<List<ProductModel>>
@Query("select * from products where products.id = :id")
fun getById(id: Int): ProductModel
}

View File

@ -0,0 +1,27 @@
package com.example.shawarma.data.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.UserModel
import kotlinx.coroutines.flow.Flow
@Dao
interface UserDao {
@Insert
suspend fun insert(user: UserModel)
@Update
suspend fun update(user: UserModel)
@Delete
suspend fun delete(user: UserModel)
@Query("select * from users order by user_login collate nocase asc")
fun getAll() : Flow<List<UserModel>>
@Query("select * from users where users.id = :id")
fun getById(id: Int): UserModel
}

View File

@ -0,0 +1,107 @@
package com.example.shawarma.data.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.shawarma.data.dao.OrderDao
import com.example.shawarma.data.dao.OrderProductDao
import com.example.shawarma.data.dao.ProductDao
import com.example.shawarma.data.dao.UserDao
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderProductModel
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.models.UserModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Date
@Database(
entities =
[
UserModel::class,
OrderModel::class,
ProductModel::class,
OrderProductModel::class,
],
version = 1,
exportSchema = false
)
@TypeConverters(Converter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao() : UserDao
abstract fun productDao() : ProductDao
abstract fun orderDao() : OrderDao
abstract fun orderProductDao() : OrderProductDao
companion object {
private const val DB_NAME: String = "shawarma-db"
@Volatile
private var INSTANCE: AppDatabase? = null
private suspend fun populateDatabase() {
INSTANCE?.let { database ->
// Users
val userDao = database.userDao()
val user1 = UserModel(1, "danya", "password", "ADMIN")
userDao.insert(user1)
// Products
val productDao = database.productDao()
val product1 = ProductModel(1, "Классик", 100, null)
val product2 = ProductModel(2, "Сырная", 120, null)
val discount1 = ProductModel(3, "Выгода", 80, 100)
val discount2 = ProductModel(4, "Кола", 50, 75)
productDao.insert(product1)
productDao.insert(product2)
productDao.insert(discount1)
productDao.insert(discount2)
// Orders
val orderDao = database.orderDao()
val order1 = OrderModel(1, OrderStatus.Готовится.toString(), 1, Date())
val order2 = OrderModel(2, OrderStatus.Неоплачено.toString(), 1, Date())
val order3 = OrderModel(3, OrderStatus.Готово.toString(), 1, Date())
val order4 = OrderModel(4, OrderStatus.Выдано.toString(), 1, Date())
orderDao.insert(order1)
orderDao.insert(order2)
orderDao.insert(order3)
orderDao.insert(order4)
// OrderProducts
val orderProductDao = database.orderProductDao()
val op1 = OrderProductModel(1, 1, 2, 200)
val op2 = OrderProductModel(2, 4, 3, 150)
val op3 = OrderProductModel(3, 3, 1, 80)
val op4 = OrderProductModel(4, 2, 1, 120)
orderProductDao.insert(op1)
orderProductDao.insert(op2)
orderProductDao.insert(op3)
orderProductDao.insert(op4)
}
}
fun getInstance(appContext: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DB_NAME
)
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch {
populateDatabase()
}
}
})
.build()
.also { INSTANCE = it }
}
}
}
}

View File

@ -0,0 +1,16 @@
package com.example.shawarma.data.db
import androidx.room.TypeConverter
import java.util.Date
class Converter {
@TypeConverter
fun fromTimestamp(value: Long?): Date? {
return value?.let { Date(it) }
}
@TypeConverter
fun dateToTimestamp(date: Date?): Long? {
return date?.time?.toLong()
}
}

View File

@ -0,0 +1,39 @@
package com.example.shawarma.data.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
import java.util.Date
@Entity(
tableName = "orders",
foreignKeys = [
ForeignKey(
entity = UserModel::class,
parentColumns = ["id"],
childColumns = ["user_id"],
)
]
)
data class OrderModel(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Int?,
@ColumnInfo(name = "order_status")
val status: String,
@ColumnInfo(name = "user_id", index = true)
val userId: Int?,
@ColumnInfo(name = "date")
val date: Date
)
fun getOrdersByUserId() : List<OrderModel> {
return listOf(
)
}
fun getAllOrders() : List<OrderModel> {
return listOf(
)
}

View File

@ -0,0 +1,16 @@
package com.example.shawarma.data.models
import androidx.room.ColumnInfo
import androidx.room.Entity
@Entity(tableName = "order_product", primaryKeys = ["order_id", "product_id"])
data class OrderProductModel(
@ColumnInfo(name = "order_id", index = true)
var orderId: Int,
@ColumnInfo(name = "product_id", index = true)
var productId: Int,
@ColumnInfo(name = "quantity")
val quantity: Int,
@ColumnInfo(name = "total_price")
val totalPrice: Int
)

View File

@ -0,0 +1,11 @@
package com.example.shawarma.data.models
import androidx.room.Embedded
import androidx.room.Relation
data class OrderProductModelWithProduct(
@Embedded
val orderProductModel: OrderProductModel,
@Relation(entity = ProductModel::class, parentColumn = "product_id", entityColumn = "id")
val productList: List<ProductModel>
)

View File

@ -0,0 +1,11 @@
package com.example.shawarma.data.models
import androidx.room.Embedded
import androidx.room.Relation
data class OrderWithProducts(
@Embedded
val order: OrderModel,
@Relation(entity = OrderProductModel::class, parentColumn = "id", entityColumn = "order_id")
val orderWithProducts: List<OrderProductModelWithProduct>
)

View File

@ -0,0 +1,36 @@
package com.example.shawarma.data.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "products")
data class ProductModel(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Int?,
@ColumnInfo(name = "product_title")
val title: String,
@ColumnInfo(name = "product_price")
val price: Int,
@ColumnInfo(name = "product_old_price")
val oldPrice: Int?,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as ProductModel
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id ?: -1
}
}
fun getProducts() :List<ProductModel> {
return listOf(
)
}

View File

@ -0,0 +1,5 @@
package com.example.shawarma.data.models
enum class OrderStatus {
Неоплачено, Готовится, Готово, Выдано
}

View File

@ -0,0 +1,30 @@
package com.example.shawarma.data.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "users")
data class UserModel(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Int?,
@ColumnInfo(name = "user_login")
val login: String,
@ColumnInfo(name = "user_password")
val password: String,
@ColumnInfo(name = "user_role")
val role: String
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as UserModel
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id ?: -1
}
}

View File

@ -1,15 +0,0 @@
package com.example.shawarma.models
data class DiscountProductModel(
val id: Int,
val title: String,
val oldPrice: Int,
val newPrice: Int
)
fun getAllDiscountProducts() : List<DiscountProductModel> {
return listOf(
DiscountProductModel(1, "Шаурма", 150, 120),
DiscountProductModel(1, "Шаурма", 150, 120)
)
}

View File

@ -1,34 +0,0 @@
package com.example.shawarma.models
import java.util.Date
data class OrderModel(
val id: Int,
val status: OrderStatus,
val productsId: HashMap<Int, Int>,
val userId: Int,
val date: Date
)
fun getOrdersByUserId() : List<OrderModel> {
val map1 = HashMap<Int, Int>()
map1[1] = 2
val map2 = HashMap<Int, Int>()
map2[1] = 2
return listOf(
OrderModel(1, OrderStatus.Unpaid, map1, 1, Date()),
OrderModel(2, OrderStatus.Preparing, map2 , 1, Date())
)
}
fun getAllOrders() : List<OrderModel> {
val map1 = HashMap<Int, Int>()
map1[1] = 2
return listOf(
OrderModel(1, OrderStatus.Prepared, map1, 1, Date()),
OrderModel(2, OrderStatus.Preparing, map1 , 1, Date()),
OrderModel(3, OrderStatus.Processed, map1 , 1, Date())
)
}

View File

@ -1,19 +0,0 @@
package com.example.shawarma.models
data class ProductModel(
val id: Int,
val title: String,
val price: Int
)
fun getProducts() :List<ProductModel> {
return listOf(
ProductModel(1, "Классика", 150),
ProductModel(1, "Классика", 150),
ProductModel(1, "Классика", 150),
ProductModel(1, "Классика", 150),
ProductModel(1, "Классика", 150),
ProductModel(1, "Классика", 150),
)
}

View File

@ -1,5 +0,0 @@
package com.example.shawarma.models
enum class OrderStatus {
Unpaid, Preparing, Prepared, Processed
}

View File

@ -1,8 +0,0 @@
package com.example.shawarma.models
data class UserModel(
val id: Int,
val login: String,
val password: String,
val role: String
)

View File

@ -2,14 +2,11 @@ package com.example.shawarma.screens.cart
import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
@ -26,27 +23,31 @@ import androidx.compose.material.Card
import androidx.compose.material.Icon import androidx.compose.material.Icon
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import com.example.shawarma.R import com.example.shawarma.R
import com.example.shawarma.models.OrderModel import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.models.OrderStatus import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.models.getOrdersByUserId import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightRed import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.ui.theme.MyMainBackground import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun CartScreen() { fun CartScreen() {
@ -61,15 +62,25 @@ fun CartScreen() {
@Composable @Composable
fun CartWidget(){ fun CartWidget(){
val orders = getOrdersByUserId() val context = LocalContext.current
val unpaidOrders = mutableListOf<OrderModel>() val orders = remember { mutableStateListOf<OrderWithProducts>() }
val preparingOrders = mutableListOf<OrderModel>() LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).orderDao().getByUserId(1).collect { data ->
orders.clear()
orders.addAll(data)
}
}
}
val unpaidOrders = mutableListOf<OrderWithProducts>()
val preparingOrders = mutableListOf<OrderWithProducts>()
for(order in orders) { for(order in orders) {
if (order.status == OrderStatus.Unpaid) { if (order.order.status == OrderStatus.Неоплачено.toString()) {
unpaidOrders.add(order) unpaidOrders.add(order)
} }
if (order.status == OrderStatus.Preparing) { if (order.order.status == OrderStatus.Готовится.toString() || order.order.status == OrderStatus.Готово.toString()) {
preparingOrders.add(order) preparingOrders.add(order)
} }
} }
@ -110,8 +121,8 @@ fun CartWidget(){
.width(340.dp) .width(340.dp)
.height(200.dp) .height(200.dp)
) { ) {
items(preparingOrders.size * 2) { items(preparingOrders.size) { index ->
PaidItem(preparingOrders[0]) PaidItem(preparingOrders[index])
} }
} }
Text( Text(
@ -126,8 +137,8 @@ fun CartWidget(){
.width(340.dp) .width(340.dp)
.height(200.dp) .height(200.dp)
) { ) {
items(unpaidOrders.size * 2) { items(unpaidOrders.size) {index ->
PaidItem(unpaidOrders[0]) CartItem(unpaidOrders[index])
} }
} }
@ -158,7 +169,7 @@ fun CartWidget(){
} }
@Composable @Composable
fun PaidItem(order : OrderModel) { fun PaidItem(order : OrderWithProducts) {
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -173,21 +184,21 @@ fun PaidItem(order : OrderModel) {
.padding(horizontal = 20.dp) .padding(horizontal = 20.dp)
){ ){
Text( Text(
text = "Классика", text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp), modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "x" + order.productsId.values.first().toString(), text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp), modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "150 руб.", text = order.orderWithProducts[0].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp), modifier = Modifier.padding(top = 15.dp),
@ -210,7 +221,7 @@ fun PaidItem(order : OrderModel) {
color = Color.Gray color = Color.Gray
) )
Text( Text(
text = "Готовится", text = order.order.status,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp), modifier = Modifier.padding(top = 15.dp),
@ -224,7 +235,7 @@ fun PaidItem(order : OrderModel) {
} }
@Composable @Composable
fun CartItem(order : OrderModel) { fun CartItem(order : OrderWithProducts) {
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -245,20 +256,20 @@ fun CartItem(order : OrderModel) {
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
){ ){
Text( Text(
text = "Классика", text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "x" + order.productsId.values.first().toString(), text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
} }
Text( Text(
text = "150 руб.", text = order.orderWithProducts[0].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold

View File

@ -20,10 +20,14 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card import androidx.compose.material.Card
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextDecoration
@ -31,8 +35,10 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import com.example.shawarma.R import com.example.shawarma.R
import com.example.shawarma.models.DiscountProductModel import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.models.getAllDiscountProducts import com.example.shawarma.data.models.DiscountProductModel
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.models.getAllDiscountProducts
import com.example.shawarma.screens.home.HomeList import com.example.shawarma.screens.home.HomeList
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
@ -41,6 +47,8 @@ import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.MyPriceBackground import com.example.shawarma.ui.theme.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun DiscountScreen() { fun DiscountScreen() {
@ -56,7 +64,16 @@ fun DiscountScreen() {
@Composable @Composable
fun DiscountList(){ fun DiscountList(){
val products = getAllDiscountProducts() val context = LocalContext.current
val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getDiscounts().collect { data ->
products.clear()
products.addAll(data)
}
}
}
Box( Box(
modifier = Modifier modifier = Modifier
@ -100,7 +117,7 @@ fun DiscountList(){
} }
@Composable @Composable
fun DiscountCard(product : DiscountProductModel){ fun DiscountCard(product : ProductModel){
Card( Card(
shape = RoundedCornerShape(20.dp), shape = RoundedCornerShape(20.dp),
backgroundColor = Color.White, backgroundColor = Color.White,
@ -144,7 +161,7 @@ fun DiscountCard(product : DiscountProductModel){
textDecoration = TextDecoration.LineThrough textDecoration = TextDecoration.LineThrough
) )
Text( Text(
text = product.newPrice.toString() + "руб. ", text = product.price.toString() + "руб. ",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 16.sp, fontSize = 16.sp,
fontWeight = FontWeight.ExtraBold, fontWeight = FontWeight.ExtraBold,

View File

@ -21,18 +21,23 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card import androidx.compose.material.Card
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import com.example.shawarma.R import com.example.shawarma.R
import com.example.shawarma.models.ProductModel import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.models.getProducts import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.models.getProducts
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground import com.example.shawarma.ui.theme.MyMainBackground
@ -40,6 +45,8 @@ import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.MyPriceBackground import com.example.shawarma.ui.theme.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun HomeScreen() { fun HomeScreen() {
@ -53,8 +60,16 @@ fun HomeScreen() {
@Composable @Composable
fun HomeList(){ fun HomeList(){
val context = LocalContext.current
val products = getProducts() val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getAll().collect { data ->
products.clear()
products.addAll(data)
}
}
}
Box( Box(
modifier = Modifier modifier = Modifier

View File

@ -22,23 +22,29 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card import androidx.compose.material.Card
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import com.example.shawarma.models.OrderModel import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.models.OrderStatus import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.models.getAllOrders import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@Composable @Composable
@ -53,20 +59,29 @@ fun OrdersScreen() {
@Composable @Composable
fun OrdersList(){ fun OrdersList(){
val context = LocalContext.current
val allOrders = remember { mutableStateListOf<OrderWithProducts>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).orderDao().getAll().collect { data ->
allOrders.clear()
allOrders.addAll(data)
}
}
}
val allOrders = getAllOrders() val preparingOrders = mutableListOf<OrderWithProducts>()
val preparingOrders = mutableListOf<OrderModel>() val preparedOrders = mutableListOf<OrderWithProducts>()
val preparedOrders = mutableListOf<OrderModel>() val processedOrders = mutableListOf<OrderWithProducts>()
val processedOrders = mutableListOf<OrderModel>()
for (order in allOrders){ for (order in allOrders){
if (order.status == OrderStatus.Preparing) { if (order.order.status == OrderStatus.Готовится.toString()) {
preparingOrders.add(order) preparingOrders.add(order)
} }
if (order.status == OrderStatus.Prepared) { if (order.order.status == OrderStatus.Готово.toString()) {
preparedOrders.add(order) preparedOrders.add(order)
} }
if (order.status == OrderStatus.Processed) { if (order.order.status == OrderStatus.Выдано.toString()) {
processedOrders.add(order) processedOrders.add(order)
} }
} }
@ -108,9 +123,11 @@ fun OrdersList(){
.width(340.dp) .width(340.dp)
.height(250.dp) .height(250.dp)
) { ) {
items(preparingOrders.size) { if (preparingOrders.size != 0) {
index -> items(preparingOrders.size) {
PreparingItem(preparedOrders[index]) index ->
PreparingItem(preparingOrders[index])
}
} }
} }
Text( Text(
@ -125,9 +142,11 @@ fun OrdersList(){
.width(340.dp) .width(340.dp)
.height(250.dp) .height(250.dp)
) { ) {
items(preparedOrders.size) { if (preparedOrders.size != 0) {
index -> items(preparedOrders.size) {
PreparedItem(preparedOrders[index]) index ->
PreparedItem(preparedOrders[index])
}
} }
} }
Text( Text(
@ -142,8 +161,10 @@ fun OrdersList(){
.width(340.dp) .width(340.dp)
.height(250.dp) .height(250.dp)
) { ) {
items(2) { if (processedOrders.size != 0) {
ProcessedItem() items(processedOrders.size) {index ->
ProcessedItem(processedOrders[index])
}
} }
} }
Spacer(modifier = Modifier.height(70.dp)) Spacer(modifier = Modifier.height(70.dp))
@ -154,7 +175,7 @@ fun OrdersList(){
} }
@Composable @Composable
fun PreparingItem(order : OrderModel){ fun PreparingItem(order : OrderWithProducts){
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -171,7 +192,7 @@ fun PreparingItem(order : OrderModel){
modifier = Modifier.fillMaxWidth(0.5f) modifier = Modifier.fillMaxWidth(0.5f)
){ ){
val localDateFormat = SimpleDateFormat("HH:mm") val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.date) val time = localDateFormat.format(order.order.date)
Text( Text(
text = time, text = time,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
@ -183,13 +204,13 @@ fun PreparingItem(order : OrderModel){
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
){ ){
Text( Text(
text = "Классика", text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "x" + order.productsId.values.first().toString(), text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
@ -222,7 +243,7 @@ fun PreparingItem(order : OrderModel){
} }
@Composable @Composable
fun PreparedItem(order : OrderModel){ fun PreparedItem(order : OrderWithProducts){
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -239,7 +260,7 @@ fun PreparedItem(order : OrderModel){
modifier = Modifier.fillMaxWidth(0.5f) modifier = Modifier.fillMaxWidth(0.5f)
){ ){
val localDateFormat = SimpleDateFormat("HH:mm") val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.date) val time = localDateFormat.format(order.order.date)
Text( Text(
text = time, text = time,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
@ -251,13 +272,13 @@ fun PreparedItem(order : OrderModel){
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
){ ){
Text( Text(
text = "Классика", text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "x" + order.productsId.values.first().toString(), text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
@ -290,7 +311,7 @@ fun PreparedItem(order : OrderModel){
} }
@Composable @Composable
fun ProcessedItem(){ fun ProcessedItem(order : OrderWithProducts){
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -306,8 +327,10 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth(0.5f) modifier = Modifier.fillMaxWidth(0.5f)
){ ){
val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.order.date)
Text( Text(
text = "16:24", text = time,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
@ -317,13 +340,13 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
){ ){
Text( Text(
text = "Классика", text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "x2", text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
@ -337,13 +360,13 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth(0.5f) modifier = Modifier.fillMaxWidth(0.5f)
){ ){
Text( Text(
text = "300 руб.", text = order.orderWithProducts[0].orderProductModel.totalPrice.toString(),
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 14.sp, fontSize = 14.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "20.03.2012", text = order.order.date.toString(),
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 14.sp, fontSize = 14.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold