Skip to content

Commit

Permalink
Merge pull request #8 from syng-im/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
virvar committed Mar 17, 2016
2 parents e7861df + b21ce2d commit 8419be3
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 214 deletions.
2 changes: 1 addition & 1 deletion src/messenger/android/chat.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(:require [om.next :as om :refer-macros [defui]]
[re-natal.support :as sup]
[messenger.state :as state]
[messenger.android.resources :as res]))
[messenger.utils.resources :as res]))

(set! js/InvertibleScrollView (js/require "react-native-invertible-scroll-view"))

Expand Down
45 changes: 0 additions & 45 deletions src/messenger/android/contacts.cljs

This file was deleted.

14 changes: 5 additions & 9 deletions src/messenger/android/contacts_list.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
(:require-macros
[natal-shell.components :refer [view text image touchable-highlight list-view
toolbar-android]]
[natal-shell.data-source :refer [data-source clone-with-rows]]
[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]
[messenger.state :as state]
[messenger.android.utils :refer [log toast http-post]]
[messenger.android.resources :as res]
[messenger.android.contacts :as contacts]
[messenger.utils.utils :refer [log toast http-post]]
[messenger.utils.resources :as res]
[messenger.comm.intercom :as intercom]
[messenger.android.chat :refer [chat]]))

(def fake-contacts? true)
Expand Down Expand Up @@ -137,9 +135,7 @@
{:nav nav})))

(defn load-contacts []
(let [contacts (contacts/load-whisper-contacts)]
(swap! state/app-state update :contacts-ds
#(clone-with-rows % contacts))))
(intercom/load-syng-contacts))

(defui ContactsList
static om/IQuery
Expand Down
27 changes: 15 additions & 12 deletions src/messenger/android/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
(:require [om.next :as om :refer-macros [defui]]
[re-natal.support :as sup]
[messenger.state :as state]
[messenger.utils.utils :refer [log toast]]
[messenger.android.login :refer [login]]
[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 :as intercom :refer [load-user-phone-number
load-user-whisper-identity]]))

(def app-registry (.-AppRegistry js/React))

Expand All @@ -34,20 +36,20 @@
(defui AppRoot
static om/IQuery
(query [this]
'[:page :contacts-ds :user-phone-number :confirmation-code])
'[:contacts-ds :user-phone-number :confirmation-code])
Object
(render [this]
(let [{:keys [page]} (om/props this)]
(navigator
{:initialRoute {:component login}
:renderScene (fn [route nav]
(when state/*nav-render*
(init-back-button-handler! nav)
(let [{:keys [component]}
(js->clj route :keywordize-keys true)]
(component (om/computed (om/props this)
{:nav nav})))))}))))
(navigator
{:initialRoute {:component login}
:renderScene (fn [route nav]
(when state/*nav-render*
(init-back-button-handler! nav)
(let [{:keys [component]}
(js->clj route :keywordize-keys true)]
(component (om/computed (om/props this)
{:nav nav})))))})))

;; TODO to service?
(swap! state/app-state assoc :contacts-ds
(data-source {:rowHasChanged (fn [row1 row2]
(not= row1 row2))}))
Expand All @@ -59,5 +61,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)))
81 changes: 15 additions & 66 deletions src/messenger/android/login.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,42 @@
(: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.android.utils :refer [log toast http-post]]
[messenger.android.resources :as res]
[messenger.comm.intercom :as intercom :refer [set-user-phone-number]]
[messenger.utils.utils :refer [log toast http-post]]
[messenger.utils.resources :as res]
[messenger.android.sign-up-confirm :refer [sign-up-confirm]]))

(def nav-atom (atom nil))

(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)]
(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
Expand All @@ -114,13 +63,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))
75 changes: 12 additions & 63 deletions src/messenger/android/sign_up_confirm.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
[natal-shell.async-storage :refer [get-item set-item]]
[natal-shell.core :refer [with-error-view]]
[natal-shell.alert :refer [alert]])
(:require [clojure.string :as cstr]
[om.next :as om :refer-macros [defui]]
(:require [om.next :as om :refer-macros [defui]]
[re-natal.support :as sup]
[syng-im.protocol.web3 :as whisper]
[messenger.state :as state]
[messenger.android.utils :refer [log toast http-post]]
[messenger.android.crypt :refer [encrypt]]
[messenger.android.resources :as res]
[messenger.android.database :as db]
[messenger.android.contacts :as contacts]
[messenger.utils.utils :refer [log toast]]
[messenger.utils.resources :as res]
[messenger.comm.intercom :as intercom :refer [set-confirmation-code]]
[messenger.android.contacts-list :refer [contacts-list]]))

(def nav-atom (atom nil))
Expand All @@ -24,76 +21,28 @@
(.replace @nav-atom (clj->js {:component contacts-list
:name "contacts-list"}))))

(defn get-contact-name [phone-contact]
(cstr/join " "
(filter #(not (cstr/blank? %))
[(:givenName phone-contact)
(:middleName phone-contact)
(:familyName phone-contact)])))
(defn sync-contacts []
(intercom/sync-contacts show-home-view))

(defn handle-load-contacts-identities-response [contacts-by-hash data]
(let [contacts (map (fn [server-contact]
(let [number-info (get contacts-by-hash
(:phone-number-hash server-contact))
phone-contact (:contact number-info)]
{:phone-number (:number number-info)
:whisper-identity (:whisper-identity server-contact)
:name (get-contact-name phone-contact)
:photo-path (:photo-path phone-contact)}))
(js->clj (:contacts data)))]
(db/add-contacts contacts)
(show-home-view)))

(defn get-contacts-by-hash [contacts]
(let [numbers-info (reduce (fn [numbers contact]
(into numbers
(map (fn [c]
{: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 send-load-contacts-identities [contacts]
(let [contacts-by-hash (get-contacts-by-hash contacts)
data (keys contacts-by-hash)]
(http-post "get-contacts" {:phone-number-hashes data}
(partial handle-load-contacts-identities-response contacts-by-hash)
(fn [error]
(toast (str error))))))

(defn load-contacts []
(contacts/load-phone-contacts
send-load-contacts-identities
(fn [error]
(toast (str error)))))

(defn handle-send-code-response [body]
(defn on-send-code-response [body]
(log body)
(toast (if (:confirmed body)
"Confirmed"
"Wrong code"))
(when (:confirmed body)
(load-contacts)))
;; TODO user action required
(sync-contacts)))

(defn code-valid? [code]
(= 4 (count code)))

(defn send-code [code]
(when (code-valid? code)
(http-post "sign-up-confirm"
{:code code}
handle-send-code-response)))
(intercom/sign-up-confirm code on-send-code-response)))

(defn update-code [value]
(let [formatted value]
(swap! state/app-state assoc :confirmation-code formatted)))
(set-confirmation-code formatted)))

(defui SignUpConfirm
static om/IQuery
Expand Down Expand Up @@ -134,7 +83,7 @@
:style {:alignSelf "center"
:borderRadius 7
:backgroundColor "#E5F5F6"

:width 100}}
(text {:style {:marginVertical 10
:textAlign "center"}}
Expand Down
Loading

0 comments on commit 8419be3

Please sign in to comment.