Skip to content

Commit f961999

Browse files
committed
[#102] Update the method of showing global dialog with actions and resolve minor review comments
1 parent e38967c commit f961999

File tree

6 files changed

+77
-39
lines changed

6 files changed

+77
-39
lines changed

app/src/main/java/co/nimblehq/compose/crypto/ui/common/AppDialogPopUp.kt

+34-14
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import androidx.compose.ui.Alignment
77
import androidx.compose.ui.Modifier
88
import androidx.compose.ui.tooling.preview.Preview
99
import androidx.compose.ui.window.Dialog
10+
import androidx.compose.ui.window.DialogProperties
1011
import co.nimblehq.compose.crypto.ui.theme.*
1112

1213
@Composable
1314
fun AppDialogPopUp(
1415
onDismiss: () -> Unit,
15-
onClick: () -> Unit,
1616
title: String,
1717
message: String,
18-
actionText: String,
18+
actions: List<DialogActionModel>,
19+
onClickAction: () -> Unit
1920
) {
2021
Dialog(
21-
onDismissRequest = onDismiss
22+
onDismissRequest = onDismiss,
23+
properties = DialogProperties(
24+
dismissOnClickOutside = false
25+
)
2226
) {
2327
Surface {
2428
Column(
@@ -36,34 +40,50 @@ fun AppDialogPopUp(
3640
color = AppTheme.colors.text,
3741
modifier = Modifier.padding(Dp16)
3842
)
39-
TextButton(
40-
onClick = onClick,
43+
Row(
4144
modifier = Modifier
4245
.align(Alignment.End)
4346
.padding(bottom = Dp16, end = Dp8)
4447
) {
45-
Text(
46-
text = actionText,
47-
style = AppTheme.styles.semiBold16,
48-
color = AppTheme.colors.dialogText,
49-
)
48+
actions.forEach {
49+
TextButton(
50+
onClick = {
51+
it.onClickAction()
52+
onClickAction()
53+
},
54+
) {
55+
Text(
56+
text = it.actionText,
57+
style = AppTheme.styles.semiBold16,
58+
color = AppTheme.colors.dialogText,
59+
)
60+
}
61+
}
5062
}
5163
}
5264
}
5365
}
5466
}
5567

68+
data class DialogActionModel(
69+
val actionText: String,
70+
val onClickAction: () -> Unit
71+
)
72+
5673
@Composable
5774
@Preview(showSystemUi = true)
5875
fun AppDialogPopUpPreview() {
5976
ComposeTheme {
6077
Box {
6178
AppDialogPopUp(
62-
onDismiss = { /*TODO*/ },
63-
onClick = { /*TODO*/ },
79+
onDismiss = {},
6480
message = "No internet connection was found. Please check your internet connection and try again.",
65-
actionText = "OK",
66-
title = "Oops!"
81+
title = "Oops!",
82+
actions = listOf(DialogActionModel(
83+
actionText = "OK",
84+
onClickAction = {}
85+
)),
86+
onClickAction = {}
6787
)
6888
}
6989
}

app/src/main/java/co/nimblehq/compose/crypto/ui/navigation/AppNavigation.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,50 @@ import androidx.navigation.*
66
import androidx.navigation.compose.*
77
import co.nimblehq.compose.crypto.R
88
import co.nimblehq.compose.crypto.ui.common.AppDialogPopUp
9+
import co.nimblehq.compose.crypto.ui.common.DialogActionModel
910
import co.nimblehq.compose.crypto.ui.screens.detail.DetailScreen
1011
import co.nimblehq.compose.crypto.ui.screens.home.HomeScreen
1112

1213
@Composable
1314
fun AppNavigation(
1415
navController: NavHostController,
1516
startDestination: String = AppDestination.Home.destination,
16-
onCallBackChange: (() -> Unit) -> Unit,
17-
globalDialogCallback: () -> Unit
1817
) {
1918

19+
var dialogActions: List<DialogActionModel> = emptyList()
20+
2021
NavHost(
2122
navController = navController,
2223
startDestination = startDestination
2324
) {
2425
composable(AppDestination.Home) {
2526
HomeScreen(
26-
navigator = { destination -> navController.navigate(destination) }
27+
navigator = { destination -> navController.navigate(destination) },
28+
onShowGlobalDialog = { actions ->
29+
dialogActions = actions
30+
}
2731
)
2832
}
2933

3034
composable(AppDestination.CoinDetail) {
3135
DetailScreen(
3236
navigator = { destination -> navController.navigate(destination) },
3337
coinId = it.arguments?.getString(KEY_COIN_ID).orEmpty(),
34-
onNetworkReconnected = { callback ->
35-
onCallBackChange(callback)
38+
onShowGlobalDialog = { actions ->
39+
dialogActions = actions
3640
}
3741
)
3842
}
3943

4044
dialog(AppDestination.NoNetwork.route) {
4145
AppDialogPopUp(
4246
onDismiss = { navController.popBackStack() },
43-
onClick = {
47+
onClickAction = {
4448
navController.popBackStack()
45-
globalDialogCallback()
4649
},
4750
message = stringResource(id = R.string.no_internet_message),
48-
actionText = stringResource(id = android.R.string.ok),
49-
title = stringResource(id = R.string.no_internet_title)
51+
title = stringResource(id = R.string.no_internet_title),
52+
actions = dialogActions
5053
)
5154
}
5255
}

app/src/main/java/co/nimblehq/compose/crypto/ui/navigation/ComposeCryptoApp.kt

+3-12
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@ fun ComposeCryptoApp(
1313
navController: NavHostController = rememberNavController(),
1414
cryptoAppState: CryptoAppState
1515
) {
16-
var globalDialogCallback: () -> Unit = {}
1716
val context = LocalContext.current
1817

1918
cryptoAppState.isNetworkConnected.collectAsEffect { isNetworkConnected ->
2019
if (isNetworkConnected == false) {
2120
val destination = AppDestination.NoNetwork
2221

2322
val currentRoute = navController.currentBackStackEntry?.destination?.route
24-
if (currentRoute == AppDestination.NoNetwork.route) {
25-
navController.popBackStack()
23+
if (currentRoute != AppDestination.NoNetwork.route) {
24+
navController.navigate(destination)
2625
}
27-
28-
navController.navigate(destination)
2926
}
3027
}
3128

@@ -34,12 +31,6 @@ fun ComposeCryptoApp(
3431
}
3532

3633
AppNavigation(
37-
navController = navController,
38-
onCallBackChange = {
39-
globalDialogCallback = it
40-
},
41-
globalDialogCallback = {
42-
globalDialogCallback.invoke()
43-
},
34+
navController = navController
4435
)
4536
}

app/src/main/java/co/nimblehq/compose/crypto/ui/screens/detail/DetailScreen.kt

+16-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import co.nimblehq.compose.crypto.data.extension.orZero
2424
import co.nimblehq.compose.crypto.domain.model.CoinPrice
2525
import co.nimblehq.compose.crypto.extension.toFormattedString
2626
import co.nimblehq.compose.crypto.lib.IsLoading
27+
import co.nimblehq.compose.crypto.ui.common.DialogActionModel
2728
import co.nimblehq.compose.crypto.ui.common.price.PriceChangeButton
2829
import co.nimblehq.compose.crypto.ui.components.chartintervals.ChartIntervalsButtonGroup
2930
import co.nimblehq.compose.crypto.ui.components.chartintervals.TimeIntervals
@@ -54,7 +55,7 @@ fun DetailScreen(
5455
viewModel: DetailViewModel = hiltViewModel(),
5556
navigator: (destination: AppDestination) -> Unit,
5657
coinId: String,
57-
onNetworkReconnected: (() -> Unit) -> Unit
58+
onShowGlobalDialog: (dialogActions: List<DialogActionModel>) -> Unit
5859
) {
5960
val context = LocalContext.current
6061
LaunchedEffect(Unit) {
@@ -85,9 +86,20 @@ fun DetailScreen(
8586
viewModel.input.getCoinId(coinId = coinId)
8687
}
8788

88-
onNetworkReconnected {
89-
viewModel.input.getCoinId(coinId = coinId)
90-
}
89+
onShowGlobalDialog(
90+
listOf(
91+
DialogActionModel(
92+
actionText = stringResource(id = android.R.string.ok),
93+
onClickAction = {}
94+
),
95+
DialogActionModel(
96+
actionText = stringResource(id = R.string.detail_dialog_action_retry),
97+
onClickAction = {
98+
viewModel.input.getCoinId(coinId = coinId)
99+
}
100+
)
101+
)
102+
)
91103
}
92104

93105
@Suppress("LongMethod", "LongParameterList")

app/src/main/java/co/nimblehq/compose/crypto/ui/screens/home/HomeScreen.kt

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import co.nimblehq.compose.crypto.R
2323
import co.nimblehq.compose.crypto.extension.boxShadow
2424
import co.nimblehq.compose.crypto.lib.IsLoading
2525
import co.nimblehq.compose.crypto.ui.base.LoadingState
26+
import co.nimblehq.compose.crypto.ui.common.DialogActionModel
2627
import co.nimblehq.compose.crypto.ui.navigation.AppDestination
2728
import co.nimblehq.compose.crypto.ui.preview.HomeScreenParams
2829
import co.nimblehq.compose.crypto.ui.preview.HomeScreenPreviewParameterProvider
@@ -41,6 +42,7 @@ const val TestTagCoinsLoader = "CoinsLoader"
4142
fun HomeScreen(
4243
viewModel: HomeViewModel = hiltViewModel(),
4344
navigator: (destination: AppDestination) -> Unit,
45+
onShowGlobalDialog: (dialogActions: List<DialogActionModel>) -> Unit
4446
) {
4547

4648
val context = LocalContext.current
@@ -87,6 +89,15 @@ fun HomeScreen(
8789
onRefresh = { viewModel.input.loadData(isRefreshing = true) },
8890
onTrendingCoinsLoadMore = { viewModel.input.getTrendingCoins(loadMore = true) }
8991
)
92+
93+
onShowGlobalDialog(
94+
listOf(
95+
DialogActionModel(
96+
actionText = stringResource(id = android.R.string.ok),
97+
onClickAction = {}
98+
)
99+
)
100+
)
90101
}
91102

92103
@OptIn(ExperimentalMaterialApi::class)

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<string name="detail_market_cap_title">Market Cap</string>
1212
<string name="detail_all_time_high_title">All Time High</string>
1313
<string name="detail_all_time_low_title">All Time Low</string>
14+
<string name="detail_dialog_action_retry">Retry</string>
1415

1516
<string name="portfolio_card_total_coin_label">Total Coins</string>
1617
<string name="portfolio_card_today_profit_label">Today\'s Profit</string>

0 commit comments

Comments
 (0)