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 af754a46e26a71d410f56026ae4b8f02276a507d..399cf52af27042606c3bc1ebabc07da0f77c5227 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) } } } 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 3554640aa5ec8dc0a728d40d9a021d5779babcc2..950e9791c4c5406f2092911b0f84e70a852d5a23 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,24 @@ 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 + ) { + try { + if (currentDestinationId == currentDestination?.id) { + navigate(id, args) + } + } catch (e: Exception) { + e.printStackTrace() + } + } }