From 55a955d8dd2f6350206fb4afa4e94343bd47e429 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 7 Apr 2020 16:34:43 +0200 Subject: [PATCH] Load messages on will-focus Signed-off-by: Andrea Maria Piana --- src/status_im/chat/models.cljs | 39 ++++++++++++++----- src/status_im/chat/models/loading.cljs | 15 ------- src/status_im/ui/screens/events.cljs | 5 +++ .../ui/screens/routing/chat_stack.cljs | 3 +- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index b39497e1c5..2d58935cdd 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -233,19 +233,38 @@ (when platform/desktop? (update-dock-badge-label)))))) +(fx/defn offload-all-messages + {:events [::offload-all-messages]} + [{:keys [db] :as cofx}] + (when-let [current-chat-id (:current-chat-id db)] + {:db + (-> db + (dissoc :loaded-chat-id) + (update-in [:chats current-chat-id] + assoc + :all-loaded? false + :cursor nil + :messages-initialized? false + :messages {} + :message-list nil))})) + (fx/defn preload-chat-data "Takes chat-id and coeffects map, returns effects necessary when navigating to chat" [{:keys [db] :as cofx} chat-id] - (fx/merge cofx - {:db (assoc db :current-chat-id chat-id)} - ;; Group chat don't need this to load as all the loading of topics - ;; happens on membership changes - (when-not (group-chat? cofx chat-id) - (transport.filters/load-chat chat-id)) - (when platform/desktop? - (mark-messages-seen chat-id)) - (when (and (one-to-one-chat? cofx chat-id) (not (contact.db/contact-exists? db chat-id))) - (contact.core/create-contact chat-id)))) + (let [old-current-chat-id (:current-chat-id db)] + (fx/merge cofx + (when-not (= old-current-chat-id chat-id) + (offload-all-messages)) + (fn [{:keys [db]}] + {:db (assoc db :current-chat-id chat-id)}) + ;; Group chat don't need this to load as all the loading of topics + ;; happens on membership changes + (when-not (group-chat? cofx chat-id) + (transport.filters/load-chat chat-id)) + (when platform/desktop? + (mark-messages-seen chat-id)) + (when (and (one-to-one-chat? cofx chat-id) (not (contact.db/contact-exists? db chat-id))) + (contact.core/create-contact chat-id))))) (fx/defn navigate-to-chat "Takes coeffects map and chat-id, returns effects necessary for navigation and preloading data" diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index 700d9d65ca..a420c014e7 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -38,21 +38,6 @@ :chats/loading? false)} (filters/load-filters)))) -(fx/defn offload-all-messages - {:events [::offload-all-messages]} - [{:keys [db] :as cofx}] - (when-let [current-chat-id (:current-chat-id db)] - {:db - (-> db - (dissoc :loaded-chat-id) - (update-in [:chats current-chat-id] - assoc - :all-loaded? false - :cursor nil - :messages-initialized? false - :messages {} - :message-list nil))})) - (fx/defn handle-chat-visibility-changed {:events [:chat.ui/message-visibility-changed]} [{:keys [db] :as cofx} event] diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 5cb5b6c177..d37d5b8ff8 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -6,6 +6,7 @@ status-im.utils.universal-links.events status-im.ui.screens.profile.events [re-frame.core :as re-frame] + [status-im.chat.models.loading :as chat.loading] [status-im.chat.models :as chat] [status-im.hardwallet.core :as hardwallet] [status-im.mailserver.core :as mailserver] @@ -203,5 +204,9 @@ :keycard-login-pin (hardwallet/enter-pin-screen-did-load %) :add-new-account-pin (hardwallet/enter-pin-screen-did-load %) :hardwallet-authentication-method (hardwallet/authentication-method-screen-did-load %) + ;; We need this as if you click on universal-links you transition + ;; from chat to chat, and therefore we won't be loading new + ;; messages + :chat (chat.loading/load-messages %) :multiaccounts (hardwallet/multiaccounts-screen-did-load %) nil)))) diff --git a/src/status_im/ui/screens/routing/chat_stack.cljs b/src/status_im/ui/screens/routing/chat_stack.cljs index 001729bc88..cff161f399 100644 --- a/src/status_im/ui/screens/routing/chat_stack.cljs +++ b/src/status_im/ui/screens/routing/chat_stack.cljs @@ -6,6 +6,7 @@ [status-im.ui.screens.group.views :as group] [status-im.ui.screens.profile.group-chat.views :as profile.group-chat] [status-im.chat.models.loading :as chat.loading] + [status-im.chat.models :as chat.models] [status-im.ui.screens.group.events :as group.events] [status-im.ui.components.tabbar.styles :as tabbar.styles] [status-im.ui.screens.stickers.views :as stickers])) @@ -16,7 +17,7 @@ [stack {:initial-route-name :home :header-mode :none} [{:name :home - :on-focus [::chat.loading/offload-all-messages] + :on-focus [::chat.models/offload-all-messages] :style {:padding-bottom tabbar.styles/tabs-diff} :component home/home} {:name :chat