-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductListScreen.kt
More file actions
111 lines (105 loc) · 4.11 KB
/
ProductListScreen.kt
File metadata and controls
111 lines (105 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package uk.co.euantorano.example.compose_paging.ui.screens
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import uk.co.euantorano.example.compose_paging.data.models.Product
import uk.co.euantorano.example.compose_paging.ui.view_models.ProductListViewModel
import uk.co.euantorano.example.compose_paging.ui.view_models.ProductRow
import uk.co.euantorano.example.compose_paging.ui.widgets.SearchBarWidget
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLifecycleComposeApi::class,
ExperimentalLayoutApi::class, ExperimentalFoundationApi::class
)
@Composable
fun ProductListScreen(
vm: ProductListViewModel,
) {
val isSearchBarVisible by vm.isSearchShowing.collectAsStateWithLifecycle()
val search by vm.search.collectAsStateWithLifecycle()
val products: LazyPagingItems<Product> = vm.productsSearchResults.collectAsLazyPagingItems()
val refreshState = rememberSwipeRefreshState(
isRefreshing = products.loadState.refresh is LoadState.Loading,
)
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
Scaffold(
topBar = {
TopAppBar(
title = {
Text("Products")
},
actions = {
IconButton(
onClick = {
vm.toggleIsSearchShowing()
},
) {
Icon(Icons.Default.Search, contentDescription = "Search products")
}
},
scrollBehavior = scrollBehavior,
)
}
) { paddingValues ->
Box(
Modifier.padding(paddingValues)
.consumedWindowInsets(paddingValues)
) {
SwipeRefresh(
state = refreshState,
onRefresh = {
products.refresh()
},
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Top,
) {
if (isSearchBarVisible) {
stickyHeader {
SearchBarWidget(
query = search,
onQueryChanged = {
vm.setSearch(it)
},
modifier = Modifier.fillMaxWidth(),
label = "Search",
placeholder = "Search products",
)
}
}
items(
items = products,
key = {
it.id
},
) { product ->
product?.let {
ProductRow(
title = it.title,
brand = it.brand,
price = it.price,
thumbnail = it.thumbnail,
)
}
Divider()
}
}
}
}
}
}