Load messages on will-focus

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2020-04-07 16:34:43 +02:00
parent 19fdced2bc
commit 55a955d8dd
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
4 changed files with 36 additions and 26 deletions

View File

@ -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"

View File

@ -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]

View File

@ -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))))

View File

@ -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