From a8c8f5630f1ed3314499aa6633f73ad4b72fc269 Mon Sep 17 00:00:00 2001 From: Sun Date: Wed, 2 Jun 2021 11:57:47 +0800 Subject: [PATCH] fix actived UI --- lib/provider.dart | 6 ++++-- src/apps/group_chat/layer.rs | 32 ++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/provider.dart b/lib/provider.dart index 089ef88..2f30ec5 100644 --- a/lib/provider.dart +++ b/lib/provider.dart @@ -115,6 +115,7 @@ class AccountProvider extends ChangeNotifier { this.coreShowWidget = DefaultCoreShow(); // load sessions. + this.actived = 0; this.sessions.clear(); this.orderKeys.clear(); rpc.send('session-list', []); @@ -131,6 +132,7 @@ class AccountProvider extends ChangeNotifier { } logout() { + this.actived = 0; this.accounts.clear(); this.clearActivedAccount(); this.sessions.clear(); @@ -193,7 +195,7 @@ class AccountProvider extends ChangeNotifier { } clearActivedSession(SessionType type) { - if (this.activedSession.type == type && this.actived > 0) { + if (this.actived > 0 && this.activedSession.type == type) { rpc.send('session-suspend', [this.actived, this.activedSession.gid, this.activedSession.type == SessionType.Group] ); @@ -227,7 +229,7 @@ class AccountProvider extends ChangeNotifier { if (online == OnlineType.Lost) { this.activedSession.online = OnlineType.Waiting; Timer(Duration(seconds: 10), () { - if (this.sessions[id].online == OnlineType.Waiting) { + if (this.sessions[id] != null && this.sessions[id].online == OnlineType.Waiting) { this.sessions[id].online = OnlineType.Lost; notifyListeners(); } diff --git a/src/apps/group_chat/layer.rs b/src/apps/group_chat/layer.rs index cf67463..67313e4 100644 --- a/src/apps/group_chat/layer.rs +++ b/src/apps/group_chat/layer.rs @@ -17,7 +17,7 @@ use tdn_storage::local::DStorage; use crate::layer::{Layer, Online}; use crate::rpc::{session_connect, session_create, session_last, session_lost, session_suspend}; -use crate::session::{connect_session, SessionType}; +use crate::session::{connect_session, Session, SessionType}; use crate::storage::{group_chat_db, session_db, write_avatar_sync}; use super::models::{from_network_message, GroupChat, Member, Request}; @@ -243,9 +243,33 @@ async fn handle_event( let msg = from_network_message(height, gid, mid, &mgid, nmsg, mtime, &base)?; results.rpcs.push(rpc::message_create(mgid, &msg)); println!("Sync: create message ok"); - results - .rpcs - .push(session_last(mgid, &sid, &msg.datetime, &msg.content, false)); + + // UPDATE SESSION. + let s_db = session_db(&base, &mgid)?; + if let Ok(id) = Session::last( + &s_db, + &gid, + &SessionType::Group, + &msg.datetime, + &msg.content, + true, + ) { + results.rpcs.push(session_last( + mgid, + &id, + &msg.datetime, + &msg.content, + false, + )); + } else { + let c_db = group_chat_db(&base, &mgid)?; + if let Some(f) = GroupChat::get_id(&c_db, &gid)? { + let mut session = f.to_session(); + session.last_content = msg.content; + session.insert(&s_db)?; + results.rpcs.push(session_create(mgid, &session)); + } + } } }