Compare commits

...

5 Commits

24 changed files with 454 additions and 107 deletions

View File

@ -5,6 +5,9 @@ plugins {
id 'kotlin-kapt'
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.dagger.hilt.android'
android {
namespace 'com.example.shawarma'
compileSdk 34
@ -73,4 +76,11 @@ dependencies {
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
// lab4
implementation "com.google.dagger:hilt-android:2.42"
kapt "com.google.dagger:hilt-android-compiler:2.42"
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
implementation "androidx.datastore:datastore-preferences:1.0.0"
}

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".ShawarmaApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
@ -19,7 +20,6 @@
android:theme="@style/Theme.Shawarma">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

View File

@ -0,0 +1,38 @@
package com.example.shawarma
import android.app.Application
import androidx.room.Room
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.repos.ProductRepository
import com.example.shawarma.data.repos.UserRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideDatabase(app: Application) : AppDatabase {
return Room.databaseBuilder(
app,
AppDatabase::class.java,
AppDatabase.DB_NAME
).build()
}
@Provides
@Singleton
fun provideUserRepository(db: AppDatabase) : UserRepository {
return UserRepository(db.userDao())
}
@Provides
@Singleton
fun provideProductRepository(db: AppDatabase) : ProductRepository {
return ProductRepository(db.productDao())
}
}

View File

@ -11,16 +11,15 @@ import com.example.shawarma.screens.authorization.AuthorizationScreen
import com.example.shawarma.screens.registration.RegistrationScreen
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.ShawarmaTheme
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.HiltAndroidApp
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainNavBar()
MainNavBar()
}
}
}

View File

@ -0,0 +1,11 @@
package com.example.shawarma
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class ShawarmaApp : Application() {
override fun onCreate() {
super.onCreate()
}
}

View File

@ -6,19 +6,17 @@ 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.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.interfaces.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 =
@ -39,7 +37,7 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun orderProductDao() : OrderProductDao
companion object {
private const val DB_NAME: String = "shawarma-db"
const val DB_NAME: String = "shawarma-db"
@Volatile
private var INSTANCE: AppDatabase? = null
@ -50,52 +48,52 @@ abstract class AppDatabase : RoomDatabase() {
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(1, 4, 3, 150)
val op3 = OrderProductModel(1, 3, 1, 80)
val op4 = OrderProductModel(2, 1, 2, 200)
val op5 = OrderProductModel(2, 4, 3, 150)
val op6 = OrderProductModel(2, 3, 1, 80)
val op7 = OrderProductModel(3, 1, 2, 200)
val op8 = OrderProductModel(3, 4, 3, 150)
val op9 = OrderProductModel(3, 3, 1, 80)
val op10 = OrderProductModel(4, 1, 2, 200)
val op11 = OrderProductModel(4, 4, 3, 150)
val op12 = OrderProductModel(4, 3, 1, 80)
orderProductDao.insert(op1)
orderProductDao.insert(op2)
orderProductDao.insert(op3)
orderProductDao.insert(op4)
orderProductDao.insert(op5)
orderProductDao.insert(op6)
orderProductDao.insert(op7)
orderProductDao.insert(op8)
orderProductDao.insert(op9)
orderProductDao.insert(op10)
orderProductDao.insert(op11)
orderProductDao.insert(op12)
// // 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(1, 4, 3, 150)
// val op3 = OrderProductModel(1, 3, 1, 80)
// val op4 = OrderProductModel(2, 1, 2, 200)
// val op5 = OrderProductModel(2, 4, 3, 150)
// val op6 = OrderProductModel(2, 3, 1, 80)
// val op7 = OrderProductModel(3, 1, 2, 200)
// val op8 = OrderProductModel(3, 4, 3, 150)
// val op9 = OrderProductModel(3, 3, 1, 80)
// val op10 = OrderProductModel(4, 1, 2, 200)
// val op11 = OrderProductModel(4, 4, 3, 150)
// val op12 = OrderProductModel(4, 3, 1, 80)
// orderProductDao.insert(op1)
// orderProductDao.insert(op2)
// orderProductDao.insert(op3)
// orderProductDao.insert(op4)
// orderProductDao.insert(op5)
// orderProductDao.insert(op6)
// orderProductDao.insert(op7)
// orderProductDao.insert(op8)
// orderProductDao.insert(op9)
// orderProductDao.insert(op10)
// orderProductDao.insert(op11)
// orderProductDao.insert(op12)
}
}

View File

@ -1,4 +1,4 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
@ -8,23 +8,18 @@ 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>>
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

@ -1,9 +1,8 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Insert
import com.example.shawarma.data.models.OrderProductModel
@Dao
interface OrderProductDao {
@Insert

View File

@ -1,4 +1,4 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
@ -7,7 +7,6 @@ import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
@Dao
interface ProductDao {
@Insert
@ -30,4 +29,5 @@ interface ProductDao {
@Query("select * from products where products.id = :id")
fun getById(id: Int): Flow<ProductModel>
}

View File

@ -1,8 +1,9 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy.Companion.ABORT
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.UserModel
@ -10,18 +11,16 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface UserDao {
@Insert
@Insert(onConflict = ABORT)
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
fun getAll(): Flow<List<UserModel>>
@Query("select * from users where id = :id")
fun getById(id: Int): Flow<UserModel>
@Query("select * from users where user_login = :login and user_password = :password limit 1")
fun login(login: String, password: String) : Flow<UserModel?>
}

View File

@ -0,0 +1,16 @@
package com.example.shawarma.data.repos
import androidx.room.Dao
import androidx.room.Insert
import com.example.shawarma.data.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.models.OrderProductModel
import javax.inject.Inject
class OrderProductRepository @Inject constructor(
private val orderProductDao: OrderProductDao
) {
suspend fun insert(order: OrderProductModel) {
return orderProductDao.insert(order)
}
}

View File

@ -0,0 +1,36 @@
package com.example.shawarma.data.repos
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.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderWithProducts
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class OrderRepository @Inject constructor(
private val orderDao: OrderDao
){
suspend fun insert(order: OrderModel) {
return orderDao.insert(order)
}
suspend fun update(order:OrderModel) {
return orderDao.update(order)
}
suspend fun delete(order: OrderModel) {
return orderDao.delete(order)
}
fun getAll(): Flow<List<OrderWithProducts>> {
return orderDao.getAll()
}
fun getById(id: Int): Flow<List<OrderWithProducts>> {
return orderDao.getById(id)
}
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>> {
return orderDao.getByUserId(userId)
}
}

View File

@ -0,0 +1,37 @@
package com.example.shawarma.data.repos
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.interfaces.dao.UserDao
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class ProductRepository @Inject constructor(
private val productDao: ProductDao
) {
suspend fun insert(product: ProductModel) {
return productDao.insert(product)
}
suspend fun update(product: ProductModel) {
return productDao.update(product)
}
suspend fun delete(product: ProductModel) {
return productDao.delete(product)
}
fun getAll(): Flow<List<ProductModel>> {
return productDao.getAll()
}
fun getDiscounts(): Flow<List<ProductModel>> {
return productDao.getDiscounts()
}
fun getItems(): Flow<List<ProductModel>> {
return productDao.getItems()
}
fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id)
}
}

View File

@ -0,0 +1,29 @@
package com.example.shawarma.data.repos
import com.example.shawarma.data.interfaces.dao.UserDao
import com.example.shawarma.data.models.UserModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class UserRepository @Inject constructor(
private val userDao: UserDao
) {
suspend fun insert(user: UserModel) {
return userDao.insert(user)
}
suspend fun update (user: UserModel) {
return userDao.update(user)
}
suspend fun delete (user: UserModel) {
return userDao.delete(user)
}
fun getAll(): Flow<List<UserModel>> {
return userDao.getAll()
}
fun getById(id: Int): Flow<UserModel> {
return userDao.getById(id)
}
fun login(login: String, password: String): Flow<UserModel?> {
return userDao.login(login, password)
}
}

View File

@ -0,0 +1,19 @@
package com.example.shawarma.data.sharedpref
import android.content.Context
import android.content.SharedPreferences
class PreferencesManager(context: Context) {
private val sharedPreferences: SharedPreferences =
context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
fun saveData(key: String, value: String) {
val editor = sharedPreferences.edit()
editor.putString(key, value)
editor.apply()
}
fun getData(key: String, defaultValue: String): String {
return sharedPreferences.getString(key, defaultValue) ?: defaultValue
}
}

View File

@ -14,21 +14,26 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.shawarma.data.sharedpref.PreferencesManager
import com.example.shawarma.ui.theme.JejuFamily
import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.viewmodels.UserViewModel
import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo1
@ -44,6 +49,19 @@ fun AuthorizationScreen(navHostController: NavHostController) {
fun AuthorizationCard(navHostController: NavHostController) {
val login = remember { mutableStateOf(TextFieldValue("")) }
val password = remember { mutableStateOf(TextFieldValue("")) }
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
val preferencesManager = PreferencesManager(LocalContext.current)
if (userViewModel.userModel.observeAsState().value != null) {
preferencesManager.saveData("user_id", userViewModel.userModel.value?.id.toString())
preferencesManager.saveData("user_role", userViewModel.userModel.value?.role.toString())
navHostController.navigate(ScreenPaths.home.name) {
popUpTo(ScreenPaths.authorization.name) {
inclusive = true
}
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
@ -97,11 +115,8 @@ fun AuthorizationCard(navHostController: NavHostController) {
)
Button(
onClick = {
navHostController.navigate(ScreenPaths.home.name) {
popUpTo(ScreenPaths.authorization.name) {
inclusive = true
}
} },
userViewModel.login(login.value.text, password.value.text)
},
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
shape = RoundedCornerShape(20.dp),
modifier = Modifier

View File

@ -37,6 +37,7 @@ 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.screens.home.ProductCard
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
@ -94,15 +95,27 @@ fun DiscountList(){
)
{
items(products.size) { index ->
if (index % 2 != 1) {
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
ProductCard(products[index])
}
}
else {
if (index % 2 != 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
}
}
}
if (index == products.size - 1) {

View File

@ -16,7 +16,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonColors
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
@ -37,7 +36,6 @@ 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
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
@ -93,7 +91,7 @@ fun HomeList(){
)
{
items(products.size) { index ->
if (index % 2 != 1) {
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
@ -101,7 +99,19 @@ fun HomeList(){
.padding(top = 10.dp)
) {
ProductCard(products[index])
ProductCard(products[index + 1])
}
}
else {
if (index % 2 != 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
ProductCard(products[index])
ProductCard(products[index + 1])
}
}
}
if (index == products.size - 1) {

View File

@ -4,19 +4,18 @@ import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
@ -28,6 +27,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
@ -36,6 +36,8 @@ import com.example.shawarma.ui.theme.MyLightYellow
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.utils.ScreenPaths
import com.example.shawarma.viewmodels.ProductViewModel
import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
@ -57,6 +59,16 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
val price = remember { mutableStateOf(TextFieldValue(""))}
val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>()
if (productViewModel.addingProductState.observeAsState().value == true) {
navHostController.navigate(ScreenPaths.products.name) {
popUpTo(ScreenPaths.product.name) {
inclusive = true
}
}
}
if (productId != 0 && productId != null) {
val context = LocalContext.current
var product = remember {
@ -92,7 +104,8 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
)
Column(
modifier = Modifier
.padding(top = 60.dp),
.padding(top = 60.dp)
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally
) {
MyTextField(
@ -138,8 +151,7 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
border = BorderStroke(2.dp, color = MyOrange),
shape = RoundedCornerShape(20.dp),
onClick = {
// TODO
navHostController.popBackStack()
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
}
) {
Text(

View File

@ -1,8 +1,5 @@
package com.example.shawarma.screens.registration
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.gestures.ScrollScope
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
@ -17,6 +14,7 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
@ -28,11 +26,12 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.shawarma.screens.authorization.AuthorizationCard
import com.example.shawarma.ui.theme.JejuFamily
import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.viewmodels.UserViewModel
import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo1
@ -50,6 +49,16 @@ fun RegistrationCard(navHostController: NavHostController){
val password = remember { mutableStateOf(TextFieldValue("")) }
val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) }
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
if (userViewModel.registrationState.observeAsState().value == true) {
navHostController.navigate(ScreenPaths.authorization.name) {
popUpTo(ScreenPaths.authorization.name) {
inclusive = true
}
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
@ -110,7 +119,7 @@ fun RegistrationCard(navHostController: NavHostController){
)
Button(
onClick = {
// TODO
userViewModel.register(login.value.text, password.value.text, passwordRepeat.value.text)
},
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
shape = RoundedCornerShape(20.dp),

View File

@ -0,0 +1,27 @@
package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.repos.ProductRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class ProductViewModel @Inject constructor(
private val productRepository: ProductRepository
) : ViewModel() {
private val _addingProductState = MutableLiveData<Boolean>(false)
val addingProductState: LiveData<Boolean?>
get() = _addingProductState
fun addProduct(name: String, price: Int, oldPrice: Int? = null) {
viewModelScope.launch {
productRepository.insert(ProductModel(null, name, price, oldPrice))
_addingProductState.postValue(true)
}
}
}

View File

@ -0,0 +1,66 @@
package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.shawarma.data.models.UserModel
import com.example.shawarma.data.repos.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@HiltViewModel
class UserViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private val _userModel = MutableLiveData<UserModel?>()
val userModel: LiveData<UserModel?>
get() = _userModel
fun login(login: String, password: String){
viewModelScope.launch {
withContext(Dispatchers.Main) {
userRepository.login(login, password).collect() { user ->
if (user != null) {
_userModel.postValue(user)
}
else {
_userModel.postValue(null)
}
}
}
}
}
private val _registrationState = MutableLiveData<Boolean?>()
val registrationState: LiveData<Boolean?>
get() = _registrationState
fun register(login: String, password: String, passwordRepeat: String) {
if (password != passwordRepeat) {
// ругаться в ui
_registrationState.postValue(false)
}
viewModelScope.launch {
withContext(Dispatchers.Main) {
userRepository.login(login, password).collect() { user ->
if (user == null) {
if (login == "admin" && password == "admin") {
userRepository.insert(UserModel(null, login, password, "ADMIN"))
_registrationState.postValue(true)
}
else {
userRepository.insert(UserModel(null, login, password, "USER"))
_registrationState.postValue(true)
}
}
}
}
}
}
}

View File

@ -5,27 +5,35 @@ import androidx.compose.foundation.layout.size
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.example.shawarma.data.sharedpref.PreferencesManager
import com.example.shawarma.utils.BottomNavItem
@Composable
fun BottomNavBar(navController: NavController) {
val items = listOf(
val preferencesManager = PreferencesManager(LocalContext.current)
val user_role = preferencesManager.getData("user_role", "USER")
val adminItems = listOf(
BottomNavItem.Discount,
BottomNavItem.Home,
BottomNavItem.Cart,
BottomNavItem.Orders,
BottomNavItem.Products
)
val userItems = listOf(
BottomNavItem.Discount,
BottomNavItem.Home,
BottomNavItem.Cart
)
BottomNavigation(
backgroundColor = Color.White,
contentColor = Color.Black,
@ -33,6 +41,7 @@ fun BottomNavBar(navController: NavController) {
) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
val items = if (user_role == "ADMIN") adminItems else userItems
items.forEach { item ->
BottomNavigationItem(
icon = {
@ -56,7 +65,6 @@ fun BottomNavBar(navController: NavController) {
restoreState = true
}
},
)
}
}

View File

@ -7,6 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {