notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

draft.rs (1499B)


      1 use poll_promise::Promise;
      2 
      3 use crate::{media_upload::Nip94Event, ui::note::PostType, Error};
      4 use std::collections::HashMap;
      5 
      6 #[derive(Default)]
      7 pub struct Draft {
      8     pub buffer: String,
      9     pub uploaded_media: Vec<Nip94Event>, // media uploads to include
     10     pub uploading_media: Vec<Promise<Result<Nip94Event, Error>>>, // promises that aren't ready yet
     11     pub upload_errors: Vec<String>,      // media upload errors to show the user
     12 }
     13 
     14 #[derive(Default)]
     15 pub struct Drafts {
     16     replies: HashMap<[u8; 32], Draft>,
     17     quotes: HashMap<[u8; 32], Draft>,
     18     compose: Draft,
     19 }
     20 
     21 impl Drafts {
     22     pub fn compose_mut(&mut self) -> &mut Draft {
     23         &mut self.compose
     24     }
     25 
     26     pub fn get_from_post_type(&mut self, post_type: &PostType) -> &mut Draft {
     27         match post_type {
     28             PostType::New => self.compose_mut(),
     29             PostType::Quote(note_id) => self.quote_mut(note_id.bytes()),
     30             PostType::Reply(note_id) => self.reply_mut(note_id.bytes()),
     31         }
     32     }
     33 
     34     pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
     35         self.replies.entry(*id).or_default()
     36     }
     37 
     38     pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
     39         self.quotes.entry(*id).or_default()
     40     }
     41 }
     42 
     43 impl Draft {
     44     pub fn new() -> Self {
     45         Draft::default()
     46     }
     47 
     48     pub fn clear(&mut self) {
     49         self.buffer = "".to_string();
     50         self.upload_errors = Vec::new();
     51         self.uploaded_media = Vec::new();
     52         self.uploading_media = Vec::new();
     53     }
     54 }