Skip to content

Commit

Permalink
implement delete popup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Sep 20, 2024
1 parent e9fd395 commit 8db7ea2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 93 deletions.
73 changes: 38 additions & 35 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub struct App {
pub chat: Chat,
pub conversations: Conversations,
pub prompt: Prompt,
// TODO: currently both popups can co-exist, it has to change
// there can be one popup at a time
// TODO: I cannot allow empty conversation
pub new_conversation_popup: NewConversationPopup,
pub delete_conversation_popup: DeleteConversationPopup,
focus: AppFocus,
Expand Down Expand Up @@ -129,6 +132,36 @@ impl App {
return Ok(());
}

if self.delete_conversation_popup.is_activated() {
self.delete_conversation_popup.deactivate();

return Ok(());
}

if self.new_conversation_popup.is_activated() {
self.new_conversation_popup.handle_input(key_event);

return Ok(());
}

if let AppFocus::Prompt = self.current_focus() {
self.prompt.handle_input(key_event);
}
}
KeyCode::Char('y') | KeyCode::Char('Y') => {
if self.delete_conversation_popup.is_activated() {
if let Some(conversation) = self.conversations.currently_selected() {
db::delete_conversation(&self.sqlite, conversation.id).await?;
self.conversations.delete_conversation(conversation);
self.chat.reset();
}
// we deactivate anyway, if there was conversation it's gone now
// if there wasn't any, no need to keep popup anymore
self.delete_conversation_popup.deactivate();

return Ok(());
}

if self.new_conversation_popup.is_activated() {
self.new_conversation_popup.handle_input(key_event);

Expand All @@ -154,14 +187,14 @@ impl App {
return Ok(());
}

// enter triggers deletion - this is default behaviour
if self.delete_conversation_popup.is_activated() {
if let Some(conversation) = self.conversations.currently_selected() {
if self.delete_conversation_popup.yes() {
db::delete_conversation(&self.sqlite, conversation.id).await?;
self.conversations.delete_conversation(conversation);
self.chat.reset();
}
db::delete_conversation(&self.sqlite, conversation.id).await?;
self.conversations.delete_conversation(conversation);
self.chat.reset();
}
self.delete_conversation_popup.deactivate();

return Ok(());
}
Expand Down Expand Up @@ -228,36 +261,6 @@ impl App {
self.prompt.handle_input(key_event);
}
},
KeyCode::Right => {
if self.delete_conversation_popup.is_activated() {
return Ok(());
}

if self.new_conversation_popup.is_activated() {
self.new_conversation_popup.handle_input(key_event);

return Ok(());
}

if let AppFocus::Prompt = self.current_focus() {
self.prompt.handle_input(key_event);
}
}
KeyCode::Left => {
if self.delete_conversation_popup.is_activated() {
return Ok(());
}

if self.new_conversation_popup.is_activated() {
self.new_conversation_popup.handle_input(key_event);

return Ok(());
}

if let AppFocus::Prompt = self.current_focus() {
self.prompt.handle_input(key_event);
}
}
KeyCode::Esc => {
if self.new_conversation_popup.is_activated() {
self.new_conversation_popup.deactivate();
Expand Down
26 changes: 1 addition & 25 deletions src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,34 +141,18 @@ impl NewConversationPopup {
}
}

enum YesOrNo {
Yes,
No,
}

pub struct DeleteConversationPopup {
activated: bool,
yes_or_no: YesOrNo,
}

#[allow(clippy::derivable_impls)]
impl Default for DeleteConversationPopup {
fn default() -> Self {
Self {
activated: Default::default(),
yes_or_no: YesOrNo::No,
}
Self { activated: false }
}
}

impl DeleteConversationPopup {
pub fn yes(&self) -> bool {
match self.yes_or_no {
YesOrNo::Yes => true,
YesOrNo::No => false,
}
}

pub fn is_activated(&self) -> bool {
self.activated
}
Expand All @@ -180,14 +164,6 @@ impl DeleteConversationPopup {
pub fn deactivate(&mut self) {
self.activated = false;
}

pub fn confirm(&mut self) {
self.yes_or_no = YesOrNo::Yes;
}

pub fn cancel(&mut self) {
self.yes_or_no = YesOrNo::No;
}
}

#[cfg(test)]
Expand Down
47 changes: 14 additions & 33 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const FOCUS_BORDER_TYPE: BorderType = BorderType::Double;
const NORMAL_BORDER_TYPE: BorderType = BorderType::Rounded;

pub fn render(app: &mut App, frame: &mut Frame) {
let dimmed = app.new_conversation_popup.is_activated();
let dimmed =
app.new_conversation_popup.is_activated() | app.delete_conversation_popup.is_activated();
let color = match dimmed {
true => Color::DarkGray,
false => Color::White,
Expand Down Expand Up @@ -161,39 +162,19 @@ pub fn render(app: &mut App, frame: &mut Frame) {
}

if app.delete_conversation_popup.is_activated() {
let padding = 2u16;
let (popup_width, popup_height) = (30, 10);
let (popup_x, popup_y) = calculate_coordinates(
(area.width, area.height),
(popup_width + padding, popup_height + padding),
);
let outer_popup_area = Rect::new(
popup_x,
popup_y,
popup_width + padding,
popup_height + padding,
);
frame.render_widget(Clear, outer_popup_area);
let popup_message = "Would you like to delete conversation? <Y/n>";
let (popup_width, popup_height) = (popup_message.len() as u16 + 4, 3);
let (popup_x, popup_y) =
calculate_coordinates((area.width, area.height), (popup_width, popup_height));

let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);
frame.render_widget(Clear, popup_area);

let vertical_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Ratio(1, 2); 2])
.split(outer_popup_area);

let paragraph = Paragraph::new("Do you really want to remove conversation?")
.alignment(Alignment::Center);
frame.render_widget(paragraph, vertical_layout[0]);

let horizontal_lower_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 2); 2])
.split(vertical_layout[1]);
let confirm_button =
Paragraph::new("Yes").block(Block::bordered().border_type(BorderType::Rounded));
frame.render_widget(confirm_button, horizontal_lower_layout[0]);
let cancel_button =
Paragraph::new("Cancel").block(Block::bordered().border_type(BorderType::Rounded));
frame.render_widget(cancel_button, horizontal_lower_layout[1]);
let paragraph = Paragraph::new(popup_message)
.centered()
.block(Block::bordered().border_type(BorderType::Rounded))
.style(Color::White);
frame.render_widget(paragraph, popup_area);
}
}

Expand Down

0 comments on commit 8db7ea2

Please sign in to comment.