Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active August 10, 2020 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivergeorge/981bc5135fa47253cba50fd125495d0b to your computer and use it in GitHub Desktop.
Save olivergeorge/981bc5135fa47253cba50fd125495d0b to your computer and use it in GitHub Desktop.
(rf/reg-fx ::navigate (fn [a] (navigation/dispatch (navigation/navigate-action a))))
(rf/reg-fx ::navigate2 (fn [[a b]] (navigation/navigate a b)))
(rf/reg-fx ::back (fn [] (navigation/dispatch (navigation/go-back-action))))
(rf/reg-fx ::reset-stack-by-key (fn [m] (navigation/dispatch (navigation/reset-action m))))
(rf/reg-fx ::pop (fn [n] (navigation/dispatch (navigation/pop-action n))))
(rf/reg-fx ::pop-to-top #(navigation/dispatch (navigation/pop-to-top-action)))
(defn app []
[rn/view {:style {:flex 1}}
[rn/status-bar {:barStyle "light-content"}]
[:> navigation/NavigationContainer
{:ref navigation/handle-ref}
[RootStackScreen]]])
(ns interop.navigation
(:require [react-native-gesture-handler]
["@react-navigation/native" :as react-navigation-native]
["@react-navigation/stack" :as react-navigation-stack]
["@react-navigation/bottom-tabs" :as bottom-tabs]
[goog.object :as gobject]
[cljs.spec.alpha :as s]
[interop.spec :as ios]))
(def NavigationContainer react-navigation-native/NavigationContainer)
(def createStackNavigator react-navigation-stack/createStackNavigator)
(def createBottomTabNavigator bottom-tabs/createBottomTabNavigator)
(defonce *navigator (atom nil))
(defonce *initial-state (atom nil))
(defn go-back-action [] (react-navigation-native/CommonActions.goBack))
(defn jump-to-action [{:keys [name params]}] (react-navigation-native/TabActions.jumpTo name (clj->js params)))
(defn pop-action
([] (react-navigation-native/StackActions.pop))
([n]
(ios/assert pos-int? n)
(react-navigation-native/StackActions.pop n)))
(defn pop-to-top-action [] (react-navigation-native/StackActions.popToTop))
(defn navigate-action [{:keys [name params] :as opts}]
(ios/assert string? name)
(react-navigation-native/CommonActions.navigate (clj->js opts)))
(defn reset-action
[m]
(ios/assert (s/keys :req-un [::index ::routes]) m)
(react-navigation-native/CommonActions.reset (clj->js m)))
(defn set-params [params] (.setParams @*navigator params))
(defn reset-root [s] (-> @*navigator (.resetRoot s)))
(defn dispatch [a] (.dispatch @*navigator a))
(defn navigate
[a b]
(ios/assert string? a)
(ios/assert map? b)
(.navigate @*navigator a (clj->js b)))
(defn can-go-back [] (.canGoBack @*navigator))
(defn get-root-state [] (some-> @*navigator (.getRootState)))
(defn get-current-route [] (.getCurrentRoute @*navigator))
(defn get-current-options [] (.getCurrentOptions @*navigator))
(defn handle-ref [ref]
(if ref
(do (reset! *navigator ref)
(some-> @*initial-state (reset-root)))
(do (reset! *initial-state (get-root-state))
(reset! *navigator ref))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment