From e7e623c65984bab25b0f058ebb4a302f005bf3e2 Mon Sep 17 00:00:00 2001 From: virvar Date: Mon, 28 Mar 2016 18:27:09 +0300 Subject: [PATCH] Rewrite sync contacts with server Former-commit-id: 2a0d39726a8ac2f1c2f3fdeadda68f49a0e4525b --- syng-im/src/syng_im/android/core.cljs | 7 +- syng-im/src/syng_im/components/login.cljs | 23 +++-- .../syng_im/components/sign_up_confirm.cljs | 86 +++++++++++++++++++ syng-im/src/syng_im/handlers.cljs | 15 ++++ syng-im/src/syng_im/handlers/contacts.cljs | 64 ++++++++++++++ syng-im/src/syng_im/handlers/server.cljs | 5 +- syng-im/src/syng_im/models/contacts.cljs | 7 +- syng-im/src/syng_im/subs.cljs | 8 +- 8 files changed, 193 insertions(+), 22 deletions(-) create mode 100644 syng-im/src/syng_im/components/sign_up_confirm.cljs create mode 100644 syng-im/src/syng_im/handlers/contacts.cljs diff --git a/syng-im/src/syng_im/android/core.cljs b/syng-im/src/syng_im/android/core.cljs index 4920ef7775..5a180101aa 100644 --- a/syng-im/src/syng_im/android/core.cljs +++ b/syng-im/src/syng_im/android/core.cljs @@ -10,6 +10,7 @@ [syng-im.components.contact-list.contact-list :refer [contact-list]] [syng-im.components.chat :refer [chat]] [syng-im.components.login :refer [login-view]] + [syng-im.components.sign-up-confirm :refer [sign-up-confirm-view]] ;; [syng-im.components.chat.chat :refer [chat]] [syng-im.components.nav :as nav] @@ -42,7 +43,7 @@ }) :render-scene (fn [route nav] (log/debug "route" route) - (when nav/*nav-render* + (when true ;; nav/*nav-render* (let [{:keys [view-id]} (js->clj route :keywordize-keys true) view-id (keyword view-id)] (init-back-button-handler! nav) @@ -50,7 +51,9 @@ :contact-list (r/as-element [contact-list {:navigator nav}]) :chat (r/as-element [chat {:navigator nav}]) - :login (r/as-element [login-view {:navigator nav}])))))}]) + :login (r/as-element [login-view {:navigator nav}]) + :sign-up-confirm (r/as-element [sign-up-confirm-view + {:navigator nav}])))))}]) (defn init [] (dispatch-sync [:initialize-db]) diff --git a/syng-im/src/syng_im/components/login.cljs b/syng-im/src/syng_im/components/login.cljs index 69f1d7d42c..11b8df3c99 100644 --- a/syng-im/src/syng_im/components/login.cljs +++ b/syng-im/src/syng_im/components/login.cljs @@ -6,20 +6,18 @@ [syng-im.components.spinner :refer [spinner]] [syng-im.components.nav :as nav] [syng-im.utils.utils :refer [log toast http-post]] - [syng-im.utils.phone-number :refer [format-phone-number]] - ;; [messenger.android.sign-up-confirm :refer [sign-up-confirm]] -)) + [syng-im.utils.phone-number :refer [format-phone-number]])) -(def nav-atom (atom nil)) +;; (def nav-atom (atom nil)) -(defn show-confirm-view [] +(defn show-confirm-view [navigator] (dispatch [:set-loading false]) ;; TODO 'nav-replace - (nav/nav-push @nav-atom {:view-id :chat})) + (nav/nav-push navigator {:view-id :sign-up-confirm})) -(defn sign-up [user-phone-number user-identity] +(defn sign-up [user-phone-number user-identity navigator] (dispatch [:set-loading true]) - (dispatch [:sign-up user-phone-number user-identity show-confirm-view])) + (dispatch [:sign-up user-phone-number user-identity #(show-confirm-view navigator)])) (defn update-phone-number [value] (let [formatted (format-phone-number value)] @@ -30,7 +28,7 @@ user-phone-number (subscribe [:get-user-phone-number]) user-identity (subscribe [:get-user-identity])] (fn [] - (reset! nav-atom navigator) + ;; (reset! nav-atom navigator) [view {:style {:flex 1}} [view {:style {:flex 1 :backgroundColor "white"}} @@ -53,7 +51,7 @@ :fontFamily "Avenir-Roman" :color "#9CBFC0"}} @user-phone-number] - [touchable-highlight {:onPress #(sign-up @user-phone-number @user-identity) + [touchable-highlight {:onPress #(sign-up @user-phone-number @user-identity navigator) :style {:alignSelf "center" :borderRadius 7 :backgroundColor "#E5F5F6" @@ -61,6 +59,5 @@ [text {:style {:marginVertical 10 :textAlign "center"}} "Sign up"]]]] - ;; (when (or loading (not user-identity)) - ;; [spinner {:visible true}]) - ]))) + (when (or @loading (not @user-identity)) + [spinner {:visible true}])]))) diff --git a/syng-im/src/syng_im/components/sign_up_confirm.cljs b/syng-im/src/syng_im/components/sign_up_confirm.cljs new file mode 100644 index 0000000000..de4566824a --- /dev/null +++ b/syng-im/src/syng_im/components/sign_up_confirm.cljs @@ -0,0 +1,86 @@ +(ns syng-im.components.sign-up-confirm + (:require-macros + [natal-shell.core :refer [with-error-view]]) + (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] + [syng-im.components.react :refer [view text image touchable-highlight list-view + toolbar-android text-input]] + [syng-im.components.resources :as res] + [syng-im.components.spinner :refer [spinner]] + [syng-im.components.nav :as nav] + [syng-im.utils.utils :refer [log toast http-post]])) + +(defn show-home-view [navigator] + (dispatch [:set-loading false]) + (nav/nav-push navigator {:view-id :contact-list})) + +(defn sync-contacts [navigator] + (dispatch [:sync-contacts #(show-home-view navigator)])) + +(defn on-send-code-response [navigator body] + (log body) + (toast (if (:confirmed body) + "Confirmed" + "Wrong code")) + (if (:confirmed body) + ;; TODO user action required + (sync-contacts navigator) + (dispatch [:set-loading false]))) + +(defn code-valid? [code] + (= 4 (count code))) + +(defn send-code [code navigator] + (when (code-valid? code) + (dispatch [:set-loading true]) + (dispatch [:sign-up-confirm code (partial on-send-code-response navigator)]))) + +(defn update-code [value] + (let [formatted value] + (dispatch [:set-confirmation-code formatted]))) + +(defn sign-up-confirm-view [{:keys [navigator]}] + (let [loading (subscribe [:get-loading]) + confirmation-code (subscribe [:get-confirmation-code])] + (fn [] + [view {:style {:flex 1}} + [view {:style {:flex 1 + :backgroundColor "white"}} + [toolbar-android {:logo res/logo-icon + :title "Confirm" + :titleColor "#4A5258" + :style {:backgroundColor "white" + :height 56 + :elevation 2}}] + [view {} + [text-input {:underlineColorAndroid "#9CBFC0" + :placeholder "Enter confirmation code" + :keyboardType "number-pad" + :maxLength 4 + :onChangeText (fn [value] + (update-code value)) + :style {:flex 1 + :marginHorizontal 18 + :lineHeight 42 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "#9CBFC0"}} + @confirmation-code] + (if (code-valid? @confirmation-code) + [touchable-highlight {:onPress #(send-code @confirmation-code navigator) + :style {:alignSelf "center" + :borderRadius 7 + :backgroundColor "#E5F5F6" + + :width 100}} + [text {:style {:marginVertical 10 + :textAlign "center"}} + "Confirm"]] + [view {:style {:alignSelf "center" + :borderRadius 7 + :backgroundColor "#AAB2B2" + :width 100}} + [text {:style {:marginVertical 10 + :textAlign "center"}} + "Confirm"]])]] + (when @loading + [spinner {:visible true}])]))) diff --git a/syng-im/src/syng_im/handlers.cljs b/syng-im/src/syng_im/handlers.cljs index d5f7882f30..ddbacc5797 100644 --- a/syng-im/src/syng_im/handlers.cljs +++ b/syng-im/src/syng_im/handlers.cljs @@ -12,6 +12,7 @@ [syng-im.models.messages :refer [save-message new-message-arrived]] [syng-im.handlers.server :as server] + [syng-im.handlers.contacts :as contacts-service] [syng-im.utils.logging :as log])) ;; -- Middleware ------------------------------------------------------------ @@ -76,6 +77,20 @@ (server/sign-up phone-number whisper-identity handler) db)) +(register-handler :set-confirmation-code + (fn [db [_ value]] + (assoc db :confirmation-code value))) + +(register-handler :sign-up-confirm + (fn [db [_ confirmation-code handler]] + (server/sign-up-confirm confirmation-code handler) + db)) + +(register-handler :sync-contacts + (fn [db [_ handler]] + (contacts-service/sync-contacts handler) + db)) + ;; -- Contacts -------------------------------------------------------------- (register-handler :load-syng-contacts diff --git a/syng-im/src/syng_im/handlers/contacts.cljs b/syng-im/src/syng_im/handlers/contacts.cljs new file mode 100644 index 0000000000..bd21ffce70 --- /dev/null +++ b/syng-im/src/syng_im/handlers/contacts.cljs @@ -0,0 +1,64 @@ +(ns syng-im.handlers.contacts + (:require-macros [cljs.core.async.macros :refer [go]]) + (:require [clojure.string :as cstr] + [cljs.core.async :as async :refer [chan put! clj (:contacts data)))) + +(defn- get-contacts-by-hash [contacts] + (let [numbers-info (reduce (fn [numbers contact] + (into numbers + (map (fn [c] + {:number (format-phone-number (:number c)) + :contact contact}) + (:phone-numbers contact)))) + '() + contacts)] + (reduce (fn [m number-info] + (let [number (:number number-info) + hash (encrypt number)] + (assoc m hash number-info))) + {} + numbers-info))) + +(defn- request-syng-contacts [contacts] + (let [contacts-by-hash (get-contacts-by-hash contacts) + data (keys contacts-by-hash) + ch (chan)] + (http-post "get-contacts" {:phone-number-hashes data} + (fn [data] + (put! ch + (to-syng-contacts contacts-by-hash data)))) + ch)) + +(defn sync-contacts [handler] + (go + (let [result (