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
(: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)))

View File

@ -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]
(let [contacts (contacts/load-whisper-contacts)]
(swap! state/app-state update :contacts-ds
#(clone-with-rows % contacts)))
(fn [error]
(toast (str error)))))
#(clone-with-rows % contacts))))
(defui ContactsList
static om/IQuery

View File

@ -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))

View File

@ -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]
(let [numbers-info (reduce (fn [numbers contact]
(into numbers
(map :number (:phone-numbers contact))))
(map (fn [c]
{:number (:number c)
:contact contact})
(:phone-numbers contact))))
'()
contacts)]
(reduce (fn [m number]
(let [hash (encrypt number)]
(assoc m hash number)))
(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)))))