Лаб 3: готова и сдана, забыл закоммитить

This commit is contained in:
Safgerd 2023-12-19 21:34:42 +04:00
parent 270e1fdcf8
commit 442d1202b2
14 changed files with 124 additions and 115 deletions

View File

@ -34,11 +34,11 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
}
buildFeatures {
compose = true

View File

@ -3,7 +3,21 @@ package com.example.mobileapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import com.example.mobileapp.db.AppDatabase
import com.example.mobileapp.models.Category
import com.example.mobileapp.models.OrderWithProducts
import com.example.mobileapp.models.Product
import com.example.mobileapp.models.Role
import com.example.mobileapp.models.Status
import com.example.mobileapp.models.User
import com.example.mobileapp.navigation.AppNavigation
import com.example.mobileapp.navigation.listOfAdminNavItems
import com.example.mobileapp.navigation.listOfUserNavItems
import com.example.mobileapp.navigation.listOfWorkerNavItems
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

View File

@ -20,5 +20,5 @@ interface CategoryDao {
fun getByName(name: String) : Flow<CategoryWithProducts>
@Query("select * from category")
fun getCatgoryWithProducts() : Flow<List<CategoryWithProducts>>
fun getCategoryWithProducts() : Flow<List<CategoryWithProducts>>
}

View File

@ -12,16 +12,16 @@ interface OrderDao {
@Delete
suspend fun delete(order: Order)
@Query("select * from 'order'")
@Query("select * from `order`")
fun getAll() : Flow<List<OrderWithProducts>>
@Query("select * from 'order' where 'order.id' =:id")
@Query("select * from `order` where `order`.`user_id` =:id")
fun getById(id: Int): Flow<List<OrderWithProducts>>
@Query("select * from 'order' where ('order.user_id' =:userId and 'order.status_id' != 1 and 'order.status_id' != 4)")
@Query("select * from `order` where `order`.`user_id` =:userId and `order`.`status_id` != 1 and `order`.`status_id` != 4")
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>>
@Query("select * from 'order' where ('order.user_id' =:userId and 'order.status_id' = 1)")
@Query("select * from `order` where `order`.`user_id` =:userId and `order`.`status_id` = 1")
fun getCartByUserId(userId: Int): Flow<OrderWithProducts>
@Query("select * from 'order' where ('order.status_id' != 1 and 'order.status_id' != 4)")
@Query("select * from `order` where `order`.`status_id` != 1 and `order`.`status_id` != 4")
fun getOrdersToWork(): Flow<List<OrderWithProducts>>
}

View File

@ -4,7 +4,9 @@ import androidx.room.*
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.mobileapp.dao.*
import com.example.mobileapp.models.*
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Database(
entities =
@ -38,36 +40,32 @@ abstract class AppDatabase : RoomDatabase() {
private suspend fun populateDatabase() {
INSTANCE?.let { database ->
//region Roles
//Roles
val roleDao = database.roleDao()
roleDao.insert(Role(1, "admin"))
roleDao.insert(Role(2, "worker"))
roleDao.insert(Role(3, "user"))
//endregion
//region Statuses
//Statuses
val statusDao = database.statusDao()
statusDao.insert(Status(1, "Корзина"))
statusDao.insert(Status(2, "Принят"))
statusDao.insert(Status(3, "Готов"))
statusDao.insert(Status(4, "Выдан"))
//endregion
//region Categories
//Categories
val categoryDao = database.categoryDao()
categoryDao.insert(Category(1, "Бургеры"))
categoryDao.insert(Category(2, "Картошка"))
categoryDao.insert(Category(3, "Напитки"))
//endregion
//region Users
//Users
val userDao = database.userDao()
userDao.insert(User(1, "admin", "admin", 1))
userDao.insert(User(2, "worker", "worker", 2))
userDao.insert(User(3, "user", "user", 3))
//endregion
//region Products
//Products
val productDao = database.productDao()
//Бургеры
productDao.insert(Product(1, "Бургер1", 1, 150))
@ -81,67 +79,36 @@ abstract class AppDatabase : RoomDatabase() {
productDao.insert(Product(7, "Напиток1", 3, 100))
productDao.insert(Product(8, "Напиток2", 3, 200))
productDao.insert(Product(9, "Напиток3", 3, 300))
//endregion
//region Orders
//Orders
val orderDao = database.orderDao()
//Корзины
orderDao.insert(Order(1,1,400,1))
orderDao.insert(Order(2,2,600,1))
orderDao.insert(Order(3,3,1800,1))
//Заказы
orderDao.insert(Order(4,1,250,3))
orderDao.insert(Order(5,1,400,2))
orderDao.insert(Order(6,2,750,3))
orderDao.insert(Order(7,2,550,2))
orderDao.insert(Order(8,3,400,3))
orderDao.insert(Order(9,3,250,2))
//endregion
orderDao.insert(Order(2,1,250,3))
orderDao.insert(Order(3,1,400,2))
//region OrderProducts
val orderProductDao = database.orderProductDao()
//Корзины
//1
orderProductDao.insert(OrderProduct(1,1,2))
orderProductDao.insert(OrderProduct(1,7,1))
//2
orderProductDao.insert(OrderProduct(2,2,1))
orderProductDao.insert(OrderProduct(2,5,1))
orderProductDao.insert(OrderProduct(2,8,1))
//3
orderProductDao.insert(OrderProduct(3,3,2))
orderProductDao.insert(OrderProduct(3,6,2))
orderProductDao.insert(OrderProduct(3,9,2))
//Заказы
//1
orderProductDao.insert(OrderProduct(4,1,1))
orderProductDao.insert(OrderProduct(4,7,1))
//2
orderProductDao.insert(OrderProduct(5,2,1))
orderProductDao.insert(OrderProduct(5,5,1))
//3
orderProductDao.insert(OrderProduct(6,2,1))
orderProductDao.insert(OrderProduct(6,3,1))
//4
orderProductDao.insert(OrderProduct(7,3,1))
orderProductDao.insert(OrderProduct(7,7,1))
//5
orderProductDao.insert(OrderProduct(8,2,1))
orderProductDao.insert(OrderProduct(8,7,1))
//6
orderProductDao.insert(OrderProduct(9,4,1))
orderProductDao.insert(OrderProduct(9,8,1))
//endregion
orderProductDao.insert(OrderProduct(2,1,1))
orderProductDao.insert(OrderProduct(2,7,1))
orderProductDao.insert(OrderProduct(3,2,1))
orderProductDao.insert(OrderProduct(3,5,1))
}
}
fun getInstance(appContext: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DB_NAME
)
Room
.databaseBuilder(
appContext,
AppDatabase::class.java,
DB_NAME
)
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

View File

@ -1,5 +1,6 @@
package com.example.mobileapp.navigation
import android.annotation.SuppressLint
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
@ -15,8 +16,10 @@ import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
@ -42,6 +45,7 @@ import com.example.mobileapp.screens.RegistrationScreen
import com.example.mobileapp.screens.UsersScreen
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@OptIn(ExperimentalMaterial3Api::class)
@ -82,34 +86,41 @@ fun AppNavigation(){
}
}
@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun CustomNavigationBar(navController: NavController) {
val user = remember { mutableStateOf(User(-1, "", "", -1)) }
val context = LocalContext.current
val listOfNavItems = remember { mutableStateListOf<NavItem>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
user.value = (AppDatabase.getInstance(context).userDao().getById(1))
(AppDatabase.getInstance(context).roleDao().getById(user.value.roleId)).collect{data ->
when (data.name) {
"admin" -> {
listOfNavItems.clear()
listOfNavItems.addAll(listOfAdminNavItems)
}
"worker" -> {
listOfNavItems.clear()
listOfNavItems.addAll(listOfWorkerNavItems)
}
"user" -> {
listOfNavItems.clear()
listOfNavItems.addAll(listOfUserNavItems)
}
}
}
}
}
Row(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(0.1f)
.background(Color(212, 206, 203))
) {
var listOfNavItems : List<NavItem>? = null
val user = remember { mutableStateOf(User(-1, "", "", -1)) }
val context = LocalContext.current
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
user.value = AppDatabase.getInstance(context).userDao().getById(1)
(AppDatabase.getInstance(context).roleDao().getByName("admin")).collect{
if (it.id == user.value.roleId) { listOfNavItems = listOfAdminNavItems }
}
(AppDatabase.getInstance(context).roleDao().getByName("worker")).collect{
if (it.id == user.value.roleId) { listOfNavItems = listOfWorkerNavItems }
}
(AppDatabase.getInstance(context).roleDao().getByName("user")).collect{
if (it.id == user.value.roleId) { listOfNavItems = listOfUserNavItems }
}
}
}
listOfNavItems?.forEachIndexed { index, navItem ->
listOfNavItems.forEachIndexed { index, navItem ->
OutlinedButton(
modifier = Modifier
.fillMaxHeight()
@ -123,11 +134,11 @@ fun CustomNavigationBar(navController: NavController) {
border = BorderStroke(4.dp, Color(222,161,69)),
shape = RoundedCornerShape(
topStart = if (index == 0) 30.dp else 0.dp,
topEnd = if (index == listOfNavItems!!.size - 1) 30.dp else 0.dp
topEnd = if (index == listOfNavItems.size - 1) 30.dp else 0.dp
),
colors = ButtonDefaults.outlinedButtonColors(Color(255,186,83)),
){
){
Icon(
painterResource(id = navItem.icon),
contentDescription = null,

View File

@ -122,7 +122,7 @@ fun CartCard(){
category.value = data.name
}
}
if (category.value == "Бургер"){
if (category.value == "Бургеры"){
Image(
painterResource(
id = R.drawable.burger

View File

@ -84,20 +84,8 @@ fun CreateProductCard(){
.padding(top = 10.dp),
textAlign = TextAlign.Center,
)
val categoryList = remember { mutableStateListOf<String>() }
var expanded by remember { mutableStateOf(false) }
val title = remember { mutableStateOf(TextFieldValue("")) }
val price = remember { mutableStateOf(TextFieldValue("")) }
val context = LocalContext.current
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).categoryDao().getAll().collect { data ->
categoryList.clear()
categoryList.addAll(data.map { it.name })
}
}
}
var selectedCategory by remember { mutableStateOf(categoryList[0]) }
Card(
shape = RoundedCornerShape(20.dp),
@ -139,6 +127,19 @@ fun CreateProductCard(){
onSurface = Color.Gray
)
){
val categoryList = remember { mutableStateListOf<String>() }
var selectedCategory by remember { mutableStateOf("") }
var expanded by remember { mutableStateOf(false) }
val context = LocalContext.current
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).categoryDao().getAll().collect { data ->
categoryList.clear()
categoryList.addAll(data.map { category -> category.name })
selectedCategory = data.first().name
}
}
}
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {

View File

@ -21,6 +21,7 @@ import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
@ -70,13 +71,13 @@ fun EditMenuCard(navController: NavController){
horizontalAlignment = Alignment.CenterHorizontally,
) {
val categoryWithProducts = remember { mutableListOf<CategoryWithProducts>()}
val categoryWithProducts = remember { mutableStateListOf<CategoryWithProducts>() }
val context = LocalContext.current
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).categoryDao().getCatgoryWithProducts().collect { data ->
AppDatabase.getInstance(context).categoryDao().getCategoryWithProducts().collect { data ->
categoryWithProducts.addAll(data)
}
}

View File

@ -1,5 +1,6 @@
package com.example.mobileapp.screens
import android.annotation.SuppressLint
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
@ -27,6 +28,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -43,6 +45,7 @@ import com.example.mobileapp.R
import com.example.mobileapp.db.AppDatabase
import com.example.mobileapp.widgets.Header
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
@ -56,6 +59,7 @@ fun EditProductScreen(id: Int){
}
}
@SuppressLint("CoroutineCreationDuringComposition")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EditProductCard(id: Int){
@ -85,11 +89,12 @@ fun EditProductCard(id: Int){
textAlign = TextAlign.Center,
)
val context = LocalContext.current
val categoryList = remember { mutableStateListOf<String>() }
var selectedCategory by remember { mutableStateOf("") }
var expanded by remember { mutableStateOf(false) }
val title = remember { mutableStateOf(TextFieldValue("")) }
val price = remember { mutableStateOf(TextFieldValue("")) }
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getById(id).collect {data ->
@ -100,10 +105,6 @@ fun EditProductCard(id: Int){
selectedCategory = it.name
}
}
AppDatabase.getInstance(context).categoryDao().getAll().collect { data ->
categoryList.clear()
categoryList.addAll(data.map { it.name })
}
}
}
Card(
@ -139,6 +140,7 @@ fun EditProductCard(id: Int){
border = BorderStroke(1.dp, Color(222,161,69)),
modifier = Modifier.fillMaxWidth().padding(top = 20.dp, start = 20.dp, end = 20.dp),
){
MaterialTheme(
colorScheme = lightColorScheme(
surfaceVariant= Color.White,
@ -146,6 +148,13 @@ fun EditProductCard(id: Int){
onSurface = Color.Gray
)
){
val categoryList = remember { mutableStateListOf<String>() }
scope.launch {
AppDatabase.getInstance(context).categoryDao().getAll().collect { data ->
categoryList.clear()
categoryList.addAll(data.map { category -> category.name })
}
}
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {

View File

@ -20,6 +20,7 @@ import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
@ -69,20 +70,17 @@ fun MenuCard(){
horizontalAlignment = Alignment.CenterHorizontally,
) {
val categoryWithProducts = remember { mutableListOf<CategoryWithProducts>()}
val categoryWithProducts = remember { mutableStateListOf<CategoryWithProducts>()}
val context = LocalContext.current
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).categoryDao().getCatgoryWithProducts().collect { data ->
(AppDatabase.getInstance(context).categoryDao().getCategoryWithProducts()).collect { data ->
categoryWithProducts.addAll(data)
}
}
}
LazyColumn(
modifier = Modifier
.fillMaxSize()

View File

@ -19,6 +19,7 @@ import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
@ -80,7 +81,7 @@ fun OrderCard(){
textAlign = TextAlign.Center,
)
val ordersWithProducts = remember { mutableListOf<OrderWithProducts> () }
val ordersWithProducts = remember { mutableStateListOf<OrderWithProducts> () }
val context = LocalContext.current
val scope = rememberCoroutineScope()
@ -125,7 +126,7 @@ fun OrderCard(){
)
Text(
text = "Статус: " + status,
text = "Статус: " + status.value,
color = Color.Black,
fontFamily = FontFamily(Font(R.font.nunito_extrabold_italic)),
fontSize = 20.sp,
@ -166,6 +167,7 @@ fun OrderCard(){
border = BorderStroke(4.dp, Color(222,161,69)),
shape = RoundedCornerShape(20.dp),
colors = ButtonDefaults.outlinedButtonColors(Color(255,186,83)),
modifier = Modifier.padding(bottom = 10.dp)
) {
Text(
text = "Готов",
@ -183,6 +185,7 @@ fun OrderCard(){
border = BorderStroke(4.dp, Color(222,161,69)),
shape = RoundedCornerShape(20.dp),
colors = ButtonDefaults.outlinedButtonColors(Color(255,186,83)),
modifier = Modifier.padding(bottom = 10.dp)
) {
Text(
text = "Выдать",

View File

@ -19,6 +19,7 @@ import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
@ -103,13 +104,14 @@ fun ProfileCard(navController: NavController){
textAlign = TextAlign.Center,
)
val ordersWithProducts = remember { mutableListOf<OrderWithProducts> () }
val ordersWithProducts = remember { mutableStateListOf<OrderWithProducts> () }
val context = LocalContext.current
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).orderDao().getByUserId(1).collect { data ->
ordersWithProducts.clear()
ordersWithProducts.addAll(data)
}
}

View File

@ -97,7 +97,7 @@ fun UsersCard(){
.padding(top = 10.dp),
textAlign = TextAlign.Center,
)
val users = remember { mutableListOf<User> () }
val users = remember { mutableStateListOf<User> () }
val context = LocalContext.current
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
@ -152,8 +152,11 @@ fun UsersCard(){
scope.launch {
AppDatabase.getInstance(context).roleDao().getAll().collect { data ->
roleList.addAll(data.map {it.name})
roleList.clear()
roleList.addAll(data.map {role -> role.name})
}
}
scope.launch {
AppDatabase.getInstance(context).roleDao().getById(user.roleId).collect { data ->
selectedRole = data.name
}