draft.rs (1060B)
1 use crate::ui::note::PostType; 2 use std::collections::HashMap; 3 4 #[derive(Default)] 5 pub struct Draft { 6 pub buffer: String, 7 } 8 9 #[derive(Default)] 10 pub struct Drafts { 11 replies: HashMap<[u8; 32], Draft>, 12 quotes: HashMap<[u8; 32], Draft>, 13 compose: Draft, 14 } 15 16 impl Drafts { 17 pub fn compose_mut(&mut self) -> &mut Draft { 18 &mut self.compose 19 } 20 21 pub fn get_from_post_type(&mut self, post_type: &PostType) -> &mut Draft { 22 match post_type { 23 PostType::New => self.compose_mut(), 24 PostType::Quote(note_id) => self.quote_mut(note_id.bytes()), 25 PostType::Reply(note_id) => self.reply_mut(note_id.bytes()), 26 } 27 } 28 29 pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 30 self.replies.entry(*id).or_default() 31 } 32 33 pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 34 self.quotes.entry(*id).or_default() 35 } 36 } 37 38 impl Draft { 39 pub fn new() -> Self { 40 Draft::default() 41 } 42 43 pub fn clear(&mut self) { 44 self.buffer = "".to_string(); 45 } 46 }