@@ -3,35 +3,47 @@ package com.flipcash.app.pools.internal.list
3
3
import androidx.compose.foundation.Image
4
4
import androidx.compose.foundation.background
5
5
import androidx.compose.foundation.layout.Box
6
+ import androidx.compose.foundation.layout.BoxScope
6
7
import androidx.compose.foundation.layout.Column
7
8
import androidx.compose.foundation.layout.Spacer
8
9
import androidx.compose.foundation.layout.WindowInsets
9
10
import androidx.compose.foundation.layout.fillMaxSize
10
11
import androidx.compose.foundation.layout.fillMaxWidth
11
12
import androidx.compose.foundation.layout.navigationBars
13
+ import androidx.compose.foundation.layout.navigationBarsPadding
12
14
import androidx.compose.foundation.layout.padding
13
15
import androidx.compose.foundation.layout.windowInsetsPadding
16
+ import androidx.compose.foundation.lazy.LazyColumn
14
17
import androidx.compose.material.Text
15
18
import androidx.compose.runtime.Composable
16
19
import androidx.compose.runtime.LaunchedEffect
17
- import androidx.lifecycle.compose.collectAsStateWithLifecycle
18
20
import androidx.compose.runtime.getValue
19
21
import androidx.compose.ui.Alignment
20
22
import androidx.compose.ui.Modifier
21
23
import androidx.compose.ui.res.painterResource
22
24
import androidx.compose.ui.res.stringResource
23
25
import androidx.compose.ui.text.style.TextAlign
24
26
import androidx.compose.ui.tooling.preview.Preview
27
+ import androidx.lifecycle.compose.collectAsStateWithLifecycle
28
+ import androidx.paging.LoadState
29
+ import androidx.paging.PagingData
30
+ import androidx.paging.compose.LazyPagingItems
31
+ import androidx.paging.compose.collectAsLazyPagingItems
25
32
import cafe.adriel.voyager.core.registry.ScreenRegistry
26
33
import com.flipcash.app.core.NavScreenProvider
34
+ import com.flipcash.app.core.pools.Pool
35
+ import com.flipcash.app.pools.internal.list.components.PoolSummaryRow
27
36
import com.flipcash.app.theme.FlipcashDesignSystem
28
37
import com.flipcash.features.pools.R
29
38
import com.getcode.navigation.core.LocalCodeNavigator
30
39
import com.getcode.theme.CodeTheme
31
40
import com.getcode.ui.theme.ButtonState
32
41
import com.getcode.ui.theme.CodeButton
42
+ import com.getcode.ui.theme.CodeScaffold
33
43
import kotlinx.coroutines.flow.filterIsInstance
44
+ import kotlinx.coroutines.flow.flowOf
34
45
import kotlinx.coroutines.flow.launchIn
46
+ import kotlinx.coroutines.flow.map
35
47
import kotlinx.coroutines.flow.onEach
36
48
37
49
@Composable
@@ -40,14 +52,20 @@ internal fun PoolListScreen(
40
52
) {
41
53
val navigator = LocalCodeNavigator .current
42
54
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
55
+ val pools = viewModel.pools.collectAsLazyPagingItems()
43
56
44
- PoolListScreenContent (state, viewModel::dispatchEvent)
57
+ PoolListScreenContent (state, pools, viewModel::dispatchEvent)
45
58
46
59
LaunchedEffect (viewModel) {
47
60
viewModel.eventFlow
48
61
.filterIsInstance<PoolListViewModel .Event .OnPoolClicked >()
62
+ .map { it.pool }
49
63
.onEach {
50
-
64
+ navigator.push(
65
+ ScreenRegistry .get(
66
+ NavScreenProvider .HomeScreen .Pools .ChoiceSelection (it.id)
67
+ )
68
+ )
51
69
}.launchIn(this )
52
70
}
53
71
@@ -63,11 +81,55 @@ internal fun PoolListScreen(
63
81
@Composable
64
82
private fun PoolListScreenContent (
65
83
state : PoolListViewModel .State ,
84
+ pools : LazyPagingItems <Pool >,
85
+ dispatch : (PoolListViewModel .Event ) -> Unit
86
+ ) {
87
+ if (pools.itemCount == 0 && pools.loadState.append is LoadState .NotLoading ) {
88
+ Box (
89
+ modifier = Modifier .fillMaxSize(),
90
+ contentAlignment = Alignment .Center
91
+ ) {
92
+ EmptyState (dispatch)
93
+ }
94
+ } else {
95
+ CodeScaffold (
96
+ bottomBar = {
97
+ CodeButton (
98
+ onClick = { dispatch(PoolListViewModel .Event .OnCreatePool ) },
99
+ text = stringResource(R .string.action_createNewPool),
100
+ buttonState = ButtonState .Filled ,
101
+ modifier = Modifier
102
+ .fillMaxWidth()
103
+ .navigationBarsPadding()
104
+ .padding(horizontal = CodeTheme .dimens.inset)
105
+ .padding(
106
+ top = CodeTheme .dimens.grid.x2,
107
+ bottom = CodeTheme .dimens.grid.x2
108
+ ),
109
+ )
110
+ }
111
+ ) { innerPadding ->
112
+ LazyColumn (modifier = Modifier .padding(innerPadding)) {
113
+ items(pools.itemCount) { index ->
114
+ pools[index]?.let {
115
+ PoolSummaryRow (
116
+ pool = it,
117
+ onClick = { dispatch(PoolListViewModel .Event .OnPoolClicked (it)) }
118
+ )
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ @Composable
127
+ private fun BoxScope.EmptyState (
66
128
dispatch : (PoolListViewModel .Event ) -> Unit
67
129
) {
68
130
Box (
69
131
modifier = Modifier
70
- .fillMaxSize ()
132
+ .matchParentSize ()
71
133
.windowInsetsPadding(WindowInsets .navigationBars),
72
134
) {
73
135
Column (
@@ -93,7 +155,7 @@ private fun PoolListScreenContent(
93
155
)
94
156
Spacer (Modifier .weight(1f ))
95
157
CodeButton (
96
- onClick = { dispatch(PoolListViewModel .Event .OnCreatePool ) },
158
+ onClick = { dispatch(PoolListViewModel .Event .OnCreatePool ) },
97
159
text = stringResource(R .string.action_createNewPool),
98
160
buttonState = ButtonState .Filled ,
99
161
modifier = Modifier
@@ -112,6 +174,7 @@ private fun Preview_EmptyState() {
112
174
Box (modifier = Modifier .background(CodeTheme .colors.background)) {
113
175
PoolListScreenContent (
114
176
state = PoolListViewModel .State (),
177
+ pools = flowOf(PagingData .empty<Pool >()).collectAsLazyPagingItems(),
115
178
dispatch = {}
116
179
)
117
180
}
0 commit comments