Show syng contacts

Former-commit-id: 20edaf4aba
This commit is contained in:
virvar 2016-03-14 14:59:52 +03:00
parent 1441edc9e8
commit fdb3aafdef
4 changed files with 64 additions and 33 deletions

View File

@ -1,6 +1,7 @@
(ns messenger.android.contacts (ns messenger.android.contacts
(:require [messenger.state :as state] (: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) (def fake-contacts? true)
@ -8,7 +9,7 @@
(defn generate-contact [n] (defn generate-contact [n]
{:name (str "Contact " n) {:name (str "Contact " n)
:photo "" :photo-path ""
:phone-numbers [{:label "mobile" :number (apply str (repeat 7 n))}] :phone-numbers [{:label "mobile" :number (apply str (repeat 7 n))}]
:delivery-status (if (< (rand) 0.5) :delivered :seen) :delivery-status (if (< (rand) 0.5) :delivered :seen)
:datetime "15:30" :datetime "15:30"
@ -18,7 +19,7 @@
(defn generate-contacts [n] (defn generate-contacts [n]
(map generate-contact (range 1 (inc 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? (if fake-contacts?
(on-success (generate-contacts 10)) (on-success (generate-contacts 10))
(.getAll react-native-contacts (.getAll react-native-contacts
@ -27,9 +28,18 @@
(let [contacts (map (fn [contact] (let [contacts (map (fn [contact]
(merge (generate-contact 1) (merge (generate-contact 1)
{:name (:givenName contact) {:name (:givenName contact)
:photo (:thumbnailPath contact) :photo-path (:thumbnailPath contact)
:phone-numbers (:phoneNumbers contact)})) :phone-numbers (:phoneNumbers contact)}))
(js->clj raw-contacts :keywordize-keys true))] (js->clj raw-contacts :keywordize-keys true))]
(on-success contacts)) (on-success contacts))
(when on-error (when on-error
(on-error 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)))

View File

@ -31,10 +31,10 @@
[:contact/by-name name]) [:contact/by-name name])
static om/IQuery static om/IQuery
(query [this] (query [this]
'[:name :photo :delivery-status :datetime :new-messages-count :online]) '[:name :photo-path :delivery-status :datetime :new-messages-count :online])
Object Object
(render [this] (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) (dissoc (om/props this) :om.next/computed)
{:keys [nav]} (om/get-computed this)] {:keys [nav]} (om/get-computed this)]
(touchable-highlight (touchable-highlight
@ -53,8 +53,8 @@
:borderRadius 50 :borderRadius 50
:backgroundColor "#FFFFFF" :backgroundColor "#FFFFFF"
:elevation 6} :elevation 6}
(image {:source (if (< 0 (count photo)) (image {:source (if (< 0 (count photo-path))
{:uri photo} {:uri photo-path}
res/user-no-photo) res/user-no-photo)
:style {:borderWidth 2 :style {:borderWidth 2
:borderColor "#FFFFFF" :borderColor "#FFFFFF"
@ -137,12 +137,9 @@
{:nav nav}))) {:nav nav})))
(defn load-contacts [] (defn load-contacts []
(contacts/load-contacts (let [contacts (contacts/load-whisper-contacts)]
(fn [contacts] (swap! state/app-state update :contacts-ds
(swap! state/app-state update :contacts-ds #(clone-with-rows % contacts))))
#(clone-with-rows % contacts)))
(fn [error]
(toast (str error)))))
(defui ContactsList (defui ContactsList
static om/IQuery static om/IQuery

View File

@ -5,7 +5,9 @@
(def realm (js/Realm. (clj->js {:schema [{:name "Contact" (def realm (js/Realm. (clj->js {:schema [{:name "Contact"
:properties {:phone-number "string" :properties {:phone-number "string"
:whisper-identity "string"}}]}))) :whisper-identity "string"
:name "string"
:photo-path "string"}}]})))
(defn write [f] (defn write [f]
(.write realm f)) (.write realm f))
@ -14,7 +16,11 @@
(.objects realm "Contact")) (.objects realm "Contact"))
(defn get-contacts [] (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] (defn filtered [objs query]
(.filtered objs query)) (.filtered objs query))
@ -22,10 +28,12 @@
(defn get-count [objs] (defn get-count [objs]
(.-length 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" (.create realm "Contact"
(clj->js {:phone-number phone-number (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] (defn contact-exist? [contacts contact]
(some #(= (:phone-number contact) (:phone-number %)) contacts)) (some #(= (:phone-number contact) (:phone-number %)) contacts))

View File

@ -5,7 +5,8 @@
[natal-shell.async-storage :refer [get-item set-item]] [natal-shell.async-storage :refer [get-item set-item]]
[natal-shell.core :refer [with-error-view]] [natal-shell.core :refer [with-error-view]]
[natal-shell.alert :refer [alert]]) [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] [re-natal.support :as sup]
[syng-im.protocol.web3 :as whisper] [syng-im.protocol.web3 :as whisper]
[messenger.state :as state] [messenger.state :as state]
@ -23,26 +24,41 @@
(.replace @nav-atom (clj->js {:component contacts-list (.replace @nav-atom (clj->js {:component contacts-list
:name "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] (defn handle-load-contacts-identities-response [contacts-by-hash data]
(let [contacts (map (fn [contact] (let [contacts (map (fn [server-contact]
{:phone-number (get contacts-by-hash (let [number-info (get contacts-by-hash
(:phone-number-hash contact)) (:phone-number-hash server-contact))
:whisper-identity (:whisper-identity 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)))] (js->clj (:contacts data)))]
(db/add-contacts contacts) (db/add-contacts contacts)
(show-home-view))) (show-home-view)))
(defn get-contacts-by-hash [contacts] (defn get-contacts-by-hash [contacts]
(let [numbers (reduce (fn [numbers contact] (let [numbers-info (reduce (fn [numbers contact]
(into numbers (into numbers
(map :number (:phone-numbers contact)))) (map (fn [c]
'() {:number (:number c)
contacts)] :contact contact})
(reduce (fn [m number] (:phone-numbers contact))))
(let [hash (encrypt number)] '()
(assoc m hash number))) 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] (defn send-load-contacts-identities [contacts]
(let [contacts-by-hash (get-contacts-by-hash contacts) (let [contacts-by-hash (get-contacts-by-hash contacts)
@ -53,7 +69,7 @@
(toast (str error)))))) (toast (str error))))))
(defn load-contacts [] (defn load-contacts []
(contacts/load-contacts (contacts/load-phone-contacts
send-load-contacts-identities send-load-contacts-identities
(fn [error] (fn [error]
(toast (str error))))) (toast (str error)))))