Browse Source

Simplify handleDeepLink logic

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 năm trước cách đây
mục cha
commit
f7ef270dae

+ 22 - 30
app/src/main/java/com/nextcloud/client/files/DeepLinkConstants.kt

@@ -7,37 +7,29 @@
 
 package com.nextcloud.client.files
 
-// deep link url format should be: https://example.com/app/<constants>
-object DeepLinkConstants {
-    const val OPEN_FILES = "openFiles"
-    const val OPEN_FAVORITES = "openFavorites"
-    const val OPEN_MEDIA = "openMedia"
-    const val OPEN_SHARED = "openShared"
-    const val OPEN_OFFLINE = "openOffline"
-    const val OPEN_NOTIFICATIONS = "openNotifications"
-    const val OPEN_DELETED = "openDeleted"
-    const val OPEN_SETTINGS = "openSettings"
-    const val OPEN_AUTO_UPLOAD = "openAutoUpload"
+import com.owncloud.android.R
 
-    // for external url the url should have the following format
-    // https://example.com/app/openUrl?url=<url_to_open>
-    const val OPEN_EXTERNAL_URL = "openUrl"
+enum class DeepLinkConstants(val route: String, val navId: Int) {
+    OPEN_FILES("openFiles", R.id.nav_all_files),
+    OPEN_FAVORITES("openFavorites", R.id.nav_favorites),
+    OPEN_MEDIA("openMedia", R.id.nav_gallery),
+    OPEN_SHARED("openShared", R.id.nav_shared),
+    OPEN_OFFLINE("openOffline", R.id.nav_on_device),
+    OPEN_NOTIFICATIONS("openNotifications", R.id.nav_notifications),
+    OPEN_DELETED("openDeleted", R.id.nav_trashbin),
+    OPEN_SETTINGS("openSettings", R.id.nav_settings),
 
-    const val ACTION_CREATE_NEW = "createNew"
-    const val ACTION_APP_UPDATE = "checkAppUpdate"
+    // Special case, handled separately
+    OPEN_AUTO_UPLOAD("openAutoUpload", -1),
+    OPEN_EXTERNAL_URL("openUrl", -1),
+    ACTION_CREATE_NEW("createNew", -1),
+    ACTION_APP_UPDATE("checkAppUpdate", -1);
 
-    val navigationPaths = listOf(
-        OPEN_FILES,
-        OPEN_FAVORITES,
-        OPEN_MEDIA,
-        OPEN_SHARED,
-        OPEN_OFFLINE,
-        OPEN_NOTIFICATIONS,
-        OPEN_DELETED,
-        OPEN_SETTINGS,
-        OPEN_AUTO_UPLOAD,
-        OPEN_EXTERNAL_URL,
-        ACTION_CREATE_NEW,
-        ACTION_APP_UPDATE
-    )
+    companion object {
+        fun fromPath(path: String?): DeepLinkConstants? {
+            return entries.find { it.route == path }
+        }
+
+        val navigationPaths = entries.map { it.route }
+    }
 }

+ 25 - 44
app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -1277,50 +1277,31 @@ public abstract class DrawerActivity extends ToolbarActivity
 
     protected void handleDeepLink(@NonNull Uri uri) {
         String path = uri.getLastPathSegment();
-        if (path != null) {
-            switch (path) {
-                case DeepLinkConstants.OPEN_FILES:
-                    handleNavItemClickEvent(R.id.nav_all_files);
-                    break;
-                case DeepLinkConstants.OPEN_FAVORITES:
-                    handleNavItemClickEvent(R.id.nav_favorites);
-                    break;
-                case DeepLinkConstants.OPEN_MEDIA:
-                    handleNavItemClickEvent(R.id.nav_gallery);
-                    break;
-                case DeepLinkConstants.OPEN_SHARED:
-                    handleNavItemClickEvent(R.id.nav_shared);
-                    break;
-                case DeepLinkConstants.OPEN_OFFLINE:
-                    handleNavItemClickEvent(R.id.nav_on_device);
-                    break;
-                case DeepLinkConstants.OPEN_NOTIFICATIONS:
-                    handleNavItemClickEvent(R.id.nav_notifications);
-                    break;
-                case DeepLinkConstants.OPEN_DELETED:
-                    handleNavItemClickEvent(R.id.nav_trashbin);
-                    break;
-                case DeepLinkConstants.OPEN_SETTINGS:
-                    handleNavItemClickEvent(R.id.nav_settings);
-                    break;
-                case DeepLinkConstants.OPEN_AUTO_UPLOAD:
-                    startActivity(SyncedFoldersActivity.class);
-                    break;
-                case DeepLinkConstants.OPEN_EXTERNAL_URL:
-                    Intent intent = new Intent(Intent.ACTION_VIEW,
-                                               Uri.parse(uri.getQueryParameter("url")));
-                    startActivity(intent);
-                    break;
-                case DeepLinkConstants.ACTION_CREATE_NEW:
-                    findViewById(R.id.fab_main).callOnClick();
-                    break;
-                case DeepLinkConstants.ACTION_APP_UPDATE:
-                    openAppStore(getPackageName(), false);
-                    break;
-                default:
-                    DisplayUtils.showSnackMessage(this, getString(R.string.invalid_url));
-                    break;
-            }
+        if (path == null) return;
+
+        DeepLinkConstants deepLinkType = DeepLinkConstants.Companion.fromPath(path);
+        if (deepLinkType == null) {
+            DisplayUtils.showSnackMessage(this, getString(R.string.invalid_url));
+            return;
+        }
+
+        switch (deepLinkType) {
+            case OPEN_AUTO_UPLOAD:
+                startActivity(new Intent(this, SyncedFoldersActivity.class));
+                break;
+            case OPEN_EXTERNAL_URL:
+                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri.getQueryParameter("url")));
+                startActivity(intent);
+                break;
+            case ACTION_CREATE_NEW:
+                findViewById(R.id.fab_main).callOnClick();
+                break;
+            case ACTION_APP_UPDATE:
+                openAppStore(getPackageName(), false);
+                break;
+            default:
+                handleNavItemClickEvent(deepLinkType.getNavId());
+                break;
         }
     }