Added better conversation list sorting

This commit is contained in:
Mark Qvist 2023-09-20 21:41:26 +02:00
parent ce9f9f42d7
commit 458ba51445
2 changed files with 50 additions and 10 deletions

View File

@ -605,8 +605,11 @@ class SidebandCore():
def read_conversation(self, context_dest): def read_conversation(self, context_dest):
self._db_conversation_set_unread(context_dest, False) self._db_conversation_set_unread(context_dest, False)
def unread_conversation(self, context_dest): def unread_conversation(self, context_dest, tx=False):
self._db_conversation_set_unread(context_dest, True) self._db_conversation_set_unread(context_dest, True, tx=tx)
def txtime_conversation(self, context_dest):
self._db_conversation_update_txtime(context_dest)
def trusted_conversation(self, context_dest): def trusted_conversation(self, context_dest):
self._db_conversation_set_trusted(context_dest, True) self._db_conversation_set_trusted(context_dest, True)
@ -883,12 +886,32 @@ class SidebandCore():
RNS.log("An error occurred during persistent setstate database operation: "+str(e), RNS.LOG_ERROR) RNS.log("An error occurred during persistent setstate database operation: "+str(e), RNS.LOG_ERROR)
self.db = None self.db = None
def _db_conversation_set_unread(self, context_dest, unread): def _db_conversation_update_txtime(self, context_dest):
db = self.__db_connect() db = self.__db_connect()
dbc = db.cursor() dbc = db.cursor()
query = "UPDATE conv set last_tx = ? where dest_context = ?"
data = (time.time(), context_dest)
dbc.execute(query, data)
result = dbc.fetchall()
db.commit()
def _db_conversation_set_unread(self, context_dest, unread, tx = False):
db = self.__db_connect()
dbc = db.cursor()
if unread:
if tx:
query = "UPDATE conv set unread = ?, last_tx = ? where dest_context = ?"
data = (unread, time.time(), context_dest)
else:
query = "UPDATE conv set unread = ?, last_rx = ? where dest_context = ?"
data = (unread, time.time(), context_dest)
else:
query = "UPDATE conv set unread = ? where dest_context = ?" query = "UPDATE conv set unread = ? where dest_context = ?"
data = (unread, context_dest) data = (unread, context_dest)
dbc.execute(query, data) dbc.execute(query, data)
result = dbc.fetchall() result = dbc.fetchall()
db.commit() db.commit()
@ -924,13 +947,20 @@ class SidebandCore():
else: else:
convs = [] convs = []
for entry in result: for entry in result:
last_rx = entry[1]
last_tx = entry[2]
last_activity = max(last_rx, last_tx)
conv = { conv = {
"dest": entry[0], "dest": entry[0],
"unread": entry[3], "unread": entry[3],
"last_rx": last_rx,
"last_tx": last_tx,
"last_activity": last_activity,
} }
convs.append(conv) convs.append(conv)
return convs return sorted(convs, key=lambda c: c["last_activity"], reverse=True)
def _db_announces(self): def _db_announces(self):
db = self.__db_connect() db = self.__db_connect()
@ -982,6 +1012,7 @@ class SidebandCore():
conv["trust"] = c[5] conv["trust"] = c[5]
conv["name"] = c[6].decode("utf-8") conv["name"] = c[6].decode("utf-8")
conv["data"] = msgpack.unpackb(c[7]) conv["data"] = msgpack.unpackb(c[7])
conv["last_activity"] = max(c[1], c[2])
return conv return conv
def _db_clear_conversation(self, context_dest): def _db_clear_conversation(self, context_dest):
@ -1019,7 +1050,7 @@ class SidebandCore():
def_name = "".encode("utf-8") def_name = "".encode("utf-8")
query = "INSERT INTO conv (dest_context, last_tx, last_rx, unread, type, trust, name, data) values (?, ?, ?, ?, ?, ?, ?, ?)" query = "INSERT INTO conv (dest_context, last_tx, last_rx, unread, type, trust, name, data) values (?, ?, ?, ?, ?, ?, ?, ?)"
data = (context_dest, 0, 0, 0, SidebandCore.CONV_P2P, 0, def_name, msgpack.packb(None)) data = (context_dest, 0, time.time(), 0, SidebandCore.CONV_P2P, 0, def_name, msgpack.packb(None))
dbc.execute(query, data) dbc.execute(query, data)
db.commit() db.commit()
@ -2000,9 +2031,11 @@ class SidebandCore():
def lxm_ingest(self, message, originator = False): def lxm_ingest(self, message, originator = False):
should_notify = False should_notify = False
is_trusted = False is_trusted = False
unread_reason_tx = False
if originator: if originator:
context_dest = message.destination_hash context_dest = message.destination_hash
unread_reason_tx = True
else: else:
context_dest = message.source_hash context_dest = message.source_hash
is_trusted = self.is_trusted(context_dest) is_trusted = self.is_trusted(context_dest)
@ -2023,14 +2056,16 @@ class SidebandCore():
if self.gui_display() == "messages_screen": if self.gui_display() == "messages_screen":
if self.gui_conversation() != context_dest: if self.gui_conversation() != context_dest:
self.unread_conversation(context_dest) self.unread_conversation(context_dest, tx=unread_reason_tx)
self.setstate("app.flags.unread_conversations", True) self.setstate("app.flags.unread_conversations", True)
else: else:
self.txtime_conversation(context_dest)
self.setstate("wants.viewupdate.conversations", True)
if self.gui_foreground(): if self.gui_foreground():
RNS.log("Squelching notification since GUI is in foreground", RNS.LOG_DEBUG) RNS.log("Squelching notification since GUI is in foreground", RNS.LOG_DEBUG)
should_notify = False should_notify = False
else: else:
self.unread_conversation(context_dest) self.unread_conversation(context_dest, tx=unread_reason_tx)
self.setstate("app.flags.unread_conversations", True) self.setstate("app.flags.unread_conversations", True)
if RNS.vendor.platformutils.is_android(): if RNS.vendor.platformutils.is_android():

View File

@ -97,11 +97,13 @@ class Conversations():
for conv in self.context_dests: for conv in self.context_dests:
context_dest = conv["dest"] context_dest = conv["dest"]
unread = conv["unread"] unread = conv["unread"]
last_activity = conv["last_activity"]
if not context_dest in self.added_item_dests: if not context_dest in self.added_item_dests:
iconl = IconLeftWidget(icon=self.trust_icon(context_dest, unread), on_release=self.app.conversation_action) iconl = IconLeftWidget(icon=self.trust_icon(context_dest, unread), on_release=self.app.conversation_action)
item = OneLineAvatarIconListItem(text=self.app.sideband.peer_display_name(context_dest), on_release=self.app.conversation_action) item = OneLineAvatarIconListItem(text=self.app.sideband.peer_display_name(context_dest), on_release=self.app.conversation_action)
item.add_widget(iconl) item.add_widget(iconl)
item.last_activity = last_activity
item.iconl = iconl item.iconl = iconl
item.sb_uid = context_dest item.sb_uid = context_dest
item.sb_unread = unread item.sb_unread = unread
@ -112,7 +114,7 @@ class Conversations():
t_s = time.time() t_s = time.time()
dest = self.conversation_dropdown.context_dest dest = self.conversation_dropdown.context_dest
try: try:
disp_name = self.app.sideband.raw_display_name(dest) disp_name = self.app.sideband.raw_display_name(dest)+" "+str(conv["last_activity"])
is_trusted = self.app.sideband.is_trusted(dest) is_trusted = self.app.sideband.is_trusted(dest)
yes_button = MDRectangleFlatButton(text="Save",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_accept, text_color=self.app.color_accept) yes_button = MDRectangleFlatButton(text="Save",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_accept, text_color=self.app.color_accept)
@ -282,12 +284,15 @@ class Conversations():
if w.sb_uid == context_dest: if w.sb_uid == context_dest:
disp_name = self.app.sideband.peer_display_name(context_dest) disp_name = self.app.sideband.peer_display_name(context_dest)
trust_icon = self.trust_icon(context_dest, unread) trust_icon = self.trust_icon(context_dest, unread)
w.last_activity = last_activity
if w.iconl.icon != trust_icon: if w.iconl.icon != trust_icon:
w.iconl.icon = trust_icon w.iconl.icon = trust_icon
w.sb_unread = unread w.sb_unread = unread
if w.text != disp_name: if w.text != disp_name:
w.text = disp_name w.text = disp_name
self.list.children.sort(key=lambda w: w.last_activity)
RNS.log("Updated conversation list widgets in "+RNS.prettytime(time.time()-us), RNS.LOG_DEBUG) RNS.log("Updated conversation list widgets in "+RNS.prettytime(time.time()-us), RNS.LOG_DEBUG)
def get_widget(self): def get_widget(self):