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",
"interface": "reagent",
"androidHost": "10.0.2.2",
"androidHost": "10.0.3.2",
"modules": [
"react-native-contacts",
"react-native-invertible-scroll-view",

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@
view
scroll-view
text
text-input
image
navigator
toolbar-android]]
@ -13,30 +14,77 @@
[syng-im.components.discovery.discovery-popular :refer [discovery-popular]]
[syng-im.components.discovery.discovery-recent :refer [discovery-recent]]
[syng-im.models.discoveries :refer [generate-discoveries
get-discovery-popular
get-discovery-recent]]
[syng-im.resources :as res]))
generate-discovery
save-discoveries]]
[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]}]
(let [discoveries (subscribe [:get-discoveries])
pop-discoveries (get-discovery-popular 3)]
(fn []
[view {:style {:flex 1
:backgroundColor "#edf2f5"}}
[toolbar-android {:title "Discover"
:titleColor "#4A5258"
:backgroundColor "#eef2f5"}}
[toolbar-android {:titleColor "#4A5258"
:navIcon res/menu
:actions [{:title "Search"
:icon res/search
:show "always"}]
:style {:backgroundColor "white"
:justifyContent "center"
:style {:backgroundColor "#eef2f5"
:justifyContent "center"
:height 56
:elevation 2}
: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]
(index))}]
(toggle-search))}
@content]
[scroll-view {:style {}}
[view {:style {:paddingTop 5}}
@ -49,18 +97,17 @@
[text {:style {:color "#b2bdc5"
:fontSize 14
:fontWeight "bold"}} "Popular Tags"]]
[discovery-popular pop-discoveries]
[discovery-popular]
[view {:style {:paddingLeft 30
:paddingTop 15
:paddingBottom 15}}
[text {:style {:color "#b2bdc5"
:fontSize 14
:fontWeight "bold"}} "Recent"]]
[discovery-recent (get-discovery-recent 10)]
[discovery-recent]
]
]
)
)
)
(comment
(def page-width (aget (natal-shell.dimensions/get "window") "width"))

View File

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

View File

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

View File

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

View File

@ -9,7 +9,6 @@
set-initialized]]
[syng-im.models.user-data :as user-data]
[syng-im.models.contacts :as contacts]
[syng-im.models.discoveries :as discoveries]
[syng-im.models.messages :refer [save-message
update-message!
message-by-id]]
@ -18,7 +17,7 @@
[syng-im.handlers.commands :refer [set-chat-command
set-chat-command-content]]
[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.chat :refer [signal-chat-updated
set-current-chat-id
@ -275,11 +274,7 @@
(log/debug action from group-id identities)
(create-chat db group-id identities true group-name)))
;; -- Discovery --------------------------------------------------------------
(register-handler :generate-discoveries
(fn [db _]
(discoveries/save-discoveries (discoveries/generate-discoveries 10))))
(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
(:require [cljs.core.async :as async :refer [chan put! <! >!]]
[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 r]
[syng-im.resources :as res]
@ -10,24 +10,22 @@
;; TODO see https://github.com/rt2zz/react-native-contacts/issues/45
(def fake-discoveries? true)
(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
(inc current)
0))))
(defn discovery-updated? [db]
(get-in db db/updated-discovery-signal-path))
(get-in db db/updated-discoveries-signal-path))
(defn- generate-discovery [n]
{:discovery-id n
:name (str "Contact " n)
:status (apply str (repeat (+ n 3) "Contact Status "))
:whisper-id (str "id-" n)
:photo ""
:tags ["tag1" "tag2"]
(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")
})
@ -80,7 +78,7 @@
(:tags (-> (r/get-by-field :discoveries :whisper-id whisper-id)
(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
;(add-tags tags)
(realm/create :discoveries
@ -88,6 +86,7 @@
:status status
:whisper-id whisper-id
:photo photo
:location location
:tags (mapv (fn [tag]
{:name tag}) tags)
: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)]
(do
;;(remove-tags old-tags)
@ -106,6 +105,7 @@
:status status
:whisper-id whisper-id
:photo photo
:location location
:tags (mapv (fn [tag]
{:name tag}) tags)
:last-updated last-updated}
@ -131,13 +131,18 @@
(defn discovery-list []
(-> (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?
(take limit fake-discoveries)
(-> (r/get-all :discoveries)
(r/sorted :last-updated :desc)
(-> (r/sorted discoveries :last-updated :desc)
(r/page 0 limit)))
)
@ -163,15 +168,6 @@
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
(group-by-tag [{:tags ["a" "b" "c"]

View File

@ -1,6 +1,6 @@
(ns syng-im.persistence.realm
(: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]])
(:refer-clojure :exclude [exists?]))
@ -57,6 +57,7 @@
:status "string"
:whisper-id "string"
:photo "string"
:location "string"
:tags {:type "list"
:objectType "tag"}
:last-updated "date"}}
@ -96,6 +97,11 @@
value))]
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]
(let [q (to-query schema-name :eq field value)]
(-> (.objects realm (name schema-name))

View File

@ -40,6 +40,8 @@
; (add-to-chat "group-chat" ":" (str (shorten from) " removed you from group chat")))
;:participant-left-group (let [{:keys [group-id from]} event]
; (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))
(log/info "Don't know how to handle" event-type)
))})

View File

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