From fdb3aafdefc87cb21bfea874639b0dc292867fae Mon Sep 17 00:00:00 2001 From: virvar Date: Mon, 14 Mar 2016 14:59:52 +0300 Subject: [PATCH] Show syng contacts Former-commit-id: 20edaf4abada72c6acb33d5bbc4fb1068ffa361d --- src/messenger/android/contacts.cljs | 18 +++++++-- src/messenger/android/contacts_list.cljs | 17 ++++---- src/messenger/android/database.cljs | 16 ++++++-- src/messenger/android/sign_up_confirm.cljs | 46 +++++++++++++++------- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/messenger/android/contacts.cljs b/src/messenger/android/contacts.cljs index 40eab0f46d..3c27b1d486 100644 --- a/src/messenger/android/contacts.cljs +++ b/src/messenger/android/contacts.cljs @@ -1,6 +1,7 @@ (ns messenger.android.contacts (:require [messenger.state :as state] - [messenger.android.utils :refer [log toast http-post]])) + [messenger.android.utils :refer [log toast http-post]] + [messenger.android.database :as db])) (def fake-contacts? true) @@ -8,7 +9,7 @@ (defn generate-contact [n] {:name (str "Contact " n) - :photo "" + :photo-path "" :phone-numbers [{:label "mobile" :number (apply str (repeat 7 n))}] :delivery-status (if (< (rand) 0.5) :delivered :seen) :datetime "15:30" @@ -18,7 +19,7 @@ (defn generate-contacts [n] (map generate-contact (range 1 (inc n)))) -(defn load-contacts [on-success on-error] +(defn load-phone-contacts [on-success on-error] (if fake-contacts? (on-success (generate-contacts 10)) (.getAll react-native-contacts @@ -27,9 +28,18 @@ (let [contacts (map (fn [contact] (merge (generate-contact 1) {:name (:givenName contact) - :photo (:thumbnailPath contact) + :photo-path (:thumbnailPath contact) :phone-numbers (:phoneNumbers contact)})) (js->clj raw-contacts :keywordize-keys true))] (on-success contacts)) (when on-error (on-error error))))))) + +(defn load-whisper-contacts [] + (map (fn [contact] + (merge contact + {:delivery-status (if (< (rand) 0.5) :delivered :seen) + :datetime "15:30" + :new-messages-count (rand-int 3) + :online (< (rand) 0.5)})) + (db/get-contacts))) diff --git a/src/messenger/android/contacts_list.cljs b/src/messenger/android/contacts_list.cljs index 5856f28248..889d74db60 100644 --- a/src/messenger/android/contacts_list.cljs +++ b/src/messenger/android/contacts_list.cljs @@ -31,10 +31,10 @@ [:contact/by-name name]) static om/IQuery (query [this] - '[:name :photo :delivery-status :datetime :new-messages-count :online]) + '[:name :photo-path :delivery-status :datetime :new-messages-count :online]) Object (render [this] - (let [{:keys [name photo delivery-status datetime new-messages-count online]} + (let [{:keys [name photo-path delivery-status datetime new-messages-count online]} (dissoc (om/props this) :om.next/computed) {:keys [nav]} (om/get-computed this)] (touchable-highlight @@ -53,8 +53,8 @@ :borderRadius 50 :backgroundColor "#FFFFFF" :elevation 6} - (image {:source (if (< 0 (count photo)) - {:uri photo} + (image {:source (if (< 0 (count photo-path)) + {:uri photo-path} res/user-no-photo) :style {:borderWidth 2 :borderColor "#FFFFFF" @@ -137,12 +137,9 @@ {:nav nav}))) (defn load-contacts [] - (contacts/load-contacts - (fn [contacts] - (swap! state/app-state update :contacts-ds - #(clone-with-rows % contacts))) - (fn [error] - (toast (str error))))) + (let [contacts (contacts/load-whisper-contacts)] + (swap! state/app-state update :contacts-ds + #(clone-with-rows % contacts)))) (defui ContactsList static om/IQuery diff --git a/src/messenger/android/database.cljs b/src/messenger/android/database.cljs index 6bac5a3a57..bb7fa11f62 100644 --- a/src/messenger/android/database.cljs +++ b/src/messenger/android/database.cljs @@ -5,7 +5,9 @@ (def realm (js/Realm. (clj->js {:schema [{:name "Contact" :properties {:phone-number "string" - :whisper-identity "string"}}]}))) + :whisper-identity "string" + :name "string" + :photo-path "string"}}]}))) (defn write [f] (.write realm f)) @@ -14,7 +16,11 @@ (.objects realm "Contact")) (defn get-contacts [] - (js->clj (get-contacts-objects) :keywordize-keys true)) + (vals (js->clj (get-contacts-objects) :keywordize-keys true))) + +(defn delete-contacts [] + (write (fn [] + (.delete realm (get-contacts-objects))))) (defn filtered [objs query] (.filtered objs query)) @@ -22,10 +28,12 @@ (defn get-count [objs] (.-length objs)) -(defn create-contact [{:keys [phone-number whisper-identity]}] +(defn create-contact [{:keys [phone-number whisper-identity name photo-path]}] (.create realm "Contact" (clj->js {:phone-number phone-number - :whisper-identity whisper-identity}))) + :whisper-identity whisper-identity + :name (or name "") + :photo-path (or photo-path "")}))) (defn contact-exist? [contacts contact] (some #(= (:phone-number contact) (:phone-number %)) contacts)) diff --git a/src/messenger/android/sign_up_confirm.cljs b/src/messenger/android/sign_up_confirm.cljs index 238d1d969c..c7146f17ca 100644 --- a/src/messenger/android/sign_up_confirm.cljs +++ b/src/messenger/android/sign_up_confirm.cljs @@ -5,7 +5,8 @@ [natal-shell.async-storage :refer [get-item set-item]] [natal-shell.core :refer [with-error-view]] [natal-shell.alert :refer [alert]]) - (:require [om.next :as om :refer-macros [defui]] + (:require [clojure.string :as cstr] + [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [syng-im.protocol.web3 :as whisper] [messenger.state :as state] @@ -23,26 +24,41 @@ (.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 handle-load-contacts-identities-response [contacts-by-hash data] - (let [contacts (map (fn [contact] - {:phone-number (get contacts-by-hash - (:phone-number-hash contact)) - :whisper-identity (:whisper-identity contact)}) + (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 (reduce (fn [numbers contact] - (into numbers - (map :number (:phone-numbers contact)))) - '() - contacts)] - (reduce (fn [m number] - (let [hash (encrypt number)] - (assoc m hash number))) + (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))) + numbers-info))) (defn send-load-contacts-identities [contacts] (let [contacts-by-hash (get-contacts-by-hash contacts) @@ -53,7 +69,7 @@ (toast (str error)))))) (defn load-contacts [] - (contacts/load-contacts + (contacts/load-phone-contacts send-load-contacts-identities (fn [error] (toast (str error)))))