-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cljs
150 lines (125 loc) · 4.49 KB
/
core.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(ns reagent-native.core
(:require [reagent.core :as r]
[cljs.spec.alpha :as s]
[clojure.string]
["react-native" :as ReactNative]))
(defn- split-on-dots
"Splits the keyword on dots.
If a string, does nothing. So:
:foo.bar -> '(\"foo\" \"bar\")
\"foo.bar\" -> '(\"foo.bar\")"
[kw]
(if (string? kw) '(kw)
(clojure.string/split (name kw) ".")))
(defn $
"Safely accesses a member of a js object in the react environment.
Does so in a way that won’t be mangled by the clojure compiler.
The member is either specified by a string or a keyword `name`. In
the latter case, the keyword may have inner periods, which directly
accesses subobjects.
If further arguments are specified, the member is assumed to be a
function, and is called on those arguments.
By default, the accessor stubs the result with the keyword name if
in the node environment. One can replace this stub with another by
adding the final opional `:stub` argument.
Example usages:
; in react
($ js/console :log \"Hello, world!\") ; outputs \"Hello, world!\"
($ js/exports :hot.accept :stub (fn [& args])) ; => module.hot.accept
(= 2 ($ #js {:foo 2} :foo)) ; true
; in node
($ js/console :log \"Hello, world!\") ; => :log
b
($ js/exports :hot.accept :stub (fn [& args])) ; => (fn [& args])
(= 2 ($ #js {:foo 2} :foo)) ; => :foo
"
[obj accessor & args]
(let [access-path (split-on-dots accessor)
item (apply aget obj access-path)
get? (zero? (count args))]
(if get?
item
(apply item args))))
(s/fdef $
:args (s/cat :obj any? :accessor ::string-or-kw :args (s/* any?))
:ret any?)
(def back-handler ($ ReactNative :BackHandler))
(defn on-back-press
[f]
(.addEventListener back-handler "hardwareBackPress" f))
(defn off-back-press
[f]
(.removeEventListener back-handler "hardwareBackPress" f))
(defn exit-app
[]
(.exitApp back-handler))
(def dimensions #(js->clj ($ ReactNative :Dimensions.get "window") :keywordize-keys true))
(def width #(:width (dimensions)))
(def height #(:height (dimensions)))
(def vw #(/ (width) 100))
(def vh #(/ (height) 100))
(def platform
(if (= "ios" ($ ReactNative :Platform.OS))
:ios
:android))
(defn r<-
[k]
(r/adapt-react-class ($ ReactNative k)))
(s/fdef r<-
:args (s/cat :accessor ::string-or-kw)
:ret any?)
;; React Native Components
(def activity-indicator (r<- :ActivityIndicator))
(def button (r<- :Button))
(def date-picker-ios (r<- :DatePickerIOS))
(def drawer-layout-android (r<- :DrawerLayoutAndroid))
(def flat-list (r<- :FlatList))
(def image (r<- :Image))
#_(def input-accessory-view (r<- :InputAccessoryView))
(def keyboard-avoiding-view (r<- :KeyboardAvoidingView))
(def list-view (r<- :ListView))
(def masked-view-ios (r<- :MaskedViewIOS))
(def modal (r<- :Modal))
(def navigator-ios (r<- :NavigatorIOS))
(def picker (r<- :Picker))
(def picker-item (r<- :Picker.Item))
(def picker-ios (r<- :PickerIOS))
(def progress-bar-android (r<- :ProgressBarAndroid))
(def progress-view-ios (r<- :ProgressViewIOS))
(def refresh-control (r<- :RefreshControl))
(def safe-area-view (r<- :SafeAreaView))
(def scroll-view (r<- :ScrollView))
(def section-list (r<- :SectionList))
(def segmented-control-ios (r<- :SegmentedControlIOS))
(def slider (r<- :Slider))
(def snapshot-view-ios (r<- :SnapshotViewIOS))
(def status-bar (r<- :StatusBar))
(def switch (r<- :Switch))
(def tab-bar-ios (r<- :TabBarIOS))
(def tab-bar-ios-item (r<- :TabBarIOS.Item))
(def view (r<- :View))
(def text-input (r<- :TextInput))
(def toolbar-android (r<- :ToolbarAndroid))
(def touchable-highlight (r<- :TouchableHighlight))
(def touchable-native-feedback
(r<- :TouchableNativeFeedback))
(def touchable-opacity (r<- :TouchableOpacity))
(def touchable-without-feedback
(r<- :TouchableWithoutFeedback))
(def text (r<- :Text))
(def view-pager-android (r<- :ViewPagerAndroid))
(def virtualized-list (r<- :VirtualizedList))
(def web-view (r<- :WebView))
(defonce counter (r/atom 1))
(defn reload!
"Forces react-native to redraw the component."
[] (swap! counter inc))
(defn register-component
"Registers a `component` with the app-registry as the given `name`.
Note, if you have a pure reagent app, you need to call this on your
main app component, with the name of your app."
[name component]
(let [reloading-component (fn [] @counter [component @counter])]
((aget ReactNative "AppRegistry" "registerComponent")
name
#(r/reactify-component reloading-component))))