Хотел сделать норм пагинацию, не получилось, но файлы сохраню на всякимй

This commit is contained in:
Кашин Максим 2023-11-24 02:11:44 +04:00
parent e903f1729c
commit b46391b649
3 changed files with 25 additions and 0 deletions

View File

@ -26,4 +26,7 @@ interface BicycleDao {
@Query("SELECT * FROM bicycles WHERE userId = :userId") @Query("SELECT * FROM bicycles WHERE userId = :userId")
suspend fun getBicyclesByUserId(userId: Int): List<Bicycle> suspend fun getBicyclesByUserId(userId: Int): List<Bicycle>
@Query("SELECT * FROM bicycles LIMIT :pageSize OFFSET :offset")
suspend fun getBicyclesByPage(offset: Int, pageSize: Int): List<Bicycle>
} }

View File

@ -29,6 +29,10 @@ class BicycleRepository(private val bicycleDao: BicycleDao) {
suspend fun getBicycleById(bicycleId: Int?): Bicycle { suspend fun getBicycleById(bicycleId: Int?): Bicycle {
return bicycleDao.getBicycleById(bicycleId) return bicycleDao.getBicycleById(bicycleId)
} }
suspend fun getBicyclesByPage(pageNumber: Int, pageSize: Int): List<Bicycle> {
val offset = (pageNumber - 1) * pageSize
return bicycleDao.getBicyclesByPage(offset, pageSize)
}
} }
class BicycleViewModelFactory(private val bicycleRepository: BicycleRepository): ViewModelProvider.Factory { class BicycleViewModelFactory(private val bicycleRepository: BicycleRepository): ViewModelProvider.Factory {

View File

@ -14,6 +14,24 @@ class BicycleViewModel(private val bicycleRepository: BicycleRepository) : ViewM
private val _bicycles = MutableLiveData<List<Bicycle>>() private val _bicycles = MutableLiveData<List<Bicycle>>()
val bicycles: StateFlow<List<Bicycle>> = MutableStateFlow(emptyList()) val bicycles: StateFlow<List<Bicycle>> = MutableStateFlow(emptyList())
private val _currentPage = MutableStateFlow(1)
val currentPage: StateFlow<Int> = _currentPage
fun fetchBicyclesByPage(pageNumber: Int, pageSize: Int) {
viewModelScope.launch {
val fetchedBicycles = bicycleRepository.getBicyclesByPage(pageNumber, pageSize)
_bicycles.value = fetchedBicycles
}
}
fun setCurrentPage(page: Int) {
_currentPage.value = page
}
fun getCurrentPage(): Int {
return _currentPage.value
}
fun fetchBicycles() { fun fetchBicycles() {
viewModelScope.launch { viewModelScope.launch {
val fetchedBicycles = bicycleRepository.getAllBicycles() val fetchedBicycles = bicycleRepository.getAllBicycles()