summary refs log tree commit diff
path: root/src/timeline
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-01-06 16:42:56 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2020-01-06 18:29:08 +0100
commit5af6f6528ba05974edd359833194c6f1dd3e1c96 (patch)
treec867831072eb83843b44edc142e0f59a64270acc /src/timeline
parentinvert timeline (diff)
downloadnheko-5af6f6528ba05974edd359833194c6f1dd3e1c96.tar.xz
Use fetchMore for native lazy loading of item model data
Diffstat (limited to 'src/timeline')
-rw-r--r--src/timeline/TimelineModel.cpp75
-rw-r--r--src/timeline/TimelineModel.h4
2 files changed, 47 insertions, 32 deletions
diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp
index 8746a31f..3dafb8c2 100644
--- a/src/timeline/TimelineModel.cpp
+++ b/src/timeline/TimelineModel.cpp
@@ -349,6 +349,50 @@ TimelineModel::data(const QModelIndex &index, int role) const
         }
 }
 
+bool
+TimelineModel::canFetchMore(const QModelIndex &) const
+{
+        if (eventOrder.empty())
+                return true;
+        if (!std::holds_alternative<mtx::events::StateEvent<mtx::events::state::Create>>(
+              events[eventOrder.back()]))
+                return true;
+        else
+
+                return false;
+}
+
+void
+TimelineModel::fetchMore(const QModelIndex &)
+{
+        if (paginationInProgress) {
+                nhlog::ui()->warn("Already loading older messages");
+                return;
+        }
+
+        paginationInProgress = true;
+        mtx::http::MessagesOpts opts;
+        opts.room_id = room_id_.toStdString();
+        opts.from    = prev_batch_token_.toStdString();
+
+        nhlog::ui()->debug("Paginationg room {}", opts.room_id);
+
+        http::client()->messages(
+          opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
+                  if (err) {
+                          nhlog::net()->error("failed to call /messages ({}): {} - {}",
+                                              opts.room_id,
+                                              mtx::errors::to_string(err->matrix_error.errcode),
+                                              err->matrix_error.error);
+                          paginationInProgress = false;
+                          return;
+                  }
+
+                  emit oldMessagesRetrieved(std::move(res));
+                  paginationInProgress = false;
+          });
+}
+
 void
 TimelineModel::addEvents(const mtx::responses::Timeline &timeline)
 {
@@ -466,37 +510,6 @@ TimelineModel::internalAddEvents(
 }
 
 void
-TimelineModel::fetchHistory()
-{
-        if (paginationInProgress) {
-                nhlog::ui()->warn("Already loading older messages");
-                return;
-        }
-
-        paginationInProgress = true;
-        mtx::http::MessagesOpts opts;
-        opts.room_id = room_id_.toStdString();
-        opts.from    = prev_batch_token_.toStdString();
-
-        nhlog::ui()->info("Paginationg room {}", opts.room_id);
-
-        http::client()->messages(
-          opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
-                  if (err) {
-                          nhlog::net()->error("failed to call /messages ({}): {} - {}",
-                                              opts.room_id,
-                                              mtx::errors::to_string(err->matrix_error.errcode),
-                                              err->matrix_error.error);
-                          paginationInProgress = false;
-                          return;
-                  }
-
-                  emit oldMessagesRetrieved(std::move(res));
-                  paginationInProgress = false;
-          });
-}
-
-void
 TimelineModel::setCurrentIndex(int index)
 {
         auto oldIndex = idToIndex(currentId);
diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h
index 4161a0fc..0f18f7ef 100644
--- a/src/timeline/TimelineModel.h
+++ b/src/timeline/TimelineModel.h
@@ -154,6 +154,9 @@ public:
         int rowCount(const QModelIndex &parent = QModelIndex()) const override;
         QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
 
+        bool canFetchMore(const QModelIndex &) const override;
+        void fetchMore(const QModelIndex &) override;
+
         Q_INVOKABLE QColor userColor(QString id, QColor background);
         Q_INVOKABLE QString displayName(QString id) const;
         Q_INVOKABLE QString avatarUrl(QString id) const;
@@ -175,7 +178,6 @@ public:
         void sendMessage(const T &msg);
 
 public slots:
-        void fetchHistory();
         void setCurrentIndex(int index);
         int currentIndex() const { return idToIndex(currentId); }
         void markEventsAsRead(const std::vector<QString> &event_ids);