Skip to content

Commit 124e045

Browse files
committed
add navigation to specific device from device controls
1 parent abd2b43 commit 124e045

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

app/src/main/java/ca/cgagnier/wlednativeandroid/ui/MainActivity.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ca.cgagnier.wlednativeandroid.ui
22

33
import android.net.Uri
4+
import android.os.Build
45
import android.os.Bundle
56
import android.util.Log
67
import android.webkit.ValueCallback
@@ -14,10 +15,14 @@ import ca.cgagnier.wlednativeandroid.FileUploadContract
1415
import ca.cgagnier.wlednativeandroid.FileUploadContractResult
1516
import ca.cgagnier.wlednativeandroid.repository.UserPreferencesRepository
1617
import ca.cgagnier.wlednativeandroid.repository.VersionWithAssetsRepository
18+
import ca.cgagnier.wlednativeandroid.service.DeviceControlsProviderService
1719
import ca.cgagnier.wlednativeandroid.service.update.ReleaseService
1820
import ca.cgagnier.wlednativeandroid.ui.theme.WLEDNativeTheme
1921
import dagger.hilt.android.AndroidEntryPoint
2022
import kotlinx.coroutines.Dispatchers
23+
import kotlinx.coroutines.flow.MutableStateFlow
24+
import kotlinx.coroutines.flow.StateFlow
25+
import kotlinx.coroutines.flow.asStateFlow
2126
import kotlinx.coroutines.launch
2227
import javax.inject.Inject
2328

@@ -31,6 +36,9 @@ class MainActivity : ComponentActivity() {
3136
@Inject
3237
lateinit var versionWithAssetsRepository: VersionWithAssetsRepository
3338

39+
private val _deviceNavigationMacAddress = MutableStateFlow<String?>(null)
40+
val deviceNavigationMacAddress: StateFlow<String?> = _deviceNavigationMacAddress.asStateFlow()
41+
3442
// For WebView file upload support
3543
var uploadMessage: ValueCallback<Array<Uri>>? = null
3644
val fileUpload =
@@ -48,9 +56,16 @@ class MainActivity : ComponentActivity() {
4856
installSplashScreen()
4957
enableEdgeToEdge()
5058
super.onCreate(savedInstanceState)
59+
60+
// Handle navigation from Device Controls on Android 11+
61+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
62+
val deviceMacAddress = intent.getStringExtra(DeviceControlsProviderService.EXTRA_DEVICE_MAC)
63+
_deviceNavigationMacAddress.value = deviceMacAddress
64+
}
65+
5166
setContent {
5267
WLEDNativeTheme {
53-
MainNavHost()
68+
MainNavHost(deviceMacAddress = deviceNavigationMacAddress)
5469
}
5570
}
5671
updateDeviceVersionList()

app/src/main/java/ca/cgagnier/wlednativeandroid/ui/MainNavHost.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package ca.cgagnier.wlednativeandroid.ui
22

33
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.getValue
5+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
46
import androidx.navigation.NavHostController
57
import androidx.navigation.compose.NavHost
68
import androidx.navigation.compose.composable
79
import androidx.navigation.compose.rememberNavController
810
import ca.cgagnier.wlednativeandroid.ui.homeScreen.DeviceListDetail
911
import ca.cgagnier.wlednativeandroid.ui.settingsScreen.Settings
12+
import kotlinx.coroutines.flow.StateFlow
1013
import kotlinx.serialization.Serializable
1114

1215
@Composable
1316
fun MainNavHost(
14-
navController: NavHostController = rememberNavController()
17+
navController: NavHostController = rememberNavController(),
18+
deviceMacAddress: StateFlow<String?>
1519
) {
20+
val currentDeviceMacAddress by deviceMacAddress.collectAsStateWithLifecycle()
1621
NavHost(
1722
navController = navController,
1823
startDestination = DeviceListDetailScreen,
@@ -21,7 +26,8 @@ fun MainNavHost(
2126
DeviceListDetail(
2227
openSettings = {
2328
navController.navigate(SettingsScreen)
24-
}
29+
},
30+
deviceMacAddress = currentDeviceMacAddress
2531
)
2632
}
2733
composable<SettingsScreen> {

app/src/main/java/ca/cgagnier/wlednativeandroid/ui/homeScreen/DeviceListDetail.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private const val TAG = "screen_DeviceListDetail"
6969
fun DeviceListDetail(
7070
modifier: Modifier = Modifier,
7171
openSettings: () -> Unit,
72+
deviceMacAddress: String? = null,
7273
viewModel: DeviceListDetailViewModel = hiltViewModel(),
7374
) {
7475
val lifecycleOwner = LocalLifecycleOwner.current
@@ -81,6 +82,22 @@ fun DeviceListDetail(
8182
rememberListDetailPaneScaffoldNavigator<Any>(scaffoldDirective = customScaffoldDirective)
8283
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
8384

85+
// Handle navigation to specific device if provided
86+
DisposableEffect(deviceMacAddress) {
87+
if (deviceMacAddress != null) {
88+
coroutineScope.launch {
89+
val device = viewModel.findDeviceByMacAddress(deviceMacAddress)
90+
if (device != null) {
91+
navigator.navigateTo(
92+
pane = ListDetailPaneScaffoldRole.Detail,
93+
contentKey = device.address
94+
)
95+
}
96+
}
97+
}
98+
onDispose { }
99+
}
100+
84101
val selectedDeviceAddress = navigator.currentDestination?.contentKey as? String ?: ""
85102
val selectedDevice =
86103
viewModel.getDeviceByAddress(selectedDeviceAddress).collectAsStateWithLifecycle(null)
@@ -390,4 +407,4 @@ private fun AddDeviceBottomSheet(
390407
},
391408
)
392409
}
393-
}
410+
}

app/src/main/java/ca/cgagnier/wlednativeandroid/ui/homeScreen/DeviceListDetailViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,7 @@ class DeviceListDetailViewModel @Inject constructor(
209209
false
210210
}
211211
}
212+
suspend fun findDeviceByMacAddress(macAddress: String): Device? {
213+
return repository.findDeviceByMacAddress(macAddress)
214+
}
212215
}

0 commit comments

Comments
 (0)