fixed realm db usage in listview

This commit is contained in:
Adrian Tiberius 2016-04-17 16:49:35 +03:00
parent 8d0e3a906d
commit 9a4b8e8b94
16 changed files with 202 additions and 78 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "SyngIm", "name": "SyngIm",
"interface": "reagent", "interface": "reagent",
"androidHost": "10.0.2.2", "androidHost": "10.0.3.2",
"modules": [ "modules": [
"react-native-contacts", "react-native-contacts",
"react-native-invertible-scroll-view", "react-native-invertible-scroll-view",

View File

@ -53,7 +53,6 @@
(defn init [] (defn init []
(dispatch-sync [:initialize-db]) (dispatch-sync [:initialize-db])
;;(dispatch-sync [:generate-discoveries])
(dispatch [:initialize-crypt]) (dispatch [:initialize-crypt])
(dispatch [:initialize-protocol]) (dispatch [:initialize-protocol])
(dispatch [:load-user-phone-number]) (dispatch [:load-user-phone-number])

View File

@ -1,5 +1,6 @@
(ns syng-im.components.discovery.discovery-popular-list-item (ns syng-im.components.discovery.discovery-popular-list-item
(:require (:require
[syng-im.utils.debug :refer [log]]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
scroll-view scroll-view
@ -13,6 +14,7 @@
) )
(defn discovery-popular-list-item [discovery] (defn discovery-popular-list-item [discovery]
(let [_ (log discovery)]
(r/as-element [view {:style {:flexDirection "row" (r/as-element [view {:style {:flexDirection "row"
:paddingTop 10 :paddingTop 10
:paddingBottom 10}} :paddingBottom 10}}
@ -37,4 +39,4 @@
:source res/user-no-photo}] :source res/user-no-photo}]
] ]
]) ])
) ))

View File

@ -4,14 +4,16 @@
) )
(:require (:require
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
scroll-view scroll-view
list-view
text text
image image
navigator navigator
toolbar-android]] toolbar-android]]
[syng-im.components.realm :refer [list-view]]
[syng-im.utils.listview :refer [to-realm-datasource]]
[syng-im.components.carousel :refer [carousel]] [syng-im.components.carousel :refer [carousel]]
[syng-im.components.discovery.discovery-popular-list-item :refer [discovery-popular-list-item]] [syng-im.components.discovery.discovery-popular-list-item :refer [discovery-popular-list-item]]
[syng-im.models.discoveries :refer [generate-discoveries]] [syng-im.models.discoveries :refer [generate-discoveries]]
@ -35,10 +37,12 @@
(not= (:discovery-id row1) (:discovery-id row2)))}) (not= (:discovery-id row1) (:discovery-id row2)))})
elements)) elements))
(defn discovery-recent [recent-discoveries] (defn discovery-recent []
[list-view {:dataSource (get-data-source recent-discoveries) (let [discoveries (subscribe [:get-discoveries])
datasource (to-realm-datasource @discoveries)]
[list-view {:dataSource datasource
:renderRow render-row :renderRow render-row
:renderSeparator render-separator :renderSeparator render-separator
:style {:backgroundColor "white" :style {:backgroundColor "white"
:paddingLeft 15}}] :paddingLeft 15}}]
) ))

View File

@ -6,6 +6,7 @@
view view
scroll-view scroll-view
text text
text-input
image image
navigator navigator
toolbar-android]] toolbar-android]]
@ -13,30 +14,77 @@
[syng-im.components.discovery.discovery-popular :refer [discovery-popular]] [syng-im.components.discovery.discovery-popular :refer [discovery-popular]]
[syng-im.components.discovery.discovery-recent :refer [discovery-recent]] [syng-im.components.discovery.discovery-recent :refer [discovery-recent]]
[syng-im.models.discoveries :refer [generate-discoveries [syng-im.models.discoveries :refer [generate-discoveries
get-discovery-popular generate-discovery
get-discovery-recent]] save-discoveries]]
[syng-im.resources :as res])) [syng-im.utils.listview :refer [to-realm-datasource]]
[syng-im.resources :as res]
[syng-im.persistence.realm :as realm]))
(def log (.-log js/console))
(def search-input (atom {:search "x"}))
(def toolbar-title [text "Discover"])
(def toolbar-search [text-input {:underlineColorAndroid "transparent"
:value (:search @search-input)
:style {:flex 1
:marginLeft 18
:lineHeight 42
:fontSize 14
:fontFamily "Avenir-Roman"
:color "#9CBFC0"}
:autoFocus true
:placeholder "Type your search tags here"
:onChangeText (fn [new-text]
(let [old-text (:search @search-input)]
(log (str new-text "-" old-text))
(if (not (= new-text old-text))
(swap! search-input assoc :search new-text))
))
:onSubmitEditing (fn [e]
(log (aget e "nativeEvent" "text")))}])
(def showSearch (r/atom false))
(def content (r/atom toolbar-title))
(defn toggle-search []
(if @showSearch
(do
(reset! showSearch false)
(reset! content toolbar-title))
(do
(reset! showSearch true)
(reset! content toolbar-search))))
(defn discovery [{:keys [navigator]}] (defn discovery [{:keys [navigator]}]
(let [discoveries (subscribe [:get-discoveries])
pop-discoveries (get-discovery-popular 3)]
(fn [] (fn []
[view {:style {:flex 1 [view {:style {:flex 1
:backgroundColor "#edf2f5"}} :backgroundColor "#eef2f5"}}
[toolbar-android {:title "Discover" [toolbar-android {:titleColor "#4A5258"
:titleColor "#4A5258"
:navIcon res/menu :navIcon res/menu
:actions [{:title "Search" :actions [{:title "Search"
:icon res/search :icon res/search
:show "always"}] :show "always"}]
:style {:backgroundColor "white" :style {:backgroundColor "#eef2f5"
:justifyContent "center" :justifyContent "center"
:height 56 :height 56
:elevation 2} :elevation 2}
:onIconClicked (fn [] :onIconClicked (fn []
(.log console "testttt")) (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])))))
:onActionSelected (fn [index] :onActionSelected (fn [index]
(index))}] (toggle-search))}
@content]
[scroll-view {:style {}} [scroll-view {:style {}}
[view {:style {:paddingTop 5}} [view {:style {:paddingTop 5}}
@ -49,19 +97,18 @@
[text {:style {:color "#b2bdc5" [text {:style {:color "#b2bdc5"
:fontSize 14 :fontSize 14
:fontWeight "bold"}} "Popular Tags"]] :fontWeight "bold"}} "Popular Tags"]]
[discovery-popular pop-discoveries] [discovery-popular]
[view {:style {:paddingLeft 30 [view {:style {:paddingLeft 30
:paddingTop 15 :paddingTop 15
:paddingBottom 15}} :paddingBottom 15}}
[text {:style {:color "#b2bdc5" [text {:style {:color "#b2bdc5"
:fontSize 14 :fontSize 14
:fontWeight "bold"}} "Recent"]] :fontWeight "bold"}} "Recent"]]
[discovery-recent (get-discovery-recent 10)] [discovery-recent]
] ]
] ]
) )
) )
)
(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"))

View File

@ -1,6 +1,8 @@
(ns syng-im.components.discovery.discovery-popular (ns syng-im.components.discovery.discovery-popular
(:require-macros [reagent.ratom :refer [reaction]])
(:require (:require
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.debug :refer [log]]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
scroll-view scroll-view
@ -10,15 +12,21 @@
toolbar-android]] toolbar-android]]
[syng-im.components.carousel :refer [carousel]] [syng-im.components.carousel :refer [carousel]]
[syng-im.components.discovery.discovery-popular-list :refer [discovery-popular-list]] [syng-im.components.discovery.discovery-popular-list :refer [discovery-popular-list]]
[syng-im.models.discoveries :refer [generate-discoveries]] [syng-im.models.discoveries :refer [generate-discoveries
generate-discovery
save-discoveries
group-by-tag
get-discovery-recent]]
[syng-im.resources :as res])) [syng-im.resources :as res]))
(defn page-width [] (defn page-width []
(.-width (.get (.. js/React -Dimensions) "window"))) (.-width (.get (.. js/React -Dimensions) "window")))
(defn discovery-popular []
(defn discovery-popular [popular-discoveries] (let [popular-tags (subscribe [:get-popular-tags 3])
(let [popular-lists (mapv #(discovery-popular-list % (get popular-discoveries %)) (keys popular-discoveries))] _ (log "Got popular tags: ")
_ (log @popular-tags)
popular-lists (mapv #(discovery-popular-list (.-name %)) @popular-tags)]
(if (> (count popular-lists) 0) (if (> (count popular-lists) 0)
(apply carousel {:pageStyle {:borderRadius 1 (apply carousel {:pageStyle {:borderRadius 1
:shadowColor "black" :shadowColor "black"

View File

@ -1,9 +1,10 @@
(ns syng-im.components.discovery.discovery-popular-list (ns syng-im.components.discovery.discovery-popular-list
(:require-macros (:require-macros
[natal-shell.data-source :refer [data-source clone-with-rows]] [natal-shell.data-source :refer [data-source clone-with-rows]]
) )
(:require (:require
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.debug :refer [log]]
[syng-im.components.react :refer [android? [syng-im.components.react :refer [android?
view view
scroll-view scroll-view
@ -13,6 +14,8 @@
navigator navigator
toolbar-android]] toolbar-android]]
[reagent.core :as r] [reagent.core :as r]
[syng-im.components.realm :refer [list-view]]
[syng-im.utils.listview :refer [to-realm-datasource]]
[syng-im.components.discovery.discovery-popular-list-item :refer [discovery-popular-list-item] ]) [syng-im.components.discovery.discovery-popular-list-item :refer [discovery-popular-list-item] ])
) )
@ -33,7 +36,10 @@
(not= (:discovery-id row1) (:discovery-id row2)))}) (not= (:discovery-id row1) (:discovery-id row2)))})
elements)) elements))
(defn discovery-popular-list [tag elements] (defn discovery-popular-list [tag]
(let [discoveries (subscribe [:get-discoveries-by-tag tag 3])
_ (log (str "Got discoveries for tag (" tag "): ") @discoveries)
_ (log @discoveries)]
(r/as-element [view {:style {:flex 1 (r/as-element [view {:style {:flex 1
:backgroundColor "white" :backgroundColor "white"
:paddingLeft 10 :paddingLeft 10
@ -50,12 +56,12 @@
:paddingBottom 2 :paddingBottom 2
:alignItems "center" :alignItems "center"
:justifyContent "center"}} (str " #" (name tag))]]] :justifyContent "center"}} (str " #" (name tag))]]]
[list-view {:dataSource (get-data-source elements) [list-view {:dataSource (to-realm-datasource @discoveries)
:renderRow render-row :renderRow render-row
:renderSeparator render-separator :renderSeparator render-separator
:style {:backgroundColor "white"}}] :style {:backgroundColor "white"}}]
]) ])
) ))
(comment (comment
list-view {:dataSource elements list-view {:dataSource elements

View File

@ -28,6 +28,6 @@
(defn chat-command-content-path [chat-id] (defn chat-command-content-path [chat-id]
[:chats chat-id :command-input :content]) [:chats chat-id :command-input :content])
(def new-group-path [:new-group]) (def new-group-path [:new-group])
(def updated-discovery-signal-path [:discovery-updated-signal]) (def updated-discoveries-signal-path [:discovery-updated-signal])
(defn updated-discovery-signal-path [discovery-id] (defn updated-discovery-signal-path [whisper-id]
[:discoveries discovery-id :discovery-updated-signal]) [:discoveries whisper-id :discovery-updated-signal])

View File

@ -9,7 +9,6 @@
set-initialized]] set-initialized]]
[syng-im.models.user-data :as user-data] [syng-im.models.user-data :as user-data]
[syng-im.models.contacts :as contacts] [syng-im.models.contacts :as contacts]
[syng-im.models.discoveries :as discoveries]
[syng-im.models.messages :refer [save-message [syng-im.models.messages :refer [save-message
update-message! update-message!
message-by-id]] message-by-id]]
@ -18,7 +17,7 @@
[syng-im.handlers.commands :refer [set-chat-command [syng-im.handlers.commands :refer [set-chat-command
set-chat-command-content]] set-chat-command-content]]
[syng-im.handlers.sign-up :as sign-up-service] [syng-im.handlers.sign-up :as sign-up-service]
[syng-im.handlers.discovery :as discovery]
[syng-im.models.chats :refer [create-chat]] [syng-im.models.chats :refer [create-chat]]
[syng-im.models.chat :refer [signal-chat-updated [syng-im.models.chat :refer [signal-chat-updated
set-current-chat-id set-current-chat-id
@ -275,11 +274,7 @@
(log/debug action from group-id identities) (log/debug action from group-id identities)
(create-chat db group-id identities true group-name))) (create-chat db group-id identities true group-name)))
;; -- Discovery --------------------------------------------------------------
(register-handler :generate-discoveries
(fn [db _]
(discoveries/save-discoveries (discoveries/generate-discoveries 10))))
(comment (comment

View File

@ -0,0 +1,24 @@
(ns syng-im.handlers.discovery
(:require [syng-im.models.discoveries :refer [save-discoveries
discovery-list
signal-discovery-updated
discovery-updated?]]))
;; -- Discovery --------------------------------------------------------------
(register-handler :discovery-response-received
(fn [db [_ from payload]]
(let [{:keys [status hashtags location]} payload]
(save-discoveries [{:name from
:status status
:whisper-id from
:photo ""
:location location
:tags hashtags
:last-updated (js/Date.)}])
)))
(register-handler :updated-discoveries
(fn [db _]
(signal-discovery-updated db)))

View File

@ -1,7 +1,7 @@
(ns syng-im.models.discoveries (ns syng-im.models.discoveries
(:require [cljs.core.async :as async :refer [chan put! <! >!]] (:require [cljs.core.async :as async :refer [chan put! <! >!]]
[re-frame.core :refer [subscribe dispatch dispatch-sync]] [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.utils :refer [log toast]] [syng-im.utils.debug :refer [log]]
[syng-im.persistence.realm :as realm] [syng-im.persistence.realm :as realm]
[syng-im.persistence.realm :as r] [syng-im.persistence.realm :as r]
[syng-im.resources :as res] [syng-im.resources :as res]
@ -10,22 +10,20 @@
;; TODO see https://github.com/rt2zz/react-native-contacts/issues/45 ;; TODO see https://github.com/rt2zz/react-native-contacts/issues/45
(def fake-discoveries? true) (def fake-discoveries? true)
(defn signal-discovery-updated [db] (defn signal-discovery-updated [db]
(update-in db db/updated-discovery-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 discovery-updated? [db]
(get-in db db/updated-discovery-signal-path)) (get-in db db/updated-discoveries-signal-path))
(defn- generate-discovery [n] (defn generate-discovery [n]
{:discovery-id n {:name (str "Contact " n)
:name (str "Contact " n)
:status (apply str (repeat (+ n 3) "Contact Status ")) :status (apply str (repeat (+ n 3) "Contact Status "))
:whisper-id (str "id-" n) :whisper-id (str "id-" n)
:location ""
:photo "" :photo ""
:tags ["tag1" "tag2"] :tags ["tag1" "tag2"]
:last-updated (js/Date. "10/01/2015") :last-updated (js/Date. "10/01/2015")
@ -80,7 +78,7 @@
(: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 tags last-updated]}] (defn- create-discovery [{:keys [name status whisper-id photo location tags last-updated]}]
(do (do
;(add-tags tags) ;(add-tags tags)
(realm/create :discoveries (realm/create :discoveries
@ -88,6 +86,7 @@
:status status :status status
:whisper-id whisper-id :whisper-id whisper-id
:photo photo :photo photo
:location location
:tags (mapv (fn [tag] :tags (mapv (fn [tag]
{:name tag}) tags) {:name tag}) tags)
:last-updated last-updated} true) :last-updated last-updated} true)
@ -96,7 +95,7 @@
(defn- update-discovery [{:keys [name status whisper-id photo 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 (do
;;(remove-tags old-tags) ;;(remove-tags old-tags)
@ -106,6 +105,7 @@
:status status :status status
:whisper-id whisper-id :whisper-id whisper-id
:photo photo :photo photo
:location location
:tags (mapv (fn [tag] :tags (mapv (fn [tag]
{:name tag}) tags) {:name tag}) tags)
:last-updated last-updated} :last-updated last-updated}
@ -131,13 +131,18 @@
(defn discovery-list [] (defn discovery-list []
(-> (r/get-all :discoveries) (-> (r/get-all :discoveries)
(r/sorted :discovery-id :desc))) (r/sorted :last-updated :desc)))
(defn get-discovery-recent [limit] (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? (if fake-discoveries?
(take limit fake-discoveries) (take limit fake-discoveries)
(-> (r/get-all :discoveries) (-> (r/sorted discoveries :last-updated :desc)
(r/sorted :last-updated :desc)
(r/page 0 limit))) (r/page 0 limit)))
) )
@ -163,15 +168,6 @@
discoveries) discoveries)
) )
(defn get-discovery-popular [tag-limit]
(if fake-discoveries?
(group-by-tag fake-discoveries tag-limit)
(-> (r/get-all :discoveries)
(r/sorted :last-updated :desc)
(.slice 0)
(group-by-tag tag-limit))
)
)
(comment (comment
(group-by-tag [{:tags ["a" "b" "c"] (group-by-tag [{:tags ["a" "b" "c"]

View File

@ -1,6 +1,6 @@
(ns syng-im.persistence.realm (ns syng-im.persistence.realm
(:require [cljs.reader :refer [read-string]] (:require [cljs.reader :refer [read-string]]
[syng-im.utils.logging :as log] [syng-im.utils.debug :refer [log]]
[syng-im.utils.types :refer [to-string]]) [syng-im.utils.types :refer [to-string]])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
@ -57,6 +57,7 @@
:status "string" :status "string"
:whisper-id "string" :whisper-id "string"
:photo "string" :photo "string"
:location "string"
:tags {:type "list" :tags {:type "list"
:objectType "tag"} :objectType "tag"}
:last-updated "date"}} :last-updated "date"}}
@ -96,6 +97,11 @@
value))] value))]
query)) query))
(defn get-by-filter [schema-name filter]
(let [_ (log filter)]
(-> (.objects realm (name schema-name))
(.filtered filter))))
(defn get-by-field [schema-name field value] (defn get-by-field [schema-name field value]
(let [q (to-query schema-name :eq field value)] (let [q (to-query schema-name :eq field value)]
(-> (.objects realm (name schema-name)) (-> (.objects realm (name schema-name))

View File

@ -40,6 +40,8 @@
; (add-to-chat "group-chat" ":" (str (shorten from) " removed you from group chat"))) ; (add-to-chat "group-chat" ":" (str (shorten from) " removed you from group chat")))
;:participant-left-group (let [{:keys [group-id from]} event] ;:participant-left-group (let [{:keys [group-id from]} event]
; (add-to-chat "group-chat" ":" (str (shorten from) " left group chat"))) ; (add-to-chat "group-chat" ":" (str (shorten from) " left group chat")))
:discover-response (let [{:keys [from payload]} event]
(dispatch [:discovery-response-received from payload]))
;(add-to-chat "chat" ":" (str "Don't know how to handle " event-type)) ;(add-to-chat "chat" ":" (str "Don't know how to handle " event-type))
(log/info "Don't know how to handle" event-type) (log/info "Don't know how to handle" event-type)
))}) ))})

View File

@ -2,12 +2,14 @@
(:require-macros [reagent.ratom :refer [reaction]]) (:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]] (:require [re-frame.core :refer [register-sub]]
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.subscriptions.discovery :as discovery]
[syng-im.models.chat :refer [current-chat-id [syng-im.models.chat :refer [current-chat-id
chat-updated?]] chat-updated?]]
[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 [syng-im.models.discoveries :refer [discovery-list
signal-discovery-updated
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]]
@ -65,15 +67,7 @@
(chat-by-id chat-id)) (chat-by-id chat-id))
(reaction))))) (reaction)))))
;; -- Discoveries list --------------------------------------------------------------
(register-sub :get-discoveries
(fn [db _]
(let [discovery-updated (-> (discovery-updated? @db)
(reaction))]
(reaction
(let [_ @discovery-updated]
(discovery-list))))))
;; -- User data -------------------------------------------------------------- ;; -- User data --------------------------------------------------------------

View File

@ -0,0 +1,38 @@
(ns syng-im.subscriptions.discovery
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]]
[syng-im.db :as db]
[syng-im.utils.debug :refer [log]]
[syng-im.models.discoveries :refer [discovery-list
get-tag-popular
discoveries-by-tag
signal-discovery-updated
discovery-updated?]]))
(register-sub :get-discoveries
(fn [db _]
(let [discovery-updated (-> (discovery-updated? @db)
(reaction))]
(reaction
(let [_ @discovery-updated]
(discovery-list))))))
(register-sub :get-discoveries-by-tag
(fn [db [_ tag limit]]
(let [discovery-updated (-> (discovery-updated? @db)
(reaction))
_ (log (str "getting discoveries for: " tag))]
(reaction
(let [_ @discovery-updated]
(discoveries-by-tag tag limit))))))
(register-sub :get-popular-tags
(fn [db [_ limit]]
(let [discovery-updated (-> (discovery-updated? @db)
(reaction))
_ (log (str "getting tags limited: " limit))]
(reaction
(let [_ @discovery-updated]
(get-tag-popular limit))))))

View File

@ -0,0 +1,3 @@
(ns syng-im.utils.debug)
(def log (.-log js/console))