Skip to content

Commit

Permalink
WIP: Pass the args to and from the mqtt QR code share and scan fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrowlands committed Oct 28, 2024
1 parent e84bd84 commit c93634f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<TextView>(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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
}
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +70,7 @@ public class MqttFragment extends AConnectionFragment<NetworkSurveyService.Surve
FragmentActivity activity = getActivity();
if (activity == null) return;
SharedViewModel viewModel = new ViewModelProvider(activity).get(SharedViewModel.class);
viewModel.triggerNavigationToQrCodeScanner();
viewModel.triggerNavigationToQrCodeScanner(getCurrentMqttConnectionSettings());
} else
{
Toast.makeText(getContext(), getString(R.string.grant_camera_permission), Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -137,9 +136,8 @@ protected void inflateAdditionalFieldsViewStub(LayoutInflater layoutInflater, Vi
codeShareButton.setVisibility(View.VISIBLE);
codeShareButton.setOnClickListener(v -> {
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());
});
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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>(
MqttConnectionSettings.KEY
)

MqttFragmentInCompose(
paddingValues = paddingValues,
mqttConnectionSettings = mqttConnectionSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,15 +43,20 @@ class SharedViewModel @Inject constructor(application: Application) :

private val _navigateToMqttConnection = MutableLiveData(false)
val navigateToMqttConnection: LiveData<Boolean> = _navigateToMqttConnection
private var _mqttConnectionSettings: MqttConnectionSettings? = null
val mqttConnectionSettings: MqttConnectionSettings?
get() = _mqttConnectionSettings

private val _navigateToSettings = MutableLiveData(false)
val navigateToSettings: LiveData<Boolean> = _navigateToSettings

fun triggerNavigationToQrCodeScanner() {
fun triggerNavigationToQrCodeScanner(mqttConnectionSettings: MqttConnectionSettings) {
_mqttConnectionSettings = mqttConnectionSettings
_navigateToQrCodeScanner.value = true
}

fun triggerNavigationToQrCodeShare() {
fun triggerNavigationToQrCodeShare(mqttConnectionSettings: MqttConnectionSettings) {
_mqttConnectionSettings = mqttConnectionSettings
_navigateToQrCodeShare.value = true
}

Expand Down Expand Up @@ -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
}
Expand All @@ -95,4 +106,8 @@ class SharedViewModel @Inject constructor(application: Application) :
_navigateToMqttConnection.value = false
_navigateToSettings.value = false
}

fun resetMqttConnectionSettings() {
_mqttConnectionSettings = null
}
}

0 comments on commit c93634f

Please sign in to comment.