[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))
(fx/defn initialize-chats
"Initialize all persisted chats on startup"
[{:keys [db default-dapps all-stored-chats] :as cofx}]
(let [chats (reduce (fn [acc {:keys [chat-id] :as chat}]
"Initialize persisted chats on startup"
[{:keys [db default-dapps get-all-stored-chats] :as cofx}
{: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 chat
:messages-initialized? false
:referenced-messages {}
:messages empty-message-map)))
{}
all-stored-chats)]
(get-all-stored-chats from to))
chats (merge old-chats chats)]
(fx/merge cofx
{:db (assoc db
:chats chats
:contacts/dapps default-dapps)}
(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
"Loads more messages for current chat"
[{{:keys [current-chat-id] :as db} :db

View File

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

View File

@ -311,7 +311,7 @@
(.sorted results (clj->js fields)))
(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]
(.filtered results filter-query))

View File

@ -94,15 +94,15 @@
(init/handle-init-store-error cofx encryption-key)))
(handlers/register-handler-fx
:init-chats
:init-rest-of-chats
[(re-frame/inject-cofx :web3/get-web3)
(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} [_]]
(log/debug "PERF" :init-rest-of-chats (.now js/Date))
(fx/merge cofx
{:db (assoc db :chats/loading? false)}
(chat-loading/initialize-chats)
(chat-loading/initialize-pending-messages))))
(chat-loading/initialize-chats {:from 10}))))
(handlers/register-handler-fx
: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-installations)
(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]]
(let [{:node/keys [status on-ready]} db]
(fx/merge
@ -118,7 +120,8 @@
(if (= status :started)
(accounts.login/login)
(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
:init.callback/keychain-reset

View File

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

View File

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

View File

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

View File

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