diff --git a/app/build.gradle b/app/build.gradle index f86c5cb..2c2d9fa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -73,4 +73,8 @@ 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.40.5" + kapt "com.google.dagger:hilt-android-compiler:2.40.5" } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/MainActivity.kt b/app/src/main/java/com/example/shawarma/MainActivity.kt index fd16c24..94fc2fb 100644 --- a/app/src/main/java/com/example/shawarma/MainActivity.kt +++ b/app/src/main/java/com/example/shawarma/MainActivity.kt @@ -11,16 +11,14 @@ 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.HiltAndroidApp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - - MainNavBar() - - + MainNavBar() } } } diff --git a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt index 3c0f6f6..2365a7d 100644 --- a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt +++ b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt @@ -6,10 +6,10 @@ 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 diff --git a/app/src/main/java/com/example/shawarma/data/dao/OrderDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt similarity index 88% rename from app/src/main/java/com/example/shawarma/data/dao/OrderDao.kt rename to app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt index 4fa164b..eba1de0 100644 --- a/app/src/main/java/com/example/shawarma/data/dao/OrderDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt @@ -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> + fun getAll(): Flow> @Query("select * from orders where orders.id =:id") fun getById(id: Int): Flow> @Query("select * from orders where orders.user_id =:userId") fun getByUserId(userId: Int): Flow> - } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/dao/OrderProductDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt similarity index 80% rename from app/src/main/java/com/example/shawarma/data/dao/OrderProductDao.kt rename to app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt index 1bef4fc..1384f66 100644 --- a/app/src/main/java/com/example/shawarma/data/dao/OrderProductDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt @@ -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 diff --git a/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt similarity index 94% rename from app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt rename to app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt index 53a2276..1bff9a8 100644 --- a/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/ProductDao.kt @@ -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 + } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/dao/UserDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt similarity index 86% rename from app/src/main/java/com/example/shawarma/data/dao/UserDao.kt rename to app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt index b00e1ca..9a4a885 100644 --- a/app/src/main/java/com/example/shawarma/data/dao/UserDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/UserDao.kt @@ -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 @@ -12,16 +12,12 @@ import kotlinx.coroutines.flow.Flow 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> - + fun getAll(): Flow> @Query("select * from users where users.id = :id") fun getById(id: Int): UserModel } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt new file mode 100644 index 0000000..0e5c1b3 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt new file mode 100644 index 0000000..756a408 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt @@ -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> { + return orderDao.getAll() + } + fun getById(id: Int): Flow> { + return orderDao.getById(id) + } + fun getByUserId(userId: Int): Flow> { + return orderDao.getByUserId(userId) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt new file mode 100644 index 0000000..daf6592 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt @@ -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> { + return productDao.getAll() + } + fun getDiscounts(): Flow> { + return productDao.getDiscounts() + } + fun getItems(): Flow> { + return productDao.getItems() + } + fun getById(id: Int): Flow { + return productDao.getById(id) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt new file mode 100644 index 0000000..de6acc6 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt @@ -0,0 +1,27 @@ +package com.example.shawarma.data.repos + +import androidx.room.Dao +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> { + return userDao.getAll() + } + fun getById(id: Int): UserModel { + return userDao.getById(id) + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 44023d2..b7bfbe5 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:4.0.0' + classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5' } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins {