mirror of
https://github.com/status-im/status-react.git
synced 2025-01-10 19:16:59 +00:00
code cleaning
Former-commit-id: 71aef771d8f35965fe21303d9b48f53de8e51e58
This commit is contained in:
parent
500645e0aa
commit
1d633930d9
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
(def defaults {:gap 10
|
(def defaults {:gap 10
|
||||||
:sneak 10
|
:sneak 10
|
||||||
:pageWidth (- (page-width) 50)})
|
:pageWidth (- (page-width) 40)})
|
||||||
|
|
||||||
|
|
||||||
(defn get-gap [data]
|
(defn get-gap [data]
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
(:require
|
(:require
|
||||||
[syng-im.utils.debug :refer [log]]
|
[syng-im.utils.debug :refer [log]]
|
||||||
[re-frame.core :refer [dispatch]]
|
[re-frame.core :refer [dispatch]]
|
||||||
|
[syng-im.models.discoveries :refer [save-discoveries]]
|
||||||
[syng-im.components.react :refer [android?
|
[syng-im.components.react :refer [android?
|
||||||
view
|
view
|
||||||
scroll-view
|
scroll-view
|
||||||
@ -38,17 +39,16 @@
|
|||||||
:height 56
|
:height 56
|
||||||
:elevation 0}
|
:elevation 0}
|
||||||
:onIconClicked (fn []
|
:onIconClicked (fn []
|
||||||
(realm/write (fn []
|
(let [number (rand-int 999)]
|
||||||
(let [number (rand-int 30)]
|
(do
|
||||||
(realm/create :discoveries
|
(save-discoveries [{:name (str "Name " number)
|
||||||
{:name (str "c" number)
|
:status (str "Status " number)
|
||||||
:status (str "Status " number)
|
:whisper-id (str number)
|
||||||
:whisper-id (str number)
|
:photo ""
|
||||||
:photo ""
|
:location ""
|
||||||
:location ""
|
:tags ["tag1" "tag2" "tag3"]
|
||||||
:tags [{:name "tag1"} {:name "tag2"}]
|
:last-updated (new js/Date)}])
|
||||||
:last-updated (new js/Date)} true)
|
(dispatch [:updated-discoveries]))))
|
||||||
(dispatch [:updated-discoveries])))))
|
|
||||||
;; temporary dispatch for testing
|
;; temporary dispatch for testing
|
||||||
:onActionSelected (fn [index]
|
:onActionSelected (fn [index]
|
||||||
(if @showSearch
|
(if @showSearch
|
||||||
@ -100,5 +100,4 @@
|
|||||||
(comment
|
(comment
|
||||||
(def page-width (aget (natal-shell.dimensions/get "window") "width"))
|
(def page-width (aget (natal-shell.dimensions/get "window") "width"))
|
||||||
(def page-height (aget (natal-shell.dimensions/get "window") "height"))
|
(def page-height (aget (natal-shell.dimensions/get "window") "height"))
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -3,31 +3,27 @@
|
|||||||
[syng-im.utils.debug :refer [log]]
|
[syng-im.utils.debug :refer [log]]
|
||||||
[syng-im.protocol.api :as api]
|
[syng-im.protocol.api :as api]
|
||||||
[syng-im.models.discoveries :refer [save-discoveries
|
[syng-im.models.discoveries :refer [save-discoveries
|
||||||
discovery-list
|
signal-discoveries-updated]]))
|
||||||
signal-discovery-updated
|
|
||||||
discovery-updated?]]))
|
|
||||||
|
|
||||||
|
|
||||||
;; -- Discovery --------------------------------------------------------------
|
;; -- Discovery --------------------------------------------------------------
|
||||||
|
|
||||||
(register-handler :discovery-response-received
|
(register-handler :discovery-response-received
|
||||||
(fn [db [_ from payload]]
|
(fn [db [_ from payload]]
|
||||||
(let [{:keys [status hashtags location]} payload
|
(let [{:keys [name status hashtags location]} payload
|
||||||
location (if location location "")]
|
location (if location location "")]
|
||||||
(save-discoveries [{:name from
|
(save-discoveries [{:name name
|
||||||
:status status
|
:status status
|
||||||
:whisper-id from
|
:whisper-id from
|
||||||
:photo ""
|
:photo ""
|
||||||
:location location
|
:location location
|
||||||
:tags hashtags
|
:tags hashtags
|
||||||
:last-updated (js/Date.)}])
|
:last-updated (js/Date.)}])
|
||||||
(signal-discovery-updated db)
|
(signal-discoveries-updated db))))
|
||||||
db)))
|
|
||||||
|
|
||||||
(register-handler :updated-discoveries
|
(register-handler :updated-discoveries
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(signal-discovery-updated db)
|
(signal-discoveries-updated db)))
|
||||||
db))
|
|
||||||
|
|
||||||
(register-handler :broadcast-status
|
(register-handler :broadcast-status
|
||||||
(fn [db [action status hashtags]]
|
(fn [db [action status hashtags]]
|
||||||
|
@ -7,116 +7,89 @@
|
|||||||
[syng-im.resources :as res]
|
[syng-im.resources :as res]
|
||||||
[syng-im.db :as db]))
|
[syng-im.db :as db]))
|
||||||
|
|
||||||
;; TODO see https://github.com/rt2zz/react-native-contacts/issues/45
|
(defn signal-discoveries-updated [db]
|
||||||
(def fake-discoveries? true)
|
|
||||||
|
|
||||||
(defn signal-discovery-updated [db]
|
|
||||||
(update-in db db/updated-discoveries-signal-path (fn [current]
|
(update-in db db/updated-discoveries-signal-path (fn [current]
|
||||||
(if current
|
(if current
|
||||||
(inc current)
|
(inc current)
|
||||||
0))))
|
0))))
|
||||||
|
|
||||||
(defn discovery-updated? [db]
|
(defn discoveries-updated? [db]
|
||||||
(get-in db db/updated-discoveries-signal-path))
|
(get-in db db/updated-discoveries-signal-path))
|
||||||
|
|
||||||
(defn generate-discovery [n]
|
|
||||||
{:name (str "Contact " n)
|
|
||||||
:status (apply str (repeat (+ n 3) "Contact Status "))
|
|
||||||
:whisper-id (str "id-" n)
|
|
||||||
:location ""
|
|
||||||
:photo ""
|
|
||||||
:tags ["tag1" "tag2"]
|
|
||||||
:last-updated (js/Date. "10/01/2015")
|
|
||||||
})
|
|
||||||
|
|
||||||
(defn- generate-discoveries [n]
|
|
||||||
(map generate-discovery (range 1 (inc n))))
|
|
||||||
|
|
||||||
(def fake-discoveries (generate-discoveries 20))
|
|
||||||
|
|
||||||
(defn- get-discoveries []
|
|
||||||
(let [list (realm/get-list :discoveries)
|
|
||||||
_ (log list)]
|
|
||||||
(if (> (.-length list) 0) (.slice list 0) [])))
|
|
||||||
|
|
||||||
(defn load-syng-discoveries [db]
|
|
||||||
(let [discoveries (map (fn [discovery]
|
|
||||||
(merge discovery
|
|
||||||
{}))
|
|
||||||
(get-discoveries))]
|
|
||||||
(assoc db :discoveries discoveries)))
|
|
||||||
|
|
||||||
(defn get-tag [tag]
|
(defn get-tag [tag]
|
||||||
|
(let [_ (log (str "Getting tag: " tag))]
|
||||||
(-> (r/get-by-field :tag :name tag)
|
(-> (r/get-by-field :tag :name tag)
|
||||||
(r/single-cljs)))
|
(r/single-cljs))))
|
||||||
|
|
||||||
(defn remove-tag [tag]
|
(defn decrease-tag-counter [tag]
|
||||||
(let [tag-object (get-tag tag)]
|
(let [tag (:name tag)
|
||||||
|
tag-object (get-tag tag)]
|
||||||
(if tag-object
|
(if tag-object
|
||||||
(realm/create :tag
|
(let [counter (dec (:count tag-object))]
|
||||||
{:name tag
|
(if (= counter 0)
|
||||||
:count (dec (:count tag-object))
|
(realm/delete tag-object)
|
||||||
}))))
|
(realm/create :tag {:name tag
|
||||||
|
:count counter}
|
||||||
|
true))))))
|
||||||
|
|
||||||
(defn add-tag [tag]
|
(defn increase-tag-counter [tag]
|
||||||
(let [tag-object (get-tag tag)
|
(let [tag (:name tag)
|
||||||
counter (if tag-object (:count tag-object) 1)]
|
tag-object (get-tag tag)]
|
||||||
(realm/create :tag
|
(if tag-object
|
||||||
{:name tag
|
(realm/create :tag {:name tag
|
||||||
:count (inc counter)
|
:count (inc (:count tag-object))}
|
||||||
}
|
true))))
|
||||||
(if tag-object true false))))
|
|
||||||
|
|
||||||
(defn remove-tags [tags]
|
(defn decrease-tags-counter [tags]
|
||||||
(doseq [tag tags]
|
(doseq [tag tags]
|
||||||
(remove-tag tag)))
|
(decrease-tag-counter tag)))
|
||||||
|
|
||||||
(defn add-tags [tags]
|
(defn increase-tags-counter [tags]
|
||||||
(doseq [tag tags]
|
(doseq [tag tags]
|
||||||
(add-tag tag)))
|
(increase-tag-counter tag)))
|
||||||
|
|
||||||
(defn get-tags [whisper-id]
|
(defn get-tags [whisper-id]
|
||||||
(:tags (-> (r/get-by-field :discoveries :whisper-id whisper-id)
|
(:tags (-> (r/get-by-field :discoveries :whisper-id whisper-id)
|
||||||
(r/single-cljs))))
|
(r/single-cljs))))
|
||||||
|
|
||||||
(defn- create-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
|
(defn- create-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
|
||||||
(do
|
(let [tags (mapv (fn [tag] {:name tag}) tags)
|
||||||
;(add-tags tags)
|
discovery {:name name
|
||||||
(realm/create :discoveries
|
|
||||||
{:name name
|
|
||||||
:status status
|
:status status
|
||||||
:whisper-id whisper-id
|
:whisper-id whisper-id
|
||||||
:photo photo
|
:photo photo
|
||||||
:location location
|
:location location
|
||||||
:tags (mapv (fn [tag]
|
:tags tags
|
||||||
{:name tag}) tags)
|
:last-updated last-updated}
|
||||||
:last-updated last-updated} true)
|
_ (log "Creating discovery")
|
||||||
)
|
_ (log discovery)
|
||||||
)
|
_ (log tags)]
|
||||||
|
(do
|
||||||
|
(realm/create :discoveries discovery true)
|
||||||
|
(increase-tags-counter tags))))
|
||||||
|
|
||||||
(defn- update-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
|
(defn- update-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
|
||||||
(let [old-tags (get-tags whisper-id)]
|
(let [old-tags (get-tags whisper-id)
|
||||||
(do
|
tags (mapv (fn [tag] {:name tag}) tags)
|
||||||
;;(remove-tags old-tags)
|
discovery {:name name
|
||||||
;;(add-tags tags)
|
|
||||||
(realm/create :discoveries
|
|
||||||
{:name name
|
|
||||||
:status status
|
:status status
|
||||||
:whisper-id whisper-id
|
:whisper-id whisper-id
|
||||||
:photo photo
|
:photo photo
|
||||||
:location location
|
:location location
|
||||||
:tags (mapv (fn [tag]
|
:tags tags
|
||||||
{:name tag}) tags)
|
:last-updated last-updated}]
|
||||||
:last-updated last-updated}
|
(do
|
||||||
true)
|
(decrease-tags-counter old-tags)
|
||||||
))
|
(realm/create :discoveries discovery true)
|
||||||
)
|
(increase-tags-counter tags))))
|
||||||
|
|
||||||
(defn- discovery-exist? [discoveries discovery]
|
(defn- discovery-exist? [discoveries discovery]
|
||||||
(some #(= (:whisper-id discovery) (:whisper-id %)) discoveries))
|
(some #(= (:whisper-id discovery) (:whisper-id %)) discoveries))
|
||||||
|
|
||||||
|
(defn discovery-list []
|
||||||
|
(-> (r/get-all :discoveries)
|
||||||
|
(r/sorted :last-updated :desc)))
|
||||||
|
|
||||||
(defn- add-discoveries [discoveries]
|
(defn- add-discoveries [discoveries]
|
||||||
(realm/write (fn []
|
(realm/write (fn []
|
||||||
(let [db-discoveries (.slice (discovery-list) 0)
|
(let [db-discoveries (.slice (discovery-list) 0)
|
||||||
@ -132,51 +105,14 @@
|
|||||||
(defn save-discoveries [discoveries]
|
(defn save-discoveries [discoveries]
|
||||||
(add-discoveries discoveries))
|
(add-discoveries discoveries))
|
||||||
|
|
||||||
(defn discovery-list []
|
|
||||||
(-> (r/get-all :discoveries)
|
|
||||||
(r/sorted :last-updated :desc)))
|
|
||||||
|
|
||||||
(defn discoveries-by-tag [tag limit]
|
(defn discoveries-by-tag [tag limit]
|
||||||
(let [_ (log (str "discoveries by tag: " tag))]
|
(let [_ (log (str "discoveries by tag: " tag))]
|
||||||
(-> (r/get-by-filter :discoveries (str "tags.name = '" tag "'"))
|
(-> (r/get-by-filter :discoveries (str "tags.name = '" tag "'"))
|
||||||
(r/sorted :last-updated :desc)
|
(r/sorted :last-updated :desc)
|
||||||
(r/page 0 limit))))
|
(r/page 0 limit))))
|
||||||
|
|
||||||
(defn get-discovery-recent [discoveries limit]
|
|
||||||
(if fake-discoveries?
|
|
||||||
(take limit fake-discoveries)
|
|
||||||
(-> (r/sorted discoveries :last-updated :desc)
|
|
||||||
(r/page 0 limit)))
|
|
||||||
)
|
|
||||||
|
|
||||||
(defn get-tag-popular [limit]
|
(defn get-tag-popular [limit]
|
||||||
(-> (r/get-all :tag)
|
(-> (r/get-all :tag)
|
||||||
(r/sorted :count :desc)
|
(r/sorted :count :desc)
|
||||||
(r/page 0 limit)))
|
(r/page 0 limit)))
|
||||||
|
|
||||||
(defn add-with-limit [data value limit]
|
|
||||||
(if (>= (count data) limit)
|
|
||||||
data
|
|
||||||
(conj data value))
|
|
||||||
)
|
|
||||||
|
|
||||||
(defn group-by-tag [discoveries tag-limit]
|
|
||||||
(reduce (fn [result discovery]
|
|
||||||
(let [keys (:tags discovery)]
|
|
||||||
(reduce (fn [data key]
|
|
||||||
(assoc data (keyword key) (add-with-limit (get data (keyword key) []) discovery tag-limit)))
|
|
||||||
result
|
|
||||||
keys)))
|
|
||||||
{}
|
|
||||||
discoveries)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
(comment
|
|
||||||
(group-by-tag [{:tags ["a" "b" "c"]
|
|
||||||
:name "test1"}
|
|
||||||
{:tags ["a" "c"]
|
|
||||||
:name "test2"}
|
|
||||||
{:tags ["c"]
|
|
||||||
:name "test3"}])
|
|
||||||
)
|
|
@ -50,7 +50,7 @@
|
|||||||
:properties {:name "string"
|
:properties {:name "string"
|
||||||
:count {:type "int"
|
:count {:type "int"
|
||||||
:optional true
|
:optional true
|
||||||
:default 1}}}
|
:default 0}}}
|
||||||
{:name :discoveries
|
{:name :discoveries
|
||||||
:primaryKey :whisper-id
|
:primaryKey :whisper-id
|
||||||
:properties {:name "string"
|
:properties {:name "string"
|
||||||
|
@ -8,9 +8,6 @@
|
|||||||
[syng-im.models.chats :refer [chats-list
|
[syng-im.models.chats :refer [chats-list
|
||||||
chats-updated?
|
chats-updated?
|
||||||
chat-by-id]]
|
chat-by-id]]
|
||||||
[syng-im.models.discoveries :refer [discovery-list
|
|
||||||
signal-discovery-updated
|
|
||||||
discovery-updated?]]
|
|
||||||
[syng-im.models.messages :refer [get-messages]]
|
[syng-im.models.messages :refer [get-messages]]
|
||||||
[syng-im.models.contacts :refer [contacts-list]]
|
[syng-im.models.contacts :refer [contacts-list]]
|
||||||
[syng-im.handlers.suggestions :refer [get-suggestions]]))
|
[syng-im.handlers.suggestions :refer [get-suggestions]]))
|
||||||
|
@ -6,33 +6,32 @@
|
|||||||
[syng-im.models.discoveries :refer [discovery-list
|
[syng-im.models.discoveries :refer [discovery-list
|
||||||
get-tag-popular
|
get-tag-popular
|
||||||
discoveries-by-tag
|
discoveries-by-tag
|
||||||
signal-discovery-updated
|
discoveries-updated?]]))
|
||||||
discovery-updated?]]))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(register-sub :get-discoveries
|
(register-sub :get-discoveries
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(let [discovery-updated (-> (discovery-updated? @db)
|
(let [discoveries-updated (-> (discoveries-updated? @db)
|
||||||
(reaction))]
|
(reaction))]
|
||||||
(reaction
|
(reaction
|
||||||
(let [_ @discovery-updated]
|
(let [_ @discoveries-updated]
|
||||||
(discovery-list))))))
|
(discovery-list))))))
|
||||||
|
|
||||||
(register-sub :get-discoveries-by-tag
|
(register-sub :get-discoveries-by-tag
|
||||||
(fn [db [_ tag limit]]
|
(fn [db [_ tag limit]]
|
||||||
(let [discovery-updated (-> (discovery-updated? @db)
|
(let [discoveries-updated (-> (discoveries-updated? @db)
|
||||||
(reaction))
|
(reaction))
|
||||||
_ (log (str "getting discoveries for: " tag))]
|
_ (log (str "getting discoveries for: " tag))]
|
||||||
(reaction
|
(reaction
|
||||||
(let [_ @discovery-updated]
|
(let [_ @discoveries-updated]
|
||||||
(discoveries-by-tag tag limit))))))
|
(discoveries-by-tag tag limit))))))
|
||||||
|
|
||||||
(register-sub :get-popular-tags
|
(register-sub :get-popular-tags
|
||||||
(fn [db [_ limit]]
|
(fn [db [_ limit]]
|
||||||
(let [discovery-updated (-> (discovery-updated? @db)
|
(let [discoveries-updated (-> (discoveries-updated? @db)
|
||||||
(reaction))
|
(reaction))
|
||||||
_ (log (str "getting tags limited: " limit))]
|
_ (log (str "getting tags limited: " limit))]
|
||||||
(reaction
|
(reaction
|
||||||
(let [_ @discovery-updated]
|
(let [_ @discoveries-updated]
|
||||||
(get-tag-popular limit))))))
|
(get-tag-popular limit))))))
|
Loading…
x
Reference in New Issue
Block a user