From 15792600a7567761ecbfa157246ffecffb4b3ec6 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <87148630+Jean-BaptisteC@users.noreply.github.com> Date: Sat, 9 Dec 2023 16:46:29 +0100 Subject: [PATCH] Add condition to do not navigate to 0 (#357) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benjamin Falière --- .../fragments/apps/model/AppsRVAdapter.kt | 17 +++++++----- .../trackers/model/TrackersRVAdapter.kt | 26 +++++++++++-------- .../exodusprivacy/utils/Navigation.kt | 10 +++++++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/org/eu/exodus_privacy/exodusprivacy/utils/Navigation.kt diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/apps/model/AppsRVAdapter.kt b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/apps/model/AppsRVAdapter.kt index d92cef47..98707c4b 100644 --- a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/apps/model/AppsRVAdapter.kt +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/apps/model/AppsRVAdapter.kt @@ -12,6 +12,7 @@ import org.eu.exodus_privacy.exodusprivacy.databinding.RecyclerViewAppItemBindin import org.eu.exodus_privacy.exodusprivacy.fragments.apps.AppsFragmentDirections import org.eu.exodus_privacy.exodusprivacy.fragments.trackerdetail.TrackerDetailFragmentDirections import org.eu.exodus_privacy.exodusprivacy.manager.database.app.ExodusApplication +import org.eu.exodus_privacy.exodusprivacy.utils.safeNavigate import org.eu.exodus_privacy.exodusprivacy.utils.setExodusColor class AppsRVAdapter( @@ -37,14 +38,16 @@ class AppsRVAdapter( holder.binding.apply { root.setOnClickListener { - val action = if (currentDestinationId == R.id.appsFragment) { - AppsFragmentDirections.actionAppsFragmentToAppDetailFragment(app.packageName) - } else { - TrackerDetailFragmentDirections.actionTrackerDetailFragmentToAppDetailFragment( - app.packageName - ) + if (currentDestinationId != 0) { + val action = if (currentDestinationId == R.id.appsFragment) { + AppsFragmentDirections.actionAppsFragmentToAppDetailFragment(app.packageName) + } else { + TrackerDetailFragmentDirections.actionTrackerDetailFragmentToAppDetailFragment( + app.packageName + ) + } + it.findNavController().safeNavigate(action) } - it.findNavController().navigate(action) } appIconIV.background = app.icon.toDrawable(context.resources) appNameTV.text = app.name diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/trackers/model/TrackersRVAdapter.kt b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/trackers/model/TrackersRVAdapter.kt index 068c47a8..737b4a26 100644 --- a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/trackers/model/TrackersRVAdapter.kt +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/trackers/model/TrackersRVAdapter.kt @@ -20,6 +20,7 @@ import org.eu.exodus_privacy.exodusprivacy.databinding.RecyclerViewTrackerItemBi import org.eu.exodus_privacy.exodusprivacy.fragments.appdetail.AppDetailFragmentDirections import org.eu.exodus_privacy.exodusprivacy.fragments.trackers.TrackersFragmentDirections import org.eu.exodus_privacy.exodusprivacy.manager.database.tracker.TrackerData +import org.eu.exodus_privacy.exodusprivacy.utils.safeNavigate class TrackersRVAdapter( private val showSuggestions: Boolean, @@ -107,18 +108,21 @@ class TrackersRVAdapter( } } root.setOnClickListener { - val action = if (currentDestinationId == R.id.appDetailFragment) { - AppDetailFragmentDirections.actionAppDetailFragmentToTrackerDetailFragment( - app.id, - trackerPercentage.toInt() - ) - } else { - TrackersFragmentDirections.actionTrackersFragmentToTrackerDetailFragment( - app.id, - trackerPercentage.toInt() - ) + if (currentDestinationId != 0) { + val action = + if (currentDestinationId == R.id.appDetailFragment) { + AppDetailFragmentDirections.actionAppDetailFragmentToTrackerDetailFragment( + app.id, + trackerPercentage.toInt() + ) + } else { + TrackersFragmentDirections.actionTrackersFragmentToTrackerDetailFragment( + app.id, + trackerPercentage.toInt() + ) + } + holder.itemView.findNavController().safeNavigate(action) } - holder.itemView.findNavController().navigate(action) } } } diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/utils/Navigation.kt b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/utils/Navigation.kt new file mode 100644 index 00000000..03196c44 --- /dev/null +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/utils/Navigation.kt @@ -0,0 +1,10 @@ +package org.eu.exodus_privacy.exodusprivacy.utils + +import androidx.navigation.NavController +import androidx.navigation.NavDirections + +fun NavController.safeNavigate(direction: NavDirections) { + currentDestination?.getAction(direction.actionId)?.run { + navigate(direction) + } +}