Refactor code to not have mut Lot if possible, implement into_lot method

for MessageSummary
This commit is contained in:
jikstra
2020-01-14 19:23:23 +01:00
parent 1038d918bf
commit 9dedfb51ad
2 changed files with 27 additions and 43 deletions

View File

@@ -50,7 +50,7 @@ pub struct Chatlist {
/// - dc_lot_t::timestamp: the timestamp of the message. 0 if not applicable. /// - dc_lot_t::timestamp: the timestamp of the message. 0 if not applicable.
/// - dc_lot_t::state: The state of the message as one of the DC_STATE_* constants (see #dc_msg_get_state()). /// - dc_lot_t::state: The state of the message as one of the DC_STATE_* constants (see #dc_msg_get_state()).
#[derive(Debug)] #[derive(Debug, Default)]
pub struct ChatlistSummary { pub struct ChatlistSummary {
username: Option<String>, username: Option<String>,
chat_type: u32, chat_type: u32,
@@ -303,10 +303,9 @@ impl Chatlist {
// "is typing". // "is typing".
// Also, sth. as "No messages" would not work if the summary comes from a message. // Also, sth. as "No messages" would not work if the summary comes from a message.
let mut ret = Lot::new();
if index >= self.ids.len() { if index >= self.ids.len() {
ret.text2 = Some("ErrBadChatlistIndex".to_string()); return Lot { text2: Some("ErrBadChatlistIndex".to_string()), ..Default::default() };
return ret;
} }
let chat_loaded: Chat; let chat_loaded: Chat;
@@ -316,18 +315,19 @@ impl Chatlist {
chat_loaded = chat; chat_loaded = chat;
&chat_loaded &chat_loaded
} else { } else {
return ret; return Lot::default();
}; };
let lastmsg_id = self.ids[index].1; let lastmsg_id = self.ids[index].1;
self._get_summary(context, ret, chat, lastmsg_id) self._get_summary(context, chat, lastmsg_id)
} }
pub fn get_summary_from_chat_id_json(&self, context: &Context, chat_id: u32) -> pub fn get_summary_json(&self, context: &Context, chat_id: u32) -> ChatlistSummary {
ChatlistSummary { ..Default::default() }
}
pub fn _get_summary(&self, context: &Context, mut ret: Lot, chat: &Chat, lastmsg_id: MsgId) -> Lot { pub fn _get_summary(&self, context: &Context, chat: &Chat, lastmsg_id: MsgId) -> Lot {
let mut lastcontact = None; let mut lastcontact = None;
let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) { let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) {
if lastmsg.from_id != DC_CONTACT_ID_SELF if lastmsg.from_id != DC_CONTACT_ID_SELF
&& (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup) && (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup)
@@ -341,20 +341,16 @@ impl Chatlist {
}; };
if chat.id.is_archived_link() { if chat.id.is_archived_link() {
ret.text2 = None; Lot { text2: None, .. Default::default() }
} else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED } else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED
{ {
ret.text2 = Some(context.stock_str(StockMessage::NoMessages).to_string()); Lot {
} else { text2: Some(context.stock_str(StockMessage::NoMessages).to_string()),
let message_summary = MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context); .. Default::default()
ret.text1 = message_summary.text1; }
ret.text1_meaning = message_summary.text1_meaning; } else {
ret.text2 = message_summary.summarytext; MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context).into_lot()
ret.timestamp = message_summary.timestamp;
ret.state = message_summary.state.into();
} }
ret
} }
} }

View File

@@ -463,8 +463,6 @@ impl Message {
} }
pub fn get_summary(&mut self, context: &Context, chat: Option<&Chat>) -> Lot { pub fn get_summary(&mut self, context: &Context, chat: Option<&Chat>) -> Lot {
let mut ret = Lot::new();
let chat_loaded: Chat; let chat_loaded: Chat;
let chat = if let Some(chat) = chat { let chat = if let Some(chat) = chat {
chat chat
@@ -472,7 +470,7 @@ impl Message {
chat_loaded = chat; chat_loaded = chat;
&chat_loaded &chat_loaded
} else { } else {
return ret; return Lot::default();
}; };
let contact = if self.from_id != DC_CONTACT_ID_SELF as u32 let contact = if self.from_id != DC_CONTACT_ID_SELF as u32
@@ -483,9 +481,7 @@ impl Message {
None None
}; };
ret.fill(self, chat, contact.as_ref(), context); MessageSummary::new(self, chat, contact.as_ref(), context).into_lot()
ret
} }
pub fn get_summarytext(&self, context: &Context, approx_characters: usize) -> String { pub fn get_summarytext(&self, context: &Context, approx_characters: usize) -> String {
@@ -776,24 +772,16 @@ impl MessageSummary {
message_summary message_summary
} }
}
impl Lot { pub fn into_lot(&mut self) -> Lot {
/* library-internal */ Lot {
/* in practice, the user additionally cuts the string himself pixel-accurate */ text1: self.text1.clone(),
pub fn fill( text1_meaning: self.text1_meaning,
&mut self, text2: self.summarytext.clone(),
msg: &mut Message, timestamp: self.timestamp,
chat: &Chat, state: self.state.into(),
contact: Option<&Contact>, .. Default::default()
context: &Context, }
) {
let message_summary = MessageSummary::new(msg, chat, contact, context);
self.text1 = message_summary.text1;
self.text1_meaning = message_summary.text1_meaning;
self.text2 = message_summary.summarytext;
self.timestamp = message_summary.timestamp;
self.state = message_summary.state.into();
} }
} }