From 34f1e2fa171db157cc4e5130c92c929105f4b85b Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 8 Jun 2016 15:14:35 +0300 Subject: [PATCH 01/26] fetch commands.js init Former-commit-id: 0f4581476d52eaf91a4dacd1d248b73c7d141c8b --- src/status_im/android/core.cljs | 1 - src/status_im/chat/handlers.cljs | 5 + src/status_im/chat/sign_up.cljs | 3 + src/status_im/chat/suggestions.cljs | 36 +++--- src/status_im/chat/views/suggestions.cljs | 47 ++++--- src/status_im/commands/handlers.cljs | 148 ++++++++++++++++++++++ src/status_im/handlers.cljs | 16 +-- src/status_im/models/commands.cljs | 6 - src/status_im/persistence/realm.cljs | 4 + src/status_im/utils/utils.cljs | 6 +- 10 files changed, 204 insertions(+), 68 deletions(-) create mode 100644 src/status_im/commands/handlers.cljs diff --git a/src/status_im/android/core.cljs b/src/status_im/android/core.cljs index 080a91fa54..c864b06ec6 100644 --- a/src/status_im/android/core.cljs +++ b/src/status_im/android/core.cljs @@ -56,7 +56,6 @@ (dispatch [:load-user-phone-number]) (dispatch [:load-contacts]) ;; load commands from remote server (todo: uncomment) - ;; (dispatch [:load-commands]) (dispatch [:init-console-chat]) (dispatch [:init-chat]) (init-back-button-handler!) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 0d0af3a2f3..253342baee 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -250,9 +250,14 @@ ([{:keys [messages current-chat-id] :as db} _] (assoc-in db [:chats current-chat-id :messages] messages))) +(defn load-commands! + [{:keys [current-chat-id]}] + (dispatch [:load-commands! current-chat-id])) + (register-handler :init-chat (-> load-messages! ((enrich init-chat)) + ((after load-commands!)) debug)) (defn initialize-chats diff --git a/src/status_im/chat/sign_up.cljs b/src/status_im/chat/sign_up.cljs index 9633d94464..0c59915e31 100644 --- a/src/status_im/chat/sign_up.cljs +++ b/src/status_im/chat/sign_up.cljs @@ -184,6 +184,9 @@ (def console-chat {:chat-id "console" :name "console" + ; todo remove/change dapp config fot console + :dapp-url "http://localhost:8185" + :dapp-hash 123 :color default-chat-color :group-chat false :is-active true diff --git a/src/status_im/chat/suggestions.cljs b/src/status_im/chat/suggestions.cljs index dcdc0d3db4..c5a0a8a53b 100644 --- a/src/status_im/chat/suggestions.cljs +++ b/src/status_im/chat/suggestions.cljs @@ -2,22 +2,29 @@ (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [status-im.db :as db] [status-im.models.commands :refer [commands - suggestions - get-commands - get-chat-command-request - get-chat-command-to-msg-id - clear-staged-commands]] + get-commands + get-chat-command-request + get-chat-command-to-msg-id + clear-staged-commands]] [status-im.utils.utils :refer [log on-error http-get]] [clojure.string :as s])) (defn suggestion? [text] (= (get text 0) "!")) -(defn get-suggestions [db text] - (if (suggestion? text) - ;; TODO change 'commands' to 'suggestions' - (filterv #(.startsWith (:text %) text) (get-commands db)) - [])) +(defn can-be-suggested? [text] + (fn [{:keys [name]}] + (.startsWith (str "!" name) text))) + +(defn get-suggestions + [{:keys [current-chat-id] :as db} text] + (let [commands (get-in db [:chats current-chat-id :commands])] + (if (suggestion? text) + ;; TODO change 'commands' to 'suggestions' + (->> commands + vals + (filter (can-be-suggested? text))) + []))) (defn get-command [db text] (when (suggestion? text) @@ -45,15 +52,6 @@ staged-commands)) (clear-staged-commands db))) -(defn execute-commands-js [body] - (.eval js/window body) - (let [commands (.-commands js/window)] - (dispatch [:set-commands (map #(update % :command keyword) - (js->clj commands :keywordize-keys true))]))) - -(defn load-commands [] - (http-get "chat-commands.js" execute-commands-js nil)) - (defn check-suggestion [db message] (when-let [suggestion-text (when (string? message) (re-matches #"^![^\s]+\s" message))] diff --git a/src/status_im/chat/views/suggestions.cljs b/src/status_im/chat/views/suggestions.cljs index 9a170cd5a5..039554d390 100644 --- a/src/status_im/chat/views/suggestions.cljs +++ b/src/status_im/chat/views/suggestions.cljs @@ -1,11 +1,12 @@ (ns status-im.chat.views.suggestions + (:require-macros [status-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch]] [status-im.components.react :refer [view - text - icon - touchable-highlight - list-view - list-item]] + text + icon + touchable-highlight + list-view + list-item]] [status-im.utils.listview :refer [to-datasource]] [status-im.chat.styles.suggestions :as st])) @@ -13,11 +14,11 @@ (dispatch [:set-chat-command command])) (defn suggestion-list-item - [{:keys [description command] - label :text + [{:keys [description] + label :name :as suggestion}] [touchable-highlight - {:onPress #(set-command-input (keyword command))} + {:onPress #(set-command-input (keyword label))} [view st/suggestion-container [view st/suggestion-sub-container [view (st/suggestion-background suggestion) @@ -28,19 +29,17 @@ (defn render-row [row _ _] (list-item [suggestion-list-item row])) -(defn suggestions-view [] - (let [suggestions-atom (subscribe [:get-suggestions])] - (fn [] - (let [suggestions @suggestions-atom] - (when (seq suggestions) - [view - [touchable-highlight {:style st/drag-down-touchable - :onPress (fn [] - ;; TODO hide suggestions? - )} - [view - [icon :drag_down st/drag-down-icon]]] - [view (st/suggestions-container (count suggestions)) - [list-view {:dataSource (to-datasource suggestions) - :enableEmptySections true - :renderRow render-row}]]]))))) +(defview suggestions-view [] + [suggestions [:get-suggestions]] + (when (seq suggestions) + [view + [touchable-highlight {:style st/drag-down-touchable + :onPress (fn [] + ;; TODO hide suggestions? + )} + [view + [icon :drag_down st/drag-down-icon]]] + [view (st/suggestions-container (count suggestions)) + [list-view {:dataSource (to-datasource suggestions) + :enableEmptySections true + :renderRow render-row}]]])) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs new file mode 100644 index 0000000000..7f8657d7b2 --- /dev/null +++ b/src/status_im/commands/handlers.cljs @@ -0,0 +1,148 @@ +(ns status-im.commands.handlers + (:require [re-frame.core :refer [register-handler after dispatch subscribe + trim-v debug]] + [status-im.utils.handlers :as u] + [status-im.components.react :as r] + [status-im.utils.utils :refer [http-get toast]] + [clojure.string :as s])) + +;; commands loading flow +; ┌────────────────────────────┐ +; │ user starts chat with dapp │ +; └────────────────────────────┘ +; │ +; ▼ +; ┌───────────────────────┐ +; │ fetch current │ +; │ `commands.js` hash │ +; └───────────────────────┘ +; │ +; ▼ +; ┌───────────────────────────┐ +; │try to fetch `commands.js` │ +; └───────────────────────────┘ +; │ +; ▼ +;┌───┐ ┌─────────────────────────┐ ┌───┐ +;│no ├────────│ there is `commands.js` │────┤yes│ +;└─┬─┘ └─────────────────────────┘ └─┬─┘ +; │ │ +; │ ▼ +; │ ┌───────────────────┐ +; │ ┌────┐ │ Is file's hash │ +; │ │nope├─────────────────│ equal to current? │ +; │ └─┬──┘ └───────────────────┘ +; │ │ │ +; │ │ ┌─┴─┐ +; │ │ │yes│ +; │ │ └─┬─┘ +; │ │ │ +; │ │ ▼ +; │ │ ┌────┐ ┌──────────────────────────┐ ┌────┐ +; │ │ │fail├──│ ask `otto` to handle js │──┤succ│ +; │ │ └──┬─┘ └──────────────────────────┘ └─┬──┘ +; │ │ │ │ +; │ │ │ ▼ +; │ │ │ ┌────────────────────────────┐ +; │ │ ▼ │ save commands │ +; │ │ ┌────────────────────────┐ │ save js ? │ +; │ │ │the dapp emit a message │ │ add some API object form │ +; │ └─▶│ saying js is broken │ │ otto to app-db │ +; │ └────────────────────────┘ └────────────────────────────┘ +; │ │ │ +; │ │ │ +; │ ┌───────────────────┘ ▼ +; │ │ ┌─────────────────────────┐ +; │ │ │ if it is necessary show │ +; │ ▼ │ as fullscreen │ +; │ ┌───────────┐ └─────────────────────────┘ +; │ │ │ │ +; └─▶│ Fin' │◀──────────────────────────────────────────┘ +; │ │ +; └───────────┘ + +(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") + +(defn fetch-commands! + [db [identity]] + (when-let [url (:dapp-url (get-in db [:chats identity]))] + (http-get (s/join "/" [url commands-js]) + #(dispatch [::validate-hash identity %]) + #(dispatch [::loading-failed! identity ::file-was-not-found])))) + +(defn dispatch-loaded! + [db [identity file]] + (if (::valid-hash db) + (dispatch [::parse-commands! identity file]) + (dispatch [::loading-failed! identity ::wrong-hash]))) + +(defn get-hash-by-dentity + [db identity] + (get-in db [:chats identity :dapp-hash])) + +(defn get-hash-by-file + [file] + ;; todo implement + 123) + +(defn get-jail [] + (.-Jail (.-NativeModules r/react))) + +(defn parse [file success-callback fail-callback] + (.parse (get-jail) file success-callback fail-callback)) + +(defn json->clj [json] + (js->clj (.parse js/JSON json) :keywordize-keys true)) + +(defn parse-commands! [_ [identity file]] + (parse file + (fn [result] + (let [commands (json->clj result)] + (dispatch [::add-commands identity commands]))) + #(dispatch [::loading-failed! identity ::error-in-jail %]))) + +(defn validate-hash + [db [identity file]] + (let [valid? (= (get-hash-by-dentity db identity) + (get-hash-by-file file))] + (assoc db ::valid-hash valid?))) + +(defn save-commands! + [db] + #_(realm/save (::new-commands db))) + +(defn add-commands + [db [id commands-obj]] + (let [commands (:commands commands-obj)] + (-> db + (update-in [:chats id :commands] merge commands) + (assoc db ::new-commands commands)))) + +(defn loading-failed + [db [id reason details]] + (let [url (get-in db [:chats id :dapp-url])] + (toast (s/join "\n" ["commands.js loading failed" + url + id + (name reason) + details])))) + +(reg-handler :load-commands! (u/side-effect! fetch-commands!)) + +(reg-handler ::validate-hash + (after dispatch-loaded!) + validate-hash) + +(reg-handler ::parse-commands! (u/side-effect! parse-commands!)) + +(reg-handler ::add-commands + (after save-commands!) + add-commands) + +(reg-handler ::loading-failed! (u/side-effect! loading-failed)) + diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 6aa74b5c5f..02d7d9e760 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -5,8 +5,6 @@ [status-im.db :refer [app-db schema]] [status-im.persistence.simple-kv-store :as kv] [status-im.protocol.state.storage :as storage] - [status-im.models.commands :refer [set-commands]] - [status-im.chat.suggestions :refer [load-commands]] [status-im.utils.logging :as log] [status-im.utils.crypt :refer [gen-random-bytes]] [status-im.utils.handlers :as u] @@ -17,7 +15,8 @@ status-im.discovery.handlers status-im.new-group.handlers status-im.participants.handlers - status-im.protocol.handlers)) + status-im.protocol.handlers + status-im.commands.handlers)) ;; -- Middleware ------------------------------------------------------------ ;; @@ -75,17 +74,6 @@ (fn [_ _] (log/debug "crypt initialized")))) -(register-handler :load-commands - (u/side-effect! - (fn [_ [action]] - (log/debug action) - (load-commands)))) - -(register-handler :set-commands - (fn [db [action commands]] - (log/debug action commands) - (set-commands db commands))) - ;; -- User data -------------------------------------------------------------- (register-handler :load-user-phone-number (fn [db [_]] diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index a9ba2ffd26..48e808bf12 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -64,12 +64,6 @@ ;; (get db :commands) commands) -(defn set-commands [db commands] - (assoc db :commands commands)) - -;; todo delete -(def suggestions (filterv :suggestion commands)) - (defn get-command [db command-key] (first (filter #(= command-key (:command %)) (get-commands db)))) diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index 5d304828fe..fa471355b1 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -51,6 +51,10 @@ :timestamp "int" :contacts {:type "list" :objectType "chat-contact"} + :dapp-url {:type :string + :optional true} + :dapp-hash {:type :int + :optional true} :last-msg-id "string"}} {:name :tag :primaryKey :name diff --git a/src/status_im/utils/utils.cljs b/src/status_im/utils/utils.cljs index c20a6c1e2a..b317dd0301 100644 --- a/src/status_im/utils/utils.cljs +++ b/src/status_im/utils/utils.cljs @@ -41,10 +41,8 @@ (toast (str error)))))))) (defn http-get - ([action on-success on-error] - (-> (.fetch js/window - (str const/server-address action) - (clj->js {:method "GET"})) + ([url on-success on-error] + (-> (.fetch js/window url (clj->js {:method "GET"})) (.then (fn [response] (log response) (.text response))) From 5f2d8e63c72b4a1af7aa4d257c85efaf64535dde Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 8 Jun 2016 15:15:23 +0300 Subject: [PATCH 02/26] jail module Former-commit-id: 32422310e81ee2190eeb11eb05738d4710dd1280 --- .../main/java/com/statusim/JailModule.java | 27 ++++++++++++++ .../main/java/com/statusim/JailPackage.java | 37 +++++++++++++++++++ .../main/java/com/statusim/MainActivity.java | 1 + 3 files changed, 65 insertions(+) create mode 100644 android/app/src/main/java/com/statusim/JailModule.java create mode 100644 android/app/src/main/java/com/statusim/JailPackage.java diff --git a/android/app/src/main/java/com/statusim/JailModule.java b/android/app/src/main/java/com/statusim/JailModule.java new file mode 100644 index 0000000000..eed3852cbb --- /dev/null +++ b/android/app/src/main/java/com/statusim/JailModule.java @@ -0,0 +1,27 @@ +package com.statusim; + +import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +public class JailModule extends ReactContextBaseJavaModule { + + public JailModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "Jail"; + } + + @ReactMethod + public void parse(String js, Callback succ, Callback fail) { + succ.invoke("{\"commands\":{\"request-money\":" + + "{\"description\":\"olala!\",\"name\":\"request-money\"," + + "\"params\":{\"foo\":{\"type\":\"string\"}}}}," + + "\"responses\":{}}"); + } +} + diff --git a/android/app/src/main/java/com/statusim/JailPackage.java b/android/app/src/main/java/com/statusim/JailPackage.java new file mode 100644 index 0000000000..c1c71d94f1 --- /dev/null +++ b/android/app/src/main/java/com/statusim/JailPackage.java @@ -0,0 +1,37 @@ +package com.statusim; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Created by rasom on 07.06.16. + */ +public class JailPackage implements ReactPackage { + + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } + + @Override + public List createNativeModules( + ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + + modules.add(new JailModule(reactContext)); + + return modules; + } +} diff --git a/android/app/src/main/java/com/statusim/MainActivity.java b/android/app/src/main/java/com/statusim/MainActivity.java index 3d411d9213..bbfa9caf05 100644 --- a/android/app/src/main/java/com/statusim/MainActivity.java +++ b/android/app/src/main/java/com/statusim/MainActivity.java @@ -76,6 +76,7 @@ public class MainActivity extends ReactActivity { protected List getPackages() { return Arrays.asList( new MainReactPackage(), + new JailPackage(), new RealmReactPackage(), new VectorIconsPackage(), new ReactNativeContacts(), From 2920984b0ce1aadf03ece702b10583d0bdb3ea43 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 9 Jun 2016 12:49:02 +0300 Subject: [PATCH 03/26] react-native-status dependency and other Former-commit-id: 76e2887b17fdc7321998280fe28895fdc314f6ea --- .re-natal | 5 ++- android/app/build.gradle | 1 + .../main/java/com/statusim/JailModule.java | 27 -------------- .../main/java/com/statusim/JailPackage.java | 37 ------------------- .../main/java/com/statusim/MainActivity.java | 1 + android/settings.gradle | 3 ++ package.json | 3 +- src/status_im/chat/sign_up.cljs | 2 +- src/status_im/commands/handlers.cljs | 5 ++- 9 files changed, 14 insertions(+), 70 deletions(-) delete mode 100644 android/app/src/main/java/com/statusim/JailModule.java delete mode 100644 android/app/src/main/java/com/statusim/JailPackage.java diff --git a/.re-natal b/.re-natal index 1455b14c43..f1c7bbd66b 100644 --- a/.re-natal +++ b/.re-natal @@ -1,7 +1,7 @@ { "name": "StatusIm", "interface": "reagent", - "androidHost": "localhost", + "androidHost": "10.0.3.2", "modules": [ "react-native-contacts", "react-native-invertible-scroll-view", @@ -16,7 +16,8 @@ "react-native-randombytes", "dismissKeyboard", "react-native-linear-gradient", - "react-native-android-sms-listener" + "react-native-android-sms-listener", + "react-native-status" ], "imageDirs": [ "images" diff --git a/android/app/build.gradle b/android/app/build.gradle index 379551061c..95beff7a12 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -130,6 +130,7 @@ dependencies { compile project(':react-native-i18n') compile project(':react-native-linear-gradient') compile project(':ReactNativeAndroidSmsListener') + compile project(':react-native-status') // compile(name:'geth', ext:'aar') compile(group: 'status-im', name: 'android-geth', version: '1.4.0-201604110816-a97a114', ext: 'aar') diff --git a/android/app/src/main/java/com/statusim/JailModule.java b/android/app/src/main/java/com/statusim/JailModule.java deleted file mode 100644 index eed3852cbb..0000000000 --- a/android/app/src/main/java/com/statusim/JailModule.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.statusim; - -import com.facebook.react.bridge.Callback; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.ReactMethod; - -public class JailModule extends ReactContextBaseJavaModule { - - public JailModule(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - public String getName() { - return "Jail"; - } - - @ReactMethod - public void parse(String js, Callback succ, Callback fail) { - succ.invoke("{\"commands\":{\"request-money\":" + - "{\"description\":\"olala!\",\"name\":\"request-money\"," + - "\"params\":{\"foo\":{\"type\":\"string\"}}}}," + - "\"responses\":{}}"); - } -} - diff --git a/android/app/src/main/java/com/statusim/JailPackage.java b/android/app/src/main/java/com/statusim/JailPackage.java deleted file mode 100644 index c1c71d94f1..0000000000 --- a/android/app/src/main/java/com/statusim/JailPackage.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.statusim; - -import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.JavaScriptModule; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Created by rasom on 07.06.16. - */ -public class JailPackage implements ReactPackage { - - @Override - public List> createJSModules() { - return Collections.emptyList(); - } - - @Override - public List createViewManagers(ReactApplicationContext reactContext) { - return Collections.emptyList(); - } - - @Override - public List createNativeModules( - ReactApplicationContext reactContext) { - List modules = new ArrayList<>(); - - modules.add(new JailModule(reactContext)); - - return modules; - } -} diff --git a/android/app/src/main/java/com/statusim/MainActivity.java b/android/app/src/main/java/com/statusim/MainActivity.java index bbfa9caf05..dc1a4cd324 100644 --- a/android/app/src/main/java/com/statusim/MainActivity.java +++ b/android/app/src/main/java/com/statusim/MainActivity.java @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.List; import java.util.Properties; import java.io.File; +import com.statusim.Jail.JailPackage; import com.i18n.reactnativei18n.ReactNativeI18n; import io.realm.react.RealmReactPackage; diff --git a/android/settings.gradle b/android/settings.gradle index 141db02933..a42e8ee4c2 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -18,3 +18,6 @@ include ':react-native-linear-gradient' project(':react-native-linear-gradient').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-linear-gradient/android') include ':ReactNativeAndroidSmsListener' project(':ReactNativeAndroidSmsListener').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-sms-listener/android') + +include ':react-native-status' +project(':react-native-status').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-status/android') diff --git a/package.json b/package.json index f325b8e9f3..e6ea438243 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "react-native-loading-spinner-overlay": "0.0.8", "react-native-randombytes": "^2.0.0", "react-native-vector-icons": "^1.3.4", - "realm": "^0.11.1" + "realm": "^0.11.1", + "react-native-status": "git+ssh://git@github.com/status-im/react-native-status" } } diff --git a/src/status_im/chat/sign_up.cljs b/src/status_im/chat/sign_up.cljs index 0c59915e31..b182354e15 100644 --- a/src/status_im/chat/sign_up.cljs +++ b/src/status_im/chat/sign_up.cljs @@ -186,7 +186,7 @@ :name "console" ; todo remove/change dapp config fot console :dapp-url "http://localhost:8185" - :dapp-hash 123 + :dapp-hash 834331894 :color default-chat-color :group-chat false :is-active true diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 7f8657d7b2..fc9e2a8ad3 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -87,8 +87,8 @@ (defn get-hash-by-file [file] - ;; todo implement - 123) + ;; todo tbd hashing algorithm + (hash file)) (defn get-jail [] (.-Jail (.-NativeModules r/react))) @@ -110,6 +110,7 @@ [db [identity file]] (let [valid? (= (get-hash-by-dentity db identity) (get-hash-by-file file))] + (println "hsh " (hash file)) (assoc db ::valid-hash valid?))) (defn save-commands! From 6a9d1af886d547ce2c96160c4a5f34a845180535 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 9 Jun 2016 14:41:25 +0300 Subject: [PATCH 04/26] store commands.js Former-commit-id: 72768fc7ddb430782b17b44b2b956f4828a3d2e9 --- src/status_im/commands/handlers.cljs | 31 ++++++++++++++++---------- src/status_im/navigation/handlers.cljs | 3 +-- src/status_im/persistence/realm.cljs | 12 +++++++++- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index fc9e2a8ad3..f7c713f5e5 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -4,7 +4,8 @@ [status-im.utils.handlers :as u] [status-im.components.react :as r] [status-im.utils.utils :refer [http-get toast]] - [clojure.string :as s])) + [clojure.string :as s] + [status-im.persistence.realm :as realm])) ;; commands loading flow ; ┌────────────────────────────┐ @@ -44,8 +45,8 @@ ; │ │ │ │ ; │ │ │ ▼ ; │ │ │ ┌────────────────────────────┐ -; │ │ ▼ │ save commands │ -; │ │ ┌────────────────────────┐ │ save js ? │ +; │ │ ▼ │ │ +; │ │ ┌────────────────────────┐ │ save commands-js │ ; │ │ │the dapp emit a message │ │ add some API object form │ ; │ └─▶│ saying js is broken │ │ otto to app-db │ ; │ └────────────────────────┘ └────────────────────────────┘ @@ -68,6 +69,12 @@ (def commands-js "commands.js") +(defn load-commands! + [_ [identity]] + (if-let [{:keys [file]} (realm/get-one-by-field :commands :chat-id identity)] + (dispatch [::parse-commands! identity file]) + (dispatch [::fetch-commands! identity]))) + (defn fetch-commands! [db [identity]] (when-let [url (:dapp-url (get-in db [:chats identity]))] @@ -103,27 +110,26 @@ (parse file (fn [result] (let [commands (json->clj result)] - (dispatch [::add-commands identity commands]))) + (dispatch [::add-commands identity file commands]))) #(dispatch [::loading-failed! identity ::error-in-jail %]))) (defn validate-hash [db [identity file]] (let [valid? (= (get-hash-by-dentity db identity) (get-hash-by-file file))] - (println "hsh " (hash file)) (assoc db ::valid-hash valid?))) -(defn save-commands! - [db] - #_(realm/save (::new-commands db))) - (defn add-commands - [db [id commands-obj]] + [db [id _ commands-obj]] (let [commands (:commands commands-obj)] (-> db (update-in [:chats id :commands] merge commands) (assoc db ::new-commands commands)))) +(defn save-commands-js! + [_ [id file]] + (realm/create-object :commands {:chat-id id :file file})) + (defn loading-failed [db [id reason details]] (let [url (get-in db [:chats id :dapp-url])] @@ -133,7 +139,8 @@ (name reason) details])))) -(reg-handler :load-commands! (u/side-effect! fetch-commands!)) +(reg-handler :load-commands! (u/side-effect! load-commands!)) +(reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) (reg-handler ::validate-hash (after dispatch-loaded!) @@ -142,7 +149,7 @@ (reg-handler ::parse-commands! (u/side-effect! parse-commands!)) (reg-handler ::add-commands - (after save-commands!) + (after save-commands-js!) add-commands) (reg-handler ::loading-failed! (u/side-effect! loading-failed)) diff --git a/src/status_im/navigation/handlers.cljs b/src/status_im/navigation/handlers.cljs index c47134e429..9e249c6186 100644 --- a/src/status_im/navigation/handlers.cljs +++ b/src/status_im/navigation/handlers.cljs @@ -1,6 +1,5 @@ (ns status-im.navigation.handlers - (:require [re-frame.core :refer [register-handler dispatch debug enrich - after]])) + (:require [re-frame.core :refer [register-handler dispatch debug enrich after]])) (defn push-view [db view-id] (-> db diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index fa471355b1..fd6e9857ad 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -1,7 +1,6 @@ (ns status-im.persistence.realm (:require [cljs.reader :refer [read-string]] [status-im.components.styles :refer [default-chat-color]] - [status-im.utils.logging :as log] [status-im.utils.types :refer [to-string]] [status-im.utils.utils :as u]) (:refer-clojure :exclude [exists?])) @@ -56,6 +55,10 @@ :dapp-hash {:type :int :optional true} :last-msg-id "string"}} + {:name :commands + :primaryKey :chat-id + :properties {:chat-id "string" + :file "string"}} {:name :tag :primaryKey :name :properties {:name "string" @@ -98,6 +101,10 @@ ([schema-name obj update?] (.create realm (to-string schema-name) (clj->js obj) update?))) +(defn create-object + [schema-name obj] + (write (fn [] (create schema-name obj true)))) + (defmulti to-query (fn [schema-name operator field value] operator)) @@ -162,3 +169,6 @@ (defn collection->map [collection] (-> (.map collection (fn [object _ _] object)) (js->clj :keywordize-keys true))) + +(defn get-one-by-field [schema-name field value] + (single-cljs (get-by-field schema-name field value))) From 75649d46aa4eda76e95bf06700d47d900c4b1328 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 9 Jun 2016 16:06:39 +0300 Subject: [PATCH 05/26] tests Former-commit-id: 84747296959ca9de8185c026967d61d12e60cace --- src/status_im/commands/handlers.cljs | 72 +++---------------- .../status_im/test/commands/handlers.cljs | 20 ++++++ test/cljs/status_im/test/runner.cljs | 6 +- 3 files changed, 32 insertions(+), 66 deletions(-) create mode 100644 test/cljs/status_im/test/commands/handlers.cljs diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index f7c713f5e5..66e2528ea5 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -7,61 +7,6 @@ [clojure.string :as s] [status-im.persistence.realm :as realm])) -;; commands loading flow -; ┌────────────────────────────┐ -; │ user starts chat with dapp │ -; └────────────────────────────┘ -; │ -; ▼ -; ┌───────────────────────┐ -; │ fetch current │ -; │ `commands.js` hash │ -; └───────────────────────┘ -; │ -; ▼ -; ┌───────────────────────────┐ -; │try to fetch `commands.js` │ -; └───────────────────────────┘ -; │ -; ▼ -;┌───┐ ┌─────────────────────────┐ ┌───┐ -;│no ├────────│ there is `commands.js` │────┤yes│ -;└─┬─┘ └─────────────────────────┘ └─┬─┘ -; │ │ -; │ ▼ -; │ ┌───────────────────┐ -; │ ┌────┐ │ Is file's hash │ -; │ │nope├─────────────────│ equal to current? │ -; │ └─┬──┘ └───────────────────┘ -; │ │ │ -; │ │ ┌─┴─┐ -; │ │ │yes│ -; │ │ └─┬─┘ -; │ │ │ -; │ │ ▼ -; │ │ ┌────┐ ┌──────────────────────────┐ ┌────┐ -; │ │ │fail├──│ ask `otto` to handle js │──┤succ│ -; │ │ └──┬─┘ └──────────────────────────┘ └─┬──┘ -; │ │ │ │ -; │ │ │ ▼ -; │ │ │ ┌────────────────────────────┐ -; │ │ ▼ │ │ -; │ │ ┌────────────────────────┐ │ save commands-js │ -; │ │ │the dapp emit a message │ │ add some API object form │ -; │ └─▶│ saying js is broken │ │ otto to app-db │ -; │ └────────────────────────┘ └────────────────────────────┘ -; │ │ │ -; │ │ │ -; │ ┌───────────────────┘ ▼ -; │ │ ┌─────────────────────────┐ -; │ │ │ if it is necessary show │ -; │ ▼ │ as fullscreen │ -; │ ┌───────────┐ └─────────────────────────┘ -; │ │ │ │ -; └─▶│ Fin' │◀──────────────────────────────────────────┘ -; │ │ -; └───────────┘ - (defn reg-handler ([name handler] (reg-handler name nil handler)) ([name middleware handler] @@ -88,7 +33,7 @@ (dispatch [::parse-commands! identity file]) (dispatch [::loading-failed! identity ::wrong-hash]))) -(defn get-hash-by-dentity +(defn get-hash-by-identity [db identity] (get-in db [:chats identity :dapp-hash])) @@ -115,22 +60,21 @@ (defn validate-hash [db [identity file]] - (let [valid? (= (get-hash-by-dentity db identity) + (let [valid? (= (get-hash-by-identity db identity) (get-hash-by-file file))] (assoc db ::valid-hash valid?))) (defn add-commands - [db [id _ commands-obj]] - (let [commands (:commands commands-obj)] - (-> db - (update-in [:chats id :commands] merge commands) - (assoc db ::new-commands commands)))) + [db [id _ {:keys [commands responses]}]] + (-> db + (update-in [:chats id :commands] merge commands) + (update-in [:chats id :responses] merge responses))) (defn save-commands-js! [_ [id file]] (realm/create-object :commands {:chat-id id :file file})) -(defn loading-failed +(defn loading-failed! [db [id reason details]] (let [url (get-in db [:chats id :dapp-url])] (toast (s/join "\n" ["commands.js loading failed" @@ -152,5 +96,5 @@ (after save-commands-js!) add-commands) -(reg-handler ::loading-failed! (u/side-effect! loading-failed)) +(reg-handler ::loading-failed! (u/side-effect! loading-failed!)) diff --git a/test/cljs/status_im/test/commands/handlers.cljs b/test/cljs/status_im/test/commands/handlers.cljs new file mode 100644 index 0000000000..92225b5f9e --- /dev/null +++ b/test/cljs/status_im/test/commands/handlers.cljs @@ -0,0 +1,20 @@ +(ns status-im.test.commands.handlers + (:require [cljs.test :refer-macros [deftest is]] + [status-im.commands.handlers :as h])) + +(deftest test-validate-hash + (let [file "some-js" + db (-> {} + (assoc-in [:chats :user :dapp-hash] -731917028) + (assoc-in [:chats :user2 :dapp-hash] 123))] + (is (::h/valid-hash (h/validate-hash db [:user file]))) + (is (not (::h/valid-hash (h/validate-hash db [:user2 file])))) + (is (not (::h/valid-hash (h/validate-hash db [:user3 file])))))) + +(deftest test-add-commands + (let [obj {:commands {:test {:name "name" + :description "desc"}} + :responses {:test-r {:name "r" + :description "desc-r"}}} + db (h/add-commands {} [:user nil obj])] + (is (= obj (select-keys (get-in db [:chats :user]) [:commands :responses]))))) diff --git a/test/cljs/status_im/test/runner.cljs b/test/cljs/status_im/test/runner.cljs index 14bda7de00..fef2b15ecf 100644 --- a/test/cljs/status_im/test/runner.cljs +++ b/test/cljs/status_im/test/runner.cljs @@ -1,5 +1,7 @@ (ns status-im.test.runner (:require [doo.runner :refer-macros [doo-tests]] - [status-im.test.handlers])) + [status-im.test.handlers] + [status-im.test.commands.handlers])) -(doo-tests 'status-im.test.handlers) +(doo-tests 'status-im.test.handlers + 'status-im.test.commands.handlers) From 5689bf787e6e157ef49801e7ad6bf168904a5dfa Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 9 Jun 2016 19:34:09 +0300 Subject: [PATCH 06/26] console commands/responses Former-commit-id: 61b6599bd37d8a2d9e6214993ca4ff2fb5e3a8bc --- src/status_im/chat/handlers.cljs | 4 ++-- src/status_im/chat/suggestions.cljs | 13 ++++------- src/status_im/chat/views/command.cljs | 2 +- src/status_im/chat/views/message.cljs | 2 +- src/status_im/chat/views/staged_command.cljs | 2 +- src/status_im/chat/views/suggestions.cljs | 23 ++++++++++--------- src/status_im/commands/handlers.cljs | 19 ++++++++++++++- src/status_im/contacts/handlers.cljs | 1 - .../handlers/content_suggestions.cljs | 2 +- src/status_im/models/commands.cljs | 20 ++++------------ 10 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 253342baee..4023473b83 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -68,7 +68,7 @@ (update-input-text db text)) (defn update-command [db [_ text]] - (let [{:keys [command]} (suggestions/check-suggestion db text)] + (let [[command] (suggestions/check-suggestion db text)] (commands/set-chat-command db command))) (register-handler :set-chat-input-text @@ -99,7 +99,7 @@ (defn prepare-message [{:keys [identity current-chat-id] :as db} _] (let [text (get-in db [:chats current-chat-id :input-text]) - {:keys [command]} (suggestions/check-suggestion db (str text " ")) + [command] (suggestions/check-suggestion db (str text " ")) message (check-author-direction db current-chat-id {:msg-id (random/id) diff --git a/src/status_im/chat/suggestions.cljs b/src/status_im/chat/suggestions.cljs index c5a0a8a53b..842a64ea32 100644 --- a/src/status_im/chat/suggestions.cljs +++ b/src/status_im/chat/suggestions.cljs @@ -20,10 +20,7 @@ [{:keys [current-chat-id] :as db} text] (let [commands (get-in db [:chats current-chat-id :commands])] (if (suggestion? text) - ;; TODO change 'commands' to 'suggestions' - (->> commands - vals - (filter (can-be-suggested? text))) + (filter (fn [[_ v]] ((can-be-suggested? text) v)) commands) []))) (defn get-command [db text] @@ -55,10 +52,10 @@ (defn check-suggestion [db message] (when-let [suggestion-text (when (string? message) (re-matches #"^![^\s]+\s" message))] - (let [suggestion-text' (s/trim suggestion-text) - [suggestion] (filter #(= suggestion-text' (:text %)) - (get-commands db))] - suggestion))) + (let [suggestion-text' (s/trim suggestion-text)] + (->> (get-commands db) + (filter #(= suggestion-text' (->> % second :name (str "!")))) + first)))) (defn typing-command? [db] (-> db diff --git a/src/status_im/chat/views/command.cljs b/src/status_im/chat/views/command.cljs index 2b550dc7bf..039f949c9b 100644 --- a/src/status_im/chat/views/command.cljs +++ b/src/status_im/chat/views/command.cljs @@ -32,7 +32,7 @@ [content-suggestions-view] [view st/command-input-container [view (st/command-text-container command) - [text {:style st/command-text} (:text command)]] + [text {:style st/command-text} (str "!" (:name command))]] [text-input (merge {:style st/command-input :autoFocus true :onChangeText set-input-message diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index ceb430fc76..0b6494acf8 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -73,7 +73,7 @@ (dispatch [:set-response-chat-command msg-id (:command command)])) (defn label [{:keys [command]}] - (->> (name command) + (->> (when command (name command)) (str "request-"))) (defn message-content-command-request diff --git a/src/status_im/chat/views/staged_command.cljs b/src/status_im/chat/views/staged_command.cljs index be8c811459..92f72b0508 100644 --- a/src/status_im/chat/views/staged_command.cljs +++ b/src/status_im/chat/views/staged_command.cljs @@ -16,7 +16,7 @@ [view st/staged-command-background [view st/staged-command-info-container [view (st/staged-command-text-container command) - [text {:style st/staged-command-text} (:text command)]] + [text {:style st/staged-command-text} (str "!" (:name command))]] [touchable-highlight {:style st/staged-command-cancel :onPress #(cancel-command-input staged-command)} [image {:source res/icon-close-gray diff --git a/src/status_im/chat/views/suggestions.cljs b/src/status_im/chat/views/suggestions.cljs index 039554d390..67c55ff148 100644 --- a/src/status_im/chat/views/suggestions.cljs +++ b/src/status_im/chat/views/suggestions.cljs @@ -14,17 +14,18 @@ (dispatch [:set-chat-command command])) (defn suggestion-list-item - [{:keys [description] - label :name - :as suggestion}] - [touchable-highlight - {:onPress #(set-command-input (keyword label))} - [view st/suggestion-container - [view st/suggestion-sub-container - [view (st/suggestion-background suggestion) - [text {:style st/suggestion-text} label]] - [text {:style st/value-text} label] - [text {:style st/description-text} description]]]]) + [[command {:keys [description] + name :name + :as suggestion}]] + (let [label (str "!" name)] + [touchable-highlight + {:onPress #(set-command-input command)} + [view st/suggestion-container + [view st/suggestion-sub-container + [view (st/suggestion-background suggestion) + [text {:style st/suggestion-text} label]] + [text {:style st/value-text} label] + [text {:style st/description-text} description]]]])) (defn render-row [row _ _] (list-item [suggestion-list-item row])) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 66e2528ea5..75811d9656 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -51,11 +51,28 @@ (defn json->clj [json] (js->clj (.parse js/JSON json) :keywordize-keys true)) +;; todo remove this +(def res {:commands {:location {:description "Send location" + :color "#9a5dcf" + :name "location"} + :phone {:description "Send phone number" + :color "#5fc48d" + :name "phone"} + :help {:description "Help" :color "#9a5dcf" :name "help"}} + :responses {:money {:description "Send money" :color "#5fc48d" :name "money"} + :confirmation-code {:description "Confirmation code" + :color "#7099e6" + :name "confirmationCode"} + :keypair-password {:description "" + :color "#7099e6" + :name "keypair-password"}}}) + (defn parse-commands! [_ [identity file]] (parse file (fn [result] (let [commands (json->clj result)] - (dispatch [::add-commands identity file commands]))) + ;; todo use commands from jail + (dispatch [::add-commands identity file res]))) #(dispatch [::loading-failed! identity ::error-in-jail %]))) (defn validate-hash diff --git a/src/status_im/contacts/handlers.cljs b/src/status_im/contacts/handlers.cljs index acb9be9b6c..2fd7e586c4 100644 --- a/src/status_im/contacts/handlers.cljs +++ b/src/status_im/contacts/handlers.cljs @@ -96,7 +96,6 @@ (remove #(identities (:whisper-identity %))) (map #(vector (:whisper-identity %) %)) (into {}))] - (println new-contacts') (-> db (update :contacts merge new-contacts') (assoc :new-contacts (vals new-contacts'))))) diff --git a/src/status_im/handlers/content_suggestions.cljs b/src/status_im/handlers/content_suggestions.cljs index f4457f931f..b8c193a8eb 100644 --- a/src/status_im/handlers/content_suggestions.cljs +++ b/src/status_im/handlers/content_suggestions.cljs @@ -14,7 +14,7 @@ (defn get-content-suggestions [db command text] (or (when command - (when-let [command-suggestions ((:command command) suggestions)] + (when-let [command-suggestions ((keyword (:name command)) suggestions)] (filterv (fn [s] (and (.startsWith (:value s) (or text "")) (not= (:value s) text))) diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 48e808bf12..84b53d47e5 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -35,16 +35,6 @@ :icon {:uri "icon_lock_gray"} :suggestion true :handler #(dispatch [:sign-up-confirm %])} - {:command :send - :text "!send" - :description (label :t/send-command-description) - :color "#9a5dcf" - :suggestion true} - {:command :request - :text "!request" - :description (label :t/request-command-description) - :color "#48ba30" - :suggestion true} {:command :keypair-password :text "!keypair-password" :description (label :t/keypair-password-command-description) @@ -59,13 +49,11 @@ :color "#9a5dcf" :suggestion true}]) -(defn get-commands [db] - ;; todo: temp. must be '(get db :commands)' - ;; (get db :commands) - commands) +(defn get-commands [{:keys [current-chat-id] :as db}] + (or (get-in db [:chats current-chat-id :commands]) {})) (defn get-command [db command-key] - (first (filter #(= command-key (:command %)) (get-commands db)))) + ((get-commands db) command-key)) (defn find-command [commands command-key] (first (filter #(= command-key (:command %)) commands))) @@ -126,4 +114,4 @@ (update content :command #(find-command commands (keyword %)))) (defn parse-command-request [commands content] - (update content :command #(find-command commands (keyword %)))) + (update content :command #((keyword %) commands))) From 195482f4314a2bc3e3de9e104bc6671e8e110dba Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 10 Jun 2016 15:17:46 +0300 Subject: [PATCH 07/26] commands/responses from loaded commands.js in chat Former-commit-id: 0319f9334c4de4dfef4e147ca4f7cbcb0b6b61c6 --- src/status_im/chat/handlers.cljs | 26 +++++----- src/status_im/chat/subs.cljs | 12 +++++ src/status_im/chat/views/message.cljs | 60 +++++++++++------------ src/status_im/chat/views/plain_input.cljs | 16 +++--- src/status_im/commands/handlers.cljs | 5 +- src/status_im/models/commands.cljs | 11 +++-- 6 files changed, 69 insertions(+), 61 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 4023473b83..98d9830521 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -102,22 +102,22 @@ [command] (suggestions/check-suggestion db (str text " ")) message (check-author-direction db current-chat-id - {:msg-id (random/id) - :chat-id current-chat-id - :content text - :to current-chat-id - :from identity - :content-type text-content-type - :outgoing true - :timestamp (time/now-ms)})] + {:msg-id (random/id) + :chat-id current-chat-id + :content text + :to current-chat-id + :from identity + :content-type text-content-type + :outgoing true + :timestamp (time/now-ms)})] (if command (commands/set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) message))))) (defn prepare-command [identity chat-id staged-command] - (let [command-key (get-in staged-command [:command :command]) - content {:command (name command-key) - :content (:content staged-command)}] + (let [command-name (get-in staged-command [:command :name]) + content {:command command-name + :content (:content staged-command)}] {:msg-id (random/id) :from identity :to chat-id @@ -301,9 +301,9 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (let [chat-id (or id current-chat-id) + (let [chat-id (or id current-chat-id) messages (get-in db [:chats chat-id :messages]) - db' (assoc db :current-chat-id chat-id)] + db' (assoc db :current-chat-id chat-id)] (if (seq messages) db' (-> db' diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index d5df5f5716..c9de7660e5 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -50,6 +50,18 @@ (fn [db _] (reaction (commands/get-commands @db)))) +(register-sub :get-responses + (fn [db _] + (let [current-chat (@db :current-chat-id)] + (reaction (or (get-in @db [:chats current-chat :responses]) {}))))) + +(register-sub :get-commands-and-responses + (fn [db _] + (let [current-chat (@db :current-chat-id)] + (reaction _ (or (->> (get-in @db [:chats current-chat]) + ((juxt :commands :responses)) + (apply merge)) {}))))) + (register-sub :get-chat-input-text (fn [db _] (->> [:chats (:current-chat-id @db) :input-text] diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 0b6494acf8..b1215423f2 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -1,18 +1,19 @@ (ns status-im.chat.views.message + (:require-macros [status-im.utils.views :refer [defview]]) (:require [clojure.string :as s] [re-frame.core :refer [subscribe dispatch]] [status-im.components.react :refer [view - text - image - touchable-highlight]] + text + image + touchable-highlight]] [status-im.chat.styles.message :as st] [status-im.models.commands :refer [parse-command-msg-content - parse-command-request]] + parse-command-request]] [status-im.resources :as res] [status-im.constants :refer [text-content-type - content-type-status - content-type-command - content-type-command-request]])) + content-type-status + content-type-command + content-type-command-request]])) (defn message-date [{:keys [date]}] [view {} @@ -51,7 +52,7 @@ [text {:style st/track-duration-text} "03:39"]]]) (defn message-content-command [content] - (let [commands-atom (subscribe [:get-commands])] + (let [commands-atom (subscribe [:get-commands-and-responses])] (fn [content] (let [commands @commands-atom {:keys [command content]} @@ -60,8 +61,8 @@ [view st/command-container [view (st/command-view command) [text {:style st/command-name} - (:text command)]]] - [image {:source (:icon command) + (str "!" (:name command))]]] + [image {:source {:uri (:icon command)} :style st/command-image}] [text {:style st/command-text} ;; TODO isn't smart @@ -70,34 +71,29 @@ content)]])))) (defn set-chat-command [msg-id command] - (dispatch [:set-response-chat-command msg-id (:command command)])) + (dispatch [:set-response-chat-command msg-id (keyword (:name command))])) (defn label [{:keys [command]}] (->> (when command (name command)) (str "request-"))) -(defn message-content-command-request +(defview message-content-command-request [{:keys [msg-id content from incoming-group]}] - (let [commands-atom (subscribe [:get-commands])] - (fn [{:keys [msg-id content from incoming-group]}] - (let [commands @commands-atom - {:keys [command content]} (parse-command-request commands content)] - [touchable-highlight {:onPress #(set-chat-command msg-id command) - :accessibility-label (label command)} - [view st/comand-request-view - [view st/command-request-message-view - (when incoming-group - [text {:style st/command-request-from-text} - from]) - [text {:style st/style-message-text} - content]] - [view (st/command-request-image-view command) - [image {:source (:request-icon command) - :style st/command-request-image}]] - (when (:request-text command) - [view st/command-request-text-view - [text {:style st/style-sub-text} - (:request-text command)]])]])))) + [commands [:get-responses]] + (let [{:keys [command content]} (parse-command-request commands content)] + [touchable-highlight {:onPress #(set-chat-command msg-id command) + :accessibility-label (label command)} + [view st/comand-request-view + [view st/command-request-message-view + (when incoming-group + [text {:style st/command-request-from-text} from]) + [text {:style st/style-message-text} content]] + [view (st/command-request-image-view command) + [image {:source {:uri (:icon command)} + :style st/command-request-image}]] + (when-let [request-text (:request-text command)] + [view st/command-request-text-view + [text {:style st/style-sub-text} request-text]])]])) (defn message-view [message content] diff --git a/src/status_im/chat/views/plain_input.cljs b/src/status_im/chat/views/plain_input.cljs index dcbc75780a..b932d53032 100644 --- a/src/status_im/chat/views/plain_input.cljs +++ b/src/status_im/chat/views/plain_input.cljs @@ -10,22 +10,18 @@ (defn set-input-message [message] (dispatch [:set-chat-input-text message])) -(defn send [chat input-message] - (let [{:keys [group-chat chat-id]} chat] - (dispatch [:send-chat-msg]))) +(defn send [] (dispatch [:send-chat-msg])) (defn message-valid? [staged-commands message] (or (and (pos? (count message)) (not= "!" message)) (pos? (count staged-commands)))) -(defn try-send [chat staged-commands message] - (when (message-valid? staged-commands message) - (send chat message))) +(defn try-send [staged-commands message] + (when (message-valid? staged-commands message) (send))) (defn plain-message-input-view [] - (let [chat (subscribe [:get-current-chat]) - input-message-atom (subscribe [:get-chat-input-text]) + (let [input-message-atom (subscribe [:get-chat-input-text]) staged-commands-atom (subscribe [:get-chat-staged-commands]) typing-command? (subscribe [:typing-command?])] (fn [] @@ -42,13 +38,13 @@ [text-input {:style st/message-input :autoFocus (pos? (count @staged-commands-atom)) :onChangeText set-input-message - :onSubmitEditing #(try-send @chat @staged-commands-atom + :onSubmitEditing #(try-send @staged-commands-atom input-message)} input-message] ;; TODO emoticons: not implemented [icon :smile st/smile-icon] (when (message-valid? @staged-commands-atom input-message) - [touchable-highlight {:on-press #(send @chat input-message) + [touchable-highlight {:on-press #(send) :accessibility-label :send-message} [view st/send-container [icon :send st/send-icon]]])]])))) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 75811d9656..9caf18473b 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -63,9 +63,10 @@ :confirmation-code {:description "Confirmation code" :color "#7099e6" :name "confirmationCode"} - :keypair-password {:description "" + :keypair-password {:description "Keypair password" :color "#7099e6" - :name "keypair-password"}}}) + :name "keypair-password" + :icon "http://localhost:8185/images/deliveryfailed.png"}}}) (defn parse-commands! [_ [identity file]] (parse file diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 84b53d47e5..896d53d2c7 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -52,8 +52,11 @@ (defn get-commands [{:keys [current-chat-id] :as db}] (or (get-in db [:chats current-chat-id :commands]) {})) -(defn get-command [db command-key] - ((get-commands db) command-key)) +(defn get-command [{:keys [current-chat-id] :as db} command-key] + ((or (->> (get-in db [:chats current-chat-id]) + ((juxt :commands :responses)) + (apply merge)) + {}) command-key)) (defn find-command [commands command-key] (first (filter #(= command-key (:command %)) commands))) @@ -74,7 +77,7 @@ [{:keys [current-chat-id] :as db} msg-id command-key] (update-in db [:chats current-chat-id :command-input] merge {:content nil - :command (get-command db command-key) + :command (merge (get-command db command-key)) :to-msg-id msg-id})) (defn set-chat-command [db command-key] @@ -111,7 +114,7 @@ #(assoc % msg-id handler))) (defn parse-command-msg-content [commands content] - (update content :command #(find-command commands (keyword %)))) + (update content :command #((keyword %) commands))) (defn parse-command-request [commands content] (update content :command #((keyword %) commands))) From 836ebc2a3044e1efe728fa752012e2ed4c4ac192 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 10 Jun 2016 16:08:21 +0300 Subject: [PATCH 08/26] resopnse icon in message Former-commit-id: 13a40fae93e4de115a28742a5b1a96f5c3c791c8 --- src/status_im/chat/styles/message.cljs | 16 ++++++++++++---- src/status_im/chat/views/message.cljs | 8 +++++--- src/status_im/commands/handlers.cljs | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/status_im/chat/styles/message.cljs b/src/status_im/chat/styles/message.cljs index 79e3cb24a7..2357e7e0f2 100644 --- a/src/status_im/chat/styles/message.cljs +++ b/src/status_im/chat/styles/message.cljs @@ -146,6 +146,17 @@ :borderRadius 50 :backgroundColor (:color command)}) +(defn command-image-view + [command] + {:position :absolute + :top 0 + :right 0 + :width 24 + :height 24 + :borderRadius 50 + :backgroundColor (:color command) + :alignItems :center}) + (def command-request-image {:position :absolute :top 9 @@ -178,12 +189,9 @@ :color color-white}) (def command-image - {:position :absolute - :top 4 - :right 0 + {:margin-top 5 :width 12 :height 13}) - (def command-text (merge style-message-text {:marginTop 8 diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index b1215423f2..93c0cec826 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -62,11 +62,13 @@ [view (st/command-view command) [text {:style st/command-name} (str "!" (:name command))]]] - [image {:source {:uri (:icon command)} - :style st/command-image}] + ;; todo doesn't reflect design + [view (st/command-image-view command) + [image {:source {:uri (:icon command)} + :style st/command-image}]] [text {:style st/command-text} ;; TODO isn't smart - (if (= (:command command) :keypair-password) + (if (= (:name command) "keypair-password") "******" content)]])))) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 9caf18473b..d413a3b011 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -66,7 +66,7 @@ :keypair-password {:description "Keypair password" :color "#7099e6" :name "keypair-password" - :icon "http://localhost:8185/images/deliveryfailed.png"}}}) + :icon "icon_lock_white"}}}) (defn parse-commands! [_ [identity file]] (parse file From f6da400ec8ed5a8f816d837b4e4c3d6ac45efae9 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 10 Jun 2016 17:04:12 +0300 Subject: [PATCH 09/26] jail component Former-commit-id: 3aef0f2d9cfccfa240f39da8d8f77f3fba4760f5 --- src/status_im/commands/handlers.cljs | 21 ++++++++------------- src/status_im/components/jail.cljs | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 src/status_im/components/jail.cljs diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index d413a3b011..9c43f0ab55 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -5,7 +5,8 @@ [status-im.components.react :as r] [status-im.utils.utils :refer [http-get toast]] [clojure.string :as s] - [status-im.persistence.realm :as realm])) + [status-im.persistence.realm :as realm] + [status-im.components.jail :as j])) (defn reg-handler ([name handler] (reg-handler name nil handler)) @@ -42,12 +43,6 @@ ;; todo tbd hashing algorithm (hash file)) -(defn get-jail [] - (.-Jail (.-NativeModules r/react))) - -(defn parse [file success-callback fail-callback] - (.parse (get-jail) file success-callback fail-callback)) - (defn json->clj [json] (js->clj (.parse js/JSON json) :keywordize-keys true)) @@ -69,12 +64,12 @@ :icon "icon_lock_white"}}}) (defn parse-commands! [_ [identity file]] - (parse file - (fn [result] - (let [commands (json->clj result)] - ;; todo use commands from jail - (dispatch [::add-commands identity file res]))) - #(dispatch [::loading-failed! identity ::error-in-jail %]))) + (j/parse identity file + (fn [result] + (let [commands (json->clj result)] + ;; todo use commands from jail + (dispatch [::add-commands identity file res]))) + #(dispatch [::loading-failed! identity ::error-in-jail %]))) (defn validate-hash [db [identity file]] diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs new file mode 100644 index 0000000000..23e904417b --- /dev/null +++ b/src/status_im/components/jail.cljs @@ -0,0 +1,15 @@ +(ns status-im.components.jail + (:require [status-im.components.react :as r])) + +(def jail (.-Jail (.-NativeModules r/react))) + +(defn parse [chat-id file success-callback fail-callback] + (.parse jail chat-id file success-callback fail-callback)) + +(defn call + [chat-id path params callback] + (.call jail chat-id (clj->js path) (clj->js params) callback)) + +(defn add-listener + [chat-id callback] + (.addListener jail chat-id callback)) From 4301c813f4a516f08f8257277bee4ab2fd5b1405 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 14:19:56 +0300 Subject: [PATCH 10/26] parsing of commands.js, command's handler, suggestions handler in jail (commit contains .aar oops) Former-commit-id: ab93b21a4c2d0ed573f1d588c537baec9e068e0a --- android/app/build.gradle | 6 +- .../libs/geth-android-16.aar.REMOVED.git-id | 1 + resources/commands.js | 176 ++++++++++++++++++ resources/status.js | 77 ++++++++ src/status_im/chat/handlers.cljs | 44 ++++- src/status_im/chat/sign_up.cljs | 6 +- .../chat/styles/content_suggestions.cljs | 27 ++- src/status_im/chat/suggestions.cljs | 3 +- .../chat/views/content_suggestions.cljs | 6 +- src/status_im/commands/handlers.cljs | 129 ++++++++++--- src/status_im/components/jail.cljs | 95 +++++++++- src/status_im/models/commands.cljs | 43 ----- 12 files changed, 505 insertions(+), 108 deletions(-) create mode 100644 android/app/libs/geth-android-16.aar.REMOVED.git-id create mode 100644 resources/commands.js create mode 100644 resources/status.js diff --git a/android/app/build.gradle b/android/app/build.gradle index 95beff7a12..edc54bc340 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -131,8 +131,9 @@ dependencies { compile project(':react-native-linear-gradient') compile project(':ReactNativeAndroidSmsListener') compile project(':react-native-status') -// compile(name:'geth', ext:'aar') - compile(group: 'status-im', name: 'android-geth', version: '1.4.0-201604110816-a97a114', ext: 'aar') + // todo replace this when jail will be integrated into geth + compile (name: "geth-android-16", ext:"aar") + //compile(group: 'status-im', name: 'android-geth', version: '1.4.0-201604110816-a97a114', ext: 'aar') compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } @@ -142,3 +143,4 @@ task copyDownloadableDepsToLibs(type: Copy) { from configurations.compile into 'libs' } + diff --git a/android/app/libs/geth-android-16.aar.REMOVED.git-id b/android/app/libs/geth-android-16.aar.REMOVED.git-id new file mode 100644 index 0000000000..ae91163656 --- /dev/null +++ b/android/app/libs/geth-android-16.aar.REMOVED.git-id @@ -0,0 +1 @@ +030bd1fbceae92a86d43251c4306e9113b8e51f4 \ No newline at end of file diff --git a/resources/commands.js b/resources/commands.js new file mode 100644 index 0000000000..bc0a456671 --- /dev/null +++ b/resources/commands.js @@ -0,0 +1,176 @@ +status.command({ + name: "location", + description: "Send location", + color: "#9a5dcf" +}).param({ + name: "address", + 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 = [ + { + number: "89171111111", + description: "Number format 1" + }, + { + number: "89371111111", + description: "Number format 1" + }, + { + number: "+79171111111", + description: "Number format 2" + }, + { + number: "9171111111", + description: "Number format 3" + } +]; + +function suggestionsContainerStyle(suggestionsCount) { + return { + marginVertical: 1, + marginHorizontal: 0, + height: Math.min(150, (56 * suggestionsCount)), + backgroundColor: "white", + borderRadius: 5 + }; +} + +var suggestionContainerStyle = { + paddingLeft: 16, + backgroundColor: "white" +}; + +var suggestionSubContainerStyle = { + height: 56, + borderBottomWidth: 1, + borderBottomColor: "#0000001f" +}; + +var valueStyle = { + marginTop: 9, + fontSize: 14, + fontFamily: "font", + color: "#000000de" +}; + +var descriptionStyle = { + marginTop: 1.5, + fontSize: 14, + fontFamily: "font", + color: "#838c93de" +}; + +function startsWith(str1, str2) { + return str1.lastIndexOf(str2, 0) == 0 && str1 != str2; +} + +function phoneSuggestions(params) { + var ph, suggestions; + if (!params.value || params.value == "") { + ph = phones; + } else { + ph = phones.filter(function (phone) { + return startsWith(phone.number, params.value); + }); + } + + if (ph.length == 0) { + return; + } + + suggestions = ph.map(function (phone) { + return touchable( + {onPress: [status.events.SET_VALUE, phone.number]}, + view(suggestionContainerStyle, + [view(suggestionSubContainerStyle, + [ + text({style: valueStyle}, phone.number), + text({style: descriptionStyle}, phone.description) + ])]) + ); + }); + + return scrollView(suggestionsContainerStyle(ph.length), suggestions); +} + +status.response({ + name: "phone", + description: "Send phone number", + color: "#5fc48d", + params: [{ + name: "phone", + type: status.types.PHONE_NUMBER, + suggestions: phoneSuggestions + }], + handler: function (params) { + return { + event: "sign-up", + params: [params.value] + }; + } +}); + +status.command({ + name: "help", + description: "Help", + color: "#9a5dcf", + params: [{ + name: "query", + type: status.types.STRING + }] +}); + +status.response({ + name: "confirmation-code", + color: "#7099e6", + description: "Confirmation code", + parameters: [{ + name: "code", + type: status.types.NUMBER + }], + handler: function (params) { + return { + event: "confirm-sign-up", + params: [params.value] + }; + } +}); + +status.response({ + name: "keypair", + color: "#7099e6", + description: "Keypair password", + icon: "icon_lock_white", + parameters: [{ + name: "password", + type: status.types.PASSWORD + }], + handler: function (params) { + return { + event: "save-password", + params: [params.value] + }; + } +}); + diff --git a/resources/status.js b/resources/status.js new file mode 100644 index 0000000000..e53cb7e174 --- /dev/null +++ b/resources/status.js @@ -0,0 +1,77 @@ +var _status_catalog = { + commands: {}, + responses: {} +}; + +function Command() { +} +function Response() { +} + +Command.prototype.addToCatalog = function () { + _status_catalog.commands[this.name] = this; +}; + +Command.prototype.param = function (parameter) { + this.params.push(parameter); + + return this; +}; + +Command.prototype.create = function (com) { + this.name = com.name; + this.description = com.description; + this.handler = com.handler; + this.color = com.color; + this.icon = com.icon; + this.params = com.params || []; + this.addToCatalog(); + + return this; +}; + + +Response.prototype = Object.create(Command.prototype); +Response.prototype.addToCatalog = function () { + _status_catalog.responses[this.name] = this; +}; +Response.prototype.onReceiveResponse = function (handler) { + this.onReceive = handler; +}; + +function call(pathStr, paramsStr) { + var params = JSON.parse(paramsStr), + path = JSON.parse(pathStr), + fn, res; + + fn = path.reduce(function (catalog, name) { + if (catalog && catalog[name]) { + return catalog[name]; + } + }, + _status_catalog + ); + + res = fn(params); + + return JSON.stringify(res); +} + +var status = { + command: function (n, d, h) { + var command = new Command(); + return command.create(n, d, h); + }, + response: function (n, d, h) { + var response = new Response(); + return response.create(n, d, h); + }, + types: { + STRING: 'string', + PHONE_NUMBER: 'phone-number', + PASSWORD: 'password' + }, + events: { + SET_VALUE: 'set-value' + } +}; diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 98d9830521..f6686ff3b9 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -16,7 +16,8 @@ [status-im.persistence.realm :as r] [status-im.handlers.server :as server] [status-im.utils.phone-number :refer [format-phone-number]] - [status-im.utils.datetime :as time])) + [status-im.utils.datetime :as time] + [status-im.components.jail :as j])) (register-handler :set-show-actions (fn [db [_ show-actions]] @@ -41,7 +42,24 @@ (assoc-in [:chats current-chat-id :command-input] {}) (update-in [:chats current-chat-id :input-text] safe-trim)))) +(defn invoke-suggestions-handler! + [{:keys [current-chat-id] :as db} _] + (let [commands (get-in db [:chats current-chat-id :commands]) + {:keys [command content]} (get-in db [:chats current-chat-id :command-input])] + (let [path [(if (commands command) :commands :responses) + (:name command) + :params + 0 + :suggestions] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:suggestions-handler {:command command + :content content} %]))))) + (register-handler :set-chat-command-content + (after invoke-suggestions-handler!) (fn [db [_ content]] (commands/set-chat-command-content db content))) @@ -55,11 +73,13 @@ {:keys [command content]} (get-in db [:chats current-chat-id :command-input]) command-info {:command command - :content content - :handler (:handler command)}] - (commands/stage-command db command-info)))) + :content content}] + (-> db + (commands/stage-command command-info) + (assoc :staged-command command-info))))) (register-handler :set-response-chat-command + (after invoke-suggestions-handler!) (fn [db [_ to-msg-id command-key]] (commands/set-response-chat-command db to-msg-id command-key))) @@ -179,6 +199,20 @@ (doseq [new-command new-commands] (messages/save-message current-chat-id (dissoc new-command :handler)))) +(defn invoke-commands-handlers! + [{:keys [new-commands current-chat-id] :as db}] + (let [commands (get-in db [:chats current-chat-id :commands])] + (doseq [{:keys [content] :as com} new-commands] + (let [{:keys [command content]} content + path [(if (commands command) :commands :responses) + command + :handler] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:command-handler! com %])))))) + (defn handle-commands [{:keys [new-commands]}] (doseq [{{content :content} :content @@ -196,6 +230,8 @@ ((after send-message!)) ((after save-message-to-realm!)) ((after save-commands-to-realm!)) + ;; todo maybe it is better to track if it was handled or not + ((after invoke-commands-handlers!)) ((after handle-commands)))) (register-handler :unstage-command diff --git a/src/status_im/chat/sign_up.cljs b/src/status_im/chat/sign_up.cljs index b182354e15..e4140eab16 100644 --- a/src/status_im/chat/sign_up.cljs +++ b/src/status_im/chat/sign_up.cljs @@ -173,7 +173,7 @@ (dispatch [:received-msg {:msg-id msg-id :content (command-content - :keypair-password + :keypair (label :t/keypair-generated)) :content-type content-type-command-request :outgoing false @@ -185,8 +185,8 @@ {:chat-id "console" :name "console" ; todo remove/change dapp config fot console - :dapp-url "http://localhost:8185" - :dapp-hash 834331894 + :dapp-url "http://localhost:8185/resources" + :dapp-hash 858845357 :color default-chat-color :group-chat false :is-active true diff --git a/src/status_im/chat/styles/content_suggestions.cljs b/src/status_im/chat/styles/content_suggestions.cljs index 198468a549..01b328fc78 100644 --- a/src/status_im/chat/styles/content_suggestions.cljs +++ b/src/status_im/chat/styles/content_suggestions.cljs @@ -1,22 +1,21 @@ (ns status-im.chat.styles.content-suggestions (:require [status-im.components.styles :refer [font - color-light-blue-transparent - color-white - color-black - color-blue - color-blue-transparent - selected-message-color - online-color - separator-color - text1-color - text2-color - text3-color]])) + color-light-blue-transparent + color-white + color-black + color-blue + color-blue-transparent + selected-message-color + online-color + separator-color + text1-color + text2-color + text3-color]])) (def suggestion-height 56) (def suggestion-container - {:flexDirection :column - :paddingLeft 16 + {:paddingLeft 16 :backgroundColor color-white}) (def suggestion-sub-container @@ -37,7 +36,7 @@ :color text2-color}) (defn suggestions-container [suggestions-count] - {:flexDirection :row + {:flex 1 :marginVertical 1 :marginHorizontal 0 :height (min 150 (* suggestion-height suggestions-count)) diff --git a/src/status_im/chat/suggestions.cljs b/src/status_im/chat/suggestions.cljs index 842a64ea32..73f90e884d 100644 --- a/src/status_im/chat/suggestions.cljs +++ b/src/status_im/chat/suggestions.cljs @@ -1,8 +1,7 @@ (ns status-im.chat.suggestions (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [status-im.db :as db] - [status-im.models.commands :refer [commands - get-commands + [status-im.models.commands :refer [get-commands get-chat-command-request get-chat-command-to-msg-id clear-staged-commands]] diff --git a/src/status_im/chat/views/content_suggestions.cljs b/src/status_im/chat/views/content_suggestions.cljs index 20fb1df9e5..1578f9484e 100644 --- a/src/status_im/chat/views/content_suggestions.cljs +++ b/src/status_im/chat/views/content_suggestions.cljs @@ -24,13 +24,11 @@ (list-item [suggestion-list-item row])) (defview content-suggestions-view [] - [suggestions [:get-content-suggestions]] + [suggestions [:get :current-suggestion]] (when (seq suggestions) [view [touchable-highlight {:style st/drag-down-touchable ;; TODO hide suggestions? :onPress (fn [])} [view [icon :drag_down st/drag-down-icon]]] - [view (st/suggestions-container (count suggestions)) - [list-view {:dataSource (to-datasource suggestions) - :renderRow render-row}]]])) + suggestions])) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 9c43f0ab55..8d63368e00 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -6,7 +6,11 @@ [status-im.utils.utils :refer [http-get toast]] [clojure.string :as s] [status-im.persistence.realm :as realm] - [status-im.components.jail :as j])) + [status-im.components.jail :as j] + [clojure.walk :as w] + [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)) @@ -17,9 +21,12 @@ (defn load-commands! [_ [identity]] - (if-let [{:keys [file]} (realm/get-one-by-field :commands :chat-id identity)] - (dispatch [::parse-commands! identity file]) - (dispatch [::fetch-commands! identity]))) + (dispatch [::fetch-commands! identity]) + ;; todo uncomment + #_(if-let [{:keys [file]} (realm/get-one-by-field :commands :chat-id + identity)] + (dispatch [::parse-commands! identity file]) + (dispatch [::fetch-commands! identity]))) (defn fetch-commands! [db [identity]] @@ -43,38 +50,28 @@ ;; todo tbd hashing algorithm (hash file)) -(defn json->clj [json] - (js->clj (.parse js/JSON json) :keywordize-keys true)) - -;; todo remove this -(def res {:commands {:location {:description "Send location" - :color "#9a5dcf" - :name "location"} - :phone {:description "Send phone number" - :color "#5fc48d" - :name "phone"} - :help {:description "Help" :color "#9a5dcf" :name "help"}} - :responses {:money {:description "Send money" :color "#5fc48d" :name "money"} - :confirmation-code {:description "Confirmation code" - :color "#7099e6" - :name "confirmationCode"} - :keypair-password {:description "Keypair password" - :color "#7099e6" - :name "keypair-password" - :icon "icon_lock_white"}}}) +(defn json->cljs [json] + (if (= json "undefined") + nil + (js->clj (.parse js/JSON json) :keywordize-keys true))) (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] - (let [commands (json->clj result)] + (let [commands (json->cljs result)] ;; todo use commands from jail - (dispatch [::add-commands identity file res]))) - #(dispatch [::loading-failed! identity ::error-in-jail %]))) + (dispatch [::add-commands identity file commands]))) + #_(dispatch [::loading-failed! identity ::error-in-jail %]))) (defn validate-hash [db [identity file]] - (let [valid? (= (get-hash-by-identity db identity) - (get-hash-by-file file))] + (let [valid? true + ;; todo check + #_(= (get-hash-by-identity db identity) + (get-hash-by-file file))] + (println :hash + (get-hash-by-identity db identity) + (get-hash-by-file file)) (assoc db ::valid-hash valid?))) (defn add-commands @@ -96,6 +93,76 @@ (name reason) 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 ::fetch-commands! (u/side-effect! fetch-commands!)) @@ -111,3 +178,9 @@ (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!)) diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 23e904417b..66b596619d 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -1,15 +1,94 @@ (ns status-im.components.jail (:require [status-im.components.react :as r])) +(def status-js + "var _status_catalog = { + commands: {}, + responses: {} +}; + +function Command() { +} +function Response() { +} + +Command.prototype.addToCatalog = function () { + _status_catalog.commands[this.name] = this; +}; + +Command.prototype.param = function (parameter) { + this.params.push(parameter); + + return this; +}; + +Command.prototype.create = function (com) { + this.name = com.name; + this.description = com.description; + this.handler = com.handler; + this.color = com.color; + this.icon = com.icon; + this.params = com.params || []; + this.addToCatalog(); + + return this; +}; + + +Response.prototype = Object.create(Command.prototype); +Response.prototype.addToCatalog = function () { + _status_catalog.responses[this.name] = this; +}; +Response.prototype.onReceiveResponse = function (handler) { + this.onReceive = handler; +}; + +function call(pathStr, paramsStr) { + var params = JSON.parse(paramsStr), + path = JSON.parse(pathStr), + fn, res; + + fn = path.reduce(function (catalog, name) { + if (catalog && catalog[name]) { + return catalog[name]; + } + }, + _status_catalog + ); + + res = fn(params); + + return JSON.stringify(res); +} + +var status = { + command: function (n, d, h) { + var command = new Command(); + return command.create(n, d, h); + }, + response: function (n, d, h) { + var response = new Response(); + return response.create(n, d, h); + }, + types: { + STRING: 'string', + PHONE_NUMBER: 'phone-number', + PASSWORD: 'password' + }, + events: { + SET_VALUE: 'set-value' + } +};") + (def jail (.-Jail (.-NativeModules r/react))) +(.init jail status-js) -(defn parse [chat-id file success-callback fail-callback] - (.parse jail chat-id file success-callback fail-callback)) +(defn parse [chat-id file callback] + (.parse jail chat-id file callback)) -(defn call - [chat-id path params callback] - (.call jail chat-id (clj->js path) (clj->js params) callback)) +(defn cljs->json [data] + (.stringify js/JSON (clj->js data))) -(defn add-listener - [chat-id callback] - (.addListener jail chat-id callback)) +(defn call [chat-id path params callback] + (println :call chat-id (cljs->json path) (cljs->json params)) + (.call jail chat-id (cljs->json path) (cljs->json params) callback)) diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 896d53d2c7..7da49a9b4b 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -6,49 +6,6 @@ [status-im.components.styles :refer [color-blue color-dark-mint]] [status-im.i18n :refer [label]])) -;; todo delete -(def commands [{:command :money - :text "!money" - :description (label :t/money-command-description) - :color color-dark-mint - :request-icon {:uri "icon_lock_white"} - :icon {:uri "icon_lock_gray"} - :suggestion true} - {:command :location - :text "!location" - :description (label :t/location-command-description) - :color "#9a5dcf" - :suggestion true} - {:command :phone - :text "!phone" - :description (label :t/phone-command-description) - :color color-dark-mint - :request-text (label :t/phone-request-text) - :suggestion true - :handler #(dispatch [:sign-up %])} - {:command :confirmation-code - :text "!confirmationCode" - :description (label :t/confirmation-code-command-description) - :request-text (label :t/confirmation-code-request-text) - :color color-blue - :request-icon {:uri "icon_lock_white"} - :icon {:uri "icon_lock_gray"} - :suggestion true - :handler #(dispatch [:sign-up-confirm %])} - {:command :keypair-password - :text "!keypair-password" - :description (label :t/keypair-password-command-description) - :color color-blue - :request-icon {:uri "icon_lock_white"} - :icon {:uri "icon_lock_gray"} - :suggestion false - :handler #(dispatch [:save-password %])} - {:command :help - :text "!help" - :description (label :t/help-command-description) - :color "#9a5dcf" - :suggestion true}]) - (defn get-commands [{:keys [current-chat-id] :as db}] (or (get-in db [:chats current-chat-id :commands]) {})) From b9973cee6d5b077827adc3b3eb83849c305b15c5 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 15:56:03 +0300 Subject: [PATCH 11/26] cleanup Former-commit-id: 62e68fc3da0c6edd1bcd820ab6d7157b66280a3e --- resources/commands.js | 41 +++----- resources/status.js | 27 ++++++ src/status_im/commands/handlers/jail.cljs | 57 +++++++++++ .../{handlers.cljs => handlers/loading.cljs} | 95 +------------------ src/status_im/commands/utils.cljs | 46 +++++++++ src/status_im/components/jail.cljs | 35 ++++++- src/status_im/handlers.cljs | 3 +- .../status_im/test/commands/handlers.cljs | 2 +- 8 files changed, 183 insertions(+), 123 deletions(-) create mode 100644 src/status_im/commands/handlers/jail.cljs rename src/status_im/commands/{handlers.cljs => handlers/loading.cljs} (52%) create mode 100644 src/status_im/commands/utils.cljs diff --git a/resources/commands.js b/resources/commands.js index bc0a456671..552d036ce5 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -7,26 +7,6 @@ status.command({ 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 = [ { number: "89171111111", @@ -100,18 +80,27 @@ function phoneSuggestions(params) { } suggestions = ph.map(function (phone) { - return touchable( + return status.components.touchable( {onPress: [status.events.SET_VALUE, phone.number]}, - view(suggestionContainerStyle, - [view(suggestionSubContainerStyle, + status.components.view(suggestionContainerStyle, + [status.components.view(suggestionSubContainerStyle, [ - text({style: valueStyle}, phone.number), - text({style: descriptionStyle}, phone.description) + status.components.text( + {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({ diff --git a/resources/status.js b/resources/status.js index e53cb7e174..e62559fae6 100644 --- a/resources/status.js +++ b/resources/status.js @@ -57,6 +57,26 @@ function call(pathStr, paramsStr) { 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 = { command: function (n, d, h) { var command = new Command(); @@ -73,5 +93,12 @@ var status = { }, events: { SET_VALUE: 'set-value' + }, + components: { + view: view, + text: text, + image: image, + touchable: touchable, + scrollView: scrollView } }; diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs new file mode 100644 index 0000000000..26192ec502 --- /dev/null +++ b/src/status_im/commands/handlers/jail.cljs @@ -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!)) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers/loading.cljs similarity index 52% rename from src/status_im/commands/handlers.cljs rename to src/status_im/commands/handlers/loading.cljs index 8d63368e00..3f8ce95762 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -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 trim-v debug]] [status-im.utils.handlers :as u] - [status-im.components.react :as r] [status-im.utils.utils :refer [http-get toast]] [clojure.string :as s] [status-im.persistence.realm :as realm] [status-im.components.jail :as j] - [clojure.walk :as w] - [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))) + [status-im.commands.utils :refer [json->cljs reg-handler]])) (def commands-js "commands.js") @@ -50,11 +41,6 @@ ;; todo tbd hashing algorithm (hash file)) -(defn json->cljs [json] - (if (= json "undefined") - nil - (js->clj (.parse js/JSON json) :keywordize-keys true))) - (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] @@ -93,76 +79,6 @@ (name reason) 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 ::fetch-commands! (u/side-effect! fetch-commands!)) @@ -177,10 +93,3 @@ add-commands) (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!)) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs new file mode 100644 index 0000000000..aa79a29eb4 --- /dev/null +++ b/src/status_im/commands/utils.cljs @@ -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))) diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 66b596619d..416968912f 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -61,6 +61,26 @@ function call(pathStr, paramsStr) { 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 = { command: function (n, d, h) { var command = new Command(); @@ -77,11 +97,22 @@ var status = { }, events: { SET_VALUE: 'set-value' + }, + components: { + view: view, + text: text, + image: image, + touchable: touchable, + scrollView: scrollView } };") -(def jail (.-Jail (.-NativeModules r/react))) -(.init jail status-js) +(def jail + (when (exists? (.-NativeModules r/react)) + (.-Jail (.-NativeModules r/react)))) + +(when jail + (.init jail status-js)) (defn parse [chat-id file callback] (.parse jail chat-id file callback)) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 02d7d9e760..bb48cec2d5 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -16,7 +16,8 @@ status-im.new-group.handlers status-im.participants.handlers status-im.protocol.handlers - status-im.commands.handlers)) + status-im.commands.handlers.loading + status-im.commands.handlers.jail)) ;; -- Middleware ------------------------------------------------------------ ;; diff --git a/test/cljs/status_im/test/commands/handlers.cljs b/test/cljs/status_im/test/commands/handlers.cljs index 92225b5f9e..cf356be4e2 100644 --- a/test/cljs/status_im/test/commands/handlers.cljs +++ b/test/cljs/status_im/test/commands/handlers.cljs @@ -1,6 +1,6 @@ (ns status-im.test.commands.handlers (:require [cljs.test :refer-macros [deftest is]] - [status-im.commands.handlers :as h])) + [status-im.commands.handlers.loading :as h])) (deftest test-validate-hash (let [file "some-js" From 8f37885e18dcaef6a0276e2175d1dd87185f6282 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 17:36:39 +0300 Subject: [PATCH 12/26] staged command preview Former-commit-id: 02f37e6c976db010c219bb1ba81e54d036f6ab0e --- resources/commands.js | 12 ++++++++ resources/status.js | 1 + src/status_im/chat/handlers.cljs | 31 +++++++++++++++----- src/status_im/chat/views/staged_command.cljs | 15 +++++----- src/status_im/commands/handlers/jail.cljs | 17 +++++++++-- src/status_im/commands/utils.cljs | 2 ++ src/status_im/components/jail.cljs | 1 + 7 files changed, 60 insertions(+), 19 deletions(-) diff --git a/resources/commands.js b/resources/commands.js index 552d036ce5..b4820adf51 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -160,6 +160,18 @@ status.response({ event: "save-password", params: [params.value] }; + }, + preview: function (params) { + return status.components.text( + { + style: { + marginTop: 5, + marginHorizontal: 0, + fontSize: 14, + fontFamily: "font", + color: "black" + } + }, "*****"); } }); diff --git a/resources/status.js b/resources/status.js index e62559fae6..c25535b159 100644 --- a/resources/status.js +++ b/resources/status.js @@ -25,6 +25,7 @@ Command.prototype.create = function (com) { this.color = com.color; this.icon = com.icon; this.params = com.params || []; + this.preview = com.preview; this.addToCatalog(); return this; diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index f6686ff3b9..2c5892a579 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -45,18 +45,18 @@ (defn invoke-suggestions-handler! [{:keys [current-chat-id] :as db} _] (let [commands (get-in db [:chats current-chat-id :commands]) - {:keys [command content]} (get-in db [:chats current-chat-id :command-input])] - (let [path [(if (commands command) :commands :responses) + {:keys [command content]} (get-in db [:chats current-chat-id :command-input]) + path [(if (commands command) :commands :responses) (:name command) :params 0 :suggestions] - params {:value content}] - (j/call current-chat-id - path - params - #(dispatch [:suggestions-handler {:command command - :content content} %]))))) + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:suggestions-handler {:command command + :content content} %])))) (register-handler :set-chat-command-content (after invoke-suggestions-handler!) @@ -67,7 +67,22 @@ [{:keys [current-chat-id] :as db} text] (assoc-in db [:chats current-chat-id :input-text] text)) + +(defn invoke-command-preview! + [{:keys [current-chat-id staged-command] :as db} _] + (let [commands (get-in db [:chats current-chat-id :commands]) + {:keys [command content]} staged-command + path [(if (commands command) :commands :responses) + (:name command) + :preview] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:command-preview current-chat-id %])))) + (register-handler :stage-command + (after invoke-command-preview!) (fn [{:keys [current-chat-id] :as db} _] (let [db (update-input-text db nil) {:keys [command content]} diff --git a/src/status_im/chat/views/staged_command.cljs b/src/status_im/chat/views/staged_command.cljs index 92f72b0508..424367c56e 100644 --- a/src/status_im/chat/views/staged_command.cljs +++ b/src/status_im/chat/views/staged_command.cljs @@ -1,9 +1,9 @@ (ns status-im.chat.views.staged-command (:require [re-frame.core :refer [subscribe dispatch]] [status-im.components.react :refer [view - image - text - touchable-highlight]] + image + text + touchable-highlight]] [status-im.resources :as res] [status-im.chat.styles.input :as st])) @@ -21,8 +21,7 @@ :onPress #(cancel-command-input staged-command)} [image {:source res/icon-close-gray :style st/staged-command-cancel-icon}]]] - [text {:style st/staged-command-content} - ;; TODO isn't smart - (if (= (:command command) :keypair-password) - "******" - (:content staged-command))]]])) + (if-let [preview (:preview staged-command)] + preview + [text {:style st/staged-command-content} + (:content staged-command)])]])) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 26192ec502..436f1d75cd 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -5,8 +5,6 @@ [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]])) @@ -40,13 +38,25 @@ (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]) + ;; todo show error? + nil)) + +(defn command-preview + [db [chat-id response-json]] + (if-let [response (json->cljs response-json)] + (let [path [:chats chat-id :staged-commands] + commands-cnt (count (get-in db path))] + ;; todo (dec commands-cnt) looks like hack have to find better way to + ;; do this + (update-in db (conj path (dec commands-cnt)) assoc + :preview (generate-hiccup response) + :preview-string (str response))) db)) (reg-handler :init-render-command! init-render-command!) @@ -55,3 +65,4 @@ (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!)) +(reg-handler :command-preview command-preview) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs index aa79a29eb4..bf4639f5dc 100644 --- a/src/status_im/commands/utils.cljs +++ b/src/status_im/commands/utils.cljs @@ -1,6 +1,8 @@ (ns status-im.commands.utils (:require [clojure.set :as set] [clojure.walk :as w] + [status-im.components.react :refer [text scroll-view view + image touchable-highlight]] [re-frame.core :refer [register-handler dispatch trim-v debug]])) (defn json->cljs [json] diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 416968912f..d58cf9e2c1 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -29,6 +29,7 @@ Command.prototype.create = function (com) { this.color = com.color; this.icon = com.icon; this.params = com.params || []; + this.preview = com.preview; this.addToCatalog(); return this; From 5d7005c64f24f9cb017af719d4070c00637504e9 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 18:09:24 +0300 Subject: [PATCH 13/26] store preview Former-commit-id: dfcf7cd47e63a0eaad02df0bc05ff561bafa0f72 --- src/status_im/chat/handlers.cljs | 29 +++++++++++++++------------ src/status_im/chat/views/message.cljs | 17 ++++++++-------- src/status_im/models/messages.cljs | 15 +++++++++----- src/status_im/persistence/realm.cljs | 4 +++- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 2c5892a579..d7d6b4cc60 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -17,7 +17,8 @@ [status-im.handlers.server :as server] [status-im.utils.phone-number :refer [format-phone-number]] [status-im.utils.datetime :as time] - [status-im.components.jail :as j])) + [status-im.components.jail :as j] + [status-im.commands.utils :refer [generate-hiccup]])) (register-handler :set-show-actions (fn [db [_ show-actions]] @@ -149,17 +150,18 @@ (commands/set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) message))))) -(defn prepare-command [identity chat-id staged-command] - (let [command-name (get-in staged-command [:command :name]) - content {:command command-name - :content (:content staged-command)}] - {:msg-id (random/id) - :from identity - :to chat-id - :content content - :content-type content-type-command - :outgoing true - :handler (:handler staged-command)})) +(defn prepare-command + [identity chat-id {:keys [preview preview-string content command]}] + (let [content {:command (command :name) + :content content}] + {:msg-id (random/id) + :from identity + :to chat-id + :content content + :content-type content-type-command + :outgoing true + :preview preview-string + :rendered-preview preview})) (defn prepare-staged-commans [{:keys [current-chat-id identity] :as db} _] @@ -212,7 +214,8 @@ (defn save-commands-to-realm! [{:keys [new-commands current-chat-id]} _] (doseq [new-command new-commands] - (messages/save-message current-chat-id (dissoc new-command :handler)))) + (messages/save-message current-chat-id + (dissoc new-command :rendered-preview)))) (defn invoke-commands-handlers! [{:keys [new-commands current-chat-id] :as db}] diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 93c0cec826..8f3ed5cb27 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -51,9 +51,9 @@ [view st/track-mark] [text {:style st/track-duration-text} "03:39"]]]) -(defn message-content-command [content] +(defn message-content-command [content preview] (let [commands-atom (subscribe [:get-commands-and-responses])] - (fn [content] + (fn [content preview] (let [commands @commands-atom {:keys [command content]} (parse-command-msg-content commands content)] @@ -66,11 +66,10 @@ [view (st/command-image-view command) [image {:source {:uri (:icon command)} :style st/command-image}]] - [text {:style st/command-text} - ;; TODO isn't smart - (if (= (:name command) "keypair-password") - "******" - content)]])))) + (if preview + preview + [text {:style st/command-text} + content])])))) (defn set-chat-command [msg-id command] (dispatch [:set-response-chat-command msg-id (keyword (:name command))])) @@ -126,9 +125,9 @@ [message-content-status message]) (defmethod message-content content-type-command - [wrapper {:keys [content] :as message}] + [wrapper {:keys [content rendered-preview] :as message}] [wrapper message - [message-view message [message-content-command content]]]) + [message-view message [message-content-command content rendered-preview]]]) (defmethod message-content :default [wrapper {:keys [content-type content] :as message}] diff --git a/src/status_im/models/messages.cljs b/src/status_im/models/messages.cljs index 5c8c17fa5f..0193217439 100644 --- a/src/status_im/models/messages.cljs +++ b/src/status_im/models/messages.cljs @@ -3,11 +3,11 @@ [re-frame.core :refer [dispatch]] [cljs.reader :refer [read-string]] [status-im.utils.random :refer [timestamp]] - [status-im.db :as db] [status-im.utils.logging :as log] [clojure.string :refer [join split]] [clojure.walk :refer [stringify-keys keywordize-keys]] - [status-im.constants :as c])) + [status-im.constants :as c] + [status-im.commands.utils :refer [generate-hiccup]])) (defn- map-to-str [m] @@ -21,7 +21,8 @@ {:outgoing false :to nil :same-author false - :same-direction false}) + :same-direction false + :preview nil}) (defn save-message ;; todo remove chat-id parameter @@ -51,9 +52,13 @@ (r/sorted :timestamp :asc) (r/collection->map)) (into '()) - (map (fn [{:keys [content-type] :as message}] + (map (fn [{:keys [content-type preview] :as message}] (if (command-type? content-type) - (update message :content str-to-map) + (-> message + (update :content str-to-map) + (assoc :rendered-preview (generate-hiccup + (read-string preview))) + (dissoc :preview)) message))))) (defn update-message! [{:keys [msg-id] :as msg}] diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index fd6e9857ad..8ed342537a 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -33,7 +33,9 @@ :delivery-status {:type "string" :optional true} :same-author "bool" - :same-direction "bool"}} + :same-direction "bool" + :preview {:type :string + :optional true}}} {:name :chat-contact :properties {:identity "string" :is-in-chat {:type "bool" From 9aa89420c61cdd0ca3ac85d619b906b30f6c3668 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 18:33:06 +0300 Subject: [PATCH 14/26] check empty :preview Former-commit-id: 3e60fd36a0f394cb218148cf4cb981e20763cf02 --- src/status_im/chat/views/message.cljs | 7 ++++--- src/status_im/models/messages.cljs | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 8f3ed5cb27..7bf6ee480e 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -63,9 +63,10 @@ [text {:style st/command-name} (str "!" (:name command))]]] ;; todo doesn't reflect design - [view (st/command-image-view command) - [image {:source {:uri (:icon command)} - :style st/command-image}]] + (when-let [icon (:icon command)] + [view (st/command-image-view command) + [image {:source {:uri icon} + :style st/command-image}]]) (if preview preview [text {:style st/command-text} diff --git a/src/status_im/models/messages.cljs b/src/status_im/models/messages.cljs index 0193217439..3d6611a887 100644 --- a/src/status_im/models/messages.cljs +++ b/src/status_im/models/messages.cljs @@ -56,8 +56,9 @@ (if (command-type? content-type) (-> message (update :content str-to-map) - (assoc :rendered-preview (generate-hiccup - (read-string preview))) + (assoc :rendered-preview (when preview + (generate-hiccup + (read-string preview)))) (dissoc :preview)) message))))) From 83a19a8817748f7f7617408761d02d0ff7c62eeb Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 15 Jun 2016 11:47:45 +0300 Subject: [PATCH 15/26] suggestions from jail Former-commit-id: 77a53cf0402d61b74e4bbd72dea6c11a88595b0d --- src/status_im/chat/handlers.cljs | 3 ++- src/status_im/chat/subs.cljs | 4 +--- src/status_im/chat/views/content_suggestions.cljs | 2 +- src/status_im/chat/views/request_message.cljs | 4 ++-- src/status_im/chat/views/response_suggestions.cljs | 5 +---- src/status_im/commands/handlers/jail.cljs | 4 ++-- 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index d66ab6440b..130635175a 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -61,7 +61,8 @@ path params #(dispatch [:suggestions-handler {:command command - :content content} %])))) + :content content + :chat-id current-chat-id} %])))) (register-handler :start-cancel-command (u/side-effect! diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index fc805b3183..2402bed67a 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -106,6 +106,4 @@ (register-sub :get-content-suggestions (fn [db _] - (let [command (reaction (commands/get-chat-command @db)) - text (reaction (commands/get-chat-command-content @db))] - (reaction (get-content-suggestions @command @text))))) + (reaction (get-in @db [:suggestions (:current-chat-id @db)])))) diff --git a/src/status_im/chat/views/content_suggestions.cljs b/src/status_im/chat/views/content_suggestions.cljs index bcf5103606..d2eb8b63b4 100644 --- a/src/status_im/chat/views/content_suggestions.cljs +++ b/src/status_im/chat/views/content_suggestions.cljs @@ -24,7 +24,7 @@ (list-item [suggestion-list-item row])) (defview content-suggestions-view [] - [suggestions [:get :current-suggestion]] + [suggestions [:get-content-suggestions]] (when (seq suggestions) [view st/container [touchable-highlight {:style st/drag-down-touchable diff --git a/src/status_im/chat/views/request_message.cljs b/src/status_im/chat/views/request_message.cljs index 907fc52311..0e5117769a 100644 --- a/src/status_im/chat/views/request_message.cljs +++ b/src/status_im/chat/views/request_message.cljs @@ -59,9 +59,9 @@ [touchable-highlight {:on-press (fn [] (reset! loop? false) (set-chat-command msg-id command)) - :style st/command-request-image-touchable + :style st/command-request-image-touchable} ;:accessibility-label (label command) - } + [animated-view {:style (st/command-request-image-view command scale-anim-val)} [image {:source {:uri (:icon command)} :style st/command-request-image}]]])}))) diff --git a/src/status_im/chat/views/response_suggestions.cljs b/src/status_im/chat/views/response_suggestions.cljs index 54eed3bb7d..bffe80175b 100644 --- a/src/status_im/chat/views/response_suggestions.cljs +++ b/src/status_im/chat/views/response_suggestions.cljs @@ -32,7 +32,4 @@ (defview response-suggestions-view [] [suggestions [:get-content-suggestions]] (when (seq suggestions) - [view st/suggestions-container - [list-view {:dataSource (to-datasource suggestions) - :keyboardShouldPersistTaps true - :renderRow render-row}]])) + suggestions)) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 436f1d75cd..2e45635869 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -36,9 +36,9 @@ (apply handler params)))) (defn suggestions-handler - [db [_ response-json]] + [db [{:keys [chat-id]} response-json]] (let [response (json->cljs response-json)] - (assoc db :current-suggestion (generate-hiccup response)))) + (assoc-in db [:suggestions chat-id] (generate-hiccup response)))) (defn suggestions-events-handler! [db [[n data]]] From 3c62b9224834e10ba2c21aaa6bca56cab91e3231 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 22 Jun 2016 12:56:43 +0300 Subject: [PATCH 16/26] move statusgo to artifactory Former-commit-id: c1e5a64836df78eed48a9f51fc5e2c15990cb8bb --- android/app/build.gradle | 4 +--- .../app/libs/geth-android-16.aar.REMOVED.git-id | 1 - .../src/main/java/com/statusim/GethService.java | 17 +++-------------- 3 files changed, 4 insertions(+), 18 deletions(-) delete mode 100644 android/app/libs/geth-android-16.aar.REMOVED.git-id diff --git a/android/app/build.gradle b/android/app/build.gradle index 4182c69686..2a8bf6a0c8 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -132,9 +132,7 @@ dependencies { compile project(':ReactNativeAndroidSmsListener') compile project(':react-native-camera') compile project(':react-native-status') - // todo replace this when jail will be integrated into geth - compile (name: "geth-android-16", ext:"aar") - //compile(group: 'status-im', name: 'android-geth', version: '1.4.0-201604110816-a97a114', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: '0.1.0-201606220917-ec4b9a', ext: 'aar') compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } diff --git a/android/app/libs/geth-android-16.aar.REMOVED.git-id b/android/app/libs/geth-android-16.aar.REMOVED.git-id deleted file mode 100644 index ae91163656..0000000000 --- a/android/app/libs/geth-android-16.aar.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -030bd1fbceae92a86d43251c4306e9113b8e51f4 \ No newline at end of file diff --git a/android/app/src/main/java/com/statusim/GethService.java b/android/app/src/main/java/com/statusim/GethService.java index 7967807cbd..54defce661 100644 --- a/android/app/src/main/java/com/statusim/GethService.java +++ b/android/app/src/main/java/com/statusim/GethService.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.os.Handler; import android.os.AsyncTask; import android.os.IBinder; -import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.support.annotation.Nullable; @@ -14,7 +13,7 @@ import android.os.Environment; import java.lang.ref.WeakReference; -import com.github.ethereum.go_ethereum.cmd.Geth; +import com.github.status_im.status_go.Statusgo; import java.io.File; @@ -78,20 +77,11 @@ public class GethService extends Service { extStore.getAbsolutePath() : getApplicationInfo().dataDir; - final Runnable addPeer = new Runnable() { - public void run() { - Log.w("Geth", "adding peer"); - Geth.run("--exec admin.addPeer(\"enode://e2f28126720452aa82f7d3083e49e6b3945502cb94d9750a15e27ee310eed6991618199f878e5fbc7dfa0e20f0af9554b41f491dc8f1dbae8f0f2d37a3a613aa@139.162.13.89:55555\") attach http://localhost:8545"); - } - }; - new Thread(new Runnable() { public void run() { - Geth.run("--shh --ipcdisable --nodiscover --rpc --rpcapi db,eth,net,web3,shh,admin --fast --datadir=" + dataFolder); + Statusgo.doStartNode(dataFolder); } }).start(); - - handler.postDelayed(addPeer, 5000); } @Nullable @@ -103,8 +93,7 @@ public class GethService extends Service { @Override public void onCreate() { super.onCreate(); - System.loadLibrary("gethraw"); - System.loadLibrary("geth"); + System.loadLibrary("statusgo"); if (!isGethInitialized) { isGethInitialized = true; From 6e20f11ed8788fa4e62e6da12d497a0f7471d9a4 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 23 Jun 2016 15:47:58 +0300 Subject: [PATCH 17/26] adapt animation Former-commit-id: 831132ccef8f9280f0f2601f540894d2469723ba --- src/status_im/chat/constants.cljs | 6 +++ src/status_im/chat/handlers.cljs | 17 +++---- src/status_im/chat/handlers/animation.cljs | 49 +++++++++---------- src/status_im/chat/screen.cljs | 1 - src/status_im/chat/styles/message_input.cljs | 5 +- src/status_im/chat/styles/response.cljs | 6 +-- src/status_im/chat/subs.cljs | 8 +-- .../chat/views/content_suggestions.cljs | 34 ------------- src/status_im/chat/views/plain_message.cljs | 22 +++++---- src/status_im/chat/views/response.cljs | 4 +- .../chat/views/response_suggestions.cljs | 32 +----------- src/status_im/commands/handlers/jail.cljs | 6 ++- src/status_im/commands/handlers/loading.cljs | 3 -- src/status_im/commands/utils.cljs | 2 +- src/status_im/components/jail.cljs | 2 +- src/status_im/handlers.cljs | 1 + 16 files changed, 66 insertions(+), 132 deletions(-) create mode 100644 src/status_im/chat/constants.cljs delete mode 100644 src/status_im/chat/views/content_suggestions.cljs diff --git a/src/status_im/chat/constants.cljs b/src/status_im/chat/constants.cljs new file mode 100644 index 0000000000..e9d95a4ed9 --- /dev/null +++ b/src/status_im/chat/constants.cljs @@ -0,0 +1,6 @@ +(ns status-im.chat.constants) + +(def input-height 56) +(def request-info-height 61) +(def response-height-normal 211) +(def minimum-suggestion-height (+ input-height request-info-height)) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 451a4f32ec..c1627a957e 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -3,7 +3,6 @@ [status-im.models.commands :as commands] [clojure.string :as str] [status-im.components.styles :refer [default-chat-color]] - [status-im.chat.styles.response :refer [request-info-height response-height-normal]] [status-im.chat.suggestions :as suggestions] [status-im.protocol.api :as api] [status-im.models.messages :as messages] @@ -20,9 +19,7 @@ [status-im.utils.phone-number :refer [format-phone-number]] [status-im.utils.datetime :as time] [status-im.components.jail :as j] - [status-im.commands.utils :refer [generate-hiccup]] - [status-im.chat.handlers.animation :refer [update-response-height - get-response-height]])) + [status-im.commands.utils :refer [generate-hiccup]])) (register-handler :set-show-actions (fn [db [_ show-actions]] @@ -74,16 +71,12 @@ (fn [{:keys [current-chat-id] :as db} [_ content]] (as-> db db (commands/set-chat-command-content db content) - (assoc-in db [:chats current-chat-id :input-text] nil) - (if (commands/get-chat-command-to-msg-id db) - (update-response-height db) - db)))) + (assoc-in db [:chats current-chat-id :input-text] nil)))) (defn update-input-text [{:keys [current-chat-id] :as db} text] (assoc-in db [:chats current-chat-id :input-text] text)) - (defn invoke-command-preview! [{:keys [current-chat-id staged-command] :as db} _] (let [commands (get-in db [:chats current-chat-id :commands]) @@ -123,7 +116,8 @@ (register-handler :set-response-chat-command [(after invoke-suggestions-handler!) (after #(dispatch [:command-edit-mode])) - (after #(dispatch [:animate-show-response]))] + ;(after #(dispatch [:animate-show-response])) + ] (fn [db [_ to-msg-id command-key]] (commands/set-response-chat-command db to-msg-id command-key))) @@ -304,7 +298,8 @@ (register-handler :set-chat-command [(after #(dispatch [:command-edit-mode])) - (after #(dispatch [:animate-show-response]))] + ;(after #(dispatch [:animate-show-response])) + ] (fn [db [_ command-key]] (commands/set-chat-command db command-key))) diff --git a/src/status_im/chat/handlers/animation.cljs b/src/status_im/chat/handlers/animation.cljs index 994a32e85f..2bd08c7a3b 100644 --- a/src/status_im/chat/handlers/animation.cljs +++ b/src/status_im/chat/handlers/animation.cljs @@ -1,14 +1,15 @@ (ns status-im.chat.handlers.animation - (:require [re-frame.core :refer [register-handler after dispatch]] + (:require [re-frame.core :refer [register-handler after dispatch debug]] [re-frame.middleware :refer [path]] [status-im.models.commands :as commands] [status-im.handlers.content-suggestions :refer [get-content-suggestions]] - [status-im.chat.styles.message-input :refer [input-height]] - [status-im.chat.styles.response :refer [request-info-height response-height-normal]] + [status-im.chat.constants :refer [input-height request-info-height + response-height-normal minimum-suggestion-height]] [status-im.chat.styles.response-suggestions :as response-suggestions-styles] [status-im.constants :refer [response-input-hiding-duration]])) -(def zero-height input-height) +;; todo magic value +(def middle-height 270) (defn animation-handler ([name handler] (animation-handler name nil handler)) @@ -19,25 +20,21 @@ (after #(dispatch [:text-edit-mode])) (fn [db _] (assoc db - :to-response-height zero-height + :to-response-height input-height :messages-offset 0))) -(defn get-response-height [db] - (let [command (commands/get-chat-command db) - text (commands/get-chat-command-content db) - suggestions (get-content-suggestions command text) - suggestions-height (reduce + 0 (map #(if (:header %) - response-suggestions-styles/header-height - response-suggestions-styles/suggestion-height) - suggestions))] - (+ zero-height +(defn get-response-height + [{:keys [current-chat-id] :as db}] + (let [suggestions (get-in db [:suggestions current-chat-id]) + suggestions-height (if suggestions middle-height 0)] + (+ input-height (min response-height-normal (+ suggestions-height request-info-height))))) (defn update-response-height [db] (assoc-in db [:animations :to-response-height] (get-response-height db))) (register-handler :animate-show-response - (after #(dispatch [:command-edit-mode])) + [(after #(dispatch [:command-edit-mode]))] (fn [db _] (-> db (assoc-in [:animations :messages-offset] request-info-height) @@ -54,26 +51,28 @@ db)))) (register-handler :fix-response-height - (fn [db [_ vy current]] + (fn [{:keys [current-chat-id] :as db} [_ vy current]] (let [max-height (get-in db [:animations :response-height-max]) - ;; todo magic value - middle 270 moving-down? (pos? vy) moving-up? (not moving-down?) - under-middle-position? (<= current middle) + under-middle-position? (<= current middle-height) over-middle-position? (not under-middle-position?) - min-height (+ zero-height request-info-height) - new-fixed (cond (and under-middle-position? moving-down?) - min-height + suggestions (get-in db [:suggestions current-chat-id]) + new-fixed (cond (not suggestions) + minimum-suggestion-height (and under-middle-position? moving-up?) - middle + middle-height (and over-middle-position? moving-down?) - middle + middle-height (and over-middle-position? moving-up?) - max-height)] + max-height + + (and under-middle-position? + moving-down?) + minimum-suggestion-height)] (-> db (assoc-in [:animations :to-response-height] new-fixed) (update-in [:animations :response-height-changed] inc))))) diff --git a/src/status_im/chat/screen.cljs b/src/status_im/chat/screen.cljs index 4927858f1e..f9394aeb5a 100644 --- a/src/status_im/chat/screen.cljs +++ b/src/status_im/chat/screen.cljs @@ -18,7 +18,6 @@ [status-im.components.invertible-scroll-view :refer [invertible-scroll-view]] [status-im.components.toolbar :refer [toolbar]] [status-im.chat.views.message :refer [chat-message]] - [status-im.chat.views.content-suggestions :refer [content-suggestions-view]] [status-im.chat.views.suggestions :refer [suggestions-view]] [status-im.chat.views.response :refer [response-view]] [status-im.chat.views.new-message :refer [chat-message-new]] diff --git a/src/status_im/chat/styles/message_input.cljs b/src/status_im/chat/styles/message_input.cljs index ccde0a7994..e999f749a9 100644 --- a/src/status_im/chat/styles/message_input.cljs +++ b/src/status_im/chat/styles/message_input.cljs @@ -1,8 +1,7 @@ (ns status-im.chat.styles.message-input (:require [status-im.components.styles :refer [color-white - color-blue]])) - -(def input-height 56) + color-blue]] + [status-im.chat.constants :refer [input-height]])) (def message-input-container {:flex 1 diff --git a/src/status_im/chat/styles/response.cljs b/src/status_im/chat/styles/response.cljs index 918bd28eec..8963f8a1fe 100644 --- a/src/status_im/chat/styles/response.cljs +++ b/src/status_im/chat/styles/response.cljs @@ -6,10 +6,8 @@ text2-color chat-background color-black]] - [status-im.chat.styles.message-input :refer [input-height]])) - -(def response-height-normal 211) -(def request-info-height 61) + [status-im.chat.constants :refer [input-height request-info-height + response-height-normal]])) (def drag-container {:height 16 diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 36da63fe4f..20bcf239ce 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -1,15 +1,15 @@ (ns status-im.chat.subs (:require-macros [reagent.ratom :refer [reaction]]) - (:require [re-frame.core :refer [register-sub dispatch subscribe]] + (:require [re-frame.core :refer [register-sub dispatch subscribe path]] [status-im.db :as db] - ;todo handlers in subs?... [status-im.chat.suggestions :refer [get-suggestions typing-command?]] [status-im.models.commands :as commands] [status-im.constants :refer [response-suggesstion-resize-duration]] [status-im.handlers.content-suggestions :refer [get-content-suggestions]] [status-im.chat.views.plain-message :as plain-message] - [status-im.chat.views.command :as command])) + [status-im.chat.views.command :as command] + [status-im.chat.constants :as c])) (register-sub :chat-properties (fn [db [_ properties]] @@ -123,7 +123,7 @@ (reaction (get-in @db [:suggestions (:current-chat-id @db)])))) (register-sub :command? - (fn [db ] + (fn [db] (->> (get-in @db [:edit-mode (:current-chat-id @db)]) (= :command) (reaction)))) diff --git a/src/status_im/chat/views/content_suggestions.cljs b/src/status_im/chat/views/content_suggestions.cljs deleted file mode 100644 index d2eb8b63b4..0000000000 --- a/src/status_im/chat/views/content_suggestions.cljs +++ /dev/null @@ -1,34 +0,0 @@ -(ns status-im.chat.views.content-suggestions - (:require-macros [status-im.utils.views :refer [defview]]) - (:require [re-frame.core :refer [subscribe dispatch]] - [status-im.components.react :refer [view - icon - text - touchable-highlight - list-view - list-item]] - [status-im.chat.styles.content-suggestions :as st] - [status-im.utils.listview :refer [to-datasource]])) - -(defn set-command-content [content] - (dispatch [:set-chat-command-content content])) - -(defn suggestion-list-item [{:keys [value description]}] - [touchable-highlight {:onPress #(set-command-content value)} - [view st/suggestion-container - [view st/suggestion-sub-container - [text {:style st/value-text} value] - [text {:style st/description-text} description]]]]) - -(defn render-row [row _ _] - (list-item [suggestion-list-item row])) - -(defview content-suggestions-view [] - [suggestions [:get-content-suggestions]] - (when (seq suggestions) - [view st/container - [touchable-highlight {:style st/drag-down-touchable - ;; TODO hide suggestions? - :onPress (fn [])} - [view [icon :drag_down st/drag-down-icon]]] - suggestions])) diff --git a/src/status_im/chat/views/plain_message.cljs b/src/status_im/chat/views/plain_message.cljs index 69829dae14..f9abe83f25 100644 --- a/src/status_im/chat/views/plain_message.cljs +++ b/src/status_im/chat/views/plain_message.cljs @@ -24,15 +24,16 @@ (defn button-animation-logic [{:keys [command? val]}] (fn [_] (let [to-scale (if @command? 0 1)] - (anim/start (anim/spring val {:toValue to-scale}))))) + (anim/start (anim/spring val {:toValue to-scale + :tension 30}))))) (defn list-container [min] (fn [{:keys [command? width]}] (let [n-width (if @command? min 56) - delay (if @command? 100 0)] + delay (if @command? 100 0)] (anim/start (anim/timing width {:toValue n-width :duration response-input-hiding-duration - :delay delay}))))) + :delay delay}))))) (defn commands-button [] (let [command? (subscribe [:command?]) @@ -62,19 +63,20 @@ (fn [_] (let [to-scale (if @command? 0 1)] (when-not @command? (anim/set-value width 56)) - (anim/start (anim/spring val {:toValue to-scale}) + (anim/start (anim/spring val {:toValue to-scale + :tension 30}) (fn [e] (when (and @command? (.-finished e)) (anim/set-value width 0.1))))))) (defn smile-button [] - (let [command? (subscribe [:command?]) - buttons-scale (anim/create-value (if @command? 1 0)) + (let [command? (subscribe [:command?]) + buttons-scale (anim/create-value (if @command? 1 0)) container-width (anim/create-value (if @command? 0.1 56)) - context {:command? command? - :val buttons-scale - :width container-width} - on-update (smile-animation-logic context)] + context {:command? command? + :val buttons-scale + :width container-width} + on-update (smile-animation-logic context)] (r/create-class {:component-did-mount on-update diff --git a/src/status_im/chat/views/response.cljs b/src/status_im/chat/views/response.cljs index faec7629fe..a079ef805a 100644 --- a/src/status_im/chat/views/response.cljs +++ b/src/status_im/chat/views/response.cljs @@ -13,7 +13,6 @@ [status-im.components.drag-drop :as drag] [status-im.chat.views.response-suggestions :refer [response-suggestions-view]] [status-im.chat.styles.response :as st] - [status-im.chat.styles.message-input :refer [input-height]] [status-im.components.animation :as anim] [status-im.components.react :as react])) @@ -112,5 +111,4 @@ (let [response-height (anim/create-value 0)] [container response-height [request-info response-height] - [response-suggestions-view] - [view st/input-placeholder]])) + [response-suggestions-view]])) diff --git a/src/status_im/chat/views/response_suggestions.cljs b/src/status_im/chat/views/response_suggestions.cljs index bffe80175b..3c6f9b549d 100644 --- a/src/status_im/chat/views/response_suggestions.cljs +++ b/src/status_im/chat/views/response_suggestions.cljs @@ -1,35 +1,7 @@ (ns status-im.chat.views.response-suggestions (:require-macros [status-im.utils.views :refer [defview]]) - (:require [re-frame.core :refer [subscribe dispatch]] - [status-im.components.react :refer [view - icon - text - touchable-highlight - list-view - list-item]] - [status-im.chat.styles.response-suggestions :as st] - [status-im.utils.listview :refer [to-datasource]])) - -(defn set-command-content [content] - (dispatch [:set-chat-command-content content])) - -(defn header-list-item [{:keys [header]}] - [view st/header-container - [text {:style st/header-text} header]]) - -(defn suggestion-list-item [{:keys [value description]}] - [touchable-highlight {:onPress #(set-command-content value)} - [view st/suggestion-container - [view st/suggestion-sub-container - [text {:style st/value-text} value] - [text {:style st/description-text} description]]]]) - -(defn render-row [row _ _] - (list-item (if (:header row) - [header-list-item row] - [suggestion-list-item row]))) + (:require [re-frame.core :refer [subscribe dispatch]])) (defview response-suggestions-view [] [suggestions [:get-content-suggestions]] - (when (seq suggestions) - suggestions)) + (when (seq suggestions) suggestions)) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 2e45635869..07a041b01d 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -50,7 +50,7 @@ (defn command-preview [db [chat-id response-json]] (if-let [response (json->cljs response-json)] - (let [path [:chats chat-id :staged-commands] + (let [path [:chats chat-id :staged-commands] commands-cnt (count (get-in db path))] ;; todo (dec commands-cnt) looks like hack have to find better way to ;; do this @@ -63,6 +63,8 @@ (reg-handler ::render-command render-command) (reg-handler :command-handler! (u/side-effect! command-nadler!)) -(reg-handler :suggestions-handler suggestions-handler) +(reg-handler :suggestions-handler + (after #(dispatch [:animate-show-response])) + suggestions-handler) (reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) (reg-handler :command-preview command-preview) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 3f8ce95762..66ee37977d 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -55,9 +55,6 @@ ;; todo check #_(= (get-hash-by-identity db identity) (get-hash-by-file file))] - (println :hash - (get-hash-by-identity db identity) - (get-hash-by-file file)) (assoc db ::valid-hash valid?))) (defn add-commands diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs index bf4639f5dc..1c07a5fe8e 100644 --- a/src/status_im/commands/utils.cljs +++ b/src/status_im/commands/utils.cljs @@ -45,4 +45,4 @@ (defn reg-handler ([name handler] (reg-handler name nil handler)) ([name middleware handler] - (register-handler name [debug trim-v middleware] handler))) + (register-handler name [#_debug trim-v middleware] handler))) diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index d58cf9e2c1..7efc67c13b 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -122,5 +122,5 @@ var status = { (.stringify js/JSON (clj->js data))) (defn call [chat-id path params callback] - (println :call chat-id (cljs->json path) (cljs->json params)) + ;(println :call chat-id (cljs->json path) (cljs->json params)) (.call jail chat-id (cljs->json path) (cljs->json params) callback)) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 6540a30948..f229403e07 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -9,6 +9,7 @@ [status-im.utils.crypt :refer [gen-random-bytes]] [status-im.utils.handlers :as u] status-im.chat.handlers + status-im.chat.handlers.animation status-im.group-settings.handlers status-im.navigation.handlers status-im.contacts.handlers From 43b943505c01269a96c5c86d817c0ac1eee44c25 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 23 Jun 2016 16:11:21 +0300 Subject: [PATCH 18/26] adapt animation & cleanup Former-commit-id: d07688136f0f23ced1363189ed9f5d5f6c8d2e00 --- src/status_im/chat/handlers/animation.cljs | 2 - .../chat/styles/response_suggestions.cljs | 59 ------------------- src/status_im/chat/views/response.cljs | 13 +++- .../chat/views/response_suggestions.cljs | 7 --- 4 files changed, 11 insertions(+), 70 deletions(-) delete mode 100644 src/status_im/chat/styles/response_suggestions.cljs delete mode 100644 src/status_im/chat/views/response_suggestions.cljs diff --git a/src/status_im/chat/handlers/animation.cljs b/src/status_im/chat/handlers/animation.cljs index 2bd08c7a3b..a1ea5c98a5 100644 --- a/src/status_im/chat/handlers/animation.cljs +++ b/src/status_im/chat/handlers/animation.cljs @@ -1,11 +1,9 @@ (ns status-im.chat.handlers.animation (:require [re-frame.core :refer [register-handler after dispatch debug]] [re-frame.middleware :refer [path]] - [status-im.models.commands :as commands] [status-im.handlers.content-suggestions :refer [get-content-suggestions]] [status-im.chat.constants :refer [input-height request-info-height response-height-normal minimum-suggestion-height]] - [status-im.chat.styles.response-suggestions :as response-suggestions-styles] [status-im.constants :refer [response-input-hiding-duration]])) ;; todo magic value diff --git a/src/status_im/chat/styles/response_suggestions.cljs b/src/status_im/chat/styles/response_suggestions.cljs deleted file mode 100644 index 2561f2ae53..0000000000 --- a/src/status_im/chat/styles/response_suggestions.cljs +++ /dev/null @@ -1,59 +0,0 @@ -(ns status-im.chat.styles.response-suggestions - (:require [status-im.components.styles :refer [font - font-medium - color-light-blue-transparent - color-white - color-black - color-blue - color-blue-transparent - selected-message-color - online-color - separator-color - text1-color - text2-color - text3-color]])) - -(def header-height 50) -(def suggestion-height 56) - -(def header-container - {:paddingLeft 16 - :height header-height - :backgroundColor color-white}) - -(def header-text - {:marginTop 18 - :fontSize 13 - :fontFamily font-medium - :color text2-color}) - -(def suggestion-container - {:flexDirection :column - :paddingLeft 16 - :height suggestion-height - :backgroundColor color-white}) - -(def suggestion-sub-container - {:height suggestion-height - :borderBottomWidth 1 - :borderBottomColor separator-color}) - -(def value-text - {:marginTop 10 - :fontSize 12 - :fontFamily font - :color text1-color}) - -(def description-text - {:marginTop 2 - :fontSize 12 - :fontFamily font - :color text2-color}) - -(def suggestions-container - {:flexDirection :row - :flex 1 - :marginVertical 1 - :marginHorizontal 0 - :backgroundColor color-white - :borderRadius 5}) diff --git a/src/status_im/chat/views/response.cljs b/src/status_im/chat/views/response.cljs index a079ef805a..4336ce9c04 100644 --- a/src/status_im/chat/views/response.cljs +++ b/src/status_im/chat/views/response.cljs @@ -11,7 +11,6 @@ text-input touchable-highlight]] [status-im.components.drag-drop :as drag] - [status-im.chat.views.response-suggestions :refer [response-suggestions-view]] [status-im.chat.styles.response :as st] [status-im.components.animation :as anim] [status-im.components.react :as react])) @@ -107,8 +106,18 @@ (into [animated-view {:style (st/response-view response-height)}] children))}))) +(defview placeholder [] + [suggestions [:get-content-suggestions]] + (when (seq suggestions) + [view st/input-placeholder])) + +(defview response-suggestions-view [] + [suggestions [:get-content-suggestions]] + (when (seq suggestions) suggestions)) + (defn response-view [] (let [response-height (anim/create-value 0)] [container response-height [request-info response-height] - [response-suggestions-view]])) + [response-suggestions-view] + [placeholder]])) diff --git a/src/status_im/chat/views/response_suggestions.cljs b/src/status_im/chat/views/response_suggestions.cljs deleted file mode 100644 index 3c6f9b549d..0000000000 --- a/src/status_im/chat/views/response_suggestions.cljs +++ /dev/null @@ -1,7 +0,0 @@ -(ns status-im.chat.views.response-suggestions - (:require-macros [status-im.utils.views :refer [defview]]) - (:require [re-frame.core :refer [subscribe dispatch]])) - -(defview response-suggestions-view [] - [suggestions [:get-content-suggestions]] - (when (seq suggestions) suggestions)) From 157393bd7aca7a3c56297b4999b859373c70de2e Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 23 Jun 2016 19:07:31 +0300 Subject: [PATCH 19/26] handle errors from jail Former-commit-id: 40dd0063d20cb56ce9d34d05b9720a7d66410d00 --- android/app/build.gradle | 2 +- src/status_im/chat/views/response.cljs | 4 ++- src/status_im/commands/handlers/jail.cljs | 37 +++++++++++++------- src/status_im/commands/handlers/loading.cljs | 20 ++++++----- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index e86f611f36..e7e086763c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -133,7 +133,7 @@ dependencies { compile project(':react-native-camera') compile project(':react-native-status') compile project(':react-native-orientation') - compile(group: 'status-im', name: 'status-go', version: '0.1.0-201606220917-ec4b9a', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: '0.1.0-201606231357-85abe1', ext: 'aar') compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } diff --git a/src/status_im/chat/views/response.cljs b/src/status_im/chat/views/response.cljs index 4336ce9c04..71933d6d8d 100644 --- a/src/status_im/chat/views/response.cljs +++ b/src/status_im/chat/views/response.cljs @@ -85,7 +85,9 @@ (defn container-animation-logic [{:keys [to-value val]}] (fn [_] (let [to-value @to-value] - (anim/start (anim/spring val {:toValue to-value}))))) + (anim/start (anim/spring val {:toValue to-value + :tension 50 + :friction 10}))))) (defn container [response-height & children] (let [;; todo to-response-height, cur-response-height must be specific diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 07a041b01d..f7fd12067a 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -26,19 +26,30 @@ (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 print-error! [error] + (toast error) + (println error)) -(defn suggestions-handler +(defn command-hadler! + [_ [{:keys [to] :as command} response]] + (let [{:keys [error result]} (json->cljs response)] + (if error + (let [m (str "Error on command handling!\n" command error)] + (print-error! m)) + (let [{:keys [event params]} result + events (if (= "console" to) + (merge regular-events console-events) + regular-events)] + (when-let [handler (events (keyword event))] + (apply handler params)))))) + +(defn suggestions-handler! [db [{:keys [chat-id]} response-json]] - (let [response (json->cljs response-json)] - (assoc-in db [:suggestions chat-id] (generate-hiccup response)))) + (let [{:keys [error result]} (json->cljs response-json)] + (when error + (let [m (str "Error on param suggestions!\n" error)] + (print-error! m))) + (assoc-in db [:suggestions chat-id] (generate-hiccup result)))) (defn suggestions-events-handler! [db [[n data]]] @@ -62,9 +73,9 @@ (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 :command-handler! (u/side-effect! command-hadler!)) (reg-handler :suggestions-handler (after #(dispatch [:animate-show-response])) - suggestions-handler) + suggestions-handler!) (reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) (reg-handler :command-preview command-preview) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 66ee37977d..d65bf21ec0 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -44,10 +44,10 @@ (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] - (let [commands (json->cljs result)] - ;; todo use commands from jail - (dispatch [::add-commands identity file commands]))) - #_(dispatch [::loading-failed! identity ::error-in-jail %]))) + (let [{:keys [error result]} (json->cljs result)] + (if error + (dispatch [::loading-failed! identity ::error-in-jail error]) + (dispatch [::add-commands identity file result])))))) (defn validate-hash [db [identity file]] @@ -70,11 +70,13 @@ (defn loading-failed! [db [id reason details]] (let [url (get-in db [:chats id :dapp-url])] - (toast (s/join "\n" ["commands.js loading failed" - url - id - (name reason) - details])))) + (let [m (s/join "\n" ["commands.js loading failed" + url + id + (name reason) + details])] + (toast m) + (println m)))) (reg-handler :load-commands! (u/side-effect! load-commands!)) (reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) From e84af2595fe1fcd9285c782dabf96e07429cb7c5 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 24 Jun 2016 15:38:51 +0300 Subject: [PATCH 20/26] (slurp "status.js") Former-commit-id: 82c6688648ed771b687d82b91b4167739609f30d --- src/status_im/components/jail.cljs | 108 +---------------------------- src/status_im/utils/slurp.clj | 5 ++ 2 files changed, 7 insertions(+), 106 deletions(-) create mode 100644 src/status_im/utils/slurp.clj diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 7efc67c13b..758d620706 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -1,112 +1,8 @@ (ns status-im.components.jail + (:require-macros [status-im.utils.slurp :refer [slurp]]) (:require [status-im.components.react :as r])) -(def status-js - "var _status_catalog = { - commands: {}, - responses: {} -}; - -function Command() { -} -function Response() { -} - -Command.prototype.addToCatalog = function () { - _status_catalog.commands[this.name] = this; -}; - -Command.prototype.param = function (parameter) { - this.params.push(parameter); - - return this; -}; - -Command.prototype.create = function (com) { - this.name = com.name; - this.description = com.description; - this.handler = com.handler; - this.color = com.color; - this.icon = com.icon; - this.params = com.params || []; - this.preview = com.preview; - this.addToCatalog(); - - return this; -}; - - -Response.prototype = Object.create(Command.prototype); -Response.prototype.addToCatalog = function () { - _status_catalog.responses[this.name] = this; -}; -Response.prototype.onReceiveResponse = function (handler) { - this.onReceive = handler; -}; - -function call(pathStr, paramsStr) { - var params = JSON.parse(paramsStr), - path = JSON.parse(pathStr), - fn, res; - - fn = path.reduce(function (catalog, name) { - if (catalog && catalog[name]) { - return catalog[name]; - } - }, - _status_catalog - ); - - res = fn(params); - - 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 = { - command: function (n, d, h) { - var command = new Command(); - return command.create(n, d, h); - }, - response: function (n, d, h) { - var response = new Response(); - return response.create(n, d, h); - }, - types: { - STRING: 'string', - PHONE_NUMBER: 'phone-number', - PASSWORD: 'password' - }, - events: { - SET_VALUE: 'set-value' - }, - components: { - view: view, - text: text, - image: image, - touchable: touchable, - scrollView: scrollView - } -};") +(def status-js (slurp "resources/status.js")) (def jail (when (exists? (.-NativeModules r/react)) diff --git a/src/status_im/utils/slurp.clj b/src/status_im/utils/slurp.clj new file mode 100644 index 0000000000..194b1384b5 --- /dev/null +++ b/src/status_im/utils/slurp.clj @@ -0,0 +1,5 @@ +(ns status-im.utils.slurp + (:refer-clojure :exclude [slurp])) + +(defmacro slurp [file] + (clojure.core/slurp file)) From 771f046e9620926a87bacba4df0e3c33080a74dc Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 25 Jun 2016 16:28:45 +0300 Subject: [PATCH 21/26] handle responses from jail Former-commit-id: 7632de08cfb67b72dffd881e0fba5d099e7726c2 --- resources/commands.js | 2 +- resources/status.js | 4 ++ src/status_im/commands/handlers/jail.cljs | 66 ++++++++++---------- src/status_im/commands/handlers/loading.cljs | 5 +- src/status_im/commands/utils.cljs | 3 +- src/status_im/components/jail.cljs | 11 +++- src/status_im/utils/types.cljs | 5 +- 7 files changed, 53 insertions(+), 43 deletions(-) diff --git a/resources/commands.js b/resources/commands.js index 47bd9909d4..94c631b979 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -124,7 +124,7 @@ status.response({ status.command({ name: "help", description: "Help", - color: "#9a5dcf", + color: "#7099e6", params: [{ name: "query", type: status.types.STRING diff --git a/resources/status.js b/resources/status.js index c25535b159..924decbf47 100644 --- a/resources/status.js +++ b/resources/status.js @@ -53,6 +53,10 @@ function call(pathStr, paramsStr) { _status_catalog ); + if(!fn) { + return null; + } + res = fn(params); return JSON.stringify(res); diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index f7fd12067a..ce2f8be32b 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -4,15 +4,14 @@ [status-im.utils.handlers :as u] [status-im.utils.utils :refer [http-get toast]] [status-im.components.jail :as j] - - [status-im.commands.utils :refer [json->cljs generate-hiccup - reg-handler]])) + [status-im.utils.types :refer [json->clj]] + [status-im.commands.utils :refer [generate-hiccup reg-handler]] + [clojure.string :as s])) (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)])))) + #(dispatch [::render-command chat-id message-id %]))) (defn render-command [db [chat-id message-id markup]] @@ -26,30 +25,19 @@ (def regular-events {}) -(defn print-error! [error] - (toast error) - (println error)) - (defn command-hadler! - [_ [{:keys [to] :as command} response]] - (let [{:keys [error result]} (json->cljs response)] - (if error - (let [m (str "Error on command handling!\n" command error)] - (print-error! m)) - (let [{:keys [event params]} result - events (if (= "console" to) - (merge regular-events console-events) - regular-events)] - (when-let [handler (events (keyword event))] - (apply handler params)))))) + [_ [{:keys [to]} {:keys [result]} ]] + (when result + (let [{:keys [event params]} result + events (if (= "console" to) + (merge regular-events console-events) + regular-events)] + (when-let [handler (events (keyword event))] + (apply handler params))))) (defn suggestions-handler! - [db [{:keys [chat-id]} response-json]] - (let [{:keys [error result]} (json->cljs response-json)] - (when error - (let [m (str "Error on param suggestions!\n" error)] - (print-error! m))) - (assoc-in db [:suggestions chat-id] (generate-hiccup result)))) + [db [{:keys [chat-id]} {:keys [result]} ]] + (assoc-in db [:suggestions chat-id] (generate-hiccup result))) (defn suggestions-events-handler! [db [[n data]]] @@ -59,23 +47,35 @@ nil)) (defn command-preview - [db [chat-id response-json]] - (if-let [response (json->cljs response-json)] + [db [chat-id {:keys [result]}]] + (println :wuuut) + (if result (let [path [:chats chat-id :staged-commands] commands-cnt (count (get-in db path))] ;; todo (dec commands-cnt) looks like hack have to find better way to ;; do this (update-in db (conj path (dec commands-cnt)) assoc - :preview (generate-hiccup response) - :preview-string (str response))) + :preview (generate-hiccup result) + :preview-string (str result))) db)) +(defn print-error-message! [message] + (fn [_ params] + (when (:error (last params)) + (toast (s/join "\n" [message params])) + (println message params)))) + (reg-handler :init-render-command! init-render-command!) (reg-handler ::render-command render-command) -(reg-handler :command-handler! (u/side-effect! command-hadler!)) +(reg-handler :command-handler! + (after (print-error-message! "Error on command handling")) + (u/side-effect! command-hadler!)) (reg-handler :suggestions-handler - (after #(dispatch [:animate-show-response])) + [(after #(dispatch [:animate-show-response])) + (after (print-error-message! "Error on param suggestions"))] suggestions-handler!) (reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) -(reg-handler :command-preview command-preview) +(reg-handler :command-preview + (after (print-error-message! "Error on command preview")) + command-preview) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index d65bf21ec0..74b08e7d86 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -6,7 +6,8 @@ [clojure.string :as s] [status-im.persistence.realm :as realm] [status-im.components.jail :as j] - [status-im.commands.utils :refer [json->cljs reg-handler]])) + [status-im.utils.types :refer [json->clj]] + [status-im.commands.utils :refer [reg-handler]])) (def commands-js "commands.js") @@ -44,7 +45,7 @@ (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] - (let [{:keys [error result]} (json->cljs result)] + (let [{:keys [error result]} (json->clj result)] (if error (dispatch [::loading-failed! identity ::error-in-jail error]) (dispatch [::add-commands identity file result])))))) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs index 1c07a5fe8e..8ae48bf0e7 100644 --- a/src/status_im/commands/utils.cljs +++ b/src/status_im/commands/utils.cljs @@ -5,12 +5,11 @@ image touchable-highlight]] [re-frame.core :refer [register-handler dispatch trim-v debug]])) -(defn json->cljs [json] +(defn json->clj [json] (if (= json "undefined") nil (js->clj (.parse js/JSON json) :keywordize-keys true))) - (def elements {:text text :view view diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 758d620706..daf278f4f8 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -1,6 +1,7 @@ (ns status-im.components.jail (:require-macros [status-im.utils.slurp :refer [slurp]]) - (:require [status-im.components.react :as r])) + (:require [status-im.components.react :as r] + [status-im.utils.types :as t])) (def status-js (slurp "resources/status.js")) @@ -18,5 +19,9 @@ (.stringify js/JSON (clj->js data))) (defn call [chat-id path params callback] - ;(println :call chat-id (cljs->json path) (cljs->json params)) - (.call jail chat-id (cljs->json path) (cljs->json params) callback)) + (println :call chat-id (cljs->json path) (cljs->json params)) + (let [cb (fn [r] + (let [r' (t/json->clj r)] + (println r') + (callback r')))] + (.call jail chat-id (cljs->json path) (cljs->json params) cb))) diff --git a/src/status_im/utils/types.cljs b/src/status_im/utils/types.cljs index 93c942df77..d30d09804b 100644 --- a/src/status_im/utils/types.cljs +++ b/src/status_im/utils/types.cljs @@ -11,5 +11,6 @@ (defn clj->json [data] (.stringify js/JSON (clj->js data))) -(defn json->clj [data] - (js->clj (.parse js/JSON data) :keywordize-keys true)) +(defn json->clj [json] + (when-not (= json "undefined") + (js->clj (.parse js/JSON json) :keywordize-keys true))) From 4b33910c805647cebd22e1e722f10fe953a647af Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 25 Jun 2016 16:38:24 +0300 Subject: [PATCH 22/26] cleanup Former-commit-id: 923b2af64859a25d279877c672f3bc99d694710c --- src/status_im/commands/handlers/jail.cljs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index ce2f8be32b..b2d1ac67af 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -48,7 +48,6 @@ (defn command-preview [db [chat-id {:keys [result]}]] - (println :wuuut) (if result (let [path [:chats chat-id :staged-commands] commands-cnt (count (get-in db path))] From f5464c18bfd8ded06ecea5d4f03e2eb0a2563d22 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 27 Jun 2016 10:24:27 +0300 Subject: [PATCH 23/26] loading of console's commands.js without running server Former-commit-id: a3de75127aecc0ec3987c53bf3d067aff07e6ab1 --- src/status_im/commands/handlers/loading.cljs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 74b08e7d86..417175bc8a 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -1,4 +1,5 @@ (ns status-im.commands.handlers.loading + (:require-macros [status-im.utils.slurp :refer [slurp]]) (:require [re-frame.core :refer [register-handler after dispatch subscribe trim-v debug]] [status-im.utils.handlers :as u] @@ -23,9 +24,11 @@ (defn fetch-commands! [db [identity]] (when-let [url (:dapp-url (get-in db [:chats identity]))] - (http-get (s/join "/" [url commands-js]) - #(dispatch [::validate-hash identity %]) - #(dispatch [::loading-failed! identity ::file-was-not-found])))) + (if (= "console" identity) + (dispatch [::validate-hash identity (slurp "resources/commands.js")]) + (http-get (s/join "/" [url commands-js]) + #(dispatch [::validate-hash identity %]) + #(dispatch [::loading-failed! identity ::file-was-not-found]))))) (defn dispatch-loaded! [db [identity file]] From 6f9881af55349b63bceeb67387bbc74dabe420a4 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 27 Jun 2016 12:22:52 +0300 Subject: [PATCH 24/26] tint-color for response's icon Former-commit-id: 3289053ff86022ffdfaecf248452c6073af864c1 --- src/status_im/chat/styles/message.cljs | 41 +++++++++++++------------- src/status_im/chat/views/message.cljs | 2 +- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/status_im/chat/styles/message.cljs b/src/status_im/chat/styles/message.cljs index 5bb60573d4..93f1472389 100644 --- a/src/status_im/chat/styles/message.cljs +++ b/src/status_im/chat/styles/message.cljs @@ -1,15 +1,15 @@ (ns status-im.chat.styles.message (:require [status-im.components.styles :refer [font - color-light-blue-transparent - color-white - color-black - color-blue - selected-message-color - online-color - text1-color - text2-color]] + color-light-blue-transparent + color-white + color-black + color-blue + selected-message-color + online-color + text1-color + text2-color]] [status-im.constants :refer [text-content-type - content-type-command]])) + content-type-command]])) (def style-message-text {:fontSize 14 @@ -153,16 +153,13 @@ :backgroundColor (:color command) :transform [{:scale scale}]}) -(defn command-image-view - [command] - {:position :absolute - :top 0 - :right 0 - :width 24 - :height 24 - :borderRadius 50 - :backgroundColor (:color command) - :alignItems :center}) +(def command-image-view + {:position :absolute + :top 0 + :right 0 + :width 24 + :height 24 + :alignItems :center}) (def command-request-image {:position :absolute @@ -197,8 +194,10 @@ (def command-image {:margin-top 5 - :width 12 - :height 13}) + :width 12 + :height 13 + :tint-color :#a9a9a9cc}) + (def command-text (merge style-message-text {:marginTop 8 diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 656cb72631..87dc476731 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -69,7 +69,7 @@ (str "!" (:name command))]]] ;; todo doesn't reflect design (when-let [icon (:icon command)] - [view (st/command-image-view command) + [view st/command-image-view [image {:source {:uri icon} :style st/command-image}]]) (if preview From b1e8a6c30fc6746830d1dcd375ac527a1ebc07e9 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 27 Jun 2016 12:33:46 +0300 Subject: [PATCH 25/26] fix exclamation mark for response messages Former-commit-id: 2894baa0e10b8c8b199524b4071b7710f8c92c58 --- src/status_im/chat/views/message.cljs | 36 +++++++++----------- src/status_im/commands/handlers/loading.cljs | 9 +++-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 87dc476731..bed514f2f9 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -56,26 +56,22 @@ [view st/track-mark] [text {:style st/track-duration-text} "03:39"]]]) -(defn message-content-command [content preview] - (let [commands-atom (subscribe [:get-commands-and-responses])] - (fn [content preview] - (let [commands @commands-atom - {:keys [command content]} - (parse-command-msg-content commands content)] - [view st/content-command-view - [view st/command-container - [view (st/command-view command) - [text {:style st/command-name} - (str "!" (:name command))]]] - ;; todo doesn't reflect design - (when-let [icon (:icon command)] - [view st/command-image-view - [image {:source {:uri icon} - :style st/command-image}]]) - (if preview - preview - [text {:style st/command-text} - content])])))) +(defview message-content-command [content preview] + [commands [:get-commands-and-responses]] + (let [{:keys [command content]} (parse-command-msg-content commands content) + {:keys [name icon type]} command] + [view st/content-command-view + [view st/command-container + [view (st/command-view command) + [text {:style st/command-name} + (str (if (= :command type) "!" "") name)]]] + (when icon + [view st/command-image-view + [image {:source {:uri icon} + :style st/command-image}]]) + (if preview + preview + [text {:style st/command-text} content])])) (defn set-chat-command [msg-id command] (dispatch [:set-response-chat-command msg-id (keyword (:name command))])) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 417175bc8a..2291ade0fc 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -61,11 +61,16 @@ (get-hash-by-file file))] (assoc db ::valid-hash valid?))) +(defn mark-as [as coll] + (->> coll + (map (fn [[k v]] [k (assoc v :type as)])) + (into {}))) + (defn add-commands [db [id _ {:keys [commands responses]}]] (-> db - (update-in [:chats id :commands] merge commands) - (update-in [:chats id :responses] merge responses))) + (update-in [:chats id :commands] merge (mark-as :command commands)) + (update-in [:chats id :responses] merge (mark-as :response responses)))) (defn save-commands-js! [_ [id file]] From 4e25602eba60d709c96367bbd7caa5f935fb6e2d Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 27 Jun 2016 15:12:53 +0300 Subject: [PATCH 26/26] cleanup Former-commit-id: 70f204681b94eeed4bde0aaf970a4a0e53058452 --- src/status_im/chat/handlers.cljs | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index c1627a957e..ffe6830993 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -46,10 +46,10 @@ (defn invoke-suggestions-handler! [{:keys [current-chat-id] :as db} _] - (let [commands (get-in db [:chats current-chat-id :commands]) - {:keys [command content]} (get-in db [:chats current-chat-id :command-input]) - path [(if (commands command) :commands :responses) - (:name command) + (let [{:keys [command content]} (get-in db [:chats current-chat-id :command-input]) + {:keys [name type]} command + path [(if (= :command type) :commands :responses) + name :params 0 :suggestions] @@ -78,11 +78,11 @@ (assoc-in db [:chats current-chat-id :input-text] text)) (defn invoke-command-preview! - [{:keys [current-chat-id staged-command] :as db} _] - (let [commands (get-in db [:chats current-chat-id :commands]) - {:keys [command content]} staged-command - path [(if (commands command) :commands :responses) - (:name command) + [{:keys [current-chat-id staged-command]} _] + (let [{:keys [command content]} staged-command + {:keys [name type]} command + path [(if (= :command type) :commands :responses) + name :preview] params {:value content}] (j/call current-chat-id @@ -258,18 +258,18 @@ (dissoc new-command :rendered-preview)))) (defn invoke-commands-handlers! - [{:keys [new-commands current-chat-id] :as db}] - (let [commands (get-in db [:chats current-chat-id :commands])] - (doseq [{:keys [content] :as com} new-commands] - (let [{:keys [command content]} content - path [(if (commands command) :commands :responses) - command - :handler] - params {:value content}] - (j/call current-chat-id - path - params - #(dispatch [:command-handler! com %])))))) + [{:keys [new-commands current-chat-id]}] + (doseq [{:keys [content] :as com} new-commands] + (let [{:keys [command content]} content + type (:type command) + path [(if (= :command type) :commands :responses) + command + :handler] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:command-handler! com %]))))) (defn handle-commands [{:keys [new-commands]}]