From 02ef25b96170db90a785da4f7913bb1fa6b1e7bc Mon Sep 17 00:00:00 2001 From: johan12345 Date: Sun, 30 Jun 2024 16:20:02 +0200 Subject: [PATCH] rework navigation handling to avoid changing start destination fixes the following issue: - start app for the first time, go through onboarding - go to settings, change to dark mode - try to go back to the map using the drawer -> stuck, only back button helps --- .../java/net/vonforst/evmap/MapsActivity.kt | 245 +++++++++--------- .../vonforst/evmap/fragment/MapFragment.kt | 6 + app/src/main/res/navigation/nav_graph.xml | 1 + 3 files changed, 131 insertions(+), 121 deletions(-) diff --git a/app/src/main/java/net/vonforst/evmap/MapsActivity.kt b/app/src/main/java/net/vonforst/evmap/MapsActivity.kt index 60e2dcd4..0b63dd14 100644 --- a/app/src/main/java/net/vonforst/evmap/MapsActivity.kt +++ b/app/src/main/java/net/vonforst/evmap/MapsActivity.kt @@ -60,6 +60,7 @@ class MapsActivity : AppCompatActivity(), setContentView(R.layout.activity_maps) + val drawerLayout = findViewById(R.id.drawer_layout) appBarConfiguration = AppBarConfiguration( setOf( R.id.map, @@ -67,7 +68,7 @@ class MapsActivity : AppCompatActivity(), R.id.about, R.id.settings ), - findViewById(R.id.drawer_layout) + drawerLayout ) val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment @@ -87,6 +88,17 @@ class MapsActivity : AppCompatActivity(), checkPlayServices(this) + navController.setGraph(navGraph, MapFragmentArgs(appStart = true).toBundle()) + var deepLink: PendingIntent? = null + + navController.addOnDestinationChangedListener { _, destination, _ -> + if (destination.id == R.id.onboarding) { + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) + } else { + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) + } + } + if (!prefs.welcomeDialogShown || !prefs.dataSourceSet) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // wait for splash screen animation to finish on first start @@ -104,134 +116,125 @@ class MapsActivity : AppCompatActivity(), } }) } - navGraph.setStartDestination(R.id.onboarding) - navController.graph = navGraph - return - } else if (!prefs.privacyAccepted) { - navGraph.setStartDestination(R.id.onboarding) - navController.graph = navGraph - return - } else { - navGraph.setStartDestination(R.id.map) - navController.setGraph(navGraph, MapFragmentArgs(appStart = true).toBundle()) - var deepLink: PendingIntent? = null + } else if (intent?.scheme == "geo") { + val query = intent.data?.query?.split("=")?.get(1) + val coords = getLocationFromIntent(intent) - if (intent?.scheme == "geo") { - val query = intent.data?.query?.split("=")?.get(1) - val coords = getLocationFromIntent(intent) - - if (coords != null) { - val lat = coords[0] - val lon = coords[1] - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments(MapFragmentArgs(latLng = LatLng(lat, lon)).toBundle()) - .createPendingIntent() - } else if (!query.isNullOrEmpty()) { - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments(MapFragmentArgs(locationName = query).toBundle()) - .createPendingIntent() - } - } else if (intent?.scheme == "https" && intent?.data?.host == "www.goingelectric.de") { - val id = intent.data?.pathSegments?.lastOrNull()?.toLongOrNull() - if (id != null) { - if (prefs.dataSource != "goingelectric") { - prefs.dataSource = "goingelectric" - Toast.makeText( - this, - getString( - R.string.data_source_switched_to, - getString(R.string.data_source_goingelectric) - ), - Toast.LENGTH_LONG - ).show() - } - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments(MapFragmentArgs(chargerId = id).toBundle()) - .createPendingIntent() - } - } else if (intent?.scheme == "https" && intent?.data?.host in listOf("openchargemap.org", "map.openchargemap.io")) { - val id = when (intent.data?.host) { - "openchargemap.org" -> intent.data?.pathSegments?.lastOrNull()?.toLongOrNull() - "map.openchargemap.io" -> intent.data?.getQueryParameter("id")?.toLongOrNull() - else -> null - } - if (id != null) { - if (prefs.dataSource != "openchargemap") { - prefs.dataSource = "openchargemap" - Toast.makeText( - this, - getString( - R.string.data_source_switched_to, - getString(R.string.data_source_openchargemap) - ), - Toast.LENGTH_LONG - ).show() - } - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments(MapFragmentArgs(chargerId = id).toBundle()) - .createPendingIntent() - } - } else if (intent.scheme == "net.vonforst.evmap") { - intent.data?.let { - if (it.host == "find_charger") { - val lat = it.getQueryParameter("latitude")?.toDouble() - val lon = it.getQueryParameter("longitude")?.toDouble() - val name = it.getQueryParameter("name") - if (lat != null && lon != null) { - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments( - MapFragmentArgs( - latLng = LatLng(lat, lon), - locationName = name - ).toBundle() - ) - .createPendingIntent() - } else if (name != null) { - deepLink = navController.createDeepLink() - .setGraph(R.navigation.nav_graph) - .setDestination(R.id.map) - .setArguments(MapFragmentArgs(locationName = name).toBundle()) - .createPendingIntent() - } - } - } - } else if (intent.hasExtra(EXTRA_CHARGER_ID)) { + if (coords != null) { + val lat = coords[0] + val lon = coords[1] deepLink = navController.createDeepLink() + .setGraph(R.navigation.nav_graph) .setDestination(R.id.map) - .setArguments( - MapFragmentArgs( - chargerId = intent.getLongExtra(EXTRA_CHARGER_ID, 0), - latLng = LatLng( - intent.getDoubleExtra(EXTRA_LAT, 0.0), - intent.getDoubleExtra(EXTRA_LON, 0.0) - ) - ).toBundle() - ) + .setArguments(MapFragmentArgs(latLng = LatLng(lat, lon)).toBundle()) .createPendingIntent() - } else if (intent.hasExtra(EXTRA_FAVORITES)) { + } else if (!query.isNullOrEmpty()) { deepLink = navController.createDeepLink() - .setGraph(navGraph) - .setDestination(R.id.favs) - .createPendingIntent() - } else if (intent.hasExtra(EXTRA_DONATE)) { - deepLink = navController.createDeepLink() - .setGraph(navGraph) - .setDestination(R.id.donate) + .setGraph(R.navigation.nav_graph) + .setDestination(R.id.map) + .setArguments(MapFragmentArgs(locationName = query).toBundle()) .createPendingIntent() } - - deepLink?.send() + } else if (intent?.scheme == "https" && intent?.data?.host == "www.goingelectric.de") { + val id = intent.data?.pathSegments?.lastOrNull()?.toLongOrNull() + if (id != null) { + if (prefs.dataSource != "goingelectric") { + prefs.dataSource = "goingelectric" + Toast.makeText( + this, + getString( + R.string.data_source_switched_to, + getString(R.string.data_source_goingelectric) + ), + Toast.LENGTH_LONG + ).show() + } + deepLink = navController.createDeepLink() + .setGraph(R.navigation.nav_graph) + .setDestination(R.id.map) + .setArguments(MapFragmentArgs(chargerId = id).toBundle()) + .createPendingIntent() + } + } else if (intent?.scheme == "https" && intent?.data?.host in listOf( + "openchargemap.org", + "map.openchargemap.io" + ) + ) { + val id = when (intent.data?.host) { + "openchargemap.org" -> intent.data?.pathSegments?.lastOrNull()?.toLongOrNull() + "map.openchargemap.io" -> intent.data?.getQueryParameter("id")?.toLongOrNull() + else -> null + } + if (id != null) { + if (prefs.dataSource != "openchargemap") { + prefs.dataSource = "openchargemap" + Toast.makeText( + this, + getString( + R.string.data_source_switched_to, + getString(R.string.data_source_openchargemap) + ), + Toast.LENGTH_LONG + ).show() + } + deepLink = navController.createDeepLink() + .setGraph(R.navigation.nav_graph) + .setDestination(R.id.map) + .setArguments(MapFragmentArgs(chargerId = id).toBundle()) + .createPendingIntent() + } + } else if (intent.scheme == "net.vonforst.evmap") { + intent.data?.let { + if (it.host == "find_charger") { + val lat = it.getQueryParameter("latitude")?.toDouble() + val lon = it.getQueryParameter("longitude")?.toDouble() + val name = it.getQueryParameter("name") + if (lat != null && lon != null) { + deepLink = navController.createDeepLink() + .setGraph(R.navigation.nav_graph) + .setDestination(R.id.map) + .setArguments( + MapFragmentArgs( + latLng = LatLng(lat, lon), + locationName = name + ).toBundle() + ) + .createPendingIntent() + } else if (name != null) { + deepLink = navController.createDeepLink() + .setGraph(R.navigation.nav_graph) + .setDestination(R.id.map) + .setArguments(MapFragmentArgs(locationName = name).toBundle()) + .createPendingIntent() + } + } + } + } else if (intent.hasExtra(EXTRA_CHARGER_ID)) { + deepLink = navController.createDeepLink() + .setDestination(R.id.map) + .setArguments( + MapFragmentArgs( + chargerId = intent.getLongExtra(EXTRA_CHARGER_ID, 0), + latLng = LatLng( + intent.getDoubleExtra(EXTRA_LAT, 0.0), + intent.getDoubleExtra(EXTRA_LON, 0.0) + ) + ).toBundle() + ) + .createPendingIntent() + } else if (intent.hasExtra(EXTRA_FAVORITES)) { + deepLink = navController.createDeepLink() + .setGraph(navGraph) + .setDestination(R.id.favs) + .createPendingIntent() + } else if (intent.hasExtra(EXTRA_DONATE)) { + deepLink = navController.createDeepLink() + .setGraph(navGraph) + .setDestination(R.id.donate) + .createPendingIntent() } + + deepLink?.send() } fun navigateTo(charger: ChargeLocation) { diff --git a/app/src/main/java/net/vonforst/evmap/fragment/MapFragment.kt b/app/src/main/java/net/vonforst/evmap/fragment/MapFragment.kt index 32b73f03..253f4dcf 100644 --- a/app/src/main/java/net/vonforst/evmap/fragment/MapFragment.kt +++ b/app/src/main/java/net/vonforst/evmap/fragment/MapFragment.kt @@ -297,6 +297,10 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac get() = resources.getBoolean(R.bool.bottom_sheet_collapsible) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + if (!prefs.welcomeDialogShown || !prefs.dataSourceSet || !prefs.privacyAccepted) { + findNavController().navigate(R.id.onboarding) + } + requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED) mapFragment!!.getMapAsync(this) @@ -1051,6 +1055,8 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac this.map = map vm.mapProjection = map.projection val context = this.context ?: return + view ?: return + chargerIconGenerator = ChargerIconGenerator(context, map.bitmapDescriptorFactory) vm.mapTrafficSupported.value = diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index c9f02a12..e762b43d 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -2,6 +2,7 @@