diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ccc4570..d3dd81e 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -83,4 +83,8 @@ dependencies { kapt("androidx.room:room-compiler:$room_version") implementation("androidx.room:room-ktx:$room_version") implementation("androidx.room:room-paging:$room_version") + + //Paging + implementation ("androidx.paging:paging-compose:3.2.1") + implementation ("androidx.paging:paging-runtime:3.2.1") } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/App.kt b/app/src/main/java/com/example/androidlabs/App.kt index 592737b..09a93da 100644 --- a/app/src/main/java/com/example/androidlabs/App.kt +++ b/app/src/main/java/com/example/androidlabs/App.kt @@ -7,13 +7,13 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class App : Application() { - val database by lazy { AppDatabase.getInstance(this)} - + lateinit var container: AppContainer override fun onCreate() { super.onCreate() //this.deleteDatabase("my-db") - CoroutineScope(Dispatchers.IO).launch { - AppDatabase.populateDatabase() - } + //CoroutineScope(Dispatchers.IO).launch { + // AppDatabase.populateDatabase() + //} + container = AppDataContainer(this) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/AppContainer.kt b/app/src/main/java/com/example/androidlabs/AppContainer.kt new file mode 100644 index 0000000..b5b0cda --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/AppContainer.kt @@ -0,0 +1,11 @@ +package com.example.androidlabs + +import com.example.androidlabs.DB.repository.HotelRepository +import com.example.androidlabs.DB.repository.OrderRepository +import com.example.androidlabs.DB.repository.UserRepository + +interface AppContainer { + val hotelRepo: HotelRepository + val userRepo: UserRepository + val orderRepo: OrderRepository +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/AppDataContainer.kt b/app/src/main/java/com/example/androidlabs/AppDataContainer.kt new file mode 100644 index 0000000..c9f7ed0 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/AppDataContainer.kt @@ -0,0 +1,22 @@ +package com.example.androidlabs + +import android.content.Context +import com.example.androidlabs.DB.AppDatabase +import com.example.androidlabs.DB.repository.HotelRepImpl +import com.example.androidlabs.DB.repository.HotelRepository +import com.example.androidlabs.DB.repository.OrderRepImpl +import com.example.androidlabs.DB.repository.OrderRepository +import com.example.androidlabs.DB.repository.UserRepImpl +import com.example.androidlabs.DB.repository.UserRepository + +class AppDataContainer(private val context: Context) : AppContainer { + override val hotelRepo: HotelRepository by lazy { + HotelRepImpl(AppDatabase.getInstance(context).hotelDao()) + } + override val userRepo: UserRepository by lazy { + UserRepImpl(AppDatabase.getInstance(context).userDao()) + } + override val orderRepo: OrderRepository by lazy { + OrderRepImpl(AppDatabase.getInstance(context).orderDao()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/AppDatabase.kt b/app/src/main/java/com/example/androidlabs/DB/AppDatabase.kt index 7f970de..1fa2823 100644 --- a/app/src/main/java/com/example/androidlabs/DB/AppDatabase.kt +++ b/app/src/main/java/com/example/androidlabs/DB/AppDatabase.kt @@ -33,9 +33,9 @@ abstract class AppDatabase : RoomDatabase() { INSTANCE?.let { database -> // User val userDao = database.userDao() - val user1 = User(null, "Artem", "Emelyanov", "artem@mail.ru", "123", RoleEnum.Admin) + val user1 = User(null, "Artem", "Emelyanov", "artem@mail.ru", "123", RoleEnum.User) val user2 = User(null, "Danil", "Markov", "danil@mail.ru", "123", RoleEnum.User) - val user3 = User(null, "Viktoria", "Presnyakova", "vika@mail.ru", "123", RoleEnum.User) + val user3 = User(null, "Viktoria", "Presnyakova", "vika@mail.ru", "123", RoleEnum.Admin) userDao.createUser(user1) userDao.createUser(user2) userDao.createUser(user3) diff --git a/app/src/main/java/com/example/androidlabs/DB/dao/HotelDao.kt b/app/src/main/java/com/example/androidlabs/DB/dao/HotelDao.kt index b3c93b6..0f067e0 100644 --- a/app/src/main/java/com/example/androidlabs/DB/dao/HotelDao.kt +++ b/app/src/main/java/com/example/androidlabs/DB/dao/HotelDao.kt @@ -1,4 +1,5 @@ package com.example.androidlabs.DB.dao +import androidx.paging.PagingSource import androidx.room.Dao import androidx.room.Delete import androidx.room.Insert @@ -19,7 +20,7 @@ interface HotelDao { suspend fun delete(hotel: Hotel) @Query("SELECT*FROM Hotel") - fun getAllHotelss(): Flow> + fun getAllHotelsPaged(): PagingSource @Query("SELECT * FROM Hotel WHERE hotelId = :id") suspend fun getHotelById(id: Int): Hotel diff --git a/app/src/main/java/com/example/androidlabs/DB/dao/OrderDao.kt b/app/src/main/java/com/example/androidlabs/DB/dao/OrderDao.kt index e612d85..ee207f4 100644 --- a/app/src/main/java/com/example/androidlabs/DB/dao/OrderDao.kt +++ b/app/src/main/java/com/example/androidlabs/DB/dao/OrderDao.kt @@ -5,6 +5,7 @@ import androidx.room.Delete import androidx.room.Insert import androidx.room.Query import com.example.androidlabs.DB.models.Order +import com.example.androidlabs.DB.models.UserWithOrder import kotlinx.coroutines.flow.Flow @Dao @@ -21,4 +22,7 @@ interface OrderDao { @Delete suspend fun delete(order: Order) + + @Query("SELECT * FROM users WHERE userId =:id") + fun getUserOrders(id: Int) : Flow } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/dao/UserDao.kt b/app/src/main/java/com/example/androidlabs/DB/dao/UserDao.kt index d90f220..ecbc674 100644 --- a/app/src/main/java/com/example/androidlabs/DB/dao/UserDao.kt +++ b/app/src/main/java/com/example/androidlabs/DB/dao/UserDao.kt @@ -26,6 +26,4 @@ interface UserDao { @Query("SELECT * FROM users WHERE email = :email") suspend fun getUserByEmail(email: String): User - @Query("SELECT * FROM users WHERE userId =:id") - fun getUserOrders(id: Int) : Flow } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepImpl.kt b/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepImpl.kt new file mode 100644 index 0000000..cabba75 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepImpl.kt @@ -0,0 +1,29 @@ +package com.example.androidlabs.DB.repository + +import androidx.paging.Pager +import androidx.paging.PagingConfig +import androidx.paging.PagingData +import com.example.androidlabs.DB.dao.HotelDao +import com.example.androidlabs.DB.models.Hotel +import androidx.paging.PagingSource +import kotlinx.coroutines.flow.Flow + + +class HotelRepImpl (private val hotelDao: HotelDao) : HotelRepository { + + override suspend fun insertHotel(hotel: Hotel) = hotelDao.insert(hotel) + + override suspend fun updateHotel(hotel: Hotel) = hotelDao.update(hotel) + + override suspend fun deleteHotel(hotel: Hotel) = hotelDao.delete(hotel) + + override suspend fun getHotelById(id: Int): Hotel = hotelDao.getHotelById(id) + override fun getAllHotelsPaged(): PagingSource = hotelDao.getAllHotelsPaged() + override fun call(): Flow> { + return Pager( + PagingConfig(pageSize = 5) + ) { + hotelDao.getAllHotelsPaged() + }.flow + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepository.kt b/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepository.kt new file mode 100644 index 0000000..f895cd8 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/HotelRepository.kt @@ -0,0 +1,14 @@ +package com.example.androidlabs.DB.repository + +import androidx.paging.PagingSource +import com.example.androidlabs.DB.models.Hotel +import kotlinx.coroutines.flow.Flow +import androidx.paging.PagingData +interface HotelRepository { + suspend fun insertHotel(hotel: Hotel) + suspend fun updateHotel(hotel: Hotel) + suspend fun deleteHotel(hotel: Hotel) + suspend fun getHotelById(id: Int): Hotel + fun getAllHotelsPaged(): PagingSource + fun call(): Flow> +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepImpl.kt b/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepImpl.kt new file mode 100644 index 0000000..3bfa70d --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepImpl.kt @@ -0,0 +1,17 @@ +package com.example.androidlabs.DB.repository + +import com.example.androidlabs.DB.dao.OrderDao +import com.example.androidlabs.DB.models.Order +import com.example.androidlabs.DB.models.UserWithOrder +import kotlinx.coroutines.flow.Flow + +class OrderRepImpl (private val orderDao: OrderDao) : OrderRepository { + + override suspend fun createOrder(order: Order): Long = orderDao.createOrder(order) + + override suspend fun delete(order: Order) = orderDao.delete(order) + + override fun getAllOrder(): Flow> = orderDao.getAllOrder() + + override fun getUserOrders(id: Int): Flow = orderDao.getUserOrders(id) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepository.kt b/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepository.kt new file mode 100644 index 0000000..2fba881 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/OrderRepository.kt @@ -0,0 +1,12 @@ +package com.example.androidlabs.DB.repository + +import com.example.androidlabs.DB.models.Order +import com.example.androidlabs.DB.models.UserWithOrder +import kotlinx.coroutines.flow.Flow + +interface OrderRepository { + suspend fun createOrder(order: Order): Long + suspend fun delete(order: Order) + fun getAllOrder(): Flow> + fun getUserOrders(id: Int) : Flow +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/UserRepImpl.kt b/app/src/main/java/com/example/androidlabs/DB/repository/UserRepImpl.kt new file mode 100644 index 0000000..b454b26 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/UserRepImpl.kt @@ -0,0 +1,17 @@ +package com.example.androidlabs.DB.repository + +import com.example.androidlabs.DB.dao.UserDao +import com.example.androidlabs.DB.models.User + +class UserRepImpl(private val userDao: UserDao) : UserRepository { + + override suspend fun createUser(user: User) = userDao.createUser(user) + + override suspend fun updateUser(user: User) = userDao.updateUser(user) + + override suspend fun deleteUser(user: User) = userDao.deleteUser(user) + + override suspend fun getUserById(id: Int): User = userDao.getUserById(id) + + override suspend fun getUserByEmail(email: String): User = userDao.getUserByEmail(email) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/repository/UserRepository.kt b/app/src/main/java/com/example/androidlabs/DB/repository/UserRepository.kt new file mode 100644 index 0000000..af8a992 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/repository/UserRepository.kt @@ -0,0 +1,11 @@ +package com.example.androidlabs.DB.repository + +import com.example.androidlabs.DB.models.User + +interface UserRepository { + suspend fun createUser(user: User) + suspend fun updateUser(user: User) + suspend fun deleteUser(user: User) + suspend fun getUserById(id: Int): User + suspend fun getUserByEmail(email: String): User +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/AppViewModelProvider.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/AppViewModelProvider.kt new file mode 100644 index 0000000..05efd45 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/AppViewModelProvider.kt @@ -0,0 +1,24 @@ +package com.example.androidlabs.DB.viewModels + +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewmodel.CreationExtras +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory +import com.example.androidlabs.App + +object AppViewModelProvider { + val Factory = viewModelFactory { + initializer { + HotelViewModel(app().container.hotelRepo) + } + initializer { + UserViewModel(app().container.userRepo) + } + initializer { + OrderViewModel(app().container.orderRepo) + } + } +} + +fun CreationExtras.app(): App = + (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as App) \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/HotelViewModel.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/HotelViewModel.kt index 65bdfed..3d165b7 100644 --- a/app/src/main/java/com/example/androidlabs/DB/viewModels/HotelViewModel.kt +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/HotelViewModel.kt @@ -6,21 +6,23 @@ import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.CreationExtras +import androidx.paging.cachedIn import com.example.androidlabs.App import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.models.Hotel +import com.example.androidlabs.DB.repository.HotelRepository import com.example.androidlabs.R import kotlinx.coroutines.launch -class HotelViewModel(val database: AppDatabase): ViewModel() { +class HotelViewModel(private val hotelRepository: HotelRepository): ViewModel() { var name = mutableStateOf("") val price = mutableStateOf("") val location = mutableStateOf("") val stars = mutableStateOf("") val info = mutableStateOf("") val img = mutableStateOf(R.drawable.img) - val HotelList = database.hotelDao().getAllHotelss() + val HotelList = hotelRepository.call().cachedIn(viewModelScope) var hotel: Hotel? = null fun insertHotel() = viewModelScope.launch { @@ -32,29 +34,18 @@ class HotelViewModel(val database: AppDatabase): ViewModel() { stars = stars.value.toInt(), info = info.value ) - database.hotelDao().insert(hotel) + hotelRepository.insertHotel(hotel) } fun deleteHotel(hotel : Hotel) = viewModelScope.launch { - database.hotelDao().delete(hotel) + hotelRepository.deleteHotel(hotel) } fun getHotelById(id: Int) = viewModelScope.launch { - database.hotelDao().getHotelById(id) + hotelRepository.getHotelById(id) } fun UpdateHotel(hotel: Hotel) = viewModelScope.launch { - database.hotelDao().update(hotel) - } - - companion object{ - val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{ - override fun create( - modelClass: Class, - extras: CreationExtras): T { - val database = (checkNotNull(extras[APPLICATION_KEY]) as App).database - return HotelViewModel(database) as T - } - } + hotelRepository.updateHotel(hotel) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt index 0b55f6d..7d64932 100644 --- a/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt @@ -10,22 +10,25 @@ import com.example.androidlabs.App import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.models.Hotel import com.example.androidlabs.DB.models.Order +import com.example.androidlabs.DB.models.UserWithOrder +import com.example.androidlabs.DB.repository.OrderRepository import com.example.androidlabs.GlobalUser +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch import java.util.Date -class OrderViewModel(val database: AppDatabase) : ViewModel() { +class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel() { var selectedItem: Hotel? = null val rooms = mutableStateOf("") var dateFrom = mutableStateOf("") var dateTo = mutableStateOf("") fun deleteOrder(order: Order) = viewModelScope.launch { - database.orderDao().delete(order) + orderRepository.delete(order) } - fun getOrderList(id: Int) = viewModelScope.launch { - database.userDao().getUserOrders(id) + fun getOrderList(id: Int) : Flow { + return orderRepository.getUserOrders(id) } @@ -42,7 +45,7 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() { hotel = selectedItem!! ) - val orderId = database.orderDao().createOrder(order) + val orderId = orderRepository.createOrder(order) rooms.value = "" @@ -52,16 +55,4 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() { fun getSubTotal(): Double { return selectedItem!!.price * rooms.value.toInt() } - - companion object{ - val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{ - override fun create( - modelClass: Class, - extras: CreationExtras - ): T { - val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database - return OrderViewModel(database) as T - } - } - } } diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt index d05e93f..24bfd5d 100644 --- a/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt @@ -10,10 +10,11 @@ import com.example.androidlabs.App import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.models.RoleEnum import com.example.androidlabs.DB.models.User +import com.example.androidlabs.DB.repository.UserRepository import com.example.androidlabs.GlobalUser import kotlinx.coroutines.launch -class UserViewModel(val database: AppDatabase): ViewModel() { +class UserViewModel(private val userRepository: UserRepository): ViewModel() { var name = mutableStateOf("") val surname = mutableStateOf("") @@ -27,10 +28,10 @@ class UserViewModel(val database: AppDatabase): ViewModel() { password = password.value, role = RoleEnum.User ) - database.userDao().createUser(user) + userRepository.createUser(user) } fun authUser() = viewModelScope.launch { - val user = database.userDao().getUserByEmail(email.value) + val user = userRepository.getUserByEmail(email.value) if (password.value != "" && user.password == password.value) { val globalUser = GlobalUser.getInstance() globalUser.setUser(user) @@ -43,15 +44,4 @@ class UserViewModel(val database: AppDatabase): ViewModel() { return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() } - companion object{ - val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{ - override fun create( - modelClass: Class, - extras: CreationExtras - ): T { - val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database - return UserViewModel(database) as T - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/Hotel.kt b/app/src/main/java/com/example/androidlabs/Hotel.kt deleted file mode 100644 index a0568b7..0000000 --- a/app/src/main/java/com/example/androidlabs/Hotel.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.androidlabs - - -data class Hotel( - val name: String, - val img: Int, - val stars: Int, - val location: String, - val info: String, - val price: Int, -) \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt b/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt index cbaf40f..d641ea7 100644 --- a/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt +++ b/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt @@ -22,9 +22,9 @@ import androidx.compose.runtime.getValue @Composable fun MyOrderScreen(orderViewModel: OrderViewModel) { - val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null) - - val orderList: List? = userWithOrder?.orders +// val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null) + val userId = GlobalUser.getInstance().getUser()?.userId + val userWithOrder = orderViewModel.getOrderList(userId!!).collectAsState(null).value?.orders println() Column( modifier = Modifier @@ -46,8 +46,8 @@ fun MyOrderScreen(orderViewModel: OrderViewModel) { modifier = Modifier .fillMaxSize() ) { - if (orderList != null) { - for (item in orderList) { + if (userWithOrder != null) { + for (item in userWithOrder) { OrderCard(item, orderViewModel) } } diff --git a/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt b/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt index 4a5d68c..07d9bbc 100644 --- a/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt +++ b/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt @@ -6,6 +6,7 @@ import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import com.example.androidlabs.DB.models.Hotel +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.OrderViewModel import com.example.androidlabs.MyOrderScreen.MyOrderScreen import com.example.androidlabs.booking.BookingScreen @@ -23,7 +24,7 @@ import com.google.gson.Gson @Composable fun NavController(navController: NavHostController) { - var orderViewModel: OrderViewModel = viewModel(factory = OrderViewModel.factory) + var orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory) NavHost( navController = navController, startDestination = NavItem.Home.route diff --git a/app/src/main/java/com/example/androidlabs/adminPanel/AddPanel.kt b/app/src/main/java/com/example/androidlabs/adminPanel/AddPanel.kt index efab913..5aafdbb 100644 --- a/app/src/main/java/com/example/androidlabs/adminPanel/AddPanel.kt +++ b/app/src/main/java/com/example/androidlabs/adminPanel/AddPanel.kt @@ -43,12 +43,13 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import com.example.androidlabs.DB.models.PhotoManager +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.HotelViewModel import com.example.androidlabs.R @Composable -fun AddPanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){ +fun AddPanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)){ val photoManager = PhotoManager() Row( modifier = Modifier diff --git a/app/src/main/java/com/example/androidlabs/adminPanel/CardHotelForChange.kt b/app/src/main/java/com/example/androidlabs/adminPanel/CardHotelForChange.kt index 2c86e78..8597379 100644 --- a/app/src/main/java/com/example/androidlabs/adminPanel/CardHotelForChange.kt +++ b/app/src/main/java/com/example/androidlabs/adminPanel/CardHotelForChange.kt @@ -31,13 +31,14 @@ import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import androidx.navigation.compose.rememberNavController import com.example.androidlabs.DB.models.Hotel +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.HotelViewModel import com.example.androidlabs.R import com.google.gson.Gson @Composable -fun CardHotelForChange(item: Hotel, navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) { +fun CardHotelForChange(item: Hotel, navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)) { Row( modifier = Modifier .fillMaxWidth() diff --git a/app/src/main/java/com/example/androidlabs/adminPanel/ChangeHotel.kt b/app/src/main/java/com/example/androidlabs/adminPanel/ChangeHotel.kt index 596c30c..869654a 100644 --- a/app/src/main/java/com/example/androidlabs/adminPanel/ChangeHotel.kt +++ b/app/src/main/java/com/example/androidlabs/adminPanel/ChangeHotel.kt @@ -39,10 +39,11 @@ import com.example.androidlabs.DB.models.Hotel import com.example.androidlabs.DB.viewModels.HotelViewModel import androidx.lifecycle.viewmodel.compose.viewModel import com.example.androidlabs.DB.models.PhotoManager +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.R @Composable -fun ChangeHotel(hotel: Hotel, onBackClick: () -> Unit, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) { +fun ChangeHotel(hotel: Hotel, onBackClick: () -> Unit, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)) { val name = remember { mutableStateOf(hotel.name) } val price = remember{ mutableStateOf(hotel.price.toString()) } val stars = remember{ mutableStateOf(hotel.stars.toString()) } diff --git a/app/src/main/java/com/example/androidlabs/adminPanel/ChangePanel.kt b/app/src/main/java/com/example/androidlabs/adminPanel/ChangePanel.kt index 30961f9..02c4bdb 100644 --- a/app/src/main/java/com/example/androidlabs/adminPanel/ChangePanel.kt +++ b/app/src/main/java/com/example/androidlabs/adminPanel/ChangePanel.kt @@ -14,14 +14,14 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController +import androidx.paging.compose.collectAsLazyPagingItems +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.HotelViewModel -import com.example.androidlabs.Hotel -import com.example.androidlabs.R @Composable -fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){ - val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value +fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)){ + val list = hotelViewModel.HotelList.collectAsLazyPagingItems() Column( modifier = Modifier .fillMaxSize() @@ -33,11 +33,12 @@ fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewM modifier = Modifier .fillMaxSize() ) { - itemsIndexed(list - ){_, item-> - CardHotelForChange(item = item, navHostController) + items(list.itemCount) { index -> + list[index]?.let { hotel -> + CardHotelForChange(item = hotel, navHostController) } } } } +} } \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/homeScreen/HomeScreen.kt b/app/src/main/java/com/example/androidlabs/homeScreen/HomeScreen.kt index 69b7cc7..2e2da0e 100644 --- a/app/src/main/java/com/example/androidlabs/homeScreen/HomeScreen.kt +++ b/app/src/main/java/com/example/androidlabs/homeScreen/HomeScreen.kt @@ -1,6 +1,5 @@ package com.example.androidlabs.homeScreen -import android.util.Log import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -8,7 +7,8 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.grid.GridCells +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material.Text @@ -17,7 +17,6 @@ import androidx.compose.runtime.collectAsState 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.res.colorResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview @@ -26,17 +25,18 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import androidx.navigation.compose.rememberNavController -import com.example.androidlabs.DB.AppDatabase +import androidx.paging.compose.collectAsLazyPagingItems +import androidx.paging.compose.itemKey +import com.example.androidlabs.DB.models.Hotel +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.HotelViewModel import com.example.androidlabs.R import com.example.androidlabs.homeScreen.CardItem.HotelCard -import com.example.androidlabs.Hotel import com.example.androidlabs.homeScreen.SearchField.SearchField -import kotlinx.coroutines.flow.count @Composable -fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) { - val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value +fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)) { + val list = hotelViewModel.HotelList.collectAsLazyPagingItems() //Log.d("MyLog", list.toString()) Column( modifier = Modifier @@ -68,12 +68,22 @@ fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel } Column ( modifier = Modifier - .verticalScroll(rememberScrollState()) + //.verticalScroll(rememberScrollState()) .padding(bottom = 60.dp) ){ - for (item in list){ - HotelCard(item, navController) + LazyVerticalGrid( + columns = GridCells.Fixed(1) + ) { + items( + count = list.itemCount, + key = list.itemKey { hotel -> hotel.hotelId!! } + ) { index: Int -> + val hotel: Hotel? = list[index] + if (hotel != null) { + HotelCard(hotel, navController) + } + } } } diff --git a/app/src/main/java/com/example/androidlabs/profileScreen/signIn/SignInCard.kt b/app/src/main/java/com/example/androidlabs/profileScreen/signIn/SignInCard.kt index 100ae2d..54fa6d1 100644 --- a/app/src/main/java/com/example/androidlabs/profileScreen/signIn/SignInCard.kt +++ b/app/src/main/java/com/example/androidlabs/profileScreen/signIn/SignInCard.kt @@ -40,11 +40,12 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import androidx.navigation.compose.rememberNavController +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.UserViewModel import com.example.androidlabs.R @Composable -fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) { +fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory)) { Row( modifier = Modifier .fillMaxWidth() diff --git a/app/src/main/java/com/example/androidlabs/profileScreen/signUp/SignUpCard.kt b/app/src/main/java/com/example/androidlabs/profileScreen/signUp/SignUpCard.kt index 2f5a24e..b819244 100644 --- a/app/src/main/java/com/example/androidlabs/profileScreen/signUp/SignUpCard.kt +++ b/app/src/main/java/com/example/androidlabs/profileScreen/signUp/SignUpCard.kt @@ -37,12 +37,13 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import androidx.navigation.compose.rememberNavController +import com.example.androidlabs.DB.viewModels.AppViewModelProvider import com.example.androidlabs.DB.viewModels.UserViewModel import com.example.androidlabs.R import com.example.androidlabs.profileScreen.signIn.LoginScreen @Composable -fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) { +fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory)) { Row( modifier = Modifier .fillMaxWidth()