-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeScreen.kt
162 lines (153 loc) · 5.85 KB
/
HomeScreen.kt
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.kwdev.catty.ui.home
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.kwdev.catty.ui.common.CollectFlowWithLifecycleEffect
import com.kwdev.catty.ui.common.component.ProgressDialog
import com.kwdev.catty.ui.common.koinViewModel
import com.kwdev.catty.ui.home.HomeViewModel.ViewAction
import com.kwdev.catty.ui.home.HomeViewModel.ViewAction.NavigateFavorites
import com.kwdev.catty.ui.home.HomeViewModel.ViewAction.ShowSnackbar
import com.kwdev.catty.ui.home.HomeViewModel.ViewEvent
import com.kwdev.catty.ui.home.HomeViewModel.ViewEvent.OnFavoritesClick
import com.kwdev.catty.ui.home.HomeViewModel.ViewEvent.OnGetRandomGifClick
import com.kwdev.catty.ui.home.HomeViewModel.ViewEvent.OnGetRandomImageClick
import com.kwdev.catty.ui.home.HomeViewModel.ViewEvent.OnMarkImageAsFavoriteClick
import com.kwdev.catty.ui.home.HomeViewModel.ViewState
import com.kwdev.catty.ui.navigation.NavRoute
import io.kamel.image.KamelImage
import io.kamel.image.asyncPainterResource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
@Composable
internal fun HomeScreen(navController: NavController) {
val viewModel = koinViewModel<HomeViewModel>()
val viewState by viewModel.viewState.collectAsStateWithLifecycle()
val snackbarState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()
CollectFlowWithLifecycleEffect(viewModel.viewAction) { action ->
onViewAction(action, navController, snackbarState, coroutineScope)
}
ScreenContent(
viewState = viewState,
snackbarState = snackbarState,
onViewEvent = viewModel::onViewEvent,
)
}
private fun onViewAction(
action: ViewAction,
navController: NavController,
snackbarState: SnackbarHostState,
coroutineScope: CoroutineScope,
) {
when (action) {
is ShowSnackbar -> coroutineScope.launch {
snackbarState.showSnackbar(message = action.message)
}
NavigateFavorites -> navController.navigate(NavRoute.Favorites)
}
}
@Composable
private fun ScreenContent(
viewState: ViewState,
snackbarState: SnackbarHostState,
onViewEvent: (ViewEvent) -> Unit,
) {
if (viewState.isLoading) {
ProgressDialog()
}
Scaffold(
snackbarHost = { SnackbarHost(snackbarState) },
) { innerPadding ->
ScreenScaffoldContent(
modifier = Modifier.padding(innerPadding),
viewState = viewState,
onViewEvent = onViewEvent,
)
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun ScreenScaffoldContent(
modifier: Modifier,
viewState: ViewState,
onViewEvent: (ViewEvent) -> Unit,
) {
Column(
modifier = modifier
.fillMaxSize()
.padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
FlowRow(
horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterHorizontally),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
FilledTonalButton(
onClick = { onViewEvent(OnGetRandomImageClick) },
content = { Text(text = "Get new random image") },
)
FilledTonalButton(
onClick = { onViewEvent(OnGetRandomGifClick) },
content = { Text(text = "Get new random gif") },
)
FilledTonalButton(
onClick = { onViewEvent(OnFavoritesClick) },
content = { Text(text = "Check all favorites") },
)
if (viewState.imageUrl != null) {
FilledTonalButton(
onClick = { onViewEvent(OnMarkImageAsFavoriteClick) },
content = { Text(text = "Mark as favorite") },
)
}
}
if (viewState.imageUrl != null) {
KamelImage(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
.padding(20.dp),
resource = { asyncPainterResource(data = viewState.imageUrl) },
contentDescription = null,
onLoading = {
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
},
onFailure = { cause ->
Text(
modifier = Modifier.align(Alignment.Center),
text = cause.message ?: "Could not download image",
)
},
)
}
Text(
modifier = Modifier.fillMaxWidth(),
text = viewState.imageUrl ?: "not loaded yet",
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Center,
)
}
}