diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml
index 7f8a351..0089646 100644
--- a/.idea/deploymentTargetDropDown.xml
+++ b/.idea/deploymentTargetDropDown.xml
@@ -7,11 +7,11 @@
-
+
-
+
\ No newline at end of file
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 2e979cc..f127ed5 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/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt
index 88db2af..3343729 100644
--- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt
+++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt
@@ -1,41 +1,62 @@
package com.example.android_programming.composeui.Screens.AdminPanel
import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.layout.wrapContentHeight
+import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
+import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.LaunchedEffect
+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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
+import androidx.paging.LoadState
+import androidx.paging.compose.collectAsLazyPagingItems
+import androidx.paging.compose.itemKey
+import com.example.android_programming.model.Sneaker
import com.example.android_programming.vmodel.AppViewModelProvider
import com.example.android_programming.vmodel.SneakerViewModel
+import kotlinx.coroutines.delay
@Composable
fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
- val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
+ val list = sneakerViewModel.sneakerList.collectAsLazyPagingItems()
+
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
- .padding(16.dp)
- ){
+ .padding(16.dp, 80.dp)
+ ) {
Row {
LazyColumn(
modifier = Modifier
.fillMaxSize()
) {
- itemsIndexed(list
- ){_, item->
- CardSneakerForChange(item = item, navHostController)
+ items(list.itemCount) { index ->
+ list[index]?.let { sneaker ->
+ CardSneakerForChange(item = sneaker, navController = navHostController)
+ }
}
}
}
}
-}
\ No newline at end of file
+}
+
+
diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt
index e224d89..80197cc 100644
--- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt
+++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt
@@ -28,13 +28,11 @@ import com.example.android_programming.vmodel.OrderViewModel
@Composable
fun HomeScreen(navHostController: NavHostController, orderViewModel: OrderViewModel) {
- val context = LocalContext.current
- val sneakers = remember { mutableStateListOf() }
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
- .verticalScroll(rememberScrollState())
+ /*.verticalScroll(rememberScrollState())*/
) {
Row {
// Поле для поиска
diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt
index 236182d..00f5a22 100644
--- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt
+++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt
@@ -1,39 +1,41 @@
package com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView
-import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
-import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
-import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.lazy.grid.GridCells
+import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
+import androidx.paging.compose.collectAsLazyPagingItems
+import androidx.paging.compose.itemKey
+import com.example.android_programming.model.Sneaker
import com.example.android_programming.vmodel.AppViewModelProvider
import com.example.android_programming.vmodel.OrderViewModel
import com.example.android_programming.vmodel.SneakerViewModel
@Composable
-fun RecyclerView(navHostController : NavHostController, orderViewModel: OrderViewModel, sneakerViewModel: SneakerViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
+fun RecyclerView(navHostController: NavHostController, orderViewModel: OrderViewModel, sneakerViewModel: SneakerViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 60.dp)
) {
- val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
- val numColumns = 2
- val chunkedList = list.chunked(numColumns)
+ val sneakerLazyPagingItems = sneakerViewModel.sneakerList.collectAsLazyPagingItems()
- for (chunkedListItem in chunkedList) {
- Row(
- horizontalArrangement = Arrangement.Center,
- modifier = Modifier.fillMaxWidth()
- ) {
- for (item in chunkedListItem) {
- CardSneaker(item, navHostController, orderViewModel.selectedItems) { selectedItem ->
+ LazyVerticalGrid(
+ columns = GridCells.Fixed(2)
+ ) {
+ items(
+ count = sneakerLazyPagingItems.itemCount,
+ key = sneakerLazyPagingItems.itemKey { sneaker -> sneaker.sneakerId!! }
+ ) { index: Int ->
+ val sneaker: Sneaker? = sneakerLazyPagingItems[index]
+ if (sneaker != null) {
+ CardSneaker(sneaker, navHostController, orderViewModel.selectedItems) { selectedItem ->
orderViewModel.addSelectedItem(selectedItem)
}
}
@@ -42,3 +44,5 @@ fun RecyclerView(navHostController : NavHostController, orderViewModel: OrderVie
}
}
+
+
diff --git a/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt b/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt
index f505a22..44379c0 100644
--- a/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt
+++ b/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt
@@ -1,5 +1,6 @@
package com.example.android_programming.dao
+import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
@@ -20,7 +21,7 @@ interface SneakerDao {
suspend fun delete(sneaker: Sneaker)
@Query("SELECT*FROM Sneaker")
- fun getAllSneakers(): Flow>
+ fun getAllSneakersPaged(): PagingSource
@Query("SELECT * FROM Sneaker WHERE sneakerId = :id")
suspend fun getSneakerById(id: Int): Sneaker
diff --git a/app/src/main/java/com/example/android_programming/repository/SneakerRepoImpl.kt b/app/src/main/java/com/example/android_programming/repository/SneakerRepoImpl.kt
index 11e97d1..03a3efa 100644
--- a/app/src/main/java/com/example/android_programming/repository/SneakerRepoImpl.kt
+++ b/app/src/main/java/com/example/android_programming/repository/SneakerRepoImpl.kt
@@ -1,5 +1,10 @@
package com.example.android_programming.repository
+import androidx.paging.Pager
+import androidx.paging.PagingConfig
+import androidx.paging.PagingData
+import androidx.paging.PagingSource
+import androidx.paging.cachedIn
import com.example.android_programming.dao.SneakerDao
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.flow.Flow
@@ -13,6 +18,12 @@ class SneakerRepoImpl(private val sneakerDao: SneakerDao) : SneakerRepository {
override suspend fun deleteSneaker(sneaker: Sneaker) = sneakerDao.delete(sneaker)
override suspend fun getSneakerById(id: Int): Sneaker = sneakerDao.getSneakerById(id)
-
- override fun getAllSneakers(): Flow> = sneakerDao.getAllSneakers()
+ override fun getAllSneakersPaged(): PagingSource = sneakerDao.getAllSneakersPaged()
+ override fun call(): Flow> {
+ return Pager(
+ PagingConfig(pageSize = 5)
+ ) {
+ sneakerDao.getAllSneakersPaged()
+ }.flow
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/android_programming/repository/SneakerRepository.kt b/app/src/main/java/com/example/android_programming/repository/SneakerRepository.kt
index 5d099ee..28c9145 100644
--- a/app/src/main/java/com/example/android_programming/repository/SneakerRepository.kt
+++ b/app/src/main/java/com/example/android_programming/repository/SneakerRepository.kt
@@ -1,5 +1,7 @@
package com.example.android_programming.repository
+import androidx.paging.PagingData
+import androidx.paging.PagingSource
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.flow.Flow
@@ -8,5 +10,6 @@ interface SneakerRepository {
suspend fun updateSneaker(sneaker: Sneaker)
suspend fun deleteSneaker(sneaker: Sneaker)
suspend fun getSneakerById(id: Int): Sneaker
- fun getAllSneakers(): Flow>
+ fun getAllSneakersPaged(): PagingSource
+ fun call(): Flow>
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt
index 87d5f94..5173f88 100644
--- a/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt
+++ b/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt
@@ -1,16 +1,24 @@
package com.example.android_programming.vmodel
+import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
+import androidx.paging.Pager
+import androidx.paging.PagingConfig
+import androidx.paging.PagingData
+import androidx.paging.cachedIn
+import androidx.paging.map
import com.example.android_programming.App
import com.example.android_programming.R
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.model.Sneaker
import com.example.android_programming.repository.SneakerRepository
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
class SneakerViewModel(private val sneakerRepository: SneakerRepository): ViewModel() {
@@ -18,8 +26,8 @@ class SneakerViewModel(private val sneakerRepository: SneakerRepository): ViewMo
val model = mutableStateOf("")
val description = mutableStateOf("")
val price = mutableStateOf("")
- val photo = mutableStateOf(R.drawable.img)
- val SneakerList = sneakerRepository.getAllSneakers()
+ val photo = mutableIntStateOf(R.drawable.img)
+ val sneakerList = sneakerRepository.call().cachedIn(viewModelScope)
var sneaker: Sneaker? = null
fun insertSneaker() = viewModelScope.launch {