From ef5c7779fc03e978bd0a02fb571aed49814653ab Mon Sep 17 00:00:00 2001 From: virvar Date: Wed, 16 Mar 2016 00:42:23 +0300 Subject: [PATCH] Login view refactoring Former-commit-id: 703c6f2d5f98c8930613535e262651c4d78d0792 --- src/messenger/android/core.cljs | 4 +- src/messenger/android/login.cljs | 79 +++++---------------------- src/messenger/android/utils.cljs | 3 + src/messenger/comm/intercom.cljs | 14 +++++ src/messenger/comm/services.cljs | 9 ++- src/messenger/models/user_data.cljs | 44 +++++++++++++-- src/messenger/services/server.cljs | 21 +++++++ src/messenger/services/user_data.cljs | 35 +++++++++++- 8 files changed, 136 insertions(+), 73 deletions(-) create mode 100644 src/messenger/services/server.cljs diff --git a/src/messenger/android/core.cljs b/src/messenger/android/core.cljs index 17f71359b2..6ea6127b07 100644 --- a/src/messenger/android/core.cljs +++ b/src/messenger/android/core.cljs @@ -13,7 +13,8 @@ [messenger.android.contacts-list :refer [contacts-list]] [messenger.android.chat :refer [chat]] [messenger.comm.pubsub :as pubsub] - [messenger.comm.intercom :refer [load-user-phone-number]])) + [messenger.comm.intercom :refer [load-user-phone-number + load-user-whisper-identity]])) (def app-registry (.-AppRegistry js/React)) @@ -59,5 +60,6 @@ (defn init [] (pubsub/setup-pub-sub) (load-user-phone-number) + (load-user-whisper-identity) (om/add-root! state/reconciler AppRoot 1) (.registerComponent app-registry "Messenger" (fn [] app-root))) diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index ec8b10cd8b..9f8c085a71 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -2,13 +2,12 @@ (:require-macros [natal-shell.components :refer [view text image touchable-highlight list-view toolbar-android text-input]] - [natal-shell.async-storage :refer [get-item set-item]] - [natal-shell.core :refer [with-error-view]] - [natal-shell.alert :refer [alert]]) + [natal-shell.core :refer [with-error-view]]) (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] - [syng-im.protocol.web3 :as whisper] [messenger.state :as state] + [messenger.comm.intercom :as intercom :refer [set-user-phone-number + save-user-phone-number]] [messenger.android.utils :refer [log toast http-post]] [messenger.android.resources :as res] [messenger.android.sign-up-confirm :refer [sign-up-confirm]])) @@ -17,78 +16,30 @@ (set! js/PhoneNumber (js/require "awesome-phonenumber")) (def country-code "US") -(def ethereum-rpc-url "http://localhost:8545") (defn show-confirm-view [] (binding [state/*nav-render* false] (.replace @nav-atom (clj->js {:component sign-up-confirm :name "sign-up-confirm"})))) -(defn sign-in [phone-number whisper-identity] - ;; (toast (str "TODO: send number: " phone-number ", " - ;; (subs whisper-identity 0 2) ".." - ;; (subs whisper-identity (- (count whisper-identity) 2) - ;; (count whisper-identity)))) - (http-post "sign-up" - {:phone-number phone-number - :whisper-identity whisper-identity} - (fn [body] - (log body) - (show-confirm-view)))) - -(defn identity-handler [error result] - (if error - (do (toast (str error)) - (.log js/console "error") - (.log js/console error)) - (toast (str result)))) - -(defn get-identity [handler] - (let [web3 (whisper/make-web3 ethereum-rpc-url)] - (str (.newIdentity (whisper/whisper web3) handler)))) - - -(defn get-whisper-identity-handler [phone-number] - (fn [identity] - ;; TODO to test newIdentity. Change to 'identity' to use saved identity. - (if false ;; identity - (sign-in phone-number identity) - (get-identity (fn [error identity] - (if (not error) - (do (set-item "user-whisper-identity" identity) - (swap! state/app-state assoc :user-whisper-identity identity) - (sign-in phone-number identity)) - (toast (str "error" error)))))))) - -(defn load-user-whisper-identity [handler] - (get-item "user-whisper-identity" - (fn [error value] - (if (not error) - (let [whisper-identity (when value (str value))] - (swap! state/app-state assoc :user-whisper-identity whisper-identity) - (handler whisper-identity)) - (toast (str "error" error)))))) - -(defn handle-phone-number [phone-number] - (when phone-number - (load-user-whisper-identity (get-whisper-identity-handler phone-number)))) - -(defn save-phone-number [] - (let [phone-number (:user-phone-number @state/app-state)] - (set-item "user-phone-number" phone-number) - (handle-phone-number phone-number))) +(defn sign-up [] + (let [app-state (state/state) + phone-number (:user-phone-number app-state) + whisper-identity (:user-whisper-identity app-state)] + (save-user-phone-number phone-number) + (intercom/sign-up phone-number whisper-identity show-confirm-view))) (defn update-phone-number [value] (let [formatted (str (.getNumber (js/PhoneNumber. value country-code "international")))] - (swap! state/app-state assoc :user-phone-number - formatted))) + (set-user-phone-number formatted))) (defui Login static om/IQuery (query [this] - '[:user-phone-number]) + '[:user-phone-number :user-whisper-identity]) + Object (render [this] - (let [{:keys [user-phone-number]} (om/props this) + (let [{:keys [user-phone-number user-whisper-identity]} (om/props this) {:keys [nav]} (om/get-computed this)] (reset! nav-atom nav) (view @@ -114,13 +65,13 @@ :fontFamily "Avenir-Roman" :color "#9CBFC0"}} user-phone-number) - (touchable-highlight {:onPress #(save-phone-number) + (touchable-highlight {:onPress #(sign-up) :style {:alignSelf "center" :borderRadius 7 :backgroundColor "#E5F5F6" :width 100}} (text {:style {:marginVertical 10 :textAlign "center"}} - "Sign in"))))))) + "Sign up"))))))) (def login (om/factory Login)) diff --git a/src/messenger/android/utils.cljs b/src/messenger/android/utils.cljs index 23130a5d1e..c3d3b5103a 100644 --- a/src/messenger/android/utils.cljs +++ b/src/messenger/android/utils.cljs @@ -12,6 +12,9 @@ (defn toast [s] (toast/show s (toast/long))) +(defn on-error [error] + (toast (str "error: " error))) + (defn http-post ([action data on-success] (http-post action data on-success nil)) diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index 84b2543150..af7c2f5b36 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -11,9 +11,23 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defn set-user-phone-number [phone-number] + (publish! :service [:user-data :user-data/set-phone-number phone-number])) + +(defn save-user-phone-number [phone-number] + (publish! :service [:user-data :user-data/save-phone-number phone-number])) + (defn load-user-phone-number [] ;; :service [service_name action_id args_map] (publish! :service [:user-data :user-data/load-phone-number nil])) +(defn load-user-whisper-identity [] + (publish! :service [:user-data :user-data/load-whisper-identity nil])) + +(defn sign-up [phone-number whisper-identity handler] + (publish! :service [:server :server/sign-up {:phone-number phone-number + :whisper-identity whisper-identity + :handler handler}])) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/src/messenger/comm/services.cljs b/src/messenger/comm/services.cljs index 7fd2648bbf..da30f30304 100644 --- a/src/messenger/comm/services.cljs +++ b/src/messenger/comm/services.cljs @@ -1,7 +1,8 @@ (ns messenger.comm.services (:require [syng-im.utils.logging :as log] - [messenger.services.user-data :refer [user-data-handler]])) + [messenger.services.user-data :refer [user-data-handler]] + [messenger.services.server :refer [server-handler]])) (defmulti service (fn [state service-id args] service-id)) @@ -10,6 +11,10 @@ [state service-id args] (user-data-handler state args)) +(defmethod service :server + [state service-id args] + (server-handler state args)) + (defn services-handler [state service-id args] (log/info "handling " service-id " args = " args) - (service state service-id args)) \ No newline at end of file + (service state service-id args)) diff --git a/src/messenger/models/user_data.cljs b/src/messenger/models/user_data.cljs index 88ba2594b2..a961947e99 100644 --- a/src/messenger/models/user_data.cljs +++ b/src/messenger/models/user_data.cljs @@ -1,16 +1,52 @@ (ns messenger.models.user-data (:require-macros - [natal-shell.async-storage :refer [get-item set-item]] - [natal-shell.alert :refer [alert]]) - (:require [messenger.state :as state])) + [natal-shell.async-storage :refer [get-item set-item]]) + (:require [cljs.core.async :as async :refer [chan put! !]] + [syng-im.protocol.web3 :as web3] + [messenger.state :as state] + [messenger.android.utils :refer [log on-error toast]])) +(def ethereum-rpc-url "http://localhost:8545") + +(defn set-phone-number [phone-number] + (swap! state/app-state assoc :user-phone-number phone-number)) + +(defn save-phone-number [phone-number] + (set-item "user-phone-number" phone-number) + (swap! state/app-state assoc :user-phone-number phone-number)) (defn load-phone-number [] (get-item "user-phone-number" (fn [error value] (if error - (alert (str "error" error)) + (on-error error) (swap! state/app-state assoc :user-phone-number (when value (str value))))))) +(defn save-whisper-identity [identity] + (set-item "user-whisper-identity" identity) + (swap! state/app-state assoc :user-whisper-identity identity)) +(defn load-whisper-identity [] + (let [ch (chan)] + (get-item "user-whisper-identity" + (fn [error value] + (log (str "load whisper identity: " value)) + (put! ch + {:error error + :value (let [whisper-identity (when value (str value))] + (swap! state/app-state assoc :user-whisper-identity + whisper-identity) + whisper-identity)}))) + ch)) + +(defn new-whisper-identity [] + (let [ch (chan)] + (let [web3 (web3/make-web3 ethereum-rpc-url)] + (.newIdentity (web3/whisper web3) + (fn [error value] + (log (str "new whisper identity: " value)) + (swap! state/app-state assoc :user-whisper-identity value) + (put! ch {:error error + :value value})))) + ch)) diff --git a/src/messenger/services/server.cljs b/src/messenger/services/server.cljs new file mode 100644 index 0000000000..05669de8fa --- /dev/null +++ b/src/messenger/services/server.cljs @@ -0,0 +1,21 @@ +(ns messenger.services.server + (:require-macros [cljs.core.async.macros :refer [go]]) + (:require [cljs.core.async :as async :refer [chan put!