This commit is contained in:
Roman Volosovskyi 2016-06-14 15:56:03 +03:00
parent ab93b21a4c
commit 62e68fc3da
8 changed files with 183 additions and 123 deletions

View File

@ -7,26 +7,6 @@ status.command({
type: status.types.STRING type: status.types.STRING
}); });
function text(options, s) {
return ["text", options, s];
}
function view(options, elements) {
return ["view", options].concat(elements);
}
function image(options) {
return ["image", options];
}
function touchable(options, element) {
return ["touchable", options, element];
}
function scrollView(options, elements) {
return ["scroll-view", options].concat(elements);
}
var phones = [ var phones = [
{ {
number: "89171111111", number: "89171111111",
@ -100,18 +80,27 @@ function phoneSuggestions(params) {
} }
suggestions = ph.map(function (phone) { suggestions = ph.map(function (phone) {
return touchable( return status.components.touchable(
{onPress: [status.events.SET_VALUE, phone.number]}, {onPress: [status.events.SET_VALUE, phone.number]},
view(suggestionContainerStyle, status.components.view(suggestionContainerStyle,
[view(suggestionSubContainerStyle, [status.components.view(suggestionSubContainerStyle,
[ [
text({style: valueStyle}, phone.number), status.components.text(
text({style: descriptionStyle}, phone.description) {style: valueStyle},
phone.number
),
status.components.text(
{style: descriptionStyle},
phone.description
)
])]) ])])
); );
}); });
return scrollView(suggestionsContainerStyle(ph.length), suggestions); return status.components.scrollView(
suggestionsContainerStyle(ph.length),
suggestions
);
} }
status.response({ status.response({

View File

@ -57,6 +57,26 @@ function call(pathStr, paramsStr) {
return JSON.stringify(res); return JSON.stringify(res);
} }
function text(options, s) {
return ['text', options, s];
}
function view(options, elements) {
return ['view', options].concat(elements);
}
function image(options) {
return ['image', options];
}
function touchable(options, element) {
return ['touchable', options, element];
}
function scrollView(options, elements) {
return ['scroll-view', options].concat(elements);
}
var status = { var status = {
command: function (n, d, h) { command: function (n, d, h) {
var command = new Command(); var command = new Command();
@ -73,5 +93,12 @@ var status = {
}, },
events: { events: {
SET_VALUE: 'set-value' SET_VALUE: 'set-value'
},
components: {
view: view,
text: text,
image: image,
touchable: touchable,
scrollView: scrollView
} }
}; };

View File

@ -0,0 +1,57 @@
(ns status-im.commands.handlers.jail
(:require [re-frame.core :refer [register-handler after dispatch subscribe
trim-v debug]]
[status-im.utils.handlers :as u]
[status-im.utils.utils :refer [http-get toast]]
[status-im.components.jail :as j]
[status-im.components.react :refer [text scroll-view view
image touchable-highlight]]
[status-im.commands.utils :refer [json->cljs generate-hiccup
reg-handler]]))
(defn init-render-command!
[_ [chat-id command message-id data]]
(j/call chat-id [command :render] data
(fn [res]
(dispatch [::render-command chat-id message-id (json->cljs res)]))))
(defn render-command
[db [chat-id message-id markup]]
(let [hiccup (generate-hiccup markup)]
(assoc-in db [:rendered-commands chat-id message-id] hiccup)))
(def console-events
{:save-password #(dispatch [:save-password %])
:sign-up #(dispatch [:sign-up %])
:confirm-sign-up #(dispatch [:sign-up-confirm %])})
(def regular-events {})
(defn command-nadler!
[_ [{:keys [to]} response]]
(let [{:keys [event params]} (json->cljs response)
events (if (= "console" to)
(merge regular-events console-events)
regular-events)]
(when-let [handler (events (keyword event))]
(apply handler params))))
(defn suggestions-handler
[db [_ response-json]]
(let [response (json->cljs response-json)]
(println response)
(assoc db :current-suggestion (generate-hiccup response))))
(defn suggestions-events-handler!
[db [[n data]]]
(case (keyword n)
:set-value (dispatch [:set-chat-command-content data])
db))
(reg-handler :init-render-command! init-render-command!)
(reg-handler ::render-command render-command)
(reg-handler :command-handler! (u/side-effect! command-nadler!))
(reg-handler :suggestions-handler suggestions-handler)
(reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!))

View File

@ -1,21 +1,12 @@
(ns status-im.commands.handlers (ns status-im.commands.handlers.loading
(:require [re-frame.core :refer [register-handler after dispatch subscribe (:require [re-frame.core :refer [register-handler after dispatch subscribe
trim-v debug]] trim-v debug]]
[status-im.utils.handlers :as u] [status-im.utils.handlers :as u]
[status-im.components.react :as r]
[status-im.utils.utils :refer [http-get toast]] [status-im.utils.utils :refer [http-get toast]]
[clojure.string :as s] [clojure.string :as s]
[status-im.persistence.realm :as realm] [status-im.persistence.realm :as realm]
[status-im.components.jail :as j] [status-im.components.jail :as j]
[clojure.walk :as w] [status-im.commands.utils :refer [json->cljs reg-handler]]))
[status-im.components.react :refer [text scroll-view view
image touchable-highlight]]
[clojure.set :as set]))
(defn reg-handler
([name handler] (reg-handler name nil handler))
([name middleware handler]
(register-handler name [debug trim-v middleware] handler)))
(def commands-js "commands.js") (def commands-js "commands.js")
@ -50,11 +41,6 @@
;; todo tbd hashing algorithm ;; todo tbd hashing algorithm
(hash file)) (hash file))
(defn json->cljs [json]
(if (= json "undefined")
nil
(js->clj (.parse js/JSON json) :keywordize-keys true)))
(defn parse-commands! [_ [identity file]] (defn parse-commands! [_ [identity file]]
(j/parse identity file (j/parse identity file
(fn [result] (fn [result]
@ -93,76 +79,6 @@
(name reason) (name reason)
details])))) details]))))
(defn init-render-command!
[_ [chat-id command message-id data]]
(j/call chat-id [command :render] data
(fn [res]
(dispatch [::render-command chat-id message-id (json->cljs res)]))))
(def elements
{:text text
:view view
:scroll-view scroll-view
:image image
:touchable touchable-highlight})
(defn get-element [n]
(elements (keyword (.toLowerCase n))))
(def events #{:onPress})
(defn wrap-event [event]
#(dispatch [:suggestions-event! event]))
(defn check-events [m]
(let [ks (set (keys m))
evs (set/intersection ks events)]
(reduce #(update %1 %2 wrap-event) m evs)))
(defn generate-hiccup [markup]
;; todo implement validation
(w/prewalk
(fn [el]
(if (and (vector? el) (string? (first el)))
(-> el
(update 0 get-element)
(update 1 check-events))
el))
markup))
(defn render-command
[db [chat-id message-id markup]]
(let [hiccup (generate-hiccup markup)]
(assoc-in db [:rendered-commands chat-id message-id] hiccup)))
(def console-events
{:save-password #(dispatch [:save-password %])
:sign-up #(dispatch [:sign-up %])
:confirm-sign-up #(dispatch [:sign-up-confirm %])})
(def regular-events {})
(defn command-nadler!
[_ [{:keys [to]} response]]
(let [{:keys [event params]} (json->cljs response)
events (if (= "console" to)
(merge regular-events console-events)
regular-events)]
(when-let [handler (events (keyword event))]
(apply handler params))))
(defn suggestions-handler
[db [_ response-json]]
(let [response (json->cljs response-json)]
(println response)
(assoc db :current-suggestion (generate-hiccup response))))
(defn suggestions-events-handler!
[db [[n data]]]
(case (keyword n)
:set-value (dispatch [:set-chat-command-content data])
db))
(reg-handler :load-commands! (u/side-effect! load-commands!)) (reg-handler :load-commands! (u/side-effect! load-commands!))
(reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) (reg-handler ::fetch-commands! (u/side-effect! fetch-commands!))
@ -177,10 +93,3 @@
add-commands) add-commands)
(reg-handler ::loading-failed! (u/side-effect! loading-failed!)) (reg-handler ::loading-failed! (u/side-effect! loading-failed!))
(reg-handler :init-render-command! init-render-command!)
(reg-handler ::render-command render-command)
(reg-handler :command-handler! (u/side-effect! command-nadler!))
(reg-handler :suggestions-handler suggestions-handler)
(reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!))

View File

@ -0,0 +1,46 @@
(ns status-im.commands.utils
(:require [clojure.set :as set]
[clojure.walk :as w]
[re-frame.core :refer [register-handler dispatch trim-v debug]]))
(defn json->cljs [json]
(if (= json "undefined")
nil
(js->clj (.parse js/JSON json) :keywordize-keys true)))
(def elements
{:text text
:view view
:scroll-view scroll-view
:image image
:touchable touchable-highlight})
(defn get-element [n]
(elements (keyword (.toLowerCase n))))
(def events #{:onPress})
(defn wrap-event [event]
#(dispatch [:suggestions-event! event]))
(defn check-events [m]
(let [ks (set (keys m))
evs (set/intersection ks events)]
(reduce #(update %1 %2 wrap-event) m evs)))
(defn generate-hiccup [markup]
;; todo implement validation
(w/prewalk
(fn [el]
(if (and (vector? el) (string? (first el)))
(-> el
(update 0 get-element)
(update 1 check-events))
el))
markup))
(defn reg-handler
([name handler] (reg-handler name nil handler))
([name middleware handler]
(register-handler name [debug trim-v middleware] handler)))

View File

@ -61,6 +61,26 @@ function call(pathStr, paramsStr) {
return JSON.stringify(res); return JSON.stringify(res);
} }
function text(options, s) {
return ['text', options, s];
}
function view(options, elements) {
return ['view', options].concat(elements);
}
function image(options) {
return ['image', options];
}
function touchable(options, element) {
return ['touchable', options, element];
}
function scrollView(options, elements) {
return ['scroll-view', options].concat(elements);
}
var status = { var status = {
command: function (n, d, h) { command: function (n, d, h) {
var command = new Command(); var command = new Command();
@ -77,11 +97,22 @@ var status = {
}, },
events: { events: {
SET_VALUE: 'set-value' SET_VALUE: 'set-value'
},
components: {
view: view,
text: text,
image: image,
touchable: touchable,
scrollView: scrollView
} }
};") };")
(def jail (.-Jail (.-NativeModules r/react))) (def jail
(.init jail status-js) (when (exists? (.-NativeModules r/react))
(.-Jail (.-NativeModules r/react))))
(when jail
(.init jail status-js))
(defn parse [chat-id file callback] (defn parse [chat-id file callback]
(.parse jail chat-id file callback)) (.parse jail chat-id file callback))

View File

@ -16,7 +16,8 @@
status-im.new-group.handlers status-im.new-group.handlers
status-im.participants.handlers status-im.participants.handlers
status-im.protocol.handlers status-im.protocol.handlers
status-im.commands.handlers)) status-im.commands.handlers.loading
status-im.commands.handlers.jail))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;

View File

@ -1,6 +1,6 @@
(ns status-im.test.commands.handlers (ns status-im.test.commands.handlers
(:require [cljs.test :refer-macros [deftest is]] (:require [cljs.test :refer-macros [deftest is]]
[status-im.commands.handlers :as h])) [status-im.commands.handlers.loading :as h]))
(deftest test-validate-hash (deftest test-validate-hash
(let [file "some-js" (let [file "some-js"