Skip to content

Commit

Permalink
Add Delete chat interaction to Conversation screen
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 20, 2024
1 parent 63b1cc6 commit 7a32dd7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
Binary file modified fonts/icebreaker-icons.ttf
Binary file not shown.
43 changes: 42 additions & 1 deletion src/data/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ impl Chat {
history: chat.history,
})
}

pub async fn delete(id: Id) -> Result<(), Error> {
fs::remove_file(Self::path(&id).await?).await?;

let _ = List::remove(&id).await;

match LastOpened::fetch().await {
Ok(LastOpened(last_opened)) if id == last_opened => {
let list = List::fetch().await.ok();

match list.as_ref().and_then(|list| list.entries.first()) {
Some(entry) => {
LastOpened::update(entry.id.clone()).await?;
}
None => {
LastOpened::delete().await?;
}
}
}
_ => {}
}

Ok(())
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -232,7 +256,18 @@ impl List {
let mut list = Self::fetch().await.unwrap_or_default();
list.entries.insert(0, entry);

let json = task::spawn_blocking(move || serde_json::to_vec_pretty(&list)).await?;
list.save().await
}

async fn remove(id: &Id) -> Result<(), Error> {
let mut list = List::fetch().await?;
list.entries.retain(|entry| &entry.id != id);

list.save().await
}

async fn save(self) -> Result<(), Error> {
let json = task::spawn_blocking(move || serde_json::to_vec_pretty(&self)).await?;

fs::write(Self::path().await?, json?).await?;

Expand Down Expand Up @@ -262,6 +297,12 @@ impl LastOpened {

Ok(())
}

async fn delete() -> Result<(), Error> {
fs::remove_file(Self::path().await?).await?;

Ok(())
}
}

async fn storage_dir() -> Result<PathBuf, io::Error> {
Expand Down
4 changes: 4 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub fn expand<'a>() -> Text<'a> {
with_codepoint('\u{E807}')
}

pub fn trash<'a>() -> Text<'a> {
with_codepoint('\u{E808}')
}

fn with_codepoint<'a>(codepoint: char) -> Text<'a> {
const FONT: Font = Font::with_name("icebreaker-icons");

Expand Down
28 changes: 25 additions & 3 deletions src/screen/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use iced::padding;
use iced::task::{self, Task};
use iced::time::{self, Duration, Instant};
use iced::widget::{
self, button, center, column, container, hover, progress_bar, row, scrollable, stack, text,
text_input, tooltip, value,
self, button, center, column, container, horizontal_space, hover, progress_bar, row,
scrollable, stack, text, text_input, tooltip, value,
};
use iced::{Center, Element, Fill, Font, Left, Right, Subscription, Theme};

Expand Down Expand Up @@ -55,6 +55,7 @@ pub enum Message {
Saved(Result<Chat, Error>),
Open(chat::Id),
ChatFetched(Result<Chat, Error>),
Delete,
New,
Search,
ToggleSidebar,
Expand Down Expand Up @@ -318,6 +319,18 @@ impl Conversation {

Action::Run(widget::focus_next())
}
Message::Delete => {
if let Some(id) = self.id.clone() {
Action::Run(Task::future(Chat::delete(id)).and_then(|_| {
Task::batch([
Task::perform(Chat::fetch_last_opened(), Message::ChatFetched),
Task::perform(Chat::list(), Message::ChatsListed),
])
}))
} else {
Action::None
}
}
Message::Search => Action::Back,
Message::ToggleSidebar => {
self.sidebar_open = !self.sidebar_open;
Expand Down Expand Up @@ -366,7 +379,16 @@ impl Conversation {
tip::Position::Right,
);

let bar = stack![title, toggle_sidebar].into();
let delete = tip(
button(icon::trash().style(text::danger))
.padding(0)
.on_press(Message::Delete)
.style(button::text),
"Delete Chat",
tip::Position::Left,
);

let bar = stack![title, row![toggle_sidebar, horizontal_space(), delete]].into();

match &self.state {
State::Booting {
Expand Down

0 comments on commit 7a32dd7

Please sign in to comment.