From c93634f6327d4490fb5600a97d3daa6a5152302b Mon Sep 17 00:00:00 2001 From: christianrowlands Date: Mon, 28 Oct 2024 12:46:24 -0400 Subject: [PATCH] WIP: Pass the args to and from the mqtt QR code share and scan fragments --- .../networksurvey/NetworkSurveyActivity.kt | 17 ------------ .../fragments/CodeScannerFragment.java | 20 +++++++------- .../fragments/MainCellularFragment.java | 1 + .../networksurvey/fragments/MqttFragment.java | 18 +++++-------- .../fragments/NetworkDetailsFragment.java | 2 +- .../fragments/QrCodeShareFragment.java | 7 ++--- .../fragments/TowerMapFragment.kt | 2 +- .../ui/gnss/GnssFilterDialog.java | 2 +- .../networksurvey/ui/main/AppNavigation.kt | 26 +++++-------------- .../networksurvey/ui/main/NsMainScreen.kt | 8 ++++++ .../networksurvey/ui/main/SharedViewModel.kt | 19 ++++++++++++-- 11 files changed, 56 insertions(+), 66 deletions(-) diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/NetworkSurveyActivity.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/NetworkSurveyActivity.kt index b216e4c7..ca3d58c9 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/NetworkSurveyActivity.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/NetworkSurveyActivity.kt @@ -119,9 +119,6 @@ class NetworkSurveyActivity : AppCompatActivity() { // TODO Delete me setupNavigation() - // Set the version number at the bottom of the navigation drawer - // TODO Delete me setAppVersionNumber() - surveyServiceConnection = SurveyServiceConnection() Application.createNotificationChannel(this) @@ -602,20 +599,6 @@ class NetworkSurveyActivity : AppCompatActivity() { onBackPressedDispatcher.addCallback(this, callback) } - /** - * Get the app version name and set it at the bottom of the navigation drawer. - */ - // TODO Delete me - private fun setAppVersionNumber() { - try { - val appVersionName = NsUtils.getAppVersionName(this) - val appVersionView = findViewById(R.id.app_version_name) - appVersionView.text = getString(R.string.app_version, appVersionName) - } catch (e: Exception) { - Timber.wtf(e, "Could not set the app version number") - } - } - /** * Starts or stops writing the Cellular log file based on the specified parameter. * diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/CodeScannerFragment.java b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/CodeScannerFragment.java index 1e680eac..56bb1bc6 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/CodeScannerFragment.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/CodeScannerFragment.java @@ -11,14 +11,17 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.navigation.Navigation; +import androidx.lifecycle.ViewModelProvider; import com.budiyev.android.codescanner.CodeScanner; import com.budiyev.android.codescanner.CodeScannerView; import com.craxiom.networksurvey.R; import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings; +import com.craxiom.networksurvey.ui.main.SharedViewModel; import com.google.gson.Gson; +import timber.log.Timber; + /** * Fragment responsible for QR code scanning. Leverages an open source code scanning library from * Yuriy Budiev. @@ -52,19 +55,16 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, final String scanSuccess = "Successfully scanned the MQTT settings"; Toast.makeText(getContext(), scanSuccess, Toast.LENGTH_SHORT).show(); - // TODO Navigate to Mqtt Fragment - Navigation.findNavController(requireActivity(), getId()) - .navigate(CodeScannerFragmentDirections.actionScannerFragmentToMqttConnectionFragment() - .setMqttConnectionSettings(mqttConnectionSettings) - ); + SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + viewModel.triggerNavigationToMqttConnection(mqttConnectionSettings); } catch (Exception e) { + Timber.i(e, "Failed to read the MQTT settings"); final String scanFailed = "Failed to read the MQTT settings"; Toast.makeText(getContext(), scanFailed, Toast.LENGTH_SHORT).show(); - Navigation.findNavController(requireActivity(), getId()) - .navigate(CodeScannerFragmentDirections.actionScannerFragmentToMqttConnectionFragment() - .setMqttConnectionSettings(CodeScannerFragmentArgs.fromBundle(getArguments()).getMqttConnectionSettings()) - ); + + SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + viewModel.triggerNavigationToMqttConnection(viewModel.getMqttConnectionSettings()); } } })); diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MainCellularFragment.java b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MainCellularFragment.java index ca799c46..02b3adb8 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MainCellularFragment.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MainCellularFragment.java @@ -185,6 +185,7 @@ public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) menuInflater.inflate(R.menu.cellular_details_menu, menu); } + // TODO Delete the menu item stuff in all the fragments @Override public boolean onMenuItemSelected(@NonNull MenuItem menuItem) { diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MqttFragment.java b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MqttFragment.java index 9f37c062..dbc9c7ac 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MqttFragment.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/MqttFragment.java @@ -24,7 +24,6 @@ import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.ViewModelProvider; -import androidx.navigation.Navigation; import com.craxiom.mqttlibrary.MqttConstants; import com.craxiom.mqttlibrary.connection.BrokerConnectionInfo; @@ -71,7 +70,7 @@ public class MqttFragment extends AConnectionFragment { storeConnectionParameters(); // Store the parameters so that the latest values are shared - Navigation.findNavController(requireActivity(), getId()) - .navigate(MqttFragmentDirections.actionMqttConnectionFragmentToShareFragment() - .setMqttConnectionSettings(getCurrentMqttConnectionSettings())); + SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + viewModel.triggerNavigationToQrCodeShare(getCurrentMqttConnectionSettings()); }); } @@ -286,10 +284,7 @@ private void updateQrCodeScanButtonVisibility(boolean visible) if (hasCameraPermission()) { SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); - viewModel.triggerNavigationToQrCodeScanner(); - /* TODO Delete me Navigation.findNavController(requireActivity(), getId()) - .navigate(MqttFragmentDirections.actionMqttConnectionFragmentToScannerFragment() - .setMqttConnectionSettings(getCurrentMqttConnectionSettings()));*/ + viewModel.triggerNavigationToQrCodeScanner(getCurrentMqttConnectionSettings()); } else { cameraPermissionRequestLauncher.launch(Manifest.permission.CAMERA); @@ -402,9 +397,10 @@ private boolean missingBluetoothPermissions() */ private void requestBluetoothPermissions() { - if (missingBluetoothPermissions()) + FragmentActivity activity = getActivity(); + if (missingBluetoothPermissions() && activity != null) { - ActivityCompat.requestPermissions(getActivity(), NetworkSurveyActivity.BLUETOOTH_PERMISSIONS, ACCESS_BLUETOOTH_PERMISSION_REQUEST_ID); + ActivityCompat.requestPermissions(activity, NetworkSurveyActivity.BLUETOOTH_PERMISSIONS, ACCESS_BLUETOOTH_PERMISSION_REQUEST_ID); } } } diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/NetworkDetailsFragment.java b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/NetworkDetailsFragment.java index d8d90d11..ecd91b54 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/NetworkDetailsFragment.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/NetworkDetailsFragment.java @@ -99,7 +99,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Bundle args = getArguments(); + Bundle args = getArguments(); // TODO WIP Test this getArguments with multiple SIM cards //noinspection DataFlowIssue subscriptionId = args.getInt(SUBSCRIPTION_ID_KEY, -1); Timber.d("Retrieving the subscriptionId from the arguments. subscriptionId=%d", subscriptionId); diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/QrCodeShareFragment.java b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/QrCodeShareFragment.java index 44def973..108c38bd 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/QrCodeShareFragment.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/QrCodeShareFragment.java @@ -9,9 +9,11 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import com.craxiom.networksurvey.R; import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings; +import com.craxiom.networksurvey.ui.main.SharedViewModel; import com.google.gson.Gson; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; @@ -33,9 +35,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ImageView imageView = view.findViewById(R.id.ivQrCode); - // FIXME Update this code with the new route approach - MqttConnectionSettings mqttConnectionSettings = - QrCodeShareFragmentArgs.fromBundle(getArguments()).getMqttConnectionSettings(); + SharedViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + MqttConnectionSettings mqttConnectionSettings = viewModel.getMqttConnectionSettings(); if (mqttConnectionSettings == null) return view; // Removing the device name because if two devices use the same MQTT client ID, then neither will be able to connect diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/TowerMapFragment.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/TowerMapFragment.kt index 55b0f1e5..2883b941 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/TowerMapFragment.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/fragments/TowerMapFragment.kt @@ -196,7 +196,7 @@ class TowerMapFragment : AServiceDataFragment(), MenuProvider, ICellularSurveyRe builder.setNegativeButton("Reject") { dialog, _ -> PreferenceUtils.setAcceptMapPrivacy(requireContext(), false) dialog.dismiss() - findNavController().popBackStack() // Go back to the previous fragment + findNavController().popBackStack() // Go back to the previous fragment // TODO WIP Test this } builder.show() } diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/gnss/GnssFilterDialog.java b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/gnss/GnssFilterDialog.java index 9cbd83ac..a3056d24 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/gnss/GnssFilterDialog.java +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/gnss/GnssFilterDialog.java @@ -29,7 +29,7 @@ public class GnssFilterDialog extends DialogFragment @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle args = getArguments(); + Bundle args = getArguments(); // TODO WIP Test this getArguments String[] items = args.getStringArray(ITEMS); mChecks = args.getBooleanArray(CHECKS); if (savedInstanceState != null) diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AppNavigation.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AppNavigation.kt index e559cf1d..b5fc5e77 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AppNavigation.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AppNavigation.kt @@ -1,6 +1,5 @@ package com.craxiom.networksurvey.ui.main -import android.os.Build import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.padding @@ -11,10 +10,8 @@ import androidx.compose.ui.viewinterop.AndroidViewBinding import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavGraphBuilder import androidx.navigation.NavHostController -import androidx.navigation.NavType import androidx.navigation.compose.composable import androidx.navigation.compose.navigation -import androidx.navigation.navArgument import com.craxiom.messaging.BluetoothRecordData import com.craxiom.networksurvey.databinding.ContainerBluetoothDetailsFragmentBinding import com.craxiom.networksurvey.databinding.ContainerGrpcFragmentBinding @@ -51,24 +48,13 @@ fun NavGraphBuilder.mainGraph( GrpcFragmentInCompose(paddingValues) } - composable( - // TODO Update the argument approach to match the wifi spectrum setup - route = "${NavDrawerOption.MqttBrokerConnection.name}?${MqttConnectionSettings.KEY}={mqttConnectionSettings}", - arguments = listOf(navArgument(MqttConnectionSettings.KEY) { - type = NavType.ParcelableType(MqttConnectionSettings::class.java) - nullable = true // Allow this argument to be nullable - }) - ) { backStackEntry -> + composable(NavDrawerOption.MqttBrokerConnection.name) + { val mqttConnectionSettings = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - backStackEntry.arguments?.getParcelable( - MqttConnectionSettings.KEY, - MqttConnectionSettings::class.java - ) - } else { - @Suppress("DEPRECATION") - backStackEntry.arguments?.getParcelable(MqttConnectionSettings.KEY) - } + mainNavController.previousBackStackEntry?.savedStateHandle?.get( + MqttConnectionSettings.KEY + ) + MqttFragmentInCompose( paddingValues = paddingValues, mqttConnectionSettings = mqttConnectionSettings diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/NsMainScreen.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/NsMainScreen.kt index 2d4612fe..03ea471c 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/NsMainScreen.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/NsMainScreen.kt @@ -20,6 +20,7 @@ import androidx.navigation.compose.NavHost import androidx.navigation.compose.rememberNavController import com.craxiom.networksurvey.R import com.craxiom.networksurvey.fragments.BLUETOOTH_DATA_KEY +import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings import com.craxiom.networksurvey.model.WifiNetwork import com.craxiom.networksurvey.ui.main.appdrawer.AppDrawerContent import com.craxiom.networksurvey.ui.main.appdrawer.AppDrawerItemInfo @@ -93,8 +94,15 @@ fun MainCompose( LaunchedEffect(viewModel.navigateToMqttConnection) { viewModel.navigateToMqttConnection.observe(lifecycleOwner) { shouldNavigate -> if (shouldNavigate) { + if (viewModel.mqttConnectionSettings != null) { + mainNavController.currentBackStackEntry?.savedStateHandle?.set( + MqttConnectionSettings.KEY, + viewModel.mqttConnectionSettings + ) + } mainNavController.navigate(NavDrawerOption.MqttBrokerConnection.name) viewModel.resetNavigationFlag() + viewModel.resetMqttConnectionSettings() } } } diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/SharedViewModel.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/SharedViewModel.kt index 04f3b470..1b5ea4c5 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/SharedViewModel.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/SharedViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.craxiom.messaging.BluetoothRecordData +import com.craxiom.networksurvey.fragments.model.MqttConnectionSettings import com.craxiom.networksurvey.model.WifiNetwork import com.craxiom.networksurvey.ui.wifi.model.WifiNetworkInfoList import dagger.hilt.android.lifecycle.HiltViewModel @@ -42,15 +43,20 @@ class SharedViewModel @Inject constructor(application: Application) : private val _navigateToMqttConnection = MutableLiveData(false) val navigateToMqttConnection: LiveData = _navigateToMqttConnection + private var _mqttConnectionSettings: MqttConnectionSettings? = null + val mqttConnectionSettings: MqttConnectionSettings? + get() = _mqttConnectionSettings private val _navigateToSettings = MutableLiveData(false) val navigateToSettings: LiveData = _navigateToSettings - fun triggerNavigationToQrCodeScanner() { + fun triggerNavigationToQrCodeScanner(mqttConnectionSettings: MqttConnectionSettings) { + _mqttConnectionSettings = mqttConnectionSettings _navigateToQrCodeScanner.value = true } - fun triggerNavigationToQrCodeShare() { + fun triggerNavigationToQrCodeShare(mqttConnectionSettings: MqttConnectionSettings) { + _mqttConnectionSettings = mqttConnectionSettings _navigateToQrCodeShare.value = true } @@ -80,6 +86,11 @@ class SharedViewModel @Inject constructor(application: Application) : _navigateToMqttConnection.value = true } + fun triggerNavigationToMqttConnection(mqttConnectionSettings: MqttConnectionSettings?) { + _mqttConnectionSettings = mqttConnectionSettings + _navigateToMqttConnection.value = true + } + fun triggerNavigationToSettings() { _navigateToSettings.value = true } @@ -95,4 +106,8 @@ class SharedViewModel @Inject constructor(application: Application) : _navigateToMqttConnection.value = false _navigateToSettings.value = false } + + fun resetMqttConnectionSettings() { + _mqttConnectionSettings = null + } } \ No newline at end of file