code cleaning

This commit is contained in:
Adrian Tiberius 2016-04-28 03:33:58 +03:00
parent ca656906c1
commit 71aef771d8
7 changed files with 79 additions and 152 deletions

View File

@ -12,7 +12,7 @@
(def defaults {:gap 10
:sneak 10
:pageWidth (- (page-width) 50)})
:pageWidth (- (page-width) 40)})
(defn get-gap [data]

View File

@ -3,6 +3,7 @@
(:require
[syng-im.utils.debug :refer [log]]
[re-frame.core :refer [dispatch]]
[syng-im.models.discoveries :refer [save-discoveries]]
[syng-im.components.react :refer [android?
view
scroll-view
@ -38,17 +39,16 @@
:height 56
:elevation 0}
:onIconClicked (fn []
(realm/write (fn []
(let [number (rand-int 30)]
(realm/create :discoveries
{:name (str "c" number)
:status (str "Status " number)
:whisper-id (str number)
:photo ""
:location ""
:tags [{:name "tag1"} {:name "tag2"}]
:last-updated (new js/Date)} true)
(dispatch [:updated-discoveries])))))
(let [number (rand-int 999)]
(do
(save-discoveries [{:name (str "Name " number)
:status (str "Status " number)
:whisper-id (str number)
:photo ""
:location ""
:tags ["tag1" "tag2" "tag3"]
:last-updated (new js/Date)}])
(dispatch [:updated-discoveries]))))
;; temporary dispatch for testing
:onActionSelected (fn [index]
(if @showSearch
@ -100,5 +100,4 @@
(comment
(def page-width (aget (natal-shell.dimensions/get "window") "width"))
(def page-height (aget (natal-shell.dimensions/get "window") "height"))
)

View File

@ -3,31 +3,27 @@
[syng-im.utils.debug :refer [log]]
[syng-im.protocol.api :as api]
[syng-im.models.discoveries :refer [save-discoveries
discovery-list
signal-discovery-updated
discovery-updated?]]))
signal-discoveries-updated]]))
;; -- Discovery --------------------------------------------------------------
(register-handler :discovery-response-received
(fn [db [_ from payload]]
(let [{:keys [status hashtags location]} payload
(let [{:keys [name status hashtags location]} payload
location (if location location "")]
(save-discoveries [{:name from
:status status
:whisper-id from
:photo ""
:location location
:tags hashtags
:last-updated (js/Date.)}])
(signal-discovery-updated db)
db)))
(save-discoveries [{:name name
:status status
:whisper-id from
:photo ""
:location location
:tags hashtags
:last-updated (js/Date.)}])
(signal-discoveries-updated db))))
(register-handler :updated-discoveries
(fn [db _]
(signal-discovery-updated db)
db))
(signal-discoveries-updated db)))
(register-handler :broadcast-status
(fn [db [action status hashtags]]

View File

@ -7,116 +7,89 @@
[syng-im.resources :as res]
[syng-im.db :as db]))
;; TODO see https://github.com/rt2zz/react-native-contacts/issues/45
(def fake-discoveries? true)
(defn signal-discovery-updated [db]
(defn signal-discoveries-updated [db]
(update-in db db/updated-discoveries-signal-path (fn [current]
(if current
(inc current)
0))))
(defn discovery-updated? [db]
(defn discoveries-updated? [db]
(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]
(let [_ (log (str "Getting tag: " tag))]
(-> (r/get-by-field :tag :name tag)
(r/single-cljs)))
(r/single-cljs))))
(defn remove-tag [tag]
(let [tag-object (get-tag tag)]
(defn decrease-tag-counter [tag]
(let [tag (:name tag)
tag-object (get-tag tag)]
(if tag-object
(realm/create :tag
{:name tag
:count (dec (:count tag-object))
}))))
(let [counter (dec (:count tag-object))]
(if (= counter 0)
(realm/delete tag-object)
(realm/create :tag {:name tag
:count counter}
true))))))
(defn add-tag [tag]
(let [tag-object (get-tag tag)
counter (if tag-object (:count tag-object) 1)]
(realm/create :tag
{:name tag
:count (inc counter)
}
(if tag-object true false))))
(defn increase-tag-counter [tag]
(let [tag (:name tag)
tag-object (get-tag tag)]
(if tag-object
(realm/create :tag {:name tag
:count (inc (:count tag-object))}
true))))
(defn remove-tags [tags]
(defn decrease-tags-counter [tags]
(doseq [tag tags]
(remove-tag tag)))
(decrease-tag-counter tag)))
(defn add-tags [tags]
(defn increase-tags-counter [tags]
(doseq [tag tags]
(add-tag tag)))
(increase-tag-counter tag)))
(defn get-tags [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]}]
(do
;(add-tags tags)
(realm/create :discoveries
{:name name
(let [tags (mapv (fn [tag] {:name tag}) tags)
discovery {:name name
:status status
:whisper-id whisper-id
:photo photo
:location location
:tags (mapv (fn [tag]
{:name tag}) tags)
:last-updated last-updated} true)
)
)
:tags tags
:last-updated last-updated}
_ (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]}]
(let [old-tags (get-tags whisper-id)]
(do
;;(remove-tags old-tags)
;;(add-tags tags)
(realm/create :discoveries
{:name name
(let [old-tags (get-tags whisper-id)
tags (mapv (fn [tag] {:name tag}) tags)
discovery {:name name
:status status
:whisper-id whisper-id
:photo photo
:location location
:tags (mapv (fn [tag]
{:name tag}) tags)
:last-updated last-updated}
true)
))
)
:tags tags
:last-updated last-updated}]
(do
(decrease-tags-counter old-tags)
(realm/create :discoveries discovery true)
(increase-tags-counter tags))))
(defn- discovery-exist? [discoveries discovery]
(some #(= (:whisper-id discovery) (:whisper-id %)) discoveries))
(defn discovery-list []
(-> (r/get-all :discoveries)
(r/sorted :last-updated :desc)))
(defn- add-discoveries [discoveries]
(realm/write (fn []
(let [db-discoveries (.slice (discovery-list) 0)
@ -132,51 +105,14 @@
(defn save-discoveries [discoveries]
(add-discoveries discoveries))
(defn discovery-list []
(-> (r/get-all :discoveries)
(r/sorted :last-updated :desc)))
(defn discoveries-by-tag [tag limit]
(let [_ (log (str "discoveries by tag: " tag))]
(-> (r/get-by-filter :discoveries (str "tags.name = '" tag "'"))
(r/sorted :last-updated :desc)
(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]
(-> (r/get-all :tag)
(r/sorted :count :desc)
(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"}])
)

View File

@ -50,7 +50,7 @@
:properties {:name "string"
:count {:type "int"
:optional true
:default 1}}}
:default 0}}}
{:name :discoveries
:primaryKey :whisper-id
:properties {:name "string"

View File

@ -8,9 +8,6 @@
[syng-im.models.chats :refer [chats-list
chats-updated?
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.contacts :refer [contacts-list]]
[syng-im.handlers.suggestions :refer [get-suggestions]]))

View File

@ -6,33 +6,32 @@
[syng-im.models.discoveries :refer [discovery-list
get-tag-popular
discoveries-by-tag
signal-discovery-updated
discovery-updated?]]))
discoveries-updated?]]))
(register-sub :get-discoveries
(fn [db _]
(let [discovery-updated (-> (discovery-updated? @db)
(let [discoveries-updated (-> (discoveries-updated? @db)
(reaction))]
(reaction
(let [_ @discovery-updated]
(let [_ @discoveries-updated]
(discovery-list))))))
(register-sub :get-discoveries-by-tag
(fn [db [_ tag limit]]
(let [discovery-updated (-> (discovery-updated? @db)
(let [discoveries-updated (-> (discoveries-updated? @db)
(reaction))
_ (log (str "getting discoveries for: " tag))]
(reaction
(let [_ @discovery-updated]
(let [_ @discoveries-updated]
(discoveries-by-tag tag limit))))))
(register-sub :get-popular-tags
(fn [db [_ limit]]
(let [discovery-updated (-> (discovery-updated? @db)
(let [discoveries-updated (-> (discoveries-updated? @db)
(reaction))
_ (log (str "getting tags limited: " limit))]
(reaction
(let [_ @discovery-updated]
(let [_ @discoveries-updated]
(get-tag-popular limit))))))