di and pagination

This commit is contained in:
VictoriaPresnyakova 2023-11-12 18:50:40 +04:00
parent 4b7e5338ef
commit 2aa2e5bc60
28 changed files with 240 additions and 98 deletions

View File

@ -83,4 +83,8 @@ dependencies {
kapt("androidx.room:room-compiler:$room_version") kapt("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version") implementation("androidx.room:room-ktx:$room_version")
implementation("androidx.room:room-paging:$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")
} }

View File

@ -7,13 +7,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class App : Application() { class App : Application() {
val database by lazy { AppDatabase.getInstance(this)} lateinit var container: AppContainer
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
//this.deleteDatabase("my-db") //this.deleteDatabase("my-db")
CoroutineScope(Dispatchers.IO).launch { //CoroutineScope(Dispatchers.IO).launch {
AppDatabase.populateDatabase() // AppDatabase.populateDatabase()
} //}
container = AppDataContainer(this)
} }
} }

View File

@ -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
}

View File

@ -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())
}
}

View File

@ -33,9 +33,9 @@ abstract class AppDatabase : RoomDatabase() {
INSTANCE?.let { database -> INSTANCE?.let { database ->
// User // User
val userDao = database.userDao() 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 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(user1)
userDao.createUser(user2) userDao.createUser(user2)
userDao.createUser(user3) userDao.createUser(user3)

View File

@ -1,4 +1,5 @@
package com.example.androidlabs.DB.dao package com.example.androidlabs.DB.dao
import androidx.paging.PagingSource
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Delete import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
@ -19,7 +20,7 @@ interface HotelDao {
suspend fun delete(hotel: Hotel) suspend fun delete(hotel: Hotel)
@Query("SELECT*FROM Hotel") @Query("SELECT*FROM Hotel")
fun getAllHotelss(): Flow<List<Hotel>> fun getAllHotelsPaged(): PagingSource<Int, Hotel>
@Query("SELECT * FROM Hotel WHERE hotelId = :id") @Query("SELECT * FROM Hotel WHERE hotelId = :id")
suspend fun getHotelById(id: Int): Hotel suspend fun getHotelById(id: Int): Hotel

View File

@ -5,6 +5,7 @@ import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import com.example.androidlabs.DB.models.Order import com.example.androidlabs.DB.models.Order
import com.example.androidlabs.DB.models.UserWithOrder
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@Dao @Dao
@ -21,4 +22,7 @@ interface OrderDao {
@Delete @Delete
suspend fun delete(order: Order) suspend fun delete(order: Order)
@Query("SELECT * FROM users WHERE userId =:id")
fun getUserOrders(id: Int) : Flow<UserWithOrder>
} }

View File

@ -26,6 +26,4 @@ interface UserDao {
@Query("SELECT * FROM users WHERE email = :email") @Query("SELECT * FROM users WHERE email = :email")
suspend fun getUserByEmail(email: String): User suspend fun getUserByEmail(email: String): User
@Query("SELECT * FROM users WHERE userId =:id")
fun getUserOrders(id: Int) : Flow<UserWithOrder>
} }

View File

@ -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<Int, Hotel> = hotelDao.getAllHotelsPaged()
override fun call(): Flow<PagingData<Hotel>> {
return Pager(
PagingConfig(pageSize = 5)
) {
hotelDao.getAllHotelsPaged()
}.flow
}
}

View File

@ -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<Int, Hotel>
fun call(): Flow<PagingData<Hotel>>
}

View File

@ -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<List<Order>> = orderDao.getAllOrder()
override fun getUserOrders(id: Int): Flow<UserWithOrder> = orderDao.getUserOrders(id)
}

View File

@ -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<List<Order>>
fun getUserOrders(id: Int) : Flow<UserWithOrder>
}

View File

@ -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)
}

View File

@ -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
}

View File

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

View File

@ -6,21 +6,23 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras import androidx.lifecycle.viewmodel.CreationExtras
import androidx.paging.cachedIn
import com.example.androidlabs.App import com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.Hotel import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.repository.HotelRepository
import com.example.androidlabs.R import com.example.androidlabs.R
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class HotelViewModel(val database: AppDatabase): ViewModel() { class HotelViewModel(private val hotelRepository: HotelRepository): ViewModel() {
var name = mutableStateOf("") var name = mutableStateOf("")
val price = mutableStateOf("") val price = mutableStateOf("")
val location = mutableStateOf("") val location = mutableStateOf("")
val stars = mutableStateOf("") val stars = mutableStateOf("")
val info = mutableStateOf("") val info = mutableStateOf("")
val img = mutableStateOf(R.drawable.img) val img = mutableStateOf(R.drawable.img)
val HotelList = database.hotelDao().getAllHotelss() val HotelList = hotelRepository.call().cachedIn(viewModelScope)
var hotel: Hotel? = null var hotel: Hotel? = null
fun insertHotel() = viewModelScope.launch { fun insertHotel() = viewModelScope.launch {
@ -32,29 +34,18 @@ class HotelViewModel(val database: AppDatabase): ViewModel() {
stars = stars.value.toInt(), stars = stars.value.toInt(),
info = info.value info = info.value
) )
database.hotelDao().insert(hotel) hotelRepository.insertHotel(hotel)
} }
fun deleteHotel(hotel : Hotel) = viewModelScope.launch { fun deleteHotel(hotel : Hotel) = viewModelScope.launch {
database.hotelDao().delete(hotel) hotelRepository.deleteHotel(hotel)
} }
fun getHotelById(id: Int) = viewModelScope.launch { fun getHotelById(id: Int) = viewModelScope.launch {
database.hotelDao().getHotelById(id) hotelRepository.getHotelById(id)
} }
fun UpdateHotel(hotel: Hotel) = viewModelScope.launch { fun UpdateHotel(hotel: Hotel) = viewModelScope.launch {
database.hotelDao().update(hotel) hotelRepository.updateHotel(hotel)
}
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras): T {
val database = (checkNotNull(extras[APPLICATION_KEY]) as App).database
return HotelViewModel(database) as T
}
}
} }
} }

View File

@ -10,22 +10,25 @@ import com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.Hotel import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.models.Order 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 com.example.androidlabs.GlobalUser
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.Date import java.util.Date
class OrderViewModel(val database: AppDatabase) : ViewModel() { class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel() {
var selectedItem: Hotel? = null var selectedItem: Hotel? = null
val rooms = mutableStateOf("") val rooms = mutableStateOf("")
var dateFrom = mutableStateOf("") var dateFrom = mutableStateOf("")
var dateTo = mutableStateOf("") var dateTo = mutableStateOf("")
fun deleteOrder(order: Order) = viewModelScope.launch { fun deleteOrder(order: Order) = viewModelScope.launch {
database.orderDao().delete(order) orderRepository.delete(order)
} }
fun getOrderList(id: Int) = viewModelScope.launch { fun getOrderList(id: Int) : Flow<UserWithOrder> {
database.userDao().getUserOrders(id) return orderRepository.getUserOrders(id)
} }
@ -42,7 +45,7 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() {
hotel = selectedItem!! hotel = selectedItem!!
) )
val orderId = database.orderDao().createOrder(order) val orderId = orderRepository.createOrder(order)
rooms.value = "" rooms.value = ""
@ -52,16 +55,4 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() {
fun getSubTotal(): Double { fun getSubTotal(): Double {
return selectedItem!!.price * rooms.value.toInt() return selectedItem!!.price * rooms.value.toInt()
} }
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database
return OrderViewModel(database) as T
}
}
}
} }

View File

@ -10,10 +10,11 @@ import com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.RoleEnum import com.example.androidlabs.DB.models.RoleEnum
import com.example.androidlabs.DB.models.User import com.example.androidlabs.DB.models.User
import com.example.androidlabs.DB.repository.UserRepository
import com.example.androidlabs.GlobalUser import com.example.androidlabs.GlobalUser
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class UserViewModel(val database: AppDatabase): ViewModel() { class UserViewModel(private val userRepository: UserRepository): ViewModel() {
var name = mutableStateOf("") var name = mutableStateOf("")
val surname = mutableStateOf("") val surname = mutableStateOf("")
@ -27,10 +28,10 @@ class UserViewModel(val database: AppDatabase): ViewModel() {
password = password.value, password = password.value,
role = RoleEnum.User role = RoleEnum.User
) )
database.userDao().createUser(user) userRepository.createUser(user)
} }
fun authUser() = viewModelScope.launch { fun authUser() = viewModelScope.launch {
val user = database.userDao().getUserByEmail(email.value) val user = userRepository.getUserByEmail(email.value)
if (password.value != "" && user.password == password.value) { if (password.value != "" && user.password == password.value) {
val globalUser = GlobalUser.getInstance() val globalUser = GlobalUser.getInstance()
globalUser.setUser(user) globalUser.setUser(user)
@ -43,15 +44,4 @@ class UserViewModel(val database: AppDatabase): ViewModel() {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
} }
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database
return UserViewModel(database) as T
}
}
}
} }

View File

@ -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,
)

View File

@ -22,9 +22,9 @@ import androidx.compose.runtime.getValue
@Composable @Composable
fun MyOrderScreen(orderViewModel: OrderViewModel) { fun MyOrderScreen(orderViewModel: OrderViewModel) {
val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null) // val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null)
val userId = GlobalUser.getInstance().getUser()?.userId
val orderList: List<Order>? = userWithOrder?.orders val userWithOrder = orderViewModel.getOrderList(userId!!).collectAsState(null).value?.orders
println() println()
Column( Column(
modifier = Modifier modifier = Modifier
@ -46,8 +46,8 @@ fun MyOrderScreen(orderViewModel: OrderViewModel) {
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
) { ) {
if (orderList != null) { if (userWithOrder != null) {
for (item in orderList) { for (item in userWithOrder) {
OrderCard(item, orderViewModel) OrderCard(item, orderViewModel)
} }
} }

View File

@ -6,6 +6,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import com.example.androidlabs.DB.models.Hotel 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.DB.viewModels.OrderViewModel
import com.example.androidlabs.MyOrderScreen.MyOrderScreen import com.example.androidlabs.MyOrderScreen.MyOrderScreen
import com.example.androidlabs.booking.BookingScreen import com.example.androidlabs.booking.BookingScreen
@ -23,7 +24,7 @@ import com.google.gson.Gson
@Composable @Composable
fun NavController(navController: NavHostController) { fun NavController(navController: NavHostController) {
var orderViewModel: OrderViewModel = viewModel(factory = OrderViewModel.factory) var orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = NavItem.Home.route startDestination = NavItem.Home.route

View File

@ -43,12 +43,13 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.androidlabs.DB.models.PhotoManager 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.DB.viewModels.HotelViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
@Composable @Composable
fun AddPanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){ fun AddPanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)){
val photoManager = PhotoManager() val photoManager = PhotoManager()
Row( Row(
modifier = Modifier modifier = Modifier

View File

@ -31,13 +31,14 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.models.Hotel 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.DB.viewModels.HotelViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.google.gson.Gson import com.google.gson.Gson
@Composable @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( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -39,10 +39,11 @@ import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.viewModels.HotelViewModel import com.example.androidlabs.DB.viewModels.HotelViewModel
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.androidlabs.DB.models.PhotoManager import com.example.androidlabs.DB.models.PhotoManager
import com.example.androidlabs.DB.viewModels.AppViewModelProvider
import com.example.androidlabs.R import com.example.androidlabs.R
@Composable @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 name = remember { mutableStateOf(hotel.name) }
val price = remember{ mutableStateOf(hotel.price.toString()) } val price = remember{ mutableStateOf(hotel.price.toString()) }
val stars = remember{ mutableStateOf(hotel.stars.toString()) } val stars = remember{ mutableStateOf(hotel.stars.toString()) }

View File

@ -14,14 +14,14 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController 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.DB.viewModels.HotelViewModel
import com.example.androidlabs.Hotel
import com.example.androidlabs.R
@Composable @Composable
fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){ fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)){
val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value val list = hotelViewModel.HotelList.collectAsLazyPagingItems()
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -33,9 +33,10 @@ fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewM
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
) { ) {
itemsIndexed(list items(list.itemCount) { index ->
){_, item-> list[index]?.let { hotel ->
CardHotelForChange(item = item, navHostController) CardHotelForChange(item = hotel, navHostController)
}
} }
} }
} }

View File

@ -1,6 +1,5 @@
package com.example.androidlabs.homeScreen package com.example.androidlabs.homeScreen
import android.util.Log
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box 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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding 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.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text import androidx.compose.material.Text
@ -17,7 +17,6 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview 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.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController 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.DB.viewModels.HotelViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.homeScreen.CardItem.HotelCard import com.example.androidlabs.homeScreen.CardItem.HotelCard
import com.example.androidlabs.Hotel
import com.example.androidlabs.homeScreen.SearchField.SearchField import com.example.androidlabs.homeScreen.SearchField.SearchField
import kotlinx.coroutines.flow.count
@Composable @Composable
fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) { fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value val list = hotelViewModel.HotelList.collectAsLazyPagingItems()
//Log.d("MyLog", list.toString()) //Log.d("MyLog", list.toString())
Column( Column(
modifier = Modifier modifier = Modifier
@ -68,12 +68,22 @@ fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel
} }
Column ( Column (
modifier = Modifier modifier = Modifier
.verticalScroll(rememberScrollState()) //.verticalScroll(rememberScrollState())
.padding(bottom = 60.dp) .padding(bottom = 60.dp)
){ ){
for (item in list){ LazyVerticalGrid(
HotelCard(item, navController) 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)
}
}
} }
} }

View File

@ -40,11 +40,12 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.viewModels.AppViewModelProvider
import com.example.androidlabs.DB.viewModels.UserViewModel import com.example.androidlabs.DB.viewModels.UserViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
@Composable @Composable
fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) { fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -37,12 +37,13 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.viewModels.AppViewModelProvider
import com.example.androidlabs.DB.viewModels.UserViewModel import com.example.androidlabs.DB.viewModels.UserViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.profileScreen.signIn.LoginScreen import com.example.androidlabs.profileScreen.signIn.LoginScreen
@Composable @Composable
fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) { fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()