Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with lists #42

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 115 additions & 92 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub enum TasksAction {
PopulateLists(Vec<List>),
Export(Vec<Task>),
AddList(List),
DeleteList,
DeleteList(Option<segmented_button::Entity>),
FetchLists,
}

Expand Down Expand Up @@ -113,9 +113,9 @@ impl ContextPage {
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DialogPage {
New(String),
Icon(String),
Rename(String),
Delete,
Icon(Option<segmented_button::Entity>, String),
Rename(Option<segmented_button::Entity>, String),
Delete(Option<segmented_button::Entity>),
Calendar(NaiveDate),
Export(String),
}
Expand Down Expand Up @@ -147,11 +147,13 @@ impl MenuAction for Action {
Action::WindowClose => Message::Application(ApplicationAction::WindowClose),
Action::WindowNew => Message::Application(ApplicationAction::WindowNew),
Action::NewList => Message::Dialog(DialogAction::Open(DialogPage::New(String::new()))),
Action::Icon => Message::Dialog(DialogAction::Open(DialogPage::Icon(String::new()))),
Action::Icon => {
Message::Dialog(DialogAction::Open(DialogPage::Icon(None, String::new())))
}
Action::RenameList => {
Message::Dialog(DialogAction::Open(DialogPage::Rename(String::new())))
Message::Dialog(DialogAction::Open(DialogPage::Rename(None, String::new())))
}
Action::DeleteList => Message::Dialog(DialogAction::Open(DialogPage::Delete)),
Action::DeleteList => Message::Dialog(DialogAction::Open(DialogPage::Delete(None))),
}
}
}
Expand Down Expand Up @@ -484,20 +486,15 @@ impl Application for Tasks {
Message::Dialog(dialog_action) => match dialog_action {
DialogAction::Open(page) => {
match page {
DialogPage::Icon(icon) => {
if self.nav_model.active_data::<List>().is_some() {
self.dialog_pages.push_back(DialogPage::Icon(icon));
}
}
DialogPage::Rename(_) => {
if let Some(list) = self.nav_model.active_data::<List>() {
DialogPage::Rename(entity, _) => {
let data = if let Some(entity) = entity {
self.nav_model.data::<List>(entity)
} else {
self.nav_model.active_data::<List>()
};
if let Some(list) = data {
self.dialog_pages
.push_back(DialogPage::Rename(list.name.clone()));
}
}
DialogPage::Delete => {
if self.nav_model.active_data::<List>().is_some() {
self.dialog_pages.push_back(DialogPage::Delete);
.push_back(DialogPage::Rename(entity, list.name.clone()));
}
}
page => self.dialog_pages.push_back(page),
Expand Down Expand Up @@ -527,34 +524,59 @@ impl Application for Tasks {
},
));
}
DialogPage::Rename(name) => {
let entity = self.nav_model.active();
self.nav_model.text_set(entity, name.clone());
if let Some(list) = self.nav_model.active_data_mut::<List>() {
DialogPage::Rename(entity, name) => {
let data = if let Some(entity) = entity {
self.nav_model.data_mut::<List>(entity)
} else {
self.nav_model.active_data_mut::<List>()
};
if let Some(list) = data {
let title = if let Some(icon) = list.icon() {
format!("{} {}", icon.clone(), &name)
} else {
name.clone()
};
list.name.clone_from(&name);
let list = list.clone();
self.nav_model
.text_set(self.nav_model.active(), title.clone());
let task = app::Task::perform(
todo::update_list(list.clone(), self.service.clone()),
|_| message::none(),
);
tasks.push(task);
tasks.push(self.update(Message::Content(
content::Message::List(Some(list)),
)));
}
}
DialogPage::Delete => {
tasks.push(self.update(Message::Tasks(TasksAction::DeleteList)));
DialogPage::Delete(entity) => {
tasks.push(
self.update(Message::Tasks(TasksAction::DeleteList(entity))),
);
}
DialogPage::Icon(icon) => {
if let Some(list) = self.nav_model.active_data::<List>() {
DialogPage::Icon(entity, icon) => {
let data = if let Some(entity) = entity {
self.nav_model.data::<List>(entity)
} else {
self.nav_model.active_data::<List>()
};
if let Some(list) = data {
let entity = self.nav_model.active();
let title = format!("{} {}", icon.clone(), list.name.clone());
self.nav_model.text_set(entity, title);
}
if let Some(list) = self.nav_model.active_data_mut::<List>() {
list.icon = Some(icon);
let list = list.clone();
let task = app::Task::perform(
todo::update_list(list.clone(), self.service.clone()),
|_| message::none(),
);
tasks.push(task);
tasks.push(self.update(Message::Content(
content::Message::List(Some(list)),
)));
}
}
DialogPage::Calendar(date) => {
Expand Down Expand Up @@ -599,8 +621,13 @@ impl Application for Tasks {
let task = self.on_nav_select(entity);
tasks.push(task);
}
TasksAction::DeleteList => {
if let Some(list) = self.nav_model.active_data::<List>() {
TasksAction::DeleteList(entity) => {
let data = if let Some(entity) = entity {
self.nav_model.data::<List>(entity)
} else {
self.nav_model.active_data::<List>()
};
if let Some(list) = data {
let task = app::Task::perform(
todo::delete_list(list.id().clone(), self.service.clone()),
|result| match result {
Expand All @@ -623,72 +650,64 @@ impl Application for Tasks {
}
}
},
Message::Application(application_action) => {
match application_action {
ApplicationAction::WindowClose => {
if let Some(window_id) = self.core.main_window_id() {
return window::close(window_id);
}
Message::Application(application_action) => match application_action {
ApplicationAction::WindowClose => {
if let Some(window_id) = self.core.main_window_id() {
return window::close(window_id);
}
ApplicationAction::WindowNew => match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).spawn() {
Ok(_) => {}
Err(err) => {
eprintln!("failed to execute {exe:?}: {err}");
}
},
}
ApplicationAction::WindowNew => match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).spawn() {
Ok(_) => {}
Err(err) => {
eprintln!("failed to get current executable path: {err}");
eprintln!("failed to execute {exe:?}: {err}");
}
},
ApplicationAction::AppTheme(index) => {
let app_theme = match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
};
config_set!(app_theme, app_theme);
return self.update_config();
Err(err) => {
eprintln!("failed to get current executable path: {err}");
}
ApplicationAction::SystemThemeModeChange => {
return self.update_config();
}
ApplicationAction::Key(modifiers, key) => {
for (key_bind, action) in &self.key_binds {
if key_bind.matches(modifiers, &key) {
return self.update(action.message());
}
},
ApplicationAction::AppTheme(index) => {
let app_theme = match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
};
config_set!(app_theme, app_theme);
return self.update_config();
}
ApplicationAction::SystemThemeModeChange => {
return self.update_config();
}
ApplicationAction::Key(modifiers, key) => {
for (key_bind, action) in &self.key_binds {
if key_bind.matches(modifiers, &key) {
return self.update(action.message());
}
}
ApplicationAction::Modifiers(modifiers) => {
self.modifiers = modifiers;
}
ApplicationAction::Focus(id) => tasks.push(widget::text_input::focus(id)),
ApplicationAction::NavMenuAction(nav_menu_action) => match nav_menu_action {
NavMenuAction::Rename(entity) => {
if self.nav_model.data::<List>(entity).is_some() {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Rename(String::new()),
))));
}
}
NavMenuAction::SetIcon(entity) => {
if self.nav_model.data::<List>(entity).is_some() {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Icon(String::new()),
))));
}
}
NavMenuAction::Delete(entity) => {
if self.nav_model.data::<List>(entity).is_some() {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Delete,
))));
}
}
},
}
}
ApplicationAction::Modifiers(modifiers) => {
self.modifiers = modifiers;
}
ApplicationAction::Focus(id) => tasks.push(widget::text_input::focus(id)),
ApplicationAction::NavMenuAction(nav_menu_action) => match nav_menu_action {
NavMenuAction::Rename(entity) => {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Rename(Some(entity), String::new()),
))));
}
NavMenuAction::SetIcon(entity) => {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Icon(Some(entity), String::new()),
))));
}
NavMenuAction::Delete(entity) => {
tasks.push(self.update(Message::Dialog(DialogAction::Open(
DialogPage::Delete(Some(entity)),
))));
}
},
},
}

app::Task::batch(tasks)
Expand Down Expand Up @@ -727,7 +746,7 @@ impl Application for Tasks {
])
.spacing(spacing.space_xxs),
),
DialogPage::Rename(name) => widget::dialog(fl!("rename-list"))
DialogPage::Rename(entity, name) => widget::dialog(fl!("rename-list"))
.primary_action(
widget::button::suggested(fl!("save"))
.on_press_maybe(Some(Message::Dialog(DialogAction::Complete))),
Expand All @@ -742,14 +761,17 @@ impl Application for Tasks {
widget::text_input("", name.as_str())
.id(self.dialog_text_input.clone())
.on_input(move |name| {
Message::Dialog(DialogAction::Update(DialogPage::Rename(name)))
Message::Dialog(DialogAction::Update(DialogPage::Rename(
entity.clone(),
name,
)))
})
.on_submit(Message::Dialog(DialogAction::Complete))
.into(),
])
.spacing(spacing.space_xxs),
),
DialogPage::Delete => widget::dialog(fl!("delete-list"))
DialogPage::Delete(_) => widget::dialog(fl!("delete-list"))
.body(fl!("delete-list-confirm"))
.primary_action(
widget::button::suggested(fl!("ok"))
Expand All @@ -759,7 +781,7 @@ impl Application for Tasks {
widget::button::standard(fl!("cancel"))
.on_press(Message::Dialog(DialogAction::Close)),
),
DialogPage::Icon(icon) => {
DialogPage::Icon(entity, icon) => {
let icon_buttons: Vec<Element<_>> = emojis::iter()
.map(|emoji| {
widget::button::custom(
Expand All @@ -770,6 +792,7 @@ impl Application for Tasks {
.align_x(Horizontal::Center),
)
.on_press(Message::Dialog(DialogAction::Update(DialogPage::Icon(
entity.clone(),
emoji.to_string(),
))))
.into()
Expand Down
14 changes: 11 additions & 3 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,18 @@ impl Content {
let mut tasks = Vec::new();
match message {
Message::List(list) => {
self.list.clone_from(&list);
if let Some(list) = list {
tasks.push(Task::GetTasks(list.id().clone()));
match (&self.list, &list) {
(Some(current), Some(list)) => {
if current.id != list.id {
tasks.push(Task::GetTasks(list.id().clone()));
}
}
(None, Some(list)) => {
tasks.push(Task::GetTasks(list.id().clone()));
}
_ => {}
}
self.list.clone_from(&list);
}
Message::TitleUpdate(id, title) => {
if let Some(task) = self.tasks.get_mut(id) {
Expand Down
Loading