load messages on scroll

This commit is contained in:
Roman Volosovskyi 2016-06-27 15:57:40 +03:00
parent dc079cc217
commit ff1291dac0
3 changed files with 35 additions and 34 deletions

View File

@ -27,13 +27,12 @@
(assoc db :show-actions show-actions))) (assoc db :show-actions show-actions)))
(register-handler :load-more-messages (register-handler :load-more-messages
(fn [db _] debug
db (fn [{:keys [current-chat-id] :as db} _]
;; TODO implement (let [messages-path [:chats current-chat-id :messages]
#_(let [chat-id (get-in db [:chat :current-chat-id]) messages (get-in db messages-path)
messages [:chats chat-id :messages] new-messages (messages/get-messages current-chat-id (count messages))]
new-messages (gen-messages 10)] (update-in db messages-path concat new-messages))))
(update-in db messages concat new-messages))))
(defn safe-trim [s] (defn safe-trim [s]
(when (string? s) (when (string? s)
@ -144,26 +143,26 @@
(defn prepare-message (defn prepare-message
[{:keys [identity current-chat-id] :as db} _] [{:keys [identity current-chat-id] :as db} _]
(let [text (get-in db [:chats current-chat-id :input-text]) (let [text (get-in db [:chats current-chat-id :input-text])
{:keys [command]} (suggestions/check-suggestion db (str text " ")) {:keys [command]} (suggestions/check-suggestion db (str text " "))
message (check-author-direction message (check-author-direction
db current-chat-id db current-chat-id
{:msg-id (random/id) {:msg-id (random/id)
:chat-id current-chat-id :chat-id current-chat-id
:content text :content text
:to current-chat-id :to current-chat-id
:from identity :from identity
:content-type text-content-type :content-type text-content-type
:outgoing true :outgoing true
:timestamp (time/now-ms)})] :timestamp (time/now-ms)})]
(if command (if command
(commands/set-chat-command db command) (commands/set-chat-command db command)
(assoc db :new-message (when-not (str/blank? text) message))))) (assoc db :new-message (when-not (str/blank? text) message)))))
(defn prepare-command [identity chat-id staged-command] (defn prepare-command [identity chat-id staged-command]
(let [command-key (get-in staged-command [:command :command]) (let [command-key (get-in staged-command [:command :command])
content {:command (name command-key) content {:command (name command-key)
:content (:content staged-command)}] :content (:content staged-command)}]
{:msg-id (random/id) {:msg-id (random/id)
:from identity :from identity
:to chat-id :to chat-id
@ -287,10 +286,8 @@
(defn load-messages! (defn load-messages!
([db] (load-messages! db nil)) ([db] (load-messages! db nil))
([db _] ([{:keys [current-chat-id] :as db} _]
(->> (:current-chat-id db) (assoc db :messages (messages/get-messages current-chat-id))))
messages/get-messages
(assoc db :messages))))
(defn init-chat (defn init-chat
([db] (init-chat db nil)) ([db] (init-chat db nil))
@ -308,7 +305,7 @@
(map (fn [{:keys [chat-id] :as chat}] (map (fn [{:keys [chat-id] :as chat}]
[chat-id chat])) [chat-id chat]))
(into {})) (into {}))
ids (set (keys chats))] ids (set (keys chats))]
(-> db (-> db
(assoc :chats chats) (assoc :chats chats)
(assoc :chats-ids ids) (assoc :chats-ids ids)

View File

@ -15,3 +15,5 @@
(def response-input-hiding-duration 100) (def response-input-hiding-duration 100)
(def response-suggesstion-resize-duration 100) (def response-suggesstion-resize-duration 100)
(def default-number-of-messages 10)

View File

@ -3,7 +3,6 @@
[re-frame.core :refer [dispatch]] [re-frame.core :refer [dispatch]]
[cljs.reader :refer [read-string]] [cljs.reader :refer [read-string]]
[status-im.utils.random :refer [timestamp]] [status-im.utils.random :refer [timestamp]]
[status-im.db :as db]
[status-im.utils.logging :as log] [status-im.utils.logging :as log]
[clojure.string :refer [join split]] [clojure.string :refer [join split]]
[clojure.walk :refer [stringify-keys keywordize-keys]] [clojure.walk :refer [stringify-keys keywordize-keys]]
@ -46,16 +45,19 @@
#{c/content-type-command c/content-type-command-request} #{c/content-type-command c/content-type-command-request}
type)) type))
(defn get-messages [chat-id] (defn get-messages
(->> (-> (r/get-by-field :msgs :chat-id chat-id) ([chat-id] (get-messages chat-id 0))
(r/sorted :timestamp :desc) ([chat-id from]
(r/collection->map)) (->> (-> (r/get-by-field :msgs :chat-id chat-id)
(into '()) (r/sorted :timestamp :desc)
reverse (r/page from (+ from c/default-number-of-messages))
(map (fn [{:keys [content-type] :as message}] (r/collection->map))
(if (command-type? content-type) (into '())
(update message :content str-to-map) reverse
message))))) (map (fn [{:keys [content-type] :as message}]
(if (command-type? content-type)
(update message :content str-to-map)
message))))))
(defn update-message! [{:keys [msg-id] :as msg}] (defn update-message! [{:keys [msg-id] :as msg}]
(log/debug "update-message!" msg) (log/debug "update-message!" msg)