Login view refactoring

This commit is contained in:
virvar 2016-03-16 00:42:23 +03:00
parent f1c3bc7b0f
commit 703c6f2d5f
8 changed files with 136 additions and 73 deletions

View File

@ -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)))

View File

@ -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))

View File

@ -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))

View File

@ -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}]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -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))
(service state service-id args))

View File

@ -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))

View File

@ -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! <!]]
[messenger.android.utils :refer [log on-error http-post]]
[syng-im.utils.logging :as log]))
(defmulti server (fn [state id args]
id))
(defmethod server :server/sign-up
[state id args]
(log/info "handling " id " args = " args)
(http-post "sign-up" (select-keys args [:phone-number :whisper-identity])
(fn [body]
(log body)
;; TODO replace with core.async
((:handler args)))))
(defn server-handler [state [id args]]
(log/info "user notification: " args)
(server state id args))

View File

@ -1,15 +1,46 @@
(ns messenger.services.user-data
(:require [messenger.models.user-data :refer [load-phone-number]]
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :as async :refer [<!]]
[messenger.models.user-data :refer [set-phone-number
save-phone-number
load-phone-number
save-whisper-identity
load-whisper-identity
new-whisper-identity]]
[messenger.android.utils :refer [log on-error]]
[syng-im.utils.logging :as log]))
(defmulti user-data (fn [state id args]
id))
(defmethod user-data :user-data/set-phone-number
[state id phone-number]
(log/info "handling " id " args = " phone-number)
(set-phone-number phone-number))
(defmethod user-data :user-data/save-phone-number
[state id phone-number]
(log/info "handling " id " args = " phone-number)
(save-phone-number phone-number))
(defmethod user-data :user-data/load-phone-number
[state id args]
(log/info "handling " id "args = " args)
(log/info "handling " id " args = " args)
(load-phone-number))
(defmethod user-data :user-data/load-whisper-identity
[state id args]
(log/info "handling " id " args = " args)
(go
(let [result (<! (load-whisper-identity))]
(if-let [error (:error result)]
(on-error error)
(when (not (:value result))
(let [result (<! (new-whisper-identity))]
(if-let [error (:error result)]
(on-error error)
(save-whisper-identity (:value result)))))))))
(defn user-data-handler [state [id args]]
(log/info "user notification: " args)
(user-data state id args))