chore: checkpoint before removing POPUP_BG
This commit is contained in:
+280
-64
@@ -2,6 +2,7 @@ use std::sync::mpsc as std_mpsc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use chrono::NaiveTime;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
@@ -29,6 +30,9 @@ pub struct App {
|
||||
pub network_status: NetworkStatus,
|
||||
pub popup_input: String,
|
||||
pub popup_cursor: usize,
|
||||
pub popup_secondary: String,
|
||||
pub popup_secondary_cursor: usize,
|
||||
pub edit_field: usize,
|
||||
pub draft_date: NaiveDateTime,
|
||||
pub should_quit: bool,
|
||||
pub task_list_scroll: u16,
|
||||
@@ -40,6 +44,8 @@ pub struct App {
|
||||
pub auth_error: Option<String>,
|
||||
pub sync_stats: SyncStats,
|
||||
last_sync_version: u64,
|
||||
editing_task_id: Option<String>,
|
||||
pending_date_key: bool,
|
||||
auth_tx: std_mpsc::Sender<AuthEvent>,
|
||||
auth_rx: std_mpsc::Receiver<AuthEvent>,
|
||||
sync_tx: mpsc::Sender<SyncCommand>,
|
||||
@@ -90,6 +96,9 @@ impl App {
|
||||
network_status: NetworkStatus::Online,
|
||||
popup_input: String::new(),
|
||||
popup_cursor: 0,
|
||||
popup_secondary: String::new(),
|
||||
popup_secondary_cursor: 0,
|
||||
edit_field: 0,
|
||||
draft_date: chrono::Local::now().naive_local(),
|
||||
should_quit: false,
|
||||
task_list_scroll: 0,
|
||||
@@ -100,6 +109,8 @@ impl App {
|
||||
auth_error: None,
|
||||
sync_stats: SyncStats::default(),
|
||||
last_sync_version: 0,
|
||||
editing_task_id: None,
|
||||
pending_date_key: false,
|
||||
auth_tx,
|
||||
auth_rx,
|
||||
sync_tx,
|
||||
@@ -181,12 +192,68 @@ impl App {
|
||||
let _ = self.sync_tx.try_send(SyncCommand::FullSync);
|
||||
}
|
||||
|
||||
fn update_task_due(&mut self, due: chrono::NaiveDateTime) {
|
||||
if self.tasks.is_empty() || self.selected_task >= self.tasks.len() {
|
||||
return;
|
||||
}
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
task.due = Some(due);
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
&task.id,
|
||||
&task.list_id,
|
||||
&serde_json::to_string(task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn set_due_today(&mut self) {
|
||||
self.update_task_due(chrono::Local::now().naive_local());
|
||||
}
|
||||
|
||||
fn set_due_tomorrow_9am(&mut self) {
|
||||
let tomorrow = chrono::Local::now().naive_local() + chrono::Duration::days(1);
|
||||
let time = NaiveTime::from_hms_opt(9, 0, 0).unwrap();
|
||||
let due = chrono::NaiveDateTime::new(tomorrow.date(), time);
|
||||
self.update_task_due(due);
|
||||
}
|
||||
|
||||
fn set_due_next_week_9am(&mut self) {
|
||||
let next_week = chrono::Local::now().naive_local() + chrono::Duration::days(7);
|
||||
let time = NaiveTime::from_hms_opt(9, 0, 0).unwrap();
|
||||
let due = chrono::NaiveDateTime::new(next_week.date(), time);
|
||||
self.update_task_due(due);
|
||||
}
|
||||
|
||||
fn set_due_next_month_9am(&mut self) {
|
||||
let next_month = chrono::Local::now().naive_local() + chrono::Duration::days(30);
|
||||
let time = NaiveTime::from_hms_opt(9, 0, 0).unwrap();
|
||||
let due = chrono::NaiveDateTime::new(next_month.date(), time);
|
||||
self.update_task_due(due);
|
||||
}
|
||||
|
||||
pub fn handle_key(&mut self, key: KeyEvent) {
|
||||
if let Some(ref popup) = self.show_popup.clone() {
|
||||
self.handle_popup_key(key, popup);
|
||||
return;
|
||||
}
|
||||
|
||||
if self.pending_date_key {
|
||||
self.pending_date_key = false;
|
||||
if self.focus == Focus::TaskList && !self.tasks.is_empty() {
|
||||
match key.code {
|
||||
KeyCode::Char('d') => self.set_due_today(),
|
||||
KeyCode::Char('t') => self.set_due_tomorrow_9am(),
|
||||
KeyCode::Char('w') => self.set_due_next_week_9am(),
|
||||
KeyCode::Char('m') => self.set_due_next_month_9am(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if key.code == KeyCode::Right && key.modifiers.contains(KeyModifiers::CONTROL) {
|
||||
if !self.lists.is_empty() && self.selected_list + 1 < self.lists.len() {
|
||||
self.selected_list += 1;
|
||||
@@ -263,9 +330,20 @@ impl App {
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
if !self.needs_auth {
|
||||
self.popup_input.clear();
|
||||
self.popup_cursor = 0;
|
||||
self.show_popup = Some(Popup::Input);
|
||||
if self.focus == Focus::Tabs {
|
||||
self.editing_task_id = None;
|
||||
self.popup_input.clear();
|
||||
self.popup_cursor = 0;
|
||||
self.show_popup = Some(Popup::Input);
|
||||
} else if !self.lists.is_empty() {
|
||||
self.editing_task_id = None;
|
||||
self.popup_input.clear();
|
||||
self.popup_cursor = 0;
|
||||
self.popup_secondary.clear();
|
||||
self.popup_secondary_cursor = 0;
|
||||
self.edit_field = 0;
|
||||
self.show_popup = Some(Popup::EditTask { field: 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Char('d') | KeyCode::Char('D') => {
|
||||
@@ -276,14 +354,38 @@ impl App {
|
||||
KeyCode::Char('e') | KeyCode::Char('E') => {
|
||||
if !self.needs_auth && self.focus == Focus::TaskList && !self.tasks.is_empty() {
|
||||
let task = &self.tasks[self.selected_task];
|
||||
self.editing_task_id = Some(task.id.clone());
|
||||
self.popup_input = task.title.clone();
|
||||
self.popup_cursor = task.title.len();
|
||||
self.show_popup = Some(Popup::Input);
|
||||
self.popup_secondary = task.notes.clone().unwrap_or_default();
|
||||
self.popup_secondary_cursor = self.popup_secondary.len();
|
||||
self.edit_field = 0;
|
||||
self.show_popup = Some(Popup::EditTask { field: 0 });
|
||||
}
|
||||
}
|
||||
KeyCode::Char('t') | KeyCode::Char('T') => {
|
||||
if self.focus == Focus::TaskList && !self.tasks.is_empty() {
|
||||
self.pending_date_key = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if self.focus == Focus::Detail && !self.tasks.is_empty() {
|
||||
self.show_popup = Some(Popup::DatePicker);
|
||||
} else if self.focus == Focus::TaskList && !self.tasks.is_empty() {
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
task.status = match task.status {
|
||||
TaskStatus::Completed => TaskStatus::NeedsAction,
|
||||
TaskStatus::NeedsAction => TaskStatus::Completed,
|
||||
};
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
&task.id,
|
||||
&task.list_id,
|
||||
&serde_json::to_string(task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
@@ -330,85 +432,54 @@ impl App {
|
||||
KeyCode::Enter => {
|
||||
let input = self.popup_input.trim().to_string();
|
||||
if !input.is_empty() {
|
||||
match self.focus {
|
||||
Focus::Tabs => {
|
||||
let list = TaskList {
|
||||
id: uuid_v4(),
|
||||
title: input,
|
||||
};
|
||||
self.db.insert_list(&list).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
&list.id,
|
||||
&list.id,
|
||||
&serde_json::to_string(&list).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_lists();
|
||||
}
|
||||
Focus::TaskList => {
|
||||
if !self.lists.is_empty() {
|
||||
let list_id = &self.lists[self.selected_list].id;
|
||||
let task = Task {
|
||||
id: uuid_v4(),
|
||||
list_id: list_id.clone(),
|
||||
title: input,
|
||||
notes: None,
|
||||
status: TaskStatus::NeedsAction,
|
||||
due: None,
|
||||
position: 0,
|
||||
};
|
||||
self.db.insert_task(&task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
&task.id,
|
||||
list_id,
|
||||
&serde_json::to_string(&task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
Focus::Detail => {
|
||||
if !self.tasks.is_empty() {
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
task.title = input;
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
&task.id,
|
||||
&task.list_id,
|
||||
&serde_json::to_string(task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
if self.focus == Focus::Tabs {
|
||||
let list = TaskList {
|
||||
id: uuid_v4(),
|
||||
title: input,
|
||||
};
|
||||
self.db.insert_list(&list).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
&list.id,
|
||||
&list.id,
|
||||
&serde_json::to_string(&list).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_lists();
|
||||
}
|
||||
}
|
||||
self.show_popup = None;
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
self.popup_input.insert(self.popup_cursor, c);
|
||||
self.popup_cursor += 1;
|
||||
self.popup_cursor += c.len_utf8();
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if self.popup_cursor > 0 {
|
||||
self.popup_cursor -= 1;
|
||||
self.popup_input.remove(self.popup_cursor);
|
||||
let before = self.popup_input.floor_char_boundary(self.popup_cursor - 1);
|
||||
self.popup_input.replace_range(before..self.popup_cursor, "");
|
||||
self.popup_cursor = before;
|
||||
}
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
if self.popup_cursor < self.popup_input.len() {
|
||||
self.popup_input.remove(self.popup_cursor);
|
||||
let s = &self.popup_input[self.popup_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_input.replace_range(self.popup_cursor..self.popup_cursor + c.len_utf8(), "");
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
self.popup_cursor = self.popup_cursor.saturating_sub(1);
|
||||
if self.popup_cursor > 0 {
|
||||
self.popup_cursor = self.popup_input.floor_char_boundary(self.popup_cursor - 1);
|
||||
}
|
||||
}
|
||||
KeyCode::Right => {
|
||||
if self.popup_cursor < self.popup_input.len() {
|
||||
self.popup_cursor += 1;
|
||||
let s = &self.popup_input[self.popup_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_cursor += c.len_utf8();
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Home => {
|
||||
@@ -419,6 +490,151 @@ impl App {
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Popup::EditTask { field } => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.editing_task_id = None;
|
||||
self.show_popup = None;
|
||||
}
|
||||
KeyCode::Tab | KeyCode::Down => {
|
||||
let new_field = (field + 1) % 2;
|
||||
self.edit_field = new_field;
|
||||
self.show_popup = Some(Popup::EditTask { field: new_field });
|
||||
}
|
||||
KeyCode::Up => {
|
||||
let new_field = (field + 1) % 2;
|
||||
self.edit_field = new_field;
|
||||
self.show_popup = Some(Popup::EditTask { field: new_field });
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let title = self.popup_input.trim().to_string();
|
||||
if title.is_empty() {
|
||||
return;
|
||||
}
|
||||
let notes = self.popup_secondary.trim().to_string();
|
||||
let notes_opt = if notes.is_empty() { None } else { Some(notes) };
|
||||
|
||||
if let Some(task_id) = self.editing_task_id.take() {
|
||||
if let Some(task) = self.tasks.iter_mut().find(|t| t.id == task_id) {
|
||||
task.title = title;
|
||||
task.notes = notes_opt;
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
&task.id,
|
||||
&task.list_id,
|
||||
&serde_json::to_string(task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
} else if !self.lists.is_empty() {
|
||||
let list_id = &self.lists[self.selected_list].id;
|
||||
let task = Task {
|
||||
id: uuid_v4(),
|
||||
list_id: list_id.clone(),
|
||||
title,
|
||||
notes: notes_opt,
|
||||
status: TaskStatus::NeedsAction,
|
||||
due: None,
|
||||
position: 0,
|
||||
};
|
||||
self.db.insert_task(&task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
&task.id,
|
||||
list_id,
|
||||
&serde_json::to_string(&task).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
self.show_popup = None;
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
if *field == 0 {
|
||||
self.popup_input.insert(self.popup_cursor, c);
|
||||
self.popup_cursor += c.len_utf8();
|
||||
} else {
|
||||
self.popup_secondary.insert(self.popup_secondary_cursor, c);
|
||||
self.popup_secondary_cursor += c.len_utf8();
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if *field == 0 {
|
||||
if self.popup_cursor > 0 {
|
||||
let before = self.popup_input.floor_char_boundary(self.popup_cursor - 1);
|
||||
self.popup_input.replace_range(before..self.popup_cursor, "");
|
||||
self.popup_cursor = before;
|
||||
}
|
||||
} else {
|
||||
if self.popup_secondary_cursor > 0 {
|
||||
let before = self.popup_secondary.floor_char_boundary(self.popup_secondary_cursor - 1);
|
||||
self.popup_secondary.replace_range(before..self.popup_secondary_cursor, "");
|
||||
self.popup_secondary_cursor = before;
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
if *field == 0 {
|
||||
if self.popup_cursor < self.popup_input.len() {
|
||||
let s = &self.popup_input[self.popup_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_input.replace_range(self.popup_cursor..self.popup_cursor + c.len_utf8(), "");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if self.popup_secondary_cursor < self.popup_secondary.len() {
|
||||
let s = &self.popup_secondary[self.popup_secondary_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_secondary.replace_range(self.popup_secondary_cursor..self.popup_secondary_cursor + c.len_utf8(), "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
if *field == 0 {
|
||||
if self.popup_cursor > 0 {
|
||||
self.popup_cursor = self.popup_input.floor_char_boundary(self.popup_cursor - 1);
|
||||
}
|
||||
} else {
|
||||
if self.popup_secondary_cursor > 0 {
|
||||
self.popup_secondary_cursor = self.popup_secondary.floor_char_boundary(self.popup_secondary_cursor - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Right => {
|
||||
if *field == 0 {
|
||||
if self.popup_cursor < self.popup_input.len() {
|
||||
let s = &self.popup_input[self.popup_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_cursor += c.len_utf8();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if self.popup_secondary_cursor < self.popup_secondary.len() {
|
||||
let s = &self.popup_secondary[self.popup_secondary_cursor..];
|
||||
if let Some(c) = s.chars().next() {
|
||||
self.popup_secondary_cursor += c.len_utf8();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Home => {
|
||||
if *field == 0 {
|
||||
self.popup_cursor = 0;
|
||||
} else {
|
||||
self.popup_secondary_cursor = 0;
|
||||
}
|
||||
}
|
||||
KeyCode::End => {
|
||||
if *field == 0 {
|
||||
self.popup_cursor = self.popup_input.len();
|
||||
} else {
|
||||
self.popup_secondary_cursor = self.popup_secondary.len();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Popup::DatePicker => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.show_popup = None;
|
||||
|
||||
Reference in New Issue
Block a user