From 75995b05e8ade2c58a5748c9ac1ab3b29c5ba477 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Wed, 13 Apr 2022 17:20:58 +0530 Subject: [PATCH 1/4] App lounge: (issue_5166) Create extension function NavController.safeNavigate --- .../e/apps/utils/modules/CommonUtilsModule.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt index 3554640aa..3639d83e6 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt @@ -22,8 +22,11 @@ import android.content.Context import android.net.ConnectivityManager import android.net.NetworkCapabilities import android.os.Build +import android.os.Bundle import android.os.Environment import android.os.StatFs +import androidx.annotation.IdRes +import androidx.navigation.NavController import com.google.gson.Gson import com.google.gson.GsonBuilder import dagger.Module @@ -127,4 +130,20 @@ object CommonUtilsModule { } return false } + + /** + * Prevents calling a route if the navigation is already done, i.e. prevents duplicate calls. + * Source: https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e + * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5166 + * Also related: https://gitlab.e.foundation/ecorp/apps/apps/-/merge_requests/28 + */ + fun NavController.safeNavigate( + @IdRes currentDestinationId: Int, + @IdRes id: Int, + args: Bundle? = null + ) { + if (currentDestinationId == currentDestination?.id) { + navigate(id, args) + } + } } -- GitLab From 258b4a962cd2073a5802b22171b63b0c6a3da061 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Wed, 13 Apr 2022 17:22:22 +0530 Subject: [PATCH 2/4] App lounge: (issue_5166) use NavController.safeNavigate in HomeFragment --- .../main/java/foundation/e/apps/home/HomeFragment.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index af754a46e..399cf52af 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -42,6 +42,7 @@ import foundation.e.apps.manager.download.data.DownloadProgress import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.User +import foundation.e.apps.utils.modules.CommonUtilsModule.safeNavigate import foundation.e.apps.utils.modules.PWAManagerModule import kotlinx.coroutines.launch import javax.inject.Inject @@ -184,8 +185,15 @@ class HomeFragment : Fragment(R.layout.fragment_home), FusedAPIInterface { private fun onTosAccepted(isTosAccepted: Boolean) { if (isTosAccepted) { + /* + * "safeNavigate" is an extension function, to prevent calling this navigation multiple times. + * This is taken from: + * https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e + * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5166 + * Also related: https://gitlab.e.foundation/ecorp/apps/apps/-/merge_requests/28 + */ view?.findNavController() - ?.navigate(R.id.action_homeFragment_to_signInFragment) + ?.safeNavigate(R.id.homeFragment, R.id.action_homeFragment_to_signInFragment) } } } -- GitLab From 9fff7759b7a3779c03dcb39524f7dab322f6bf15 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Wed, 13 Apr 2022 17:23:10 +0530 Subject: [PATCH 3/4] App lounge: (issue_5166) surround navigation code with try-catch just in case. --- app/src/main/java/foundation/e/apps/home/HomeFragment.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index 399cf52af..566ad672f 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -192,8 +192,12 @@ class HomeFragment : Fragment(R.layout.fragment_home), FusedAPIInterface { * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5166 * Also related: https://gitlab.e.foundation/ecorp/apps/apps/-/merge_requests/28 */ - view?.findNavController() - ?.safeNavigate(R.id.homeFragment, R.id.action_homeFragment_to_signInFragment) + try { + view?.findNavController() + ?.safeNavigate(R.id.homeFragment, R.id.action_homeFragment_to_signInFragment) + } catch (e: Exception) { + e.printStackTrace() + } } } } -- GitLab From 890bdbda159ea6a8d8c508423023e9a2ec224109 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Wed, 13 Apr 2022 17:41:01 +0530 Subject: [PATCH 4/4] App lounge: (issue_5166) move try-catch block inside safeNavigate function. --- app/src/main/java/foundation/e/apps/home/HomeFragment.kt | 8 ++------ .../foundation/e/apps/utils/modules/CommonUtilsModule.kt | 8 ++++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index 566ad672f..399cf52af 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -192,12 +192,8 @@ class HomeFragment : Fragment(R.layout.fragment_home), FusedAPIInterface { * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5166 * Also related: https://gitlab.e.foundation/ecorp/apps/apps/-/merge_requests/28 */ - try { - view?.findNavController() - ?.safeNavigate(R.id.homeFragment, R.id.action_homeFragment_to_signInFragment) - } catch (e: Exception) { - e.printStackTrace() - } + view?.findNavController() + ?.safeNavigate(R.id.homeFragment, R.id.action_homeFragment_to_signInFragment) } } } diff --git a/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt index 3639d83e6..950e9791c 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/CommonUtilsModule.kt @@ -142,8 +142,12 @@ object CommonUtilsModule { @IdRes id: Int, args: Bundle? = null ) { - if (currentDestinationId == currentDestination?.id) { - navigate(id, args) + try { + if (currentDestinationId == currentDestination?.id) { + navigate(id, args) + } + } catch (e: Exception) { + e.printStackTrace() } } } -- GitLab