diff --git a/sbapp/main.py b/sbapp/main.py index d9fceb9..7496518 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -149,6 +149,10 @@ class SidebandApp(MDApp): else: self.sideband.start() + # Pre-load announce stream widgets + self.init_announces_view() + self.announces_view.update() + ################################################# # General helpers # @@ -245,6 +249,8 @@ class SidebandApp(MDApp): else: self.icon = self.sideband.asset_dir+"/icon.png" + self.announces_view = None + screen = Builder.load_string(root_layout) return screen @@ -304,6 +310,8 @@ class SidebandApp(MDApp): self.root.ids.screen_manager.app = self self.root.ids.app_version_info.text = "Sideband v"+__version__+" "+__variant__ + self.root.ids.screen_manager.transition.duration = 0.25 + self.root.ids.screen_manager.transition.bind(on_complete=self.screen_transition_complete) Clock.schedule_once(self.start_core, 3.5) @@ -958,20 +966,23 @@ class SidebandApp(MDApp): ### Announce Stream screen ###################################### + def init_announces_view(self, sender=None): + if not self.announces_view: + self.announces_view = Announces(self) + self.sideband.setstate("app.flags.new_announces", True) + + for child in self.root.ids.announces_scrollview.children: + self.root.ids.announces_scrollview.remove_widget(child) + + self.root.ids.announces_scrollview.add_widget(self.announces_view.get_widget()) + def announces_action(self, sender=None): self.root.ids.screen_manager.transition.direction = "left" self.root.ids.nav_drawer.set_state("closed") - self.announces_view = Announces(self) - - - # info = "The [b]Announce Stream[/b] feature is not yet implemented in Sideband.\n\nWant it faster? Go to [u][ref=link]https://unsigned.io/sideband[/ref][/u] to support the project." - # self.root.ids.announces_info.text = info - # self.root.ids.announces_info.bind(on_ref_press=link_exec) - - for child in self.root.ids.announces_scrollview.children: - self.root.ids.announces_scrollview.remove_widget(child) - - self.root.ids.announces_scrollview.add_widget(self.announces_view.get_widget()) + + if self.sideband.getstate("app.flags.new_announces"): + self.init_announces_view() + self.announces_view.update() self.root.ids.screen_manager.current = "announces_screen" self.sideband.setstate("app.displaying", self.root.ids.screen_manager.current) @@ -982,6 +993,9 @@ class SidebandApp(MDApp): def announce_filter_action(self, sender=None): pass + def screen_transition_complete(self, sender): + if self.root.ids.screen_manager.current == "announces_screen": + pass ### Keys screen ###################################### diff --git a/sbapp/sideband/core.py b/sbapp/sideband/core.py index 8554932..696247c 100644 --- a/sbapp/sideband/core.py +++ b/sbapp/sideband/core.py @@ -622,18 +622,22 @@ class SidebandCore(): return None else: announces = [] + added_dests = [] for entry in result: try: - announce = { - "dest": entry[2], - "data": entry[3].decode("utf-8"), - "time": entry[1], - "type": entry[4] - } - announces.append(announce) + if not entry[2] in added_dests: + announce = { + "dest": entry[2], + "data": entry[3].decode("utf-8"), + "time": entry[1], + "type": entry[4] + } + added_dests.append(entry[2]) + announces.append(announce) except Exception as e: RNS.log("Exception while fetching announce from DB: "+str(e), RNS.LOG_ERROR) + announces.reverse() return announces def _db_conversation(self, context_dest): @@ -855,6 +859,9 @@ class SidebandCore(): db = sqlite3.connect(self.db_path) dbc = db.cursor() + query = "delete from announce where (source=:source);" + dbc.execute(query, {"source": destination_hash}) + query = "INSERT INTO announce (received, source, data, dest_type) values (?, ?, ?, ?)" data = ( time.time(), diff --git a/sbapp/ui/announces.py b/sbapp/ui/announces.py index a76b9c0..26c85c2 100644 --- a/sbapp/ui/announces.py +++ b/sbapp/ui/announces.py @@ -23,7 +23,12 @@ class Announces(): self.context_dests = [] self.added_item_dests = [] self.list = None - self.update() + self.fetch_announces() + self.list = MDList() + # self.update() + + def fetch_announces(self): + self.announces = self.app.sideband.list_announces() def reload(self): self.clear_list() @@ -37,15 +42,31 @@ class Announces(): self.added_item_dests = [] def update(self): - self.clear_list() - self.announces = self.app.sideband.list_announces() + self.fetch_announces() self.update_widget() self.app.sideband.setstate("app.flags.new_announces", False) def update_widget(self): if self.list == None: self.list = MDList() + + remove_widgets = [] + for item in self.list.children: + if not item.sb_uid in (a["dest"] for a in self.announces): + remove_widgets.append(item) + else: + for announce in self.announces: + if announce["dest"] == item.sb_uid: + if announce["time"] > item.ts: + remove_widgets.append(item) + break + + for item in remove_widgets: + if item.sb_uid in self.added_item_dests: + self.added_item_dests.remove(item.sb_uid) + self.list.remove_widget(item) + for announce in self.announces: context_dest = announce["dest"] ts = announce["time"] @@ -100,6 +121,7 @@ class Announces(): item = OneLineAvatarIconListItem(text=time_string+": "+disp_name, on_release=gen_info(time_string, context_dest, a_data, dest_type)) item.add_widget(iconl) item.sb_uid = context_dest + item.ts = ts def gen_del(dest, item): def x(): @@ -191,7 +213,7 @@ class Announces(): item.add_widget(item.iconr) self.added_item_dests.append(context_dest) - self.list.add_widget(item) + self.list.add_widget(item, index=len(self.list.children)) def get_widget(self): return self.list \ No newline at end of file