Работает всё кроме экрана админ панели

This commit is contained in:
Данила Мочалов 2023-10-25 05:28:41 +04:00
parent 9408592281
commit 39499b30ad
11 changed files with 329 additions and 46 deletions

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")
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

@ -1,4 +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,103 @@
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.OrderProductModelWithProduct
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
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 ->
// // Groups
// val groupDao = database.groupDao()
// val group1 = Group(1, "Группа 1")
// groupDao.insert(group1)
// // Students
//
// 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.Preparing.toString(), 1, Date())
orderDao.insert(order1)
// OrderProducts
val orderProductDao = database.orderProductDao()
val op1 = OrderProductModel(1, 1, 2, 200)
orderProductDao.insert(op1)
}
}
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

@ -19,7 +19,7 @@ import java.util.Date
data class OrderModel(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Int,
val id: Int?,
@ColumnInfo(name = "order_status")
val status: String,
@ColumnInfo(name = "user_id", index = true)

View File

@ -26,10 +26,14 @@ import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
@ -38,8 +42,11 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.models.getOrdersByUserId
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightRed
@ -47,6 +54,8 @@ import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun CartScreen() {
@ -61,15 +70,25 @@ fun CartScreen() {
@Composable
fun CartWidget(){
val orders = getOrdersByUserId()
val unpaidOrders = mutableListOf<OrderModel>()
val preparingOrders = mutableListOf<OrderModel>()
val context = LocalContext.current
val orders = remember { mutableStateListOf<OrderWithProducts>() }
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) {
if (order.status == OrderStatus.Unpaid) {
if (order.order.status == OrderStatus.Unpaid.toString()) {
unpaidOrders.add(order)
}
if (order.status == OrderStatus.Preparing) {
if (order.order.status == OrderStatus.Preparing.toString()) {
preparingOrders.add(order)
}
}
@ -110,8 +129,8 @@ fun CartWidget(){
.width(340.dp)
.height(200.dp)
) {
items(preparingOrders.size * 2) {
PaidItem(preparingOrders[0])
items(preparingOrders.size) { index ->
PaidItem(preparingOrders[index])
}
}
Text(
@ -126,8 +145,8 @@ fun CartWidget(){
.width(340.dp)
.height(200.dp)
) {
items(unpaidOrders.size * 2) {
PaidItem(unpaidOrders[0])
items(unpaidOrders.size) {index ->
PaidItem(unpaidOrders[index])
}
}
@ -158,7 +177,7 @@ fun CartWidget(){
}
@Composable
fun PaidItem(order : OrderModel) {
fun PaidItem(order : OrderWithProducts) {
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -173,21 +192,21 @@ fun PaidItem(order : OrderModel) {
.padding(horizontal = 20.dp)
){
Text(
text = "Классика",
text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.productsId.values.first().toString(),
text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
Text(
text = "150 руб.",
text = order.orderWithProducts[0].orderProductModel.totalPrice.toString(),
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
@ -224,7 +243,7 @@ fun PaidItem(order : OrderModel) {
}
@Composable
fun CartItem(order : OrderModel) {
fun CartItem(order : OrderWithProducts) {
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -245,20 +264,20 @@ fun CartItem(order : OrderModel) {
modifier = Modifier.fillMaxWidth()
){
Text(
text = "Классика",
text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.productsId.values.first().toString(),
text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
Text(
text = "150 руб.",
text = order.orderWithProducts[0].orderProductModel.totalPrice.toString(),
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold

View File

@ -20,10 +20,14 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
@ -31,7 +35,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
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.ui.theme.MarckFamily
@ -41,6 +47,8 @@ import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun DiscountScreen() {
@ -56,7 +64,16 @@ fun DiscountScreen() {
@Composable
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(
modifier = Modifier
@ -100,7 +117,7 @@ fun DiscountList(){
}
@Composable
fun DiscountCard(product : DiscountProductModel){
fun DiscountCard(product : ProductModel){
Card(
shape = RoundedCornerShape(20.dp),
backgroundColor = Color.White,
@ -144,7 +161,7 @@ fun DiscountCard(product : DiscountProductModel){
textDecoration = TextDecoration.LineThrough
)
Text(
text = product.newPrice.toString() + "руб. ",
text = product.price.toString() + "руб. ",
fontFamily = NunitoFamily,
fontSize = 16.sp,
fontWeight = FontWeight.ExtraBold,

View File

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

View File

@ -22,16 +22,22 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.data.models.getAllOrders
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow
@ -39,6 +45,8 @@ import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
@Composable
@ -53,20 +61,29 @@ fun OrdersScreen() {
@Composable
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<OrderModel>()
val preparedOrders = mutableListOf<OrderModel>()
val processedOrders = mutableListOf<OrderModel>()
val preparingOrders = mutableListOf<OrderWithProducts>()
val preparedOrders = mutableListOf<OrderWithProducts>()
val processedOrders = mutableListOf<OrderWithProducts>()
for (order in allOrders){
if (order.status == OrderStatus.Preparing) {
if (order.order.status == OrderStatus.Preparing.toString()) {
preparingOrders.add(order)
}
if (order.status == OrderStatus.Prepared) {
if (order.order.status == OrderStatus.Prepared.toString()) {
preparedOrders.add(order)
}
if (order.status == OrderStatus.Processed) {
if (order.order.status == OrderStatus.Processed.toString()) {
processedOrders.add(order)
}
}
@ -142,8 +159,8 @@ fun OrdersList(){
.width(340.dp)
.height(250.dp)
) {
items(2) {
ProcessedItem()
items(processedOrders.size) {index ->
ProcessedItem(processedOrders[index])
}
}
Spacer(modifier = Modifier.height(70.dp))
@ -154,7 +171,7 @@ fun OrdersList(){
}
@Composable
fun PreparingItem(order : OrderModel){
fun PreparingItem(order : OrderWithProducts){
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -171,7 +188,7 @@ fun PreparingItem(order : OrderModel){
modifier = Modifier.fillMaxWidth(0.5f)
){
val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.date)
val time = localDateFormat.format(order.order.date)
Text(
text = time,
fontFamily = NunitoFamily,
@ -183,13 +200,13 @@ fun PreparingItem(order : OrderModel){
modifier = Modifier.fillMaxWidth()
){
Text(
text = "Классика",
text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.productsId.values.first().toString(),
text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
@ -222,7 +239,7 @@ fun PreparingItem(order : OrderModel){
}
@Composable
fun PreparedItem(order : OrderModel){
fun PreparedItem(order : OrderWithProducts){
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -239,7 +256,7 @@ fun PreparedItem(order : OrderModel){
modifier = Modifier.fillMaxWidth(0.5f)
){
val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.date)
val time = localDateFormat.format(order.order.date)
Text(
text = time,
fontFamily = NunitoFamily,
@ -251,13 +268,13 @@ fun PreparedItem(order : OrderModel){
modifier = Modifier.fillMaxWidth()
){
Text(
text = "Классика",
text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.productsId.values.first().toString(),
text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
@ -290,7 +307,7 @@ fun PreparedItem(order : OrderModel){
}
@Composable
fun ProcessedItem(){
fun ProcessedItem(order : OrderWithProducts){
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -306,8 +323,10 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth(0.5f)
){
val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.order.date)
Text(
text = "16:24",
text = time,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
@ -317,13 +336,13 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth()
){
Text(
text = "Классика",
text = order.orderWithProducts[0].productList[0].title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "x2",
text = "x" + order.orderWithProducts[0].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
@ -337,13 +356,13 @@ fun ProcessedItem(){
modifier = Modifier.fillMaxWidth(0.5f)
){
Text(
text = "300 руб.",
text = order.orderWithProducts[0].orderProductModel.totalPrice.toString(),
fontFamily = NunitoFamily,
fontSize = 14.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "20.03.2012",
text = order.order.date.toString(),
fontFamily = NunitoFamily,
fontSize = 14.sp,
fontWeight = FontWeight.Bold