Compare commits
13 Commits
320a1823f3
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cfad78ef8 | |||
| 6fd5b82941 | |||
| 66b833b7ac | |||
| 83762720a1 | |||
| d669ca5c05 | |||
| 2fb550229e | |||
| 10a8d1d75e | |||
| 00fec516ac | |||
| 9649ca96b0 | |||
| a35eab35af | |||
| 3379cbd057 | |||
| 7ebafec3c0 | |||
| 822c335864 |
+16
@@ -1,2 +1,18 @@
|
||||
# Rust
|
||||
target/
|
||||
**/*.rs.bk
|
||||
*.pdb
|
||||
|
||||
# IDE / Editor
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
|
||||
# Environment
|
||||
.env
|
||||
|
||||
# Database (runtime)
|
||||
*.db
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# task_app
|
||||
|
||||
Aplicação TUI de gestão de tarefas com sincronização Google Tasks.
|
||||
|
||||
## Funcionalidades
|
||||
|
||||
- Listas e tarefas do Google Tasks num terminal
|
||||
- Offline-first: dados guardados localmente em SQLite
|
||||
- Sincronização bidirecional automática em background
|
||||
- Calendário com eventos do Google Calendar
|
||||
- Reordenação de tarefas com persistência
|
||||
- Operações CRUD em listas e tarefas
|
||||
- Seleção múltipla e ações em lote
|
||||
|
||||
## Stack
|
||||
|
||||
- **UI:** ratatui + crossterm
|
||||
- **Async:** tokio
|
||||
- **DB:** rusqlite (SQLite)
|
||||
- **Auth:** yup-oauth2 (OAuth 2.0)
|
||||
- **API:** reqwest (Google Tasks + Calendar)
|
||||
|
||||
## Requisitos
|
||||
|
||||
- Rust (edition 2021)
|
||||
- Conta Google com Tasks ativado
|
||||
- `client_secret.json` da Google Cloud Console
|
||||
|
||||
## Configuração
|
||||
|
||||
Coloca o `client_secret.json` num dos seguintes locais:
|
||||
|
||||
- `$GOOGLE_CLIENT_SECRET_FILE`
|
||||
- `~/.config/task_app/client_secret.json`
|
||||
- `./client_secret.json` (diretório atual)
|
||||
|
||||
## Uso
|
||||
|
||||
```bash
|
||||
cargo run
|
||||
```
|
||||
|
||||
### Atalhos
|
||||
|
||||
| Tecla | Ação |
|
||||
|---|---|
|
||||
| `Tab` | Navegar entre painéis |
|
||||
| `↑/↓` | Navegar dentro do painel |
|
||||
| `Ctrl+←/→` | Mudar de lista |
|
||||
| `Alt+↑/↓` | Reordenar tarefa |
|
||||
| `n` | Nova tarefa / lista |
|
||||
| `e` | Editar tarefa |
|
||||
| `d` | Apagar (Enter confirma) |
|
||||
| `Enter` | Completar/descompletar tarefa |
|
||||
| `t` | Definir data (d=today, t=tomorrow, w=week, m=month) |
|
||||
| `Ctrl+r` | Sincronização forçada |
|
||||
| `q` | Sair |
|
||||
+485
-32
@@ -1,3 +1,4 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::mpsc as std_mpsc;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -39,7 +40,8 @@ pub struct App {
|
||||
pub task_list_scroll: u16,
|
||||
pub detail_scroll: u16,
|
||||
pub notes_scroll: u16,
|
||||
pub calendar_scroll: u16,
|
||||
pub calendar_scrolls: [u16; 4],
|
||||
pub calendar_active_week: usize,
|
||||
pub db: Arc<Db>,
|
||||
#[allow(dead_code)]
|
||||
pub api_client: Arc<ApiClient>,
|
||||
@@ -49,6 +51,12 @@ pub struct App {
|
||||
last_sync_version: u64,
|
||||
editing_task_id: Option<String>,
|
||||
pending_date_key: bool,
|
||||
pending_new_key: bool,
|
||||
pending_bulk_move: bool,
|
||||
pub selected_tasks: BTreeSet<usize>,
|
||||
pub bulk_action_selected: usize,
|
||||
pub popup_list_indices: Vec<(String, String)>,
|
||||
pub popup_list_selected: usize,
|
||||
auth_tx: std_mpsc::Sender<AuthEvent>,
|
||||
auth_rx: std_mpsc::Receiver<AuthEvent>,
|
||||
sync_tx: mpsc::Sender<SyncCommand>,
|
||||
@@ -109,7 +117,8 @@ impl App {
|
||||
task_list_scroll: 0,
|
||||
detail_scroll: 0,
|
||||
notes_scroll: 0,
|
||||
calendar_scroll: 0,
|
||||
calendar_scrolls: [0; 4],
|
||||
calendar_active_week: 0,
|
||||
db,
|
||||
api_client,
|
||||
needs_auth: !has_token,
|
||||
@@ -118,6 +127,12 @@ impl App {
|
||||
last_sync_version: 0,
|
||||
editing_task_id: None,
|
||||
pending_date_key: false,
|
||||
pending_new_key: false,
|
||||
pending_bulk_move: false,
|
||||
selected_tasks: BTreeSet::new(),
|
||||
bulk_action_selected: 0,
|
||||
popup_list_indices: Vec::new(),
|
||||
popup_list_selected: 0,
|
||||
auth_tx,
|
||||
auth_rx,
|
||||
sync_tx,
|
||||
@@ -209,6 +224,10 @@ impl App {
|
||||
}
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
task.due = Some(due);
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK UPDATE: title=\"{}\" id={} due={}",
|
||||
task.title, task.id, due
|
||||
));
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
@@ -246,6 +265,33 @@ impl App {
|
||||
}
|
||||
|
||||
pub fn handle_key(&mut self, key: KeyEvent) {
|
||||
if self.pending_new_key {
|
||||
self.pending_new_key = false;
|
||||
match key.code {
|
||||
KeyCode::Char('l') | KeyCode::Char('L') => {
|
||||
self.editing_task_id = None;
|
||||
self.popup_input.clear();
|
||||
self.popup_cursor = 0;
|
||||
self.show_popup = Some(Popup::Input);
|
||||
return;
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
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.notes_scroll = 0;
|
||||
self.show_popup = Some(Popup::EditTask { field: 0 });
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref popup) = self.show_popup.clone() {
|
||||
self.handle_popup_key(key, popup);
|
||||
return;
|
||||
@@ -283,13 +329,41 @@ impl App {
|
||||
|
||||
match key.code {
|
||||
KeyCode::Tab => {
|
||||
match self.focus {
|
||||
Focus::Calendar => {
|
||||
if self.calendar_active_week < 3 {
|
||||
self.calendar_active_week += 1;
|
||||
} else {
|
||||
self.calendar_active_week = 0;
|
||||
self.focus = Focus::Tabs;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.focus = match self.focus {
|
||||
Focus::Tabs => Focus::TaskList,
|
||||
Focus::TaskList => Focus::Detail,
|
||||
Focus::Detail => Focus::Calendar,
|
||||
Focus::Calendar => Focus::Tabs,
|
||||
_ => Focus::Tabs,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Up if key.modifiers.contains(KeyModifiers::SHIFT) => {
|
||||
if self.focus == Focus::TaskList && !self.tasks.is_empty() && self.selected_task > 0 {
|
||||
self.selected_tasks.insert(self.selected_task);
|
||||
self.selected_task -= 1;
|
||||
self.selected_tasks.insert(self.selected_task);
|
||||
self.task_list_scroll = self.task_list_scroll.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
KeyCode::Down if key.modifiers.contains(KeyModifiers::SHIFT) => {
|
||||
if self.focus == Focus::TaskList && !self.tasks.is_empty() && self.selected_task + 1 < self.tasks.len() {
|
||||
self.selected_tasks.insert(self.selected_task);
|
||||
self.selected_task += 1;
|
||||
self.selected_tasks.insert(self.selected_task);
|
||||
self.task_list_scroll += 1;
|
||||
}
|
||||
}
|
||||
KeyCode::Up if key.modifiers.contains(KeyModifiers::ALT) => {
|
||||
if self.focus == Focus::TaskList && !self.tasks.is_empty() {
|
||||
self.reorder_task(-1);
|
||||
@@ -303,6 +377,7 @@ impl App {
|
||||
KeyCode::Up => match self.focus {
|
||||
Focus::TaskList => {
|
||||
if self.selected_task > 0 {
|
||||
self.clear_selection();
|
||||
self.selected_task -= 1;
|
||||
self.task_list_scroll = self.task_list_scroll.saturating_sub(1);
|
||||
}
|
||||
@@ -311,13 +386,14 @@ impl App {
|
||||
self.detail_scroll = self.detail_scroll.saturating_sub(1);
|
||||
}
|
||||
Focus::Calendar => {
|
||||
self.calendar_scroll = self.calendar_scroll.saturating_sub(1);
|
||||
self.calendar_scrolls[self.calendar_active_week] = self.calendar_scrolls[self.calendar_active_week].saturating_sub(1);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
KeyCode::Down => match self.focus {
|
||||
Focus::TaskList => {
|
||||
if self.selected_task + 1 < self.tasks.len() {
|
||||
self.clear_selection();
|
||||
self.selected_task += 1;
|
||||
self.task_list_scroll += 1;
|
||||
}
|
||||
@@ -326,48 +402,68 @@ impl App {
|
||||
self.detail_scroll += 1;
|
||||
}
|
||||
Focus::Calendar => {
|
||||
self.calendar_scroll += 1;
|
||||
self.calendar_scrolls[self.calendar_active_week] = self.calendar_scrolls[self.calendar_active_week].saturating_add(1);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
KeyCode::Right => {
|
||||
if self.focus == Focus::Tabs && !self.lists.is_empty() {
|
||||
if self.selected_list + 1 < self.lists.len() {
|
||||
match self.focus {
|
||||
Focus::Tabs => {
|
||||
if !self.lists.is_empty() && self.selected_list + 1 < self.lists.len() {
|
||||
self.selected_list += 1;
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
Focus::Calendar => {
|
||||
if self.calendar_active_week < 3 {
|
||||
self.calendar_active_week += 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
if self.focus == Focus::Tabs && !self.lists.is_empty() {
|
||||
if self.selected_list > 0 {
|
||||
match self.focus {
|
||||
Focus::Tabs => {
|
||||
if !self.lists.is_empty() && self.selected_list > 0 {
|
||||
self.selected_list -= 1;
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
Focus::Calendar => {
|
||||
if self.calendar_active_week > 0 {
|
||||
self.calendar_active_week -= 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
if !self.needs_auth {
|
||||
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.notes_scroll = 0;
|
||||
self.show_popup = Some(Popup::EditTask { field: 0 });
|
||||
}
|
||||
self.pending_new_key = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Char('d') | KeyCode::Char('D') => {
|
||||
if !self.needs_auth {
|
||||
self.show_popup = Some(Popup::ConfirmDelete);
|
||||
let context = match self.focus {
|
||||
Focus::Tabs => {
|
||||
if self.selected_list < self.lists.len() {
|
||||
format!("Delete list: \"{}\"?", self.lists[self.selected_list].title)
|
||||
} else {
|
||||
"Delete this list?".to_string()
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if !self.tasks.is_empty() && self.selected_task < self.tasks.len() {
|
||||
let title = &self.tasks[self.selected_task].title;
|
||||
let preview: String = title.chars().take(40).collect();
|
||||
format!("Delete task: \"{}\"?", preview)
|
||||
} else {
|
||||
"Delete this task?".to_string()
|
||||
}
|
||||
}
|
||||
};
|
||||
self.show_popup = Some(Popup::ConfirmDelete { context });
|
||||
}
|
||||
}
|
||||
KeyCode::Char('e') | KeyCode::Char('E') => {
|
||||
@@ -392,11 +488,19 @@ impl App {
|
||||
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() {
|
||||
if !self.selected_tasks.is_empty() {
|
||||
self.show_popup = Some(Popup::BulkAction);
|
||||
} else {
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
let old_status = task.status.clone();
|
||||
task.status = match task.status {
|
||||
TaskStatus::Completed => TaskStatus::NeedsAction,
|
||||
TaskStatus::NeedsAction => TaskStatus::Completed,
|
||||
};
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK UPDATE: title=\"{}\" id={} status={:?}->{:?}",
|
||||
task.title, task.id, old_status, task.status
|
||||
));
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
@@ -408,8 +512,13 @@ impl App {
|
||||
self.load_tasks();
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
if self.show_popup.is_some() {
|
||||
self.show_popup = None;
|
||||
} else {
|
||||
self.clear_selection();
|
||||
}
|
||||
}
|
||||
KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
self.trigger_full_sync();
|
||||
@@ -447,28 +556,37 @@ impl App {
|
||||
},
|
||||
Popup::Input => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.pending_bulk_move = false;
|
||||
self.show_popup = None;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let input = self.popup_input.trim().to_string();
|
||||
if !input.is_empty() {
|
||||
if self.focus == Focus::Tabs {
|
||||
if self.pending_bulk_move && !input.is_empty() {
|
||||
self.pending_bulk_move = false;
|
||||
self.bulk_move_to_new_list(&input);
|
||||
self.show_popup = None;
|
||||
} else if !input.is_empty() {
|
||||
let list = TaskList {
|
||||
id: uuid_v4(),
|
||||
title: input,
|
||||
};
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] LIST CREATE: title=\"{}\" id={}",
|
||||
list.title, list.id
|
||||
));
|
||||
self.db.insert_list(&list).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
SyncAction::CreateList,
|
||||
&list.id,
|
||||
&list.id,
|
||||
&serde_json::to_string(&list).unwrap_or_default(),
|
||||
).ok();
|
||||
self.trigger_sync();
|
||||
self.load_lists();
|
||||
}
|
||||
}
|
||||
self.show_popup = None;
|
||||
} else {
|
||||
self.show_popup = None;
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
self.popup_input.insert(self.popup_cursor, c);
|
||||
@@ -568,6 +686,10 @@ impl App {
|
||||
if let Some(d) = due {
|
||||
task.due = Some(d);
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK UPDATE: title=\"{}\" id={} has_notes={} has_due={}",
|
||||
task.title, task.id, task.notes.is_some(), task.due.is_some()
|
||||
));
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
@@ -591,6 +713,10 @@ impl App {
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
};
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK CREATE: title=\"{}\" id={} list={}",
|
||||
task.title, task.id, list_id
|
||||
));
|
||||
self.db.insert_task(&task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Create,
|
||||
@@ -696,6 +822,10 @@ impl App {
|
||||
if !self.tasks.is_empty() {
|
||||
let task = &mut self.tasks[self.selected_task];
|
||||
task.due = Some(self.draft_date);
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK UPDATE: title=\"{}\" id={} due={}",
|
||||
task.title, task.id, self.draft_date
|
||||
));
|
||||
self.db.update_task(task).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Update,
|
||||
@@ -716,7 +846,7 @@ impl App {
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Popup::ConfirmDelete => match key.code {
|
||||
Popup::ConfirmDelete { context: _ } => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.show_popup = None;
|
||||
}
|
||||
@@ -725,13 +855,18 @@ impl App {
|
||||
Focus::Tabs => {
|
||||
if self.selected_list < self.lists.len() {
|
||||
let list_id = self.lists[self.selected_list].id.clone();
|
||||
let title = self.lists[self.selected_list].title.clone();
|
||||
self.db.delete_list(&list_id).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::Delete,
|
||||
SyncAction::DeleteList,
|
||||
&list_id,
|
||||
&list_id,
|
||||
"",
|
||||
).ok();
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] LIST DELETE: title=\"{}\" id={}",
|
||||
title, list_id
|
||||
));
|
||||
self.trigger_sync();
|
||||
self.load_lists();
|
||||
if self.selected_list >= self.lists.len() {
|
||||
@@ -745,13 +880,20 @@ impl App {
|
||||
let task = &self.tasks[self.selected_task];
|
||||
let task_id = task.id.clone();
|
||||
let list_id = task.list_id.clone();
|
||||
let title = task.title.clone();
|
||||
self.db.delete_task(&task_id).ok();
|
||||
if !list_id.contains('-') {
|
||||
self.db.push_sync(
|
||||
SyncAction::Delete,
|
||||
&task_id,
|
||||
&list_id,
|
||||
"",
|
||||
).ok();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK DELETE: title=\"{}\" id={} list={}",
|
||||
title, task_id, list_id
|
||||
));
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
if self.selected_task >= self.tasks.len() {
|
||||
@@ -764,6 +906,308 @@ impl App {
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Popup::BulkAction => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.show_popup = None;
|
||||
}
|
||||
KeyCode::Up => {
|
||||
self.bulk_action_selected = self.bulk_action_selected.saturating_sub(1);
|
||||
}
|
||||
KeyCode::Down => {
|
||||
if self.bulk_action_selected < 6 {
|
||||
self.bulk_action_selected += 1;
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let action = self.bulk_action_selected;
|
||||
self.execute_bulk_action(action);
|
||||
if action <= 4 {
|
||||
self.show_popup = None;
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
if let Some(n) = c.to_digit(10) {
|
||||
let idx = n as usize - 1;
|
||||
if idx <= 6 {
|
||||
self.execute_bulk_action(idx);
|
||||
if idx <= 4 {
|
||||
self.show_popup = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Popup::PickList => match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.show_popup = Some(Popup::BulkAction);
|
||||
}
|
||||
KeyCode::Up => {
|
||||
self.popup_list_selected = self.popup_list_selected.saturating_sub(1);
|
||||
}
|
||||
KeyCode::Down => {
|
||||
if self.popup_list_selected + 1 < self.popup_list_indices.len() {
|
||||
self.popup_list_selected += 1;
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if !self.popup_list_indices.is_empty() {
|
||||
let list_id = self.popup_list_indices[self.popup_list_selected].1.clone();
|
||||
self.bulk_move_to_existing_list(&list_id);
|
||||
self.show_popup = None;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn bulk_mark_completed(&mut self) {
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let task = &mut self.tasks[i];
|
||||
task.status = 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();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] BULK COMPLETED: {} tasks",
|
||||
indices.len()
|
||||
));
|
||||
self.clear_selection();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn bulk_set_due_today(&mut self) {
|
||||
let now = chrono::Local::now().naive_local();
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let task = &mut self.tasks[i];
|
||||
task.due = Some(now);
|
||||
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();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] BULK DUE: {} tasks -> today",
|
||||
indices.len()
|
||||
));
|
||||
self.clear_selection();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn bulk_move_to_new_list(&mut self, list_name: &str) {
|
||||
let list = TaskList {
|
||||
id: uuid_v4(),
|
||||
title: list_name.to_string(),
|
||||
};
|
||||
crate::log_msg(&format!("[task_app] bulk_move_to_new_list: list={} title={} selected={} tasks_len={}",
|
||||
list.id, list_name, self.selected_tasks.len(), self.tasks.len()));
|
||||
for &i in self.selected_tasks.iter() {
|
||||
if i < self.tasks.len() {
|
||||
crate::log_msg(&format!("[task_app] selected[{}]: task={} list_id={} has_hyphen={}",
|
||||
i, self.tasks[i].id, self.tasks[i].list_id, self.tasks[i].list_id.contains('-')));
|
||||
}
|
||||
}
|
||||
|
||||
self.db.insert_list(&list).ok();
|
||||
self.db.push_sync(
|
||||
SyncAction::CreateList,
|
||||
&list.id,
|
||||
&list.id,
|
||||
&serde_json::to_string(&list).unwrap_or_default(),
|
||||
).ok();
|
||||
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let original = &self.tasks[i];
|
||||
|
||||
// Always update local DB immediately
|
||||
self.db.update_task_list_id(&original.id, &list.id).ok();
|
||||
|
||||
if !original.list_id.contains('-') {
|
||||
// Source list has server ID — also push Move sync
|
||||
let payload = serde_json::to_string(&MovePayload {
|
||||
destination_list_id: list.id.clone(),
|
||||
}).unwrap_or_default();
|
||||
crate::log_msg(&format!("[task_app] pushing Move: task={} source={} dest={}",
|
||||
original.id, original.list_id, list.id));
|
||||
self.db.push_sync(
|
||||
SyncAction::Move,
|
||||
&original.id,
|
||||
&original.list_id,
|
||||
&payload,
|
||||
).ok();
|
||||
} else {
|
||||
crate::log_msg(&format!("[task_app] skipping Move (source has hyphen): task={} list_id={}",
|
||||
original.id, original.list_id));
|
||||
}
|
||||
}
|
||||
|
||||
self.clear_selection();
|
||||
self.load_lists();
|
||||
// Switch to the new list
|
||||
if let Some(pos) = self.lists.iter().position(|l| l.id == list.id) {
|
||||
self.selected_list = pos;
|
||||
}
|
||||
self.load_tasks();
|
||||
self.trigger_full_sync();
|
||||
}
|
||||
|
||||
fn bulk_mark_uncomplete(&mut self) {
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let task = &mut self.tasks[i];
|
||||
task.status = TaskStatus::NeedsAction;
|
||||
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();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] BULK UNCOMPLETE: {} tasks",
|
||||
indices.len()
|
||||
));
|
||||
self.clear_selection();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn bulk_set_due_tomorrow(&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);
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let task = &mut self.tasks[i];
|
||||
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();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] BULK DUE: {} tasks -> tomorrow",
|
||||
indices.len()
|
||||
));
|
||||
self.clear_selection();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn bulk_set_due_next_week(&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);
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let task = &mut self.tasks[i];
|
||||
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();
|
||||
}
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] BULK DUE: {} tasks -> next week",
|
||||
indices.len()
|
||||
));
|
||||
self.clear_selection();
|
||||
self.trigger_sync();
|
||||
self.load_tasks();
|
||||
}
|
||||
|
||||
fn bulk_move_to_existing_list(&mut self, target_list_id: &str) {
|
||||
crate::log_msg(&format!("[task_app] bulk_move_to_existing_list: target={} selected={} tasks_len={}",
|
||||
target_list_id, self.selected_tasks.len(), self.tasks.len()));
|
||||
for &i in self.selected_tasks.iter() {
|
||||
if i < self.tasks.len() {
|
||||
crate::log_msg(&format!("[task_app] selected[{}]: task={} list_id={} has_hyphen={}",
|
||||
i, self.tasks[i].id, self.tasks[i].list_id, self.tasks[i].list_id.contains('-')));
|
||||
}
|
||||
}
|
||||
|
||||
let indices: Vec<usize> = self.selected_tasks.iter().copied().collect();
|
||||
for &i in &indices {
|
||||
if i >= self.tasks.len() { continue; }
|
||||
let original = &self.tasks[i];
|
||||
|
||||
// Always update local DB immediately
|
||||
self.db.update_task_list_id(&original.id, target_list_id).ok();
|
||||
|
||||
if !original.list_id.contains('-') {
|
||||
let payload = serde_json::to_string(&MovePayload {
|
||||
destination_list_id: target_list_id.to_string(),
|
||||
}).unwrap_or_default();
|
||||
crate::log_msg(&format!("[task_app] pushing Move: task={} source={} dest={}",
|
||||
original.id, original.list_id, target_list_id));
|
||||
self.db.push_sync(
|
||||
SyncAction::Move,
|
||||
&original.id,
|
||||
&original.list_id,
|
||||
&payload,
|
||||
).ok();
|
||||
} else {
|
||||
crate::log_msg(&format!("[task_app] skipping Move (source has hyphen): task={} list_id={}",
|
||||
original.id, original.list_id));
|
||||
}
|
||||
}
|
||||
self.clear_selection();
|
||||
if let Some(pos) = self.lists.iter().position(|l| l.id == target_list_id) {
|
||||
self.selected_list = pos;
|
||||
}
|
||||
self.load_tasks();
|
||||
self.trigger_full_sync();
|
||||
}
|
||||
|
||||
fn execute_bulk_action(&mut self, action_idx: usize) {
|
||||
match action_idx {
|
||||
0 => self.bulk_mark_completed(),
|
||||
1 => self.bulk_mark_uncomplete(),
|
||||
2 => self.bulk_set_due_today(),
|
||||
3 => self.bulk_set_due_tomorrow(),
|
||||
4 => self.bulk_set_due_next_week(),
|
||||
5 => {
|
||||
self.popup_input.clear();
|
||||
self.popup_cursor = 0;
|
||||
self.pending_bulk_move = true;
|
||||
self.show_popup = Some(Popup::Input);
|
||||
}
|
||||
6 => {
|
||||
self.load_lists();
|
||||
self.popup_list_indices = self.lists.iter()
|
||||
.map(|l| (l.title.clone(), l.id.clone()))
|
||||
.collect();
|
||||
self.popup_list_selected = 0;
|
||||
self.show_popup = Some(Popup::PickList);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -792,6 +1236,10 @@ impl App {
|
||||
let new_pos = self.tasks[new_index as usize].position;
|
||||
|
||||
if self.db.reorder_task(&task_id, new_pos).is_ok() {
|
||||
crate::log_msg(&format!(
|
||||
"[task_app] TASK REORDER: id={} list={} new_pos={}",
|
||||
task_id, list_id, new_pos
|
||||
));
|
||||
let payload = serde_json::json!({
|
||||
"task_id": task_id,
|
||||
"new_position": new_pos
|
||||
@@ -813,6 +1261,7 @@ impl App {
|
||||
}
|
||||
|
||||
fn load_tasks(&mut self) {
|
||||
self.selected_tasks.clear();
|
||||
if self.selected_list < self.lists.len() {
|
||||
let mut tasks = self.db.get_tasks(&self.lists[self.selected_list].id);
|
||||
sort_tasks(&mut tasks);
|
||||
@@ -826,6 +1275,10 @@ impl App {
|
||||
self.selected_task = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_selection(&mut self) {
|
||||
self.selected_tasks.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_tasks(tasks: &mut Vec<Task>) {
|
||||
|
||||
@@ -32,6 +32,14 @@ pub enum SyncAction {
|
||||
Update,
|
||||
Delete,
|
||||
Reorder,
|
||||
Move,
|
||||
CreateList,
|
||||
DeleteList,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MovePayload {
|
||||
pub destination_list_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
||||
+132
-93
@@ -78,15 +78,24 @@ impl ApiClient {
|
||||
Ok(c) => c,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let value: serde_json::Value = match serde_json::from_str(&content) {
|
||||
let entries: Vec<serde_json::Value> = match serde_json::from_str(&content) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let scope = match value["scope"].as_str() {
|
||||
for entry in &entries {
|
||||
let scopes = match entry["scopes"].as_array() {
|
||||
Some(s) => s,
|
||||
None => return false,
|
||||
None => continue,
|
||||
};
|
||||
SCOPES.iter().all(|s| scope.contains(s))
|
||||
let has_all: Vec<&str> = scopes
|
||||
.iter()
|
||||
.filter_map(|s| s.as_str())
|
||||
.collect();
|
||||
if SCOPES.iter().all(|s| has_all.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub async fn start_and_wait_for_auth(&self) -> Result<(), ApiError> {
|
||||
@@ -187,47 +196,7 @@ impl ApiClient {
|
||||
let tasks = all_items
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, item)| {
|
||||
let due_str = item["due"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(16)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
let updated = item["updated"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(19)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
Task {
|
||||
id: item["id"].as_str().unwrap_or("").to_string(),
|
||||
list_id: list_id.to_string(),
|
||||
title: item["title"].as_str().unwrap_or("").to_string(),
|
||||
notes: item["notes"].as_str().map(|s| s.to_string()),
|
||||
status: if item["status"].as_str() == Some("completed") {
|
||||
TaskStatus::Completed
|
||||
} else {
|
||||
TaskStatus::NeedsAction
|
||||
},
|
||||
due: due_str,
|
||||
position: i as i64,
|
||||
created_at: None,
|
||||
updated_at: updated,
|
||||
}
|
||||
})
|
||||
.map(|(i, item)| parse_task_value(item, list_id, i))
|
||||
.collect();
|
||||
|
||||
Ok(tasks)
|
||||
@@ -281,53 +250,13 @@ impl ApiClient {
|
||||
let tasks = all_items
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, item)| {
|
||||
let due_str = item["due"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(16)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
let updated = item["updated"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(19)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
Task {
|
||||
id: item["id"].as_str().unwrap_or("").to_string(),
|
||||
list_id: list_id.to_string(),
|
||||
title: item["title"].as_str().unwrap_or("").to_string(),
|
||||
notes: item["notes"].as_str().map(|s| s.to_string()),
|
||||
status: if item["status"].as_str() == Some("completed") {
|
||||
TaskStatus::Completed
|
||||
} else {
|
||||
TaskStatus::NeedsAction
|
||||
},
|
||||
due: due_str,
|
||||
position: i as i64,
|
||||
created_at: None,
|
||||
updated_at: updated,
|
||||
}
|
||||
})
|
||||
.map(|(i, item)| parse_task_value(item, list_id, i))
|
||||
.collect();
|
||||
|
||||
Ok(tasks)
|
||||
}
|
||||
|
||||
pub async fn create_task(&self, list_id: &str, task: &Task) -> Result<(), ApiError> {
|
||||
pub async fn create_task(&self, list_id: &str, task: &Task) -> Result<Task, ApiError> {
|
||||
let token = self.get_token().await?;
|
||||
|
||||
let mut body = serde_json::json!({
|
||||
@@ -360,13 +289,48 @@ impl ApiClient {
|
||||
.map_err(|e| ApiError::Network(e.to_string()))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(ApiError::Api(format!(
|
||||
"Create failed: {}",
|
||||
resp.status()
|
||||
)));
|
||||
let status = resp.status();
|
||||
let body_text = resp.text().await.unwrap_or_default();
|
||||
return Err(ApiError::Api(format!("Create failed: {} - {}", status, body_text)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
let data: serde_json::Value = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| ApiError::Api(format!("Create parse error: {}", e)))?;
|
||||
|
||||
Ok(parse_task_value(&data, list_id, 0))
|
||||
}
|
||||
|
||||
pub async fn create_list(&self, title: &str) -> Result<TaskList, ApiError> {
|
||||
let token = self.get_token().await?;
|
||||
|
||||
let body = serde_json::json!({ "title": title });
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.post("https://tasks.googleapis.com/tasks/v1/users/@me/lists")
|
||||
.bearer_auth(&token)
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ApiError::Network(e.to_string()))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let body_text = resp.text().await.unwrap_or_default();
|
||||
return Err(ApiError::Api(format!("Create list failed: {} - {}", status, body_text)));
|
||||
}
|
||||
|
||||
let data: serde_json::Value = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| ApiError::Api(format!("Create list parse error: {}", e)))?;
|
||||
|
||||
Ok(TaskList {
|
||||
id: data["id"].as_str().unwrap_or("").to_string(),
|
||||
title: data["title"].as_str().unwrap_or("").to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn update_task(&self, list_id: &str, task: &Task) -> Result<(), ApiError> {
|
||||
@@ -429,6 +393,10 @@ impl ApiClient {
|
||||
.map_err(|e| ApiError::Network(e.to_string()))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
// 404 means already deleted — treat as success
|
||||
if resp.status() == 404 {
|
||||
return Ok(());
|
||||
}
|
||||
return Err(ApiError::Api(format!(
|
||||
"Delete failed: {}",
|
||||
resp.status()
|
||||
@@ -438,6 +406,35 @@ impl ApiClient {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_list(&self, list_id: &str) -> Result<(), ApiError> {
|
||||
let token = self.get_token().await?;
|
||||
|
||||
let url = format!(
|
||||
"https://tasks.googleapis.com/tasks/v1/users/@me/lists/{}",
|
||||
list_id
|
||||
);
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.delete(&url)
|
||||
.bearer_auth(&token)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ApiError::Network(e.to_string()))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
// 404 means already deleted — treat as success
|
||||
if status == 404 {
|
||||
return Ok(());
|
||||
}
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(ApiError::Api(format!("Delete list failed: {} - {}", status, body)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn move_task(
|
||||
&self,
|
||||
list_id: &str,
|
||||
@@ -457,7 +454,7 @@ impl ApiClient {
|
||||
req = req.query(&[("previous", p)]);
|
||||
}
|
||||
if let Some(s) = sibling {
|
||||
req = req.query(&[("destinationTaskList", s)]);
|
||||
req = req.query(&[("destinationTasklist", s)]);
|
||||
}
|
||||
|
||||
let resp = req
|
||||
@@ -521,6 +518,48 @@ impl ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_task_value(item: &serde_json::Value, list_id: &str, position: usize) -> Task {
|
||||
let due_str = item["due"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(16)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
let updated = item["updated"].as_str().and_then(|s| {
|
||||
chrono::NaiveDateTime::parse_from_str(
|
||||
&s.replace("T", " ")
|
||||
.replace("Z", "")
|
||||
.chars()
|
||||
.take(19)
|
||||
.collect::<String>(),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
|
||||
Task {
|
||||
id: item["id"].as_str().unwrap_or("").to_string(),
|
||||
list_id: list_id.to_string(),
|
||||
title: item["title"].as_str().unwrap_or("").to_string(),
|
||||
notes: item["notes"].as_str().map(|s| s.to_string()),
|
||||
status: if item["status"].as_str() == Some("completed") {
|
||||
TaskStatus::Completed
|
||||
} else {
|
||||
TaskStatus::NeedsAction
|
||||
},
|
||||
due: due_str,
|
||||
position: position as i64,
|
||||
created_at: None,
|
||||
updated_at: updated,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_calendar_time(obj: &serde_json::Value) -> Option<chrono::NaiveDateTime> {
|
||||
if let Some(dt) = obj["dateTime"].as_str() {
|
||||
chrono::DateTime::parse_from_rfc3339(dt).ok().map(|d| d.naive_local())
|
||||
|
||||
@@ -40,6 +40,11 @@ impl Db {
|
||||
payload TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
retries INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS list_id_map (
|
||||
old_id TEXT PRIMARY KEY,
|
||||
new_id TEXT NOT NULL
|
||||
);",
|
||||
)?;
|
||||
conn.execute_batch(
|
||||
@@ -206,6 +211,15 @@ impl Db {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_task_list_id(&self, task_id: &str, new_list_id: &str) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE tasks SET list_id = ?1, updated_at = ?2 WHERE id = ?3",
|
||||
params![new_list_id, chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(), task_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reorder_task(&self, task_id: &str, new_position: i64) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let (old_position, list_id): (i64, String) = conn.query_row(
|
||||
@@ -281,6 +295,9 @@ impl Db {
|
||||
SyncAction::Update => "Update",
|
||||
SyncAction::Delete => "Delete",
|
||||
SyncAction::Reorder => "Reorder",
|
||||
SyncAction::Move => "Move",
|
||||
SyncAction::CreateList => "CreateList",
|
||||
SyncAction::DeleteList => "DeleteList",
|
||||
};
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
@@ -306,6 +323,114 @@ impl Db {
|
||||
count > 0
|
||||
}
|
||||
|
||||
pub fn update_task_id(&self, old_id: &str, new_id: &str) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE tasks SET id = ?1 WHERE id = ?2",
|
||||
params![new_id, old_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_sync_task_id(&self, old_id: &str, new_id: &str) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE sync_queue SET task_id = ?1 WHERE task_id = ?2",
|
||||
params![new_id, old_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_list_id(&self, old_id: &str, new_id: &str) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let title: Option<String> = conn.query_row(
|
||||
"SELECT title FROM task_lists WHERE id = ?1",
|
||||
params![old_id],
|
||||
|row| row.get(0),
|
||||
).ok();
|
||||
conn.execute("DELETE FROM task_lists WHERE id = ?1", params![old_id])?;
|
||||
conn.execute("DELETE FROM task_lists WHERE id = ?1", params![new_id])?;
|
||||
if let Some(title) = title {
|
||||
conn.execute(
|
||||
"INSERT INTO task_lists (id, title) VALUES (?1, ?2)",
|
||||
params![new_id, title],
|
||||
)?;
|
||||
}
|
||||
conn.execute(
|
||||
"UPDATE tasks SET list_id = ?1 WHERE list_id = ?2",
|
||||
params![new_id, old_id],
|
||||
)?;
|
||||
conn.execute(
|
||||
"UPDATE sync_queue SET list_id = ?1 WHERE list_id = ?2",
|
||||
params![new_id, old_id],
|
||||
)?;
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO list_id_map (old_id, new_id) VALUES (?1, ?2)",
|
||||
params![old_id, new_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn resolve_list_id(&self, list_id: &str) -> String {
|
||||
if !list_id.contains('-') {
|
||||
return list_id.to_string();
|
||||
}
|
||||
let conn = self.conn.lock().unwrap();
|
||||
// Check if this list_id has been mapped to a server ID
|
||||
let result: Option<String> = conn
|
||||
.query_row(
|
||||
"SELECT new_id FROM list_id_map WHERE old_id = ?1",
|
||||
params![list_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.ok();
|
||||
// Also check if task_lists has this id directly (already a server ID from old schema)
|
||||
let result = result.or_else(|| {
|
||||
conn.query_row(
|
||||
"SELECT id FROM task_lists WHERE id = ?1 AND id NOT LIKE '%-%'",
|
||||
params![list_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
// Also check if task_lists has this list with a different id (same title)
|
||||
let result = result.or_else(|| {
|
||||
conn.query_row(
|
||||
"SELECT id FROM task_lists WHERE title = (SELECT title FROM task_lists WHERE id = ?1) AND id NOT LIKE '%-%'",
|
||||
params![list_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
if result.is_none() && list_id.contains('-') {
|
||||
let log_path = {
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
|
||||
let mut p = std::path::PathBuf::from(home);
|
||||
p.push(".local/share/task_app/sync.log");
|
||||
p
|
||||
};
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_path)
|
||||
{
|
||||
use std::io::Write;
|
||||
let _ = writeln!(f, "[task_app] resolve_list_id: {} NOT FOUND in list_id_map", list_id);
|
||||
}
|
||||
}
|
||||
result.unwrap_or_else(|| list_id.to_string())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn update_sync_list_id(&self, old_id: &str, new_id: &str) -> SqlResult<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE sync_queue SET list_id = ?1 WHERE list_id = ?2",
|
||||
params![new_id, old_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn drain_sync(&self) -> Vec<SyncQueueItem> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let items: Vec<SyncQueueItem> = {
|
||||
@@ -319,6 +444,9 @@ impl Db {
|
||||
"\"Update\"" | "Update" => SyncAction::Update,
|
||||
"\"Delete\"" | "Delete" => SyncAction::Delete,
|
||||
"\"Reorder\"" | "Reorder" => SyncAction::Reorder,
|
||||
"\"Move\"" | "Move" => SyncAction::Move,
|
||||
"\"CreateList\"" | "CreateList" => SyncAction::CreateList,
|
||||
"\"DeleteList\"" | "DeleteList" => SyncAction::DeleteList,
|
||||
_ => SyncAction::Update,
|
||||
};
|
||||
Ok(SyncQueueItem {
|
||||
|
||||
+158
-16
@@ -20,6 +20,28 @@ use crate::infrastructure::api::ApiClient;
|
||||
use crate::infrastructure::db::Db;
|
||||
use crate::ui::{draw, AppView, NetworkStatus};
|
||||
|
||||
fn log_file_path() -> PathBuf {
|
||||
let data_dir = dirs_data_dir();
|
||||
std::fs::create_dir_all(&data_dir).ok();
|
||||
data_dir.join("sync.log")
|
||||
}
|
||||
|
||||
pub fn log_msg(msg: &str) {
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_file_path())
|
||||
{
|
||||
use std::io::Write;
|
||||
let _ = writeln!(f, "{}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
fn dirs_data_dir() -> PathBuf {
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
|
||||
PathBuf::from(home).join(".local/share/task_app")
|
||||
}
|
||||
|
||||
fn find_secret_file() -> Option<PathBuf> {
|
||||
if let Ok(path) = std::env::var("GOOGLE_CLIENT_SECRET_FILE") {
|
||||
let p = PathBuf::from(&path);
|
||||
@@ -150,9 +172,14 @@ fn main() -> io::Result<()> {
|
||||
task_list_scroll: app.task_list_scroll,
|
||||
detail_scroll: app.detail_scroll,
|
||||
notes_scroll: app.notes_scroll,
|
||||
calendar_scroll: app.calendar_scroll,
|
||||
calendar_scrolls: &app.calendar_scrolls,
|
||||
calendar_active_week: app.calendar_active_week,
|
||||
auth_error: app.auth_error.as_deref(),
|
||||
sync_stats: &app.sync_stats,
|
||||
selected_tasks: &app.selected_tasks,
|
||||
bulk_action_selected: app.bulk_action_selected,
|
||||
popup_list_indices: &app.popup_list_indices,
|
||||
popup_list_selected: app.popup_list_selected,
|
||||
};
|
||||
draw(frame, view);
|
||||
})?;
|
||||
@@ -211,11 +238,21 @@ async fn run_initial_sync(
|
||||
Ok(lists) => {
|
||||
total_lists = lists.len();
|
||||
for list in &lists {
|
||||
log_msg(&format!(
|
||||
"[task_app] LIST SYNC: title=\"{}\" id={}",
|
||||
list.title, list.id
|
||||
));
|
||||
db.insert_list(list).ok();
|
||||
}
|
||||
for list in &lists {
|
||||
if let Ok(tasks) = api.fetch_tasks(&list.id).await {
|
||||
total_tasks += tasks.len();
|
||||
log_msg(&format!(
|
||||
"[task_app] TASK SYNC: {} tasks in list=\"{}\" id={}",
|
||||
tasks.len(),
|
||||
list.title,
|
||||
list.id
|
||||
));
|
||||
db.replace_all_tasks(&list.id, &tasks).ok();
|
||||
}
|
||||
}
|
||||
@@ -246,7 +283,7 @@ async fn push_sync(
|
||||
return;
|
||||
}
|
||||
|
||||
let items = db.drain_sync();
|
||||
let mut items = db.drain_sync();
|
||||
let count = items.len();
|
||||
|
||||
if count == 0 {
|
||||
@@ -257,12 +294,68 @@ async fn push_sync(
|
||||
*network_status.lock().await = NetworkStatus::Syncing;
|
||||
|
||||
let mut all_ok = true;
|
||||
|
||||
log_msg(&format!("[task_app] push_sync: {} items in queue", items.len()));
|
||||
for (idx, item) in items.iter().enumerate() {
|
||||
log_msg(&format!("[task_app] item[{}]: action={:?} task={} list={} payload_len={}",
|
||||
idx, item.action, item.task_id, item.list_id, item.payload.len()));
|
||||
}
|
||||
|
||||
// First pass: CreateList items (so list IDs are updated before task operations)
|
||||
for i in 0..items.len() {
|
||||
if items[i].action != SyncAction::CreateList {
|
||||
continue;
|
||||
}
|
||||
let list: TaskList = serde_json::from_str(&items[i].payload).unwrap_or_else(|_| TaskList {
|
||||
id: items[i].task_id.clone(),
|
||||
title: String::new(),
|
||||
});
|
||||
match api.create_list(&list.title).await {
|
||||
Ok(server_list) => {
|
||||
log_msg(&format!("[task_app] CreateList success: local={} -> server={}",
|
||||
items[i].task_id, server_list.id));
|
||||
if server_list.id != items[i].task_id {
|
||||
let _ = db.update_list_id(&items[i].task_id, &server_list.id);
|
||||
for j in (i + 1)..items.len() {
|
||||
if items[j].list_id == items[i].task_id {
|
||||
log_msg(&format!("[task_app] -> updating batch item[{}] list_id: {} -> {}",
|
||||
j, items[j].list_id, server_list.id));
|
||||
items[j].list_id = server_list.id.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log_msg(&format!("[task_app] Sync failed (retry {}/{}): action=CreateList list={} error={}",
|
||||
items[i].retries, MAX_SYNC_RETRIES, items[i].task_id, err));
|
||||
if items[i].retries < MAX_SYNC_RETRIES {
|
||||
let _ = db.push_sync_with_retry(
|
||||
SyncAction::CreateList,
|
||||
&items[i].task_id,
|
||||
&items[i].list_id,
|
||||
&items[i].payload,
|
||||
items[i].retries + 1,
|
||||
);
|
||||
}
|
||||
all_ok = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: everything else
|
||||
for item in &items {
|
||||
if item.action == SyncAction::CreateList {
|
||||
continue;
|
||||
}
|
||||
let api_list_id = db.resolve_list_id(&item.list_id);
|
||||
if api_list_id != item.list_id {
|
||||
log_msg(&format!("[task_app] -> resolved list_id for API call: {} -> {}", item.list_id, api_list_id));
|
||||
}
|
||||
let result = match item.action {
|
||||
SyncAction::Create => {
|
||||
let task = serde_json::from_str::<Task>(&item.payload).unwrap_or_else(|_| Task {
|
||||
id: item.task_id.clone(),
|
||||
list_id: item.list_id.clone(),
|
||||
list_id: api_list_id.clone(),
|
||||
title: String::new(),
|
||||
notes: None,
|
||||
status: TaskStatus::NeedsAction,
|
||||
@@ -271,12 +364,17 @@ async fn push_sync(
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
});
|
||||
api.create_task(&item.list_id, &task).await
|
||||
api.create_task(&api_list_id, &task).await.map(|server_task| {
|
||||
if server_task.id != item.task_id {
|
||||
let _ = db.update_task_id(&item.task_id, &server_task.id);
|
||||
let _ = db.update_sync_task_id(&item.task_id, &server_task.id);
|
||||
}
|
||||
})
|
||||
}
|
||||
SyncAction::Update => {
|
||||
let task = serde_json::from_str::<Task>(&item.payload).unwrap_or_else(|_| Task {
|
||||
id: item.task_id.clone(),
|
||||
list_id: item.list_id.clone(),
|
||||
list_id: api_list_id.clone(),
|
||||
title: String::new(),
|
||||
notes: None,
|
||||
status: TaskStatus::NeedsAction,
|
||||
@@ -285,30 +383,60 @@ async fn push_sync(
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
});
|
||||
api.update_task(&item.list_id, &task).await
|
||||
api.update_task(&api_list_id, &task).await
|
||||
}
|
||||
SyncAction::Delete => {
|
||||
api.delete_task(&item.list_id, &item.task_id).await
|
||||
api.delete_task(&api_list_id, &item.task_id).await
|
||||
}
|
||||
SyncAction::Reorder => {
|
||||
api.move_task(&item.list_id, &item.task_id, None, None).await
|
||||
api.move_task(&api_list_id, &item.task_id, None, None).await
|
||||
}
|
||||
SyncAction::Move => {
|
||||
let move_payload: MovePayload = serde_json::from_str(&item.payload).unwrap_or_else(|_| MovePayload {
|
||||
destination_list_id: String::new(),
|
||||
});
|
||||
let resolved_dest = db.resolve_list_id(&move_payload.destination_list_id);
|
||||
log_msg(&format!("[task_app] Move: task={} source={} dest_raw={} dest_resolved={}",
|
||||
item.task_id, api_list_id, move_payload.destination_list_id, resolved_dest));
|
||||
api.move_task(&api_list_id, &item.task_id, None, Some(&resolved_dest)).await
|
||||
}
|
||||
SyncAction::DeleteList => {
|
||||
api.delete_list(&item.task_id).await
|
||||
}
|
||||
_ => Ok(()),
|
||||
};
|
||||
|
||||
if let Err(err) = result {
|
||||
eprintln!("[task_app] Sync failed (retry {}/{}): action={:?} task={} error={}",
|
||||
item.retries, MAX_SYNC_RETRIES, item.action, item.task_id, err);
|
||||
if item.retries < MAX_SYNC_RETRIES {
|
||||
log_msg(&format!("[task_app] Sync failed (retry {}/{}): action={:?} task={} list_id={} error={}",
|
||||
item.retries, MAX_SYNC_RETRIES, item.action, item.task_id, item.list_id, err));
|
||||
let resolved_list_id = db.resolve_list_id(&item.list_id);
|
||||
if resolved_list_id != item.list_id {
|
||||
log_msg(&format!("[task_app] -> resolved list_id: {} -> {}", item.list_id, resolved_list_id));
|
||||
}
|
||||
if resolved_list_id.contains('-') {
|
||||
log_msg("[task_app] -> list_id is local UUID (list not synced), dropping item");
|
||||
} else if item.retries < MAX_SYNC_RETRIES {
|
||||
let re_payload = if item.action == SyncAction::Move {
|
||||
let move_payload: MovePayload = serde_json::from_str(&item.payload).unwrap_or_else(|_| MovePayload {
|
||||
destination_list_id: String::new(),
|
||||
});
|
||||
let resolved_dest = db.resolve_list_id(&move_payload.destination_list_id);
|
||||
serde_json::to_string(&MovePayload {
|
||||
destination_list_id: resolved_dest,
|
||||
}).unwrap_or_else(|_| item.payload.clone())
|
||||
} else {
|
||||
item.payload.clone()
|
||||
};
|
||||
let _ = db.push_sync_with_retry(
|
||||
item.action.clone(),
|
||||
&item.task_id,
|
||||
&item.list_id,
|
||||
&item.payload,
|
||||
&resolved_list_id,
|
||||
&re_payload,
|
||||
item.retries + 1,
|
||||
);
|
||||
} else {
|
||||
eprintln!("[task_app] Dropping sync item after {} failed attempts: action={:?} task={}",
|
||||
MAX_SYNC_RETRIES, item.action, item.task_id);
|
||||
log_msg(&format!("[task_app] Dropping sync item after {} failed attempts: action={:?} task={}",
|
||||
MAX_SYNC_RETRIES, item.action, item.task_id));
|
||||
}
|
||||
all_ok = false;
|
||||
}
|
||||
@@ -364,6 +492,10 @@ async fn pull_sync(
|
||||
Ok(lists) => {
|
||||
total_lists = lists.len();
|
||||
for list in &lists {
|
||||
log_msg(&format!(
|
||||
"[task_app] LIST SYNC: title=\"{}\" id={}",
|
||||
list.title, list.id
|
||||
));
|
||||
db.insert_list(list).ok();
|
||||
}
|
||||
for list in &lists {
|
||||
@@ -375,10 +507,20 @@ async fn pull_sync(
|
||||
if let Ok(tasks) = result {
|
||||
total_tasks += tasks.len();
|
||||
if use_incremental {
|
||||
log_msg(&format!(
|
||||
"[task_app] TASK SYNC (incremental): {} tasks in list=\"{}\"",
|
||||
tasks.len(),
|
||||
list.title
|
||||
));
|
||||
for task in &tasks {
|
||||
db.insert_task(task).ok();
|
||||
}
|
||||
} else {
|
||||
log_msg(&format!(
|
||||
"[task_app] TASK SYNC (full): {} tasks in list=\"{}\"",
|
||||
tasks.len(),
|
||||
list.title
|
||||
));
|
||||
db.replace_all_tasks(&list.id, &tasks).ok();
|
||||
}
|
||||
}
|
||||
@@ -410,7 +552,7 @@ async fn refresh_calendar(
|
||||
*guard = events;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[task_app] Calendar fetch failed: {}", e);
|
||||
log_msg(&format!("[task_app] Calendar fetch failed: {}", e));
|
||||
*network_status.lock().await = NetworkStatus::Offline;
|
||||
}
|
||||
}
|
||||
|
||||
+187
-63
@@ -1,3 +1,7 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use chrono::Datelike;
|
||||
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span, Text};
|
||||
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Tabs, Wrap};
|
||||
@@ -74,6 +78,7 @@ pub fn render_task_list(
|
||||
selected: usize,
|
||||
focused: bool,
|
||||
_scroll: u16,
|
||||
selected_tasks: &BTreeSet<usize>,
|
||||
) {
|
||||
let total = tasks.len();
|
||||
let done = tasks.iter().filter(|t| t.status == TaskStatus::Completed).count();
|
||||
@@ -83,7 +88,9 @@ pub fn render_task_list(
|
||||
|
||||
let items: Vec<ListItem> = tasks
|
||||
.iter()
|
||||
.map(|task| {
|
||||
.enumerate()
|
||||
.map(|(idx, task)| {
|
||||
let is_selected = selected_tasks.contains(&idx);
|
||||
let checkbox = match task.status {
|
||||
TaskStatus::Completed => "[\u{2713}]",
|
||||
TaskStatus::NeedsAction => "[ ]",
|
||||
@@ -119,25 +126,29 @@ pub fn render_task_list(
|
||||
content_width.saturating_sub(used)
|
||||
};
|
||||
|
||||
let mut spans = vec![
|
||||
Span::styled(
|
||||
checkbox_str,
|
||||
Style::default().fg(if task.status == TaskStatus::Completed {
|
||||
Color::Green
|
||||
let checkbox_style = if is_selected {
|
||||
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD).bg(Color::DarkGray)
|
||||
} else if task.status == TaskStatus::Completed {
|
||||
Style::default().fg(Color::Green)
|
||||
} else {
|
||||
Style::default().fg(Color::DarkGray)
|
||||
};
|
||||
|
||||
let title_style = if is_selected {
|
||||
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD).bg(Color::DarkGray)
|
||||
} else {
|
||||
Color::DarkGray
|
||||
}),
|
||||
),
|
||||
Span::styled(
|
||||
display_title,
|
||||
Style::default().fg(DETAIL_COLOR).add_modifier(
|
||||
if task.status == TaskStatus::Completed {
|
||||
Modifier::CROSSED_OUT
|
||||
} else {
|
||||
Modifier::empty()
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
};
|
||||
|
||||
let mut spans = vec![
|
||||
Span::styled(checkbox_str, checkbox_style),
|
||||
Span::styled(display_title, title_style),
|
||||
];
|
||||
|
||||
if !due_text.is_empty() {
|
||||
@@ -466,8 +477,8 @@ pub fn render_date_picker(
|
||||
frame.render_widget(paragraph, popup_area);
|
||||
}
|
||||
|
||||
pub fn render_confirm_popup(frame: &mut Frame, area: Rect) {
|
||||
let popup_area = centered_rect(50, 5, area);
|
||||
pub fn render_confirm_popup(frame: &mut Frame, area: Rect, context: &str) {
|
||||
let popup_area = centered_rect(60, 6, area);
|
||||
frame.render_widget(Clear, popup_area);
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
@@ -479,7 +490,7 @@ pub fn render_confirm_popup(frame: &mut Frame, area: Rect) {
|
||||
let text = Text::from(vec![
|
||||
Line::from(""),
|
||||
Line::from(Span::styled(
|
||||
" Delete this item? ",
|
||||
format!(" {} ", context),
|
||||
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
|
||||
)),
|
||||
Line::from(""),
|
||||
@@ -496,6 +507,91 @@ pub fn render_confirm_popup(frame: &mut Frame, area: Rect) {
|
||||
frame.render_widget(paragraph, popup_area);
|
||||
}
|
||||
|
||||
pub fn render_bulk_action_popup(frame: &mut Frame, area: Rect, count: usize, selected: usize) {
|
||||
let popup_area = centered_rect(55, 12, area);
|
||||
frame.render_widget(Clear, popup_area);
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default().bg(POPUP_BG))
|
||||
.border_style(Style::default().fg(POPUP_BORDER))
|
||||
.title(format!(" Bulk Actions ({} selected) ", count))
|
||||
.title_alignment(Alignment::Left);
|
||||
|
||||
let options = [
|
||||
"1. Mark as completed",
|
||||
"2. Mark as uncomplete",
|
||||
"3. Set due date to Today",
|
||||
"4. Set due date to Tomorrow",
|
||||
"5. Set due date to Next Week",
|
||||
"6. Move to new list...",
|
||||
"7. Move to existing list...",
|
||||
];
|
||||
|
||||
let mut lines = vec![Line::from("")];
|
||||
for (i, opt) in options.iter().enumerate() {
|
||||
let style = if i == selected {
|
||||
Style::default().fg(FOCUS_COLOR).add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(Color::Cyan)
|
||||
};
|
||||
lines.push(Line::from(Span::styled(
|
||||
format!(" {}", opt),
|
||||
style,
|
||||
)));
|
||||
}
|
||||
lines.push(Line::from(""));
|
||||
lines.push(Line::from(Span::styled(
|
||||
" ↑/↓: navigate Enter:ok 1-7:shortcut Esc:cancel",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
)));
|
||||
|
||||
let paragraph = Paragraph::new(Text::from(lines))
|
||||
.block(block)
|
||||
.alignment(Alignment::Left);
|
||||
|
||||
frame.render_widget(paragraph, popup_area);
|
||||
}
|
||||
|
||||
pub fn render_pick_list_popup(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
lists: &[(String, String)],
|
||||
selected: usize,
|
||||
) {
|
||||
let popup_area = centered_rect(60, 10, area);
|
||||
frame.render_widget(Clear, popup_area);
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default().bg(POPUP_BG))
|
||||
.border_style(Style::default().fg(POPUP_BORDER))
|
||||
.title(" Select List ")
|
||||
.title_alignment(Alignment::Left);
|
||||
|
||||
let mut lines = vec![Line::from("")];
|
||||
for (i, (title, _)) in lists.iter().enumerate() {
|
||||
let style = if i == selected {
|
||||
Style::default().fg(FOCUS_COLOR).add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(Color::Cyan)
|
||||
};
|
||||
lines.push(Line::from(Span::styled(
|
||||
format!(" {}", title),
|
||||
style,
|
||||
)));
|
||||
}
|
||||
lines.push(Line::from(""));
|
||||
lines.push(Line::from(Span::styled(
|
||||
" ↑/↓: navigate Enter:ok Esc:cancel",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
)));
|
||||
|
||||
let paragraph = Paragraph::new(Text::from(lines))
|
||||
.block(block)
|
||||
.alignment(Alignment::Left);
|
||||
|
||||
frame.render_widget(paragraph, popup_area);
|
||||
}
|
||||
|
||||
pub fn render_device_auth_popup(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
@@ -600,51 +696,86 @@ pub fn render_calendar_panel(
|
||||
area: Rect,
|
||||
events: &[CalendarEvent],
|
||||
focused: bool,
|
||||
scroll: u16,
|
||||
scrolls: &[u16; 4],
|
||||
active_week: usize,
|
||||
) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(if focused { FOCUS_COLOR } else { Color::DarkGray }))
|
||||
.title(" Calendar ")
|
||||
.title_alignment(Alignment::Left);
|
||||
|
||||
if events.is_empty() {
|
||||
let paragraph = Paragraph::new(Text::from(Line::from(Span::styled(
|
||||
" No upcoming events ",
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))))
|
||||
.block(block)
|
||||
.alignment(Alignment::Center);
|
||||
frame.render_widget(paragraph, area);
|
||||
if area.width < 20 || area.height < 3 {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
let mut current_date: Option<chrono::NaiveDate> = None;
|
||||
let today = chrono::Local::now().naive_local().date();
|
||||
let weekday = today.weekday().num_days_from_monday();
|
||||
let this_monday = today - chrono::Duration::days(weekday as i64);
|
||||
|
||||
for event in events {
|
||||
if let Some(start) = event.start {
|
||||
let event_date = start.date();
|
||||
if Some(event_date) != current_date {
|
||||
current_date = Some(event_date);
|
||||
let day_header = format!(
|
||||
" --- {} {} --- ",
|
||||
event_date.format("%A"),
|
||||
event_date.format("%d/%m"),
|
||||
);
|
||||
lines.push(Line::from(Span::styled(
|
||||
day_header,
|
||||
Style::default().fg(Color::Gray),
|
||||
)));
|
||||
let cols = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Ratio(1, 4); 4])
|
||||
.split(area);
|
||||
|
||||
let has_events = events.iter().any(|e| {
|
||||
e.start.map_or(false, |s| {
|
||||
let d = s.date();
|
||||
d >= this_monday && d < this_monday + chrono::Duration::days(28)
|
||||
})
|
||||
});
|
||||
|
||||
for week_idx in 0..4 {
|
||||
let week_start = this_monday + chrono::Duration::weeks(week_idx as i64);
|
||||
let week_title = format!(" W/C {} ", week_start.format("%d/%m"));
|
||||
let col_area = cols[week_idx];
|
||||
|
||||
let border = if focused && week_idx == active_week { FOCUS_COLOR } else { Color::DarkGray };
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(border))
|
||||
.title(week_title)
|
||||
.title_alignment(Alignment::Left);
|
||||
|
||||
if !has_events {
|
||||
let msg = if week_idx == 0 {
|
||||
" No upcoming events "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let paragraph = Paragraph::new(Text::from(Line::from(Span::styled(
|
||||
msg,
|
||||
Style::default().fg(Color::DarkGray),
|
||||
))))
|
||||
.block(block);
|
||||
frame.render_widget(paragraph, col_area);
|
||||
continue;
|
||||
}
|
||||
|
||||
let time_str = start.format("%H:%M").to_string();
|
||||
let summary = &event.summary;
|
||||
let line_text = if summary.len() > 30 {
|
||||
format!(" {} {:.30}…", time_str, summary)
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
|
||||
for day_offset in 0..7 {
|
||||
let day = week_start + chrono::Duration::days(day_offset);
|
||||
|
||||
let day_style = if day == today {
|
||||
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
|
||||
} else if matches!(day.weekday(), chrono::Weekday::Sat | chrono::Weekday::Sun) {
|
||||
Style::default().fg(Color::Magenta).add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
format!(" {} {}", time_str, summary)
|
||||
Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)
|
||||
};
|
||||
let day_label = format!(
|
||||
" {} {}",
|
||||
match day.weekday() {
|
||||
chrono::Weekday::Mon => "Mon",
|
||||
chrono::Weekday::Tue => "Tue",
|
||||
chrono::Weekday::Wed => "Wed",
|
||||
chrono::Weekday::Thu => "Thu",
|
||||
chrono::Weekday::Fri => "Fri",
|
||||
chrono::Weekday::Sat => "Sat",
|
||||
chrono::Weekday::Sun => "Sun",
|
||||
},
|
||||
day.format("%d/%m")
|
||||
);
|
||||
lines.push(Line::from(Span::styled(day_label, day_style)));
|
||||
|
||||
for event in events.iter().filter(|e| e.start.map_or(false, |s| s.date() == day)) {
|
||||
let time_str = event.start.map(|s| s.format("%H:%M").to_string()).unwrap_or_default();
|
||||
let line_text = format!(" {} {}", time_str, event.summary);
|
||||
lines.push(Line::from(Span::styled(
|
||||
line_text,
|
||||
Style::default().fg(DETAIL_COLOR),
|
||||
@@ -652,18 +783,11 @@ pub fn render_calendar_panel(
|
||||
}
|
||||
}
|
||||
|
||||
let inner_h = (area.height as usize).saturating_sub(2);
|
||||
let visible_lines: Vec<Line> = lines
|
||||
.iter()
|
||||
.skip(scroll as usize)
|
||||
.take(inner_h)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let paragraph = Paragraph::new(Text::from(visible_lines))
|
||||
let paragraph = Paragraph::new(Text::from(lines))
|
||||
.block(block)
|
||||
.scroll((0, 0));
|
||||
frame.render_widget(paragraph, area);
|
||||
.scroll((scrolls[week_idx], 0));
|
||||
frame.render_widget(paragraph, col_area);
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple word wrap: splits text at word boundaries to fit max_width chars per line
|
||||
|
||||
+29
-19
@@ -1,5 +1,7 @@
|
||||
pub mod components;
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::Frame;
|
||||
|
||||
@@ -20,7 +22,9 @@ pub enum Popup {
|
||||
Input,
|
||||
EditTask { field: usize },
|
||||
DatePicker,
|
||||
ConfirmDelete,
|
||||
ConfirmDelete { context: String },
|
||||
BulkAction,
|
||||
PickList,
|
||||
DeviceAuth { url: String, code: String },
|
||||
}
|
||||
|
||||
@@ -48,9 +52,14 @@ pub struct AppView<'a> {
|
||||
pub detail_scroll: u16,
|
||||
pub notes_scroll: u16,
|
||||
pub calendar_events: &'a [CalendarEvent],
|
||||
pub calendar_scroll: u16,
|
||||
pub calendar_scrolls: &'a [u16; 4],
|
||||
pub calendar_active_week: usize,
|
||||
pub auth_error: Option<&'a str>,
|
||||
pub sync_stats: &'a SyncStats,
|
||||
pub selected_tasks: &'a BTreeSet<usize>,
|
||||
pub bulk_action_selected: usize,
|
||||
pub popup_list_indices: &'a [(String, String)],
|
||||
pub popup_list_selected: usize,
|
||||
}
|
||||
|
||||
pub fn draw(frame: &mut Frame, view: AppView) {
|
||||
@@ -61,13 +70,15 @@ pub fn draw(frame: &mut Frame, view: AppView) {
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(12),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
let tabs_area = main_layout[0];
|
||||
let body_area = main_layout[1];
|
||||
let status_area = main_layout[2];
|
||||
let calendar_area = main_layout[2];
|
||||
let status_area = main_layout[3];
|
||||
|
||||
let is_tabs_focused = view.focus == Focus::Tabs;
|
||||
render_tabs_bar(frame, tabs_area, view.lists, view.selected_list, is_tabs_focused);
|
||||
@@ -77,28 +88,15 @@ pub fn draw(frame: &mut Frame, view: AppView) {
|
||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
.split(body_area);
|
||||
|
||||
let left_col = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([Constraint::Min(0), Constraint::Length(8)])
|
||||
.split(body_layout[0]);
|
||||
|
||||
let is_task_list_focused = view.focus == Focus::TaskList;
|
||||
render_task_list(
|
||||
frame,
|
||||
left_col[0],
|
||||
body_layout[0],
|
||||
view.tasks,
|
||||
view.selected_task,
|
||||
is_task_list_focused,
|
||||
view.task_list_scroll,
|
||||
);
|
||||
|
||||
let is_calendar_focused = view.focus == Focus::Calendar;
|
||||
render_calendar_panel(
|
||||
frame,
|
||||
left_col[1],
|
||||
view.calendar_events,
|
||||
is_calendar_focused,
|
||||
view.calendar_scroll,
|
||||
view.selected_tasks,
|
||||
);
|
||||
|
||||
let is_detail_focused = view.focus == Focus::Detail;
|
||||
@@ -110,6 +108,16 @@ pub fn draw(frame: &mut Frame, view: AppView) {
|
||||
view.detail_scroll,
|
||||
);
|
||||
|
||||
let is_calendar_focused = view.focus == Focus::Calendar;
|
||||
render_calendar_panel(
|
||||
frame,
|
||||
calendar_area,
|
||||
view.calendar_events,
|
||||
is_calendar_focused,
|
||||
view.calendar_scrolls,
|
||||
view.calendar_active_week,
|
||||
);
|
||||
|
||||
render_status_bar(frame, status_area, view.network_status, view.sync_stats);
|
||||
|
||||
if let Some(popup) = view.show_popup {
|
||||
@@ -121,7 +129,9 @@ pub fn draw(frame: &mut Frame, view: AppView) {
|
||||
view.notes_scroll, *field,
|
||||
),
|
||||
Popup::DatePicker => render_date_picker(frame, area, view.draft_date),
|
||||
Popup::ConfirmDelete => render_confirm_popup(frame, area),
|
||||
Popup::ConfirmDelete { context } => render_confirm_popup(frame, area, context),
|
||||
Popup::BulkAction => render_bulk_action_popup(frame, area, view.selected_tasks.len(), view.bulk_action_selected),
|
||||
Popup::PickList => render_pick_list_popup(frame, area, view.popup_list_indices, view.popup_list_selected),
|
||||
Popup::DeviceAuth { url, code } => render_device_auth_popup(frame, area, url, code, view.auth_error),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user