From e04a6eec4fea748f003e707822e53dbdf7826279 Mon Sep 17 00:00:00 2001 From: virvar Date: Fri, 8 Apr 2016 12:17:10 +0300 Subject: [PATCH] Show console chat on first run --- src/syng_im/android/core.cljs | 33 ++++++++++++++++++------------- src/syng_im/constants.cljs | 4 ++-- src/syng_im/db.cljs | 3 ++- src/syng_im/handlers.cljs | 14 ++++++------- src/syng_im/handlers/sign_up.cljs | 27 ++++++++++++++++++++----- src/syng_im/subs.cljs | 8 ++++---- 6 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/syng_im/android/core.cljs b/src/syng_im/android/core.cljs index 073c5150bd..fd1979193e 100644 --- a/src/syng_im/android/core.cljs +++ b/src/syng_im/android/core.cljs @@ -12,6 +12,7 @@ [syng-im.components.chats.chats-list :refer [chats-list]] [syng-im.components.chats.new-group :refer [new-group]] [syng-im.utils.logging :as log] + [syng-im.utils.utils :refer [toast]] [syng-im.navigation :as nav] [syng-im.utils.encryption])) @@ -32,18 +33,23 @@ (add-event-listener "hardwareBackPress" new-listener))))) (defn app-root [] - [navigator {:initial-route (clj->js {:view-id :chat-list}) - :render-scene (fn [route nav] - (log/debug "route" route) - (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) - (case view-id - :chat-list (r/as-element [chats-list {:navigator nav}]) - :new-group (r/as-element [new-group {:navigator nav}]) - :contact-list (r/as-element [contact-list {:navigator nav}]) - :chat (r/as-element [chat {:navigator nav}])))))}]) + (let [signed-up-atom (subscribe [:signed-up])] + (fn [] + (let [signed-up @signed-up-atom] + [navigator {:initial-route (clj->js {:view-id :chat-list}) + :render-scene (fn [route nav] + (log/debug "route" route) + (when true ;; nav/*nav-render* + (if signed-up + (let [{:keys [view-id]} (js->clj route :keywordize-keys true) + view-id (keyword view-id)] + (init-back-button-handler! nav) + (case view-id + :chat-list (r/as-element [chats-list {:navigator nav}]) + :new-group (r/as-element [new-group {:navigator nav}]) + :contact-list (r/as-element [contact-list {:navigator nav}]) + :chat (r/as-element [chat {:navigator nav}]))) + (r/as-element [chat {:navigator nav}]))))}])))) (defn init [] (dispatch-sync [:initialize-db]) @@ -51,6 +57,5 @@ (dispatch [:initialize-protocol]) (dispatch [:load-user-phone-number]) (dispatch [:load-syng-contacts]) - ;; TODO execute on first run only - (dispatch [:set-sign-up-chat]) + (dispatch-sync [:init-console-chat]) (.registerComponent app-registry "SyngIm" #(r/reactify-component app-root))) diff --git a/src/syng_im/constants.cljs b/src/syng_im/constants.cljs index 09cf68ac3a..f4662dd6e6 100644 --- a/src/syng_im/constants.cljs +++ b/src/syng_im/constants.cljs @@ -2,8 +2,8 @@ (def ethereum-rpc-url "http://localhost:8545") -;; (def server-address "http://rpc0.syng.im:20000/") -(def server-address "http://10.0.3.2:3000/") +(def server-address "http://rpc0.syng.im:20000/") +;; (def server-address "http://10.0.3.2:3000/") (def text-content-type "text/plain") (def content-type-command "command") diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index e36bd8d605..9bc355ab95 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -12,7 +12,8 @@ :command nil} :chats {} :chats-updated-signal 0 - :new-group #{}}) + :new-group #{} + :signed-up false}) (def protocol-initialized-path [:protocol-initialized]) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index dd8d8039c9..581014686b 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -195,10 +195,6 @@ (fn [db [_ phone-number handler]] (server/sign-up db phone-number handler))) -(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) @@ -224,11 +220,13 @@ (dispatch [:navigate-to navigator {:view-id :chat}]) db))) -(register-handler :set-sign-up-chat +(register-handler :init-console-chat (fn [db [_]] - (-> db - (set-current-chat-id "console") - sign-up-service/intro))) + (sign-up-service/init db))) + +(register-handler :set-signed-up + (fn [db [_ signed-up]] + (sign-up-service/set-signed-up db signed-up))) ;; -- Chat -------------------------------------------------------------- diff --git a/src/syng_im/handlers/sign_up.cljs b/src/syng_im/handlers/sign_up.cljs index 54448fdf9c..d84a98170d 100644 --- a/src/syng_im/handlers/sign_up.cljs +++ b/src/syng_im/handlers/sign_up.cljs @@ -1,6 +1,9 @@ (ns syng-im.handlers.sign-up (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] + [syng-im.persistence.simple-kv-store :as kv] + [syng-im.protocol.state.storage :as s] [syng-im.db :as db] + [syng-im.models.chat :refer [set-current-chat-id]] [syng-im.models.commands :as commands] [syng-im.utils.utils :refer [log on-error http-post toast]] [syng-im.utils.logging :as log] @@ -18,6 +21,10 @@ :content-type text-content-type :outgoing true}) +(defn- set-signed-up [db signed-up] + (s/put kv/kv-store :signed-up signed-up) + (assoc db :signed-up signed-up)) + ;; -- Send confirmation code and synchronize contacts--------------------------- (defn on-sync-contacts [] @@ -27,7 +34,8 @@ :content-type text-content-type :outgoing false :from "console" - :to "me"}])) + :to "me"}]) + (dispatch [:set-signed-up true])) (defn sync-contacts [] (dispatch [:sync-contacts on-sync-contacts])) @@ -129,7 +137,7 @@ :phone (str "Your phone number is also required to use the app. Type the " "exclamation mark or hit the icon to open the command list " - "and choose the !phone command") ) + "and choose the !phone command")) :content-type content-type-command-request :outgoing false :from "console" @@ -142,14 +150,14 @@ (defn intro [db] (dispatch [:received-msg - {:msg-id "1" + {:msg-id (random/id) :content "Hello there! It's Syng, a Dapp browser in your phone." :content-type text-content-type :outgoing false :from "console" :to "me"}]) (dispatch [:received-msg - {:msg-id "2" + {:msg-id (random/id) :content (str "Syng uses a highly secure key-pair authentication type " "to provide you a reliable way to access your account") :content-type text-content-type @@ -157,7 +165,7 @@ :from "console" :to "me"}]) (dispatch [:received-msg - {:msg-id "3" + {:msg-id (random/id) :content (commands/format-command-request-msg-content :keypair-password (str "A key pair has been generated and saved to your device. " @@ -180,3 +188,12 @@ :content (commands/format-command-msg-content command-key content) :content-type content-type-command :outgoing true}) + +(defn init [db] + (let [signed-up (s/get kv/kv-store :signed-up) + db (if signed-up + db + (-> db + (set-current-chat-id "console") + intro))] + (assoc db :signed-up signed-up))) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index fdb37167fc..2b5defef45 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -94,10 +94,10 @@ (get @db :loading)))) (register-sub - :get-confirmation-code - (fn [db _] - (reaction - (get @db :confirmation-code)))) + :signed-up + (fn [db _] + (reaction + (get @db :signed-up)))) (register-sub :get-contacts