Browse Source

remove requestNavigation

Tienson Qin 3 months ago
parent
commit
6af863e6ac

+ 39 - 25
ios/App/App/AppDelegate.swift

@@ -97,10 +97,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDel
             UIApplication.shared.open(url, options: [:], completionHandler: nil)
         }
     }
-    private func navigateTo(_ urlString: String) {
-        UILocalPlugin.requestNavigation(path: urlString)
-        pushIfNeeded(path: urlString, animated: true)
-    }
 
     private func normalizedPath(_ raw: String?) -> String {
         guard let raw = raw, !raw.isEmpty else { return "/" }
@@ -141,34 +137,52 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDel
         }
     }
 
-    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
-        guard let current = viewController as? NativePageViewController else { return }
+    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
+        guard let toVC = viewController as? NativePageViewController else { return }
+        guard let coordinator = navigationController.transitionCoordinator else { return }
+        let vcs = navigationController.viewControllers
+        let toIndex = vcs.firstIndex(of: toVC)
+        let fromVC = coordinator.viewController(forKey: .from) as? NativePageViewController
+        let fromIndex = fromVC.flatMap { vcs.firstIndex(of: $0) }
 
-        let visibleCount = navigationController.viewControllers.count
-        let popCount = max(0, pathStack.count - visibleCount)
+        let isPop = if let toIdx = toIndex, let fromIdx = fromIndex {
+            toIdx < fromIdx
+        } else {
+            // fallback: compare counts
+            vcs.count < pathStack.count
+        }
 
-        if popCount > 0 {
-            // User swiped back / popped: mirror it by popping native stack tracker
-            for _ in 0..<popCount { _ = pathStack.popLast() }
+        print("navigationController willShow isPop:", isPop,
+              "from:", fromVC?.targetPath ?? "nil",
+              "to:", toVC.targetPath)
 
-            // And let the single WebView go back in history (once per pop)
-            if let webView = SharedWebViewController.instance.bridgeController.bridge?.webView {
-                ignoreRoutePopCount += popCount
-                for _ in 0..<popCount {
-                    webView.goBack()
-                }
+        if isPop {
+            // Keep native bookkeeping aligned with the upcoming pop
+            if pathStack.count > 1 {
+                _ = pathStack.popLast()
+            }
+            if let last = pathStack.last, last != toVC.targetPath {
+                pathStack[pathStack.count - 1] = toVC.targetPath
             }
-            return
-        }
 
-        // Not a pop: ensure stack alignment and tell JS which route to render (no extra history entry)
-        if pathStack.isEmpty {
-            pathStack.append(current.targetPath)
-        } else if let last = pathStack.last, last != current.targetPath {
-            pathStack[pathStack.count - 1] = current.targetPath
+            // Move shared webview to destination before pop completes
+            SharedWebViewController.instance.attach(to: toVC)
+
+            // Trigger browser back so Reitit updates route-match while attached to destination
+            if let webView = SharedWebViewController.instance.bridgeController.bridge?.webView,
+               webView.canGoBack {
+                ignoreRoutePopCount += 1
+                webView.goBack()
+            } else {
+                // Fallback: ask JS to render without adding history
+                ignoreRoutePopCount += 1
+            }
         }
+    }
 
-        UILocalPlugin.requestNavigation(path: current.targetPath, push: false)
+    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
+        guard let current = viewController as? NativePageViewController else { return }
+        SharedWebViewController.instance.attach(to: current)
     }
 
     private func observeRouteChanges() {

+ 0 - 2
ios/App/App/NativePageViewController.swift

@@ -31,7 +31,5 @@ class NativePageViewController: UIViewController {
 
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
-        // Tell JS to navigate to the desired path
-        UILocalPlugin.requestNavigation(path: targetPath, push: push)
     }
 }

+ 0 - 26
ios/App/App/UILocalPlugin.swift

@@ -537,32 +537,6 @@ private func scoreTranscript(_ text: String, locale: Locale) -> Int {
         }
     }
 
-    @objc public static func requestNavigation(_ route: [String: Any]) {
-        // Use this to ask the WebView to navigate with a named route map.
-        if navigationReady {
-            NotificationCenter.default.post(
-                name: UILocalPlugin.navigationNotification,
-                object: nil,
-                userInfo: ["route": route]
-            )
-        } else {
-            pendingRoutes.append(route)
-        }
-    }
-
-    @objc public static func requestNavigation(path: String, push: Bool = true) {
-        // Use this to ask the WebView to navigate to a specific path (e.g. /page/Today).
-        if navigationReady {
-            NotificationCenter.default.post(
-                name: UILocalPlugin.navigationNotification,
-                object: nil,
-                userInfo: ["path": path, "push": push]
-            )
-        } else {
-            pendingPaths.append((path: path, push: push))
-        }
-    }
-
     @objc func routeDidChange(_ call: CAPPluginCall) {
         let route = call.getObject("route") as? [String: Any]
         let path = call.getString("path")

+ 4 - 2
src/main/mobile/components/app.cljs

@@ -190,7 +190,9 @@
       :settings
       (settings/page)
       :search
-      (search/search)
+      (if view
+        (view route-match)
+        (search/search))
       "Not Found")))
 
 (rum/defc app
@@ -219,7 +221,7 @@
                         {:data-tab (str tab)}
                         (main-content tab *home)])))
 
-      (mobile-header/header tab login?)
+      ;; (mobile-header/header tab login?)
 
       (mobile-left-sidebar/left-sidebar)
 

+ 2 - 4
src/main/mobile/components/search.cljs

@@ -3,6 +3,7 @@
   (:require [clojure.string :as string]
             [frontend.components.cmdk.core :as cmdk]
             [frontend.db.async :as db-async]
+            [frontend.handler.route :as route-handler]
             [frontend.handler.search :as search-handler]
             [frontend.search :as search]
             [frontend.state :as state]
@@ -141,10 +142,7 @@
              [:li.flex.gap-1
               {:on-click (fn []
                            (when-let [id (:block/uuid block)]
-                             (p/let [block (db-async/<get-block (state/get-current-repo) id
-                                                                {:children? false
-                                                                 :skip-refresh? true})]
-                               (when block (mobile-state/open-block-modal! block)))))}
+                             (route-handler/redirect-to-page! (str id))))}
               [:div.flex.flex-col.gap-1.py-1
                (when header
                  [:div.opacity-60.text-sm

+ 6 - 5
src/main/mobile/navigation.cljs

@@ -40,8 +40,9 @@
                              route (assoc :route route)
                              (or path (.-hash js/location))
                              (assoc :path (strip-fragment (or path (.-hash js/location))))))]
-      (-> (.routeDidChange mobile-util/ui-local payload)
-          (p/catch (fn [err]
-                     (log/warn :mobile-native-navigation/route-report-failed
-                               {:error err
-                                :payload payload})))))))
+      (when-not (= nav-type "pop")
+        (-> (.routeDidChange mobile-util/ui-local payload)
+            (p/catch (fn [err]
+                       (log/warn :mobile-native-navigation/route-report-failed
+                                 {:error err
+                                  :payload payload}))))))))

+ 6 - 1
src/main/mobile/routes.cljs

@@ -1,5 +1,6 @@
 (ns mobile.routes
-  (:require [frontend.components.page :as page]))
+  (:require [frontend.components.page :as page]
+            [mobile.components.search :as search]))
 
 (def routes
   [["/"
@@ -8,6 +9,10 @@
     {:name :page
      :view (fn [route-match]
              (page/page-cp route-match))}]
+   ["/search"
+    {:name :search
+     :view (fn []
+             (search/search))}]
    ["/graphs"
     {:name :graphs}]
    ["/import"