Skip to content

Commit

Permalink
WIP: Setup navigation to work beyond the bottom bar and nav drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrowlands committed Oct 24, 2024
1 parent 7752583 commit 329650f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.craxiom.mqttlibrary.MqttConstants;
Expand All @@ -34,6 +35,7 @@
import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings;
import com.craxiom.networksurvey.mqtt.MqttConnectionInfo;
import com.craxiom.networksurvey.services.NetworkSurveyService;
import com.craxiom.networksurvey.ui.main.SharedViewModel;
import com.craxiom.networksurvey.util.MdmUtils;

import timber.log.Timber;
Expand Down Expand Up @@ -66,9 +68,11 @@ public class MqttFragment extends AConnectionFragment<NetworkSurveyService.Surve
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted)
{
Navigation.findNavController(requireActivity(), getId())
SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
viewModel.triggerNavigationToQrCodeScanner();
/* TODO Delete me Navigation.findNavController(requireActivity(), getId())
.navigate(MqttFragmentDirections.actionMqttConnectionFragmentToScannerFragment()
.setMqttConnectionSettings(getCurrentMqttConnectionSettings()));
.setMqttConnectionSettings(getCurrentMqttConnectionSettings()));*/
} else
{
Toast.makeText(getContext(), getString(R.string.grant_camera_permission), Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -282,9 +286,11 @@ private void updateQrCodeScanButtonVisibility(boolean visible)
qrCodeScanButton.setOnClickListener(v -> {
if (hasCameraPermission())
{
Navigation.findNavController(requireActivity(), getId())
SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
viewModel.triggerNavigationToQrCodeScanner();
/* TODO Delete me Navigation.findNavController(requireActivity(), getId())
.navigate(MqttFragmentDirections.actionMqttConnectionFragmentToScannerFragment()
.setMqttConnectionSettings(getCurrentMqttConnectionSettings()));
.setMqttConnectionSettings(getCurrentMqttConnectionSettings()));*/
} else
{
cameraPermissionRequestLauncher.launch(Manifest.permission.CAMERA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
Expand All @@ -19,6 +22,7 @@ import com.craxiom.networksurvey.R
import com.craxiom.networksurvey.ui.main.appdrawer.AppDrawerContent
import com.craxiom.networksurvey.ui.main.appdrawer.AppDrawerItemInfo
import com.craxiom.networksurvey.ui.main.appdrawer.NavDrawerOption
import com.craxiom.networksurvey.ui.main.appdrawer.NavOption
import com.craxiom.networksurvey.ui.main.appdrawer.mainGraph
import com.craxiom.networksurvey.util.NsTheme

Expand All @@ -31,6 +35,17 @@ fun MainCompose(
) {
// TODO Is this needed? HandleBackPress(navController)
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current

val viewModel = viewModel<SharedViewModel>()
LaunchedEffect(viewModel.navigateToQrCodeScanner) {
viewModel.navigateToQrCodeScanner.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.QrCodeScanner.name)
viewModel.resetNavigationFlag()
}
}
}

NsTheme {
Scaffold { paddingValues ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.craxiom.networksurvey.ui.main

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class SharedViewModel @Inject constructor(application: Application) :
AndroidViewModel(application) {
private val _navigateToQrCodeScanner = MutableLiveData(false)
val navigateToQrCodeScanner: LiveData<Boolean> = _navigateToQrCodeScanner

fun triggerNavigationToQrCodeScanner() {
_navigateToQrCodeScanner.value = true
}

fun resetNavigationFlag() {
_navigateToQrCodeScanner.value = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.navigation.compose.navigation
import androidx.navigation.navArgument
import com.craxiom.networksurvey.databinding.ContainerGrpcFragmentBinding
import com.craxiom.networksurvey.databinding.ContainerMqttFragmentBinding
import com.craxiom.networksurvey.databinding.ContainerMqttQrCodeScannerFragmentBinding
import com.craxiom.networksurvey.databinding.ContainerSettingsFragmentBinding
import com.craxiom.networksurvey.fragments.MqttFragment
import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings
Expand Down Expand Up @@ -68,6 +69,11 @@ fun NavGraphBuilder.mainGraph(
composable(NavDrawerOption.Settings.name) {
SettingsFragmentInCompose(paddingValues)
}

// --------- Deeper navigation (beyond the nav drawer) ---------
composable(NavOption.QrCodeScanner.name) {
QrCodeScannerInCompose(paddingValues)
}
}
}

Expand All @@ -85,6 +91,10 @@ enum class NavDrawerOption {
GitHub
}

enum class NavOption {
QrCodeScanner
}

@Composable
fun GrpcFragmentInCompose(paddingValues: PaddingValues) {
AndroidViewBinding(
Expand Down Expand Up @@ -116,3 +126,12 @@ fun SettingsFragmentInCompose(paddingValues: PaddingValues) {
) {
}
}

@Composable
fun QrCodeScannerInCompose(paddingValues: PaddingValues) {
AndroidViewBinding(
ContainerMqttQrCodeScannerFragmentBinding::inflate,
modifier = Modifier.padding(paddingValues = paddingValues)
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mqtt_qr_code_scanner_fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.craxiom.networksurvey.fragments.CodeScannerFragment" />

0 comments on commit 329650f

Please sign in to comment.