@@ -45,6 +45,7 @@ import androidx.navigation.compose.rememberNavController
4545import androidx.navigation.navArgument
4646import com.tien.piholeconnect.R
4747import com.tien.piholeconnect.model.BottomTabItem
48+ import com.tien.piholeconnect.model.LoadState
4849import com.tien.piholeconnect.model.Screen
4950import com.tien.piholeconnect.model.Theme
5051import com.tien.piholeconnect.model.TopBarOptionsMenuItem
@@ -90,24 +91,30 @@ fun App(viewModel: AppViewModel = hiltViewModel()) {
9091 )
9192 )
9293
93- selectedPiHole?.second?.let {
94- optionsMenuItems.add(
95- TopBarOptionsMenuItem (
96- URLBuilder (
97- protocol = it.configuration.protocol.toKtorURLProtocol(),
98- host = it.configuration.host,
99- port = it.configuration.port,
100- user = it.configuration.basicAuthUsername.ifBlank { null },
101- password = it.configuration.basicAuthPassword.ifBlank { null },
102- pathSegments = listOf (" admin" ),
94+ when (val selectedPiHole = selectedPiHole) {
95+ is LoadState .Success <* > -> {
96+ selectedPiHole.data?.second?.let {
97+ optionsMenuItems.add(
98+ TopBarOptionsMenuItem (
99+ URLBuilder (
100+ protocol = it.configuration.protocol.toKtorURLProtocol(),
101+ host = it.configuration.host,
102+ port = it.configuration.port,
103+ user = it.configuration.basicAuthUsername.ifBlank { null },
104+ password = it.configuration.basicAuthPassword.ifBlank { null },
105+ pathSegments = listOf (" admin" ),
106+ )
107+ .buildString(),
108+ R .string.options_menu_web_dashboard,
109+ Icons .AutoMirrored .TwoTone .OpenInNew ,
110+ isExternalLink = true ,
103111 )
104- .buildString(),
105- R .string.options_menu_web_dashboard,
106- Icons .AutoMirrored .TwoTone .OpenInNew ,
107- isExternalLink = true ,
108- )
109- )
112+ )
113+ }
114+ }
115+ else -> Unit
110116 }
117+
111118 optionsMenuItems.add(
112119 TopBarOptionsMenuItem (
113120 Screen .TipJar .route,
@@ -142,7 +149,7 @@ fun App(viewModel: AppViewModel = hiltViewModel()) {
142149 val defaultOptionsMenu =
143150 @Composable {
144151 OptionsMenu (
145- selectedPiHoleConnectionId = selectedPiHole?.first,
152+ selectedPiHoleConnectionId = selectedPiHole.data ?.first,
146153 piHoleConnections = piHoleConnections,
147154 optionsMenuItems = optionsMenuItems,
148155 onOptionsMenuItemClick = {
@@ -159,29 +166,37 @@ fun App(viewModel: AppViewModel = hiltViewModel()) {
159166
160167 @Composable
161168 fun ConnectionGuard (content : @Composable () -> Unit ) {
162- if (selectedPiHole == null ) {
163- Box (Modifier .fillMaxSize().padding(24 .dp), contentAlignment = Alignment .Center ) {
164- Button (
165- onClick = { navController.navigate(Screen .PiHoleConnection .route) },
166- Modifier .fillMaxWidth(),
167- ) {
168- Icon (
169- Icons .Default .AddCircleOutline ,
170- null ,
171- Modifier .size(ButtonDefaults .IconSize * 2 ),
172- )
173- Spacer (Modifier .size(ButtonDefaults .IconSpacing ))
174- Text (
175- stringResource(R .string.add_pi_hole_button),
176- style =
177- MaterialTheme .typography.labelLarge.copy(
178- fontSize = MaterialTheme .typography.labelLarge.fontSize * 2
179- ),
180- )
169+ when (val selectedPiHole = selectedPiHole) {
170+ is LoadState .Success -> {
171+ if (selectedPiHole.data != null ) {
172+ content()
173+ } else {
174+ Box (
175+ Modifier .fillMaxSize().padding(24 .dp),
176+ contentAlignment = Alignment .Center ,
177+ ) {
178+ Button (
179+ onClick = { navController.navigate(Screen .PiHoleConnection .route) },
180+ Modifier .fillMaxWidth(),
181+ ) {
182+ Icon (
183+ Icons .Default .AddCircleOutline ,
184+ null ,
185+ Modifier .size(ButtonDefaults .IconSize * 2 ),
186+ )
187+ Spacer (Modifier .size(ButtonDefaults .IconSpacing ))
188+ Text (
189+ stringResource(R .string.add_pi_hole_button),
190+ style =
191+ MaterialTheme .typography.labelLarge.copy(
192+ fontSize = MaterialTheme .typography.labelLarge.fontSize * 2
193+ ),
194+ )
195+ }
196+ }
181197 }
182198 }
183- } else {
184- content()
199+ else -> Unit
185200 }
186201 }
187202
0 commit comments