From 4982e37231dd69c099bf2325d0b9ce5905737687 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 23 Nov 2022 13:28:26 +0100 Subject: [PATCH] Work on message screen partial loading --- sbapp/main.py | 1 - sbapp/sideband/core.py | 45 +++++++++++++++++++++++++++++++++++------- sbapp/ui/messages.py | 36 ++++++++++++++++++++++++++++++--- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/sbapp/main.py b/sbapp/main.py index 1ed9f31..b784964 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -831,7 +831,6 @@ class SidebandApp(MDApp): self.root.ids.message_send_button.disabled = False Clock.schedule_once(cb, 0.5) - def message_propagation_action(self, sender): if self.outbound_mode_paper: self.outbound_mode_paper = False diff --git a/sbapp/sideband/core.py b/sbapp/sideband/core.py index 7f79a1a..88dba1a 100644 --- a/sbapp/sideband/core.py +++ b/sbapp/sideband/core.py @@ -589,8 +589,15 @@ class SidebandCore(): def named_conversation(self, name, context_dest): self._db_conversation_set_name(context_dest, name) - def list_messages(self, context_dest, after = None): - result = self._db_messages(context_dest, after) + def count_messages(self, context_dest): + result = self._db_message_count(context_dest) + if result != None: + return result + else: + return None + + def list_messages(self, context_dest, after = None, before = None, limit = None): + result = self._db_messages(context_dest, after, before, limit) if result != None: return result else: @@ -1012,16 +1019,38 @@ class SidebandCore(): } return message - def _db_messages(self, context_dest, after = None): + def _db_message_count(self, context_dest): db = sqlite3.connect(self.db_path) dbc = db.cursor() - if after == None: - query = "select * from lxm where dest=:context_dest or source=:context_dest" - dbc.execute(query, {"context_dest": context_dest}) + query = "select count(*) from lxm where dest=:context_dest or source=:context_dest" + dbc.execute(query, {"context_dest": context_dest}) + + result = dbc.fetchall() + + db.close() + + if len(result) < 1: + return None else: + return result[0][0] + + def _db_messages(self, context_dest, after = None, before = None, limit = None): + db = sqlite3.connect(self.db_path) + dbc = db.cursor() + + if after != None and before == None: query = "select * from lxm where (dest=:context_dest or source=:context_dest) and rx_ts>:after_ts" dbc.execute(query, {"context_dest": context_dest, "after_ts": after}) + elif after == None and before != None: + query = "select * from lxm where (dest=:context_dest or source=:context_dest) and rx_ts<:before_ts" + dbc.execute(query, {"context_dest": context_dest, "before_ts": before}) + elif after != None and before != None: + query = "select * from lxm where (dest=:context_dest or source=:context_dest) and rx_ts<:before_ts and rx_ts>:after_ts" + dbc.execute(query, {"context_dest": context_dest, "before_ts": before, "after_ts": after}) + else: + query = "select * from lxm where dest=:context_dest or source=:context_dest" + dbc.execute(query, {"context_dest": context_dest}) result = dbc.fetchall() @@ -1057,8 +1086,10 @@ class SidebandCore(): "method": entry[7], "lxm": lxm } - messages.append(message) + messages.append(message) + if len(messages) > limit: + messages = messages[-limit:] return messages def _db_save_lxm(self, lxm, context_dest): diff --git a/sbapp/ui/messages.py b/sbapp/ui/messages.py index 50f4985..f8fc3f2 100644 --- a/sbapp/ui/messages.py +++ b/sbapp/ui/messages.py @@ -13,7 +13,7 @@ from kivy.uix.gridlayout import GridLayout from kivy.uix.boxlayout import BoxLayout from kivy.clock import Clock -from kivymd.uix.button import MDRectangleFlatButton +from kivymd.uix.button import MDRectangleFlatButton, MDRectangleFlatIconButton from kivymd.uix.dialog import MDDialog import os @@ -43,6 +43,7 @@ class Messages(): self.list = None self.widgets = [] self.send_error_dialog = None + self.load_more_button = None self.update() def reload(self): @@ -56,21 +57,45 @@ class Messages(): self.update() - def update(self): - self.messages = self.app.sideband.list_messages(self.context_dest, self.latest_message_timestamp) + def update(self, limit=8): + s_ts = time.time() + self.messages = self.app.sideband.list_messages(self.context_dest, after=self.latest_message_timestamp,limit=limit) + + self.db_message_count = self.app.sideband.count_messages(self.context_dest) + RNS.log("Total messages in db: "+str(self.db_message_count)) + RNS.log("Added items: "+str(len(self.added_item_hashes))) + + if self.load_more_button == None: + self.load_more_button = MDRectangleFlatIconButton( + icon="message-text-clock-outline", + text="Load earlier messages", + font_size=dp(18), + theme_text_color="Custom", + size_hint=[1.0, None], + ) + if self.list == None: layout = GridLayout(cols=1, spacing=dp(16), padding=dp(16), size_hint_y=None) layout.bind(minimum_height=layout.setter('height')) self.list = layout + + if (len(self.added_item_hashes) < self.db_message_count) and not self.load_more_button in self.list.children: + # if self.load_more_button in self.list.children: + # RNS.log("Removing for reinsertion") + # self.list.remove_widget(self.load_more_button) + self.list.add_widget(self.load_more_button, len(self.list.children)) + c_ts = time.time() if len(self.messages) > 0: self.update_widget() + RNS.log("Cards created in "+RNS.prettytime(time.time()-c_ts), RNS.LOG_DEBUG) if self.app.sideband.config["dark_ui"]: intensity_msgs = intensity_msgs_dark else: intensity_msgs = intensity_msgs_light + upd_ts = time.time() for w in self.widgets: m = w.m if self.app.sideband.config["dark_ui"]: @@ -116,6 +141,8 @@ class Messages(): w.heading = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Failed" m["state"] = msg["state"] + RNS.log("Updated message widgets in "+RNS.prettytime(time.time()-upd_ts), RNS.LOG_DEBUG) + RNS.log("Creating messages view took "+RNS.prettytime(time.time()-s_ts), RNS.LOG_DEBUG) def update_widget(self): if self.app.sideband.config["dark_ui"]: @@ -126,6 +153,7 @@ class Messages(): mt_color = [1.0, 1.0, 1.0, 0.95] for m in self.messages: + s_ts = time.time() if not m["hash"] in self.added_item_hashes: txstr = time.strftime(ts_format, time.localtime(m["sent"])) rxstr = time.strftime(ts_format, time.localtime(m["received"])) @@ -411,6 +439,8 @@ class Messages(): if self.latest_message_timestamp == None or m["received"] > self.latest_message_timestamp: self.latest_message_timestamp = m["received"] + RNS.log("Created message card in "+RNS.prettytime(time.time()-s_ts), RNS.LOG_DEBUG) + def get_widget(self): return self.list