Use `initialNumToRender` property + simplify our messaging FlatList.

see https://facebook.github.io/react-native/docs/flatlist#initialnumtorender

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Igor Mandrigin 2019-06-06 13:50:52 +02:00 committed by Andrey Shovkoplyas
parent 287e097471
commit 2204ad5142
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
1 changed files with 4 additions and 32 deletions

View File

@ -339,33 +339,13 @@
(defonce messages-list-ref (atom nil)) (defonce messages-list-ref (atom nil))
(def ^:const initial-limit 5)
(def ^:const second-initial-limit-step 6)
(def second-limit (+ initial-limit second-initial-limit-step))
(def ^:const third-initial-limit-step 11)
(def ^:const default-limit-step 20)
(def ^:const initial-threshold 0.5)
(def ^:const default-threshold 2)
(defonce messages-limit (reagent/atom initial-limit))
(defn increment-limit [lim]
(+ lim
(case lim
initial-limit second-initial-limit-step
second-limit third-initial-limit-step
default-limit-step)))
(defview messages-view (defview messages-view
[{:keys [group-chat chat-id pending-invite-inviter-name contact] :as chat} [{:keys [group-chat chat-id pending-invite-inviter-name contact] :as chat}
modal?] modal?]
(letsubs [messages [:chats/current-chat-messages-stream] (letsubs [messages [:chats/current-chat-messages-stream]
photo-path [:chats/photo-path chat-id] photo-path [:chats/photo-path chat-id]
current-public-key [:account/public-key]] current-public-key [:account/public-key]]
{:component-will-mount {:component-did-mount
(fn []
(reset! messages-limit initial-limit))
:component-did-mount
(fn [args] (fn [args]
(when-not (:messages-initialized? (second (.-argv (.-props args)))) (when-not (:messages-initialized? (second (.-argv (.-props args))))
(re-frame/dispatch [:chat.ui/load-more-messages])) (re-frame/dispatch [:chat.ui/load-more-messages]))
@ -373,12 +353,8 @@
{:messages-focused? true {:messages-focused? true
:input-focused? false}]))} :input-focused? false}]))}
(let [no-messages (empty? messages) (let [no-messages (empty? messages)
m-limit @messages-limit
threshold (if (= m-limit initial-limit)
initial-threshold
default-threshold)
flat-list-conf flat-list-conf
{:data (take m-limit messages) {:data messages
:ref #(reset! messages-list-ref %) :ref #(reset! messages-list-ref %)
:footer [chat-intro-header-container chat no-messages] :footer [chat-intro-header-container chat no-messages]
:key-fn #(or (:message-id %) (:value %)) :key-fn #(or (:message-id %) (:value %))
@ -390,13 +366,9 @@
:row message :row message
:idx idx :idx idx
:list-ref messages-list-ref}]) :list-ref messages-list-ref}])
:initialNumToRender 6
:inverted true :inverted true
:onEndReachedThreshold threshold :onEndReached #(re-frame/dispatch [:chat.ui/load-more-messages])
:onEndReached (fn []
(swap! messages-limit increment-limit)
(when (> @messages-limit (count messages))
(re-frame/dispatch
[:chat.ui/load-more-messages])))
:keyboardShouldPersistTaps :handled} :keyboardShouldPersistTaps :handled}
group-header {:header [group-chat-footer chat-id]}] group-header {:header [group-chat-footer chat-id]}]
(if pending-invite-inviter-name (if pending-invite-inviter-name