[slow sign in] Chats preloading

10 last chats are loaded to `app-db` before showing `:home` screen, in
result a user will not see two consequent activity indicators. In this
case opening of `:home` screen is a bit slower but looks better from
UI/UX pov. As it is limited to 10 chats on initialization, the time
necessary for opening `:home` screen will not depend on a total number
of chats in `app-db` if an account contains 10+ chats.

Signed-off-by: Igor Mandrigin <i@mandrigin.ru>
This commit is contained in:
Roman Volosovskyi 2018-12-18 15:28:37 +02:00 committed by Igor Mandrigin
parent cc542969c0
commit aceea457ce
No known key found for this signature in database
GPG Key ID: 4A0EDDE26E66BC8B
8 changed files with 32 additions and 56 deletions

View File

@ -67,43 +67,25 @@
statuses)) statuses))
(fx/defn initialize-chats (fx/defn initialize-chats
"Initialize all persisted chats on startup" "Initialize persisted chats on startup"
[{:keys [db default-dapps all-stored-chats] :as cofx}] [{:keys [db default-dapps get-all-stored-chats] :as cofx}
(let [chats (reduce (fn [acc {:keys [chat-id] :as chat}] {:keys [from to] :or {from 0 to nil}}]
(let [old-chats (:chats db)
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(assoc acc chat-id (assoc acc chat-id
(assoc chat (assoc chat
:messages-initialized? false :messages-initialized? false
:referenced-messages {} :referenced-messages {}
:messages empty-message-map))) :messages empty-message-map)))
{} {}
all-stored-chats)] (get-all-stored-chats from to))
chats (merge old-chats chats)]
(fx/merge cofx (fx/merge cofx
{:db (assoc db {:db (assoc db
:chats chats :chats chats
:contacts/dapps default-dapps)} :contacts/dapps default-dapps)}
(commands/load-commands commands/register)))) (commands/load-commands commands/register))))
(fx/defn initialize-pending-messages
"Change status of own messages which are still in `sending` status to `not-sent`
(If signal from status-go has not been received)"
[{:keys [db] :as cofx}]
(let [me (accounts.db/current-public-key cofx)
pending-statuses (->> (vals (:chats db))
(mapcat :message-statuses)
(mapcat (fn [[_ user-id->status]]
(filter (comp (partial = :sending) :status)
(get user-id->status me)))))
updated-statuses (map #(assoc % :status :not-sent) pending-statuses)]
{:data-store/tx [(user-statuses-store/save-statuses-tx updated-statuses)]
:db (reduce
(fn [acc {:keys [chat-id message-id status public-key]}]
(assoc-in acc
[:chats chat-id :message-status message-id
public-key :status]
status))
db
updated-statuses)}))
(defn load-more-messages (defn load-more-messages
"Loads more messages for current chat" "Loads more messages for current chat"
[{{:keys [current-chat-id] :as db} :db [{{:keys [current-chat-id] :as db} :db

View File

@ -60,11 +60,14 @@
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/all-chats :data-store/all-chats
(fn [cofx _] (fn [cofx _]
(assoc cofx :all-stored-chats (map normalize-chat (assoc cofx :get-all-stored-chats
(-> @core/account-realm (fn [from to]
(core/get-all :chat) (map normalize-chat
(core/sorted :timestamp :desc) (-> @core/account-realm
(core/all-clj :chat)))))) (core/get-all :chat)
(core/sorted :timestamp :desc)
(core/page from to)
(core/all-clj :chat)))))))
(defn save-chat-tx (defn save-chat-tx
"Returns tx function for saving chat" "Returns tx function for saving chat"

View File

@ -311,7 +311,7 @@
(.sorted results (clj->js fields))) (.sorted results (clj->js fields)))
(defn page [results from to] (defn page [results from to]
(js/Array.prototype.slice.call results from to)) (js/Array.prototype.slice.call results from (or to -1)))
(defn filtered [results filter-query] (defn filtered [results filter-query]
(.filtered results filter-query)) (.filtered results filter-query))

View File

@ -94,15 +94,15 @@
(init/handle-init-store-error cofx encryption-key))) (init/handle-init-store-error cofx encryption-key)))
(handlers/register-handler-fx (handlers/register-handler-fx
:init-chats :init-rest-of-chats
[(re-frame/inject-cofx :web3/get-web3) [(re-frame/inject-cofx :web3/get-web3)
(re-frame/inject-cofx :get-default-dapps) (re-frame/inject-cofx :get-default-dapps)
(re-frame/inject-cofx :data-store/all-chats)] (re-frame/inject-cofx :data-store/all-chats)]
(fn [{:keys [db] :as cofx} [_ address]] (fn [{:keys [db] :as cofx} [_]]
(log/debug "PERF" :init-rest-of-chats (.now js/Date))
(fx/merge cofx (fx/merge cofx
{:db (assoc db :chats/loading? false)} {:db (assoc db :chats/loading? false)}
(chat-loading/initialize-chats) (chat-loading/initialize-chats {:from 10}))))
(chat-loading/initialize-pending-messages))))
(handlers/register-handler-fx (handlers/register-handler-fx
:init.callback/account-change-success :init.callback/account-change-success
@ -110,7 +110,9 @@
(re-frame/inject-cofx :data-store/get-all-contacts) (re-frame/inject-cofx :data-store/get-all-contacts)
(re-frame/inject-cofx :data-store/get-all-installations) (re-frame/inject-cofx :data-store/get-all-installations)
(re-frame/inject-cofx :data-store/all-browsers) (re-frame/inject-cofx :data-store/all-browsers)
(re-frame/inject-cofx :data-store/all-dapp-permissions)] (re-frame/inject-cofx :data-store/all-dapp-permissions)
(re-frame/inject-cofx :get-default-dapps)
(re-frame/inject-cofx :data-store/all-chats)]
(fn [{:keys [db] :as cofx} [_ address]] (fn [{:keys [db] :as cofx} [_ address]]
(let [{:node/keys [status on-ready]} db] (let [{:node/keys [status on-ready]} db]
(fx/merge (fx/merge
@ -118,7 +120,8 @@
(if (= status :started) (if (= status :started)
(accounts.login/login) (accounts.login/login)
(node/initialize (get-in db [:accounts/login :address]))) (node/initialize (get-in db [:accounts/login :address])))
(init/initialize-account address))))) (init/initialize-account address)
(chat-loading/initialize-chats {:to 10})))))
(handlers/register-handler-fx (handlers/register-handler-fx
:init.callback/keychain-reset :init.callback/keychain-reset

View File

@ -18,7 +18,6 @@
- restoring existing symetric keys along with their unique filters - restoring existing symetric keys along with their unique filters
- (optionally) initializing mailserver" - (optionally) initializing mailserver"
[{:keys [db web3] :as cofx}] [{:keys [db web3] :as cofx}]
(log/debug :init-whisper)
(when-let [public-key (get-in db [:account/account :public-key])] (when-let [public-key (get-in db [:account/account :public-key])]
(let [public-key-topics (keep (fn [[chat-id {:keys [topic sym-key]}]] (let [public-key-topics (keep (fn [[chat-id {:keys [topic sym-key]}]]
(when (and (not sym-key) (when (and (not sym-key)

View File

@ -85,7 +85,6 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:shh.callback/filter-added :shh.callback/filter-added
(fn [{:keys [db] :as cofx} [_ topic chat-id filter]] (fn [{:keys [db] :as cofx} [_ topic chat-id filter]]
(log/debug "PERF" :shh.callback/filter-added)
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:transport/filters chat-id] filter)} {:db (assoc-in db [:transport/filters chat-id] filter)}
(mailserver/reset-request-to) (mailserver/reset-request-to)

View File

@ -147,7 +147,7 @@
(fn [this] (fn [this]
(let [[_ loading?] (.. this -props -argv)] (let [[_ loading?] (.. this -props -argv)]
(when loading? (when loading?
(re-frame/dispatch [:init-chats]))))} (re-frame/dispatch [:init-rest-of-chats]))))}
[react/view {:style styles/chat-list-view} [react/view {:style styles/chat-list-view}
[react/view {:style styles/chat-list-header} [react/view {:style styles/chat-list-header}
[search-input search-filter] [search-input search-filter]
@ -155,16 +155,11 @@
[react/touchable-highlight {:on-press #(re-frame/dispatch [:set-in [:desktop :popup] popup])} [react/touchable-highlight {:on-press #(re-frame/dispatch [:set-in [:desktop :popup] popup])}
[react/view {:style styles/add-new} [react/view {:style styles/add-new}
[icons/icon :icons/add {:style {:tint-color :white}}]]]]] [icons/icon :icons/add {:style {:tint-color :white}}]]]]]
(if loading? [react/scroll-view {:enableArrayScrollingOptimization true}
[react/view {:style {:flex 1 [react/view
:justify-content :center (for [[index chat] (map-indexed vector filtered-home-items)]
:align-items :center}} ^{:key (first chat)}
[components/activity-indicator {:animating true}]] [chat-list-item chat])]]]))
[react/scroll-view {:enableArrayScrollingOptimization true}
[react/view
(for [[index chat] (map-indexed vector filtered-home-items)]
^{:key (first chat)}
[chat-list-item chat])]])]))
(views/defview chat-list-view-wrapper [] (views/defview chat-list-view-wrapper []
(views/letsubs [loading? [:get :chats/loading?]] (views/letsubs [loading? [:get :chats/loading?]]

View File

@ -99,17 +99,12 @@
(let [[_ loading?] (.. this -props -argv)] (let [[_ loading?] (.. this -props -argv)]
(when loading? (when loading?
(utils/set-timeout (utils/set-timeout
#(re-frame/dispatch [:init-chats]) #(re-frame/dispatch [:init-rest-of-chats])
100))))} 100))))}
[react/view styles/container [react/view styles/container
[toolbar show-welcome? (and network-initialized? (not rpc-network?)) sync-state latest-block-number] [toolbar show-welcome? (and network-initialized? (not rpc-network?)) sync-state latest-block-number]
(cond show-welcome? (cond show-welcome?
[welcome view-id] [welcome view-id]
loading?
[react/view {:style {:flex 1
:justify-content :center
:align-items :center}}
[components/activity-indicator {:animating true}]]
:else :else
[chats-list]) [chats-list])
(when platform/android? (when platform/android?