From 2283d55551a036bd31d97c1f65c83fa54461e93a Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Mon, 31 Oct 2022 15:59:58 +0100 Subject: [PATCH] update due date in app --- cmd/android-app/component/tasks.go | 15 +++++ cmd/android-app/runner/runner.go | 9 +++ cmd/android-app/screen/config.go | 8 +-- cmd/android-app/screen/log.go | 2 +- cmd/android-app/screen/new.go | 18 +++--- cmd/android-app/screen/request.go | 5 ++ cmd/android-app/screen/screen.go | 26 +++++---- cmd/android-app/screen/tasks.go | 45 ++++++++++----- cmd/android-app/screen/update.go | 88 ++++++++++++++++++++++++++++++ 9 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 cmd/android-app/screen/update.go diff --git a/cmd/android-app/component/tasks.go b/cmd/android-app/component/tasks.go index 65484ea..11b321c 100644 --- a/cmd/android-app/component/tasks.go +++ b/cmd/android-app/component/tasks.go @@ -137,3 +137,18 @@ func (t *Tasks) Add(fields map[string]string) error { return nil } + +func (t *Tasks) Update(id, newDue string) error { + due := task.NewDateFromString(newDue) + localTask, err := t.local.FindById(id) + if err != nil { + return err + } + update := &task.LocalUpdate{ + ForVersion: localTask.Version, + Due: due, + Fields: []string{task.FIELD_DUE}, + } + + return process.NewUpdate(t.local, localTask.Id, update).Process() +} diff --git a/cmd/android-app/runner/runner.go b/cmd/android-app/runner/runner.go index e9f008e..a1863b1 100644 --- a/cmd/android-app/runner/runner.go +++ b/cmd/android-app/runner/runner.go @@ -107,6 +107,15 @@ func (r *Runner) processRequest() { } r.status = "saved" r.refresh <- true + case screen.UpdateTaskRequest: + r.status = "saving..." + r.refresh <- true + if err := r.tasks.Update(v.ID, v.Due); err != nil { + r.logger.Log(err.Error()) + } + r.logger.Log(fmt.Sprintf("updated due date task %q", v.ID)) + r.status = "saved" + r.refresh <- true default: r.logger.Log("request unknown") } diff --git a/cmd/android-app/screen/config.go b/cmd/android-app/screen/config.go index 5c3f297..116f63e 100644 --- a/cmd/android-app/screen/config.go +++ b/cmd/android-app/screen/config.go @@ -9,11 +9,11 @@ import ( type Config struct { fields []*FormField commands chan interface{} - show chan string + show chan ShowRequest root *fyne.Container } -func NewConfig(commands chan interface{}, show chan string) *Config { +func NewConfig(commands chan interface{}, show chan ShowRequest) *Config { fields := []*FormField{} for _, f := range [][2]string{ {"ConfigIMAPURL", "imap url"}, @@ -50,7 +50,7 @@ func (cf *Config) Save() { req.Fields[f.Key] = f.GetValue() } cf.commands <- req - cf.show <- "tasks" + cf.show <- ShowRequest{Screen: "tasks"} } func (cf *Config) Refresh(state State) { @@ -90,6 +90,6 @@ func (cf *Config) Hide() { cf.root.Hide() } -func (cf *Config) Show() { +func (cf *Config) Show(_ Task) { cf.root.Show() } diff --git a/cmd/android-app/screen/log.go b/cmd/android-app/screen/log.go index b446012..6508beb 100644 --- a/cmd/android-app/screen/log.go +++ b/cmd/android-app/screen/log.go @@ -52,6 +52,6 @@ func (l *Log) Hide() { l.root.Hide() } -func (l *Log) Show() { +func (l *Log) Show(_ Task) { l.root.Show() } diff --git a/cmd/android-app/screen/new.go b/cmd/android-app/screen/new.go index 85a6ead..80d7c85 100644 --- a/cmd/android-app/screen/new.go +++ b/cmd/android-app/screen/new.go @@ -1,23 +1,19 @@ package screen import ( - "sync" - "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" ) -var newLock sync.Mutex - type NewTask struct { fields []*FormField commands chan interface{} - show chan string + show chan ShowRequest root *fyne.Container } -func NewNewTask(commands chan interface{}, show chan string) *NewTask { +func NewNewTask(commands chan interface{}, show chan ShowRequest) *NewTask { fields := []*FormField{} for _, f := range [][2]string{ {"action", "action"}, @@ -48,6 +44,8 @@ func (nt *NewTask) Init() { taskForm.SubmitText = "save" taskForm.OnSubmit = nt.Save + taskForm.CancelText = "cancel" + taskForm.OnCancel = nt.Cancel taskForm.Enable() nt.clearForm() @@ -68,11 +66,15 @@ func (nt *NewTask) Save() { req.Fields[f.Key] = f.GetValue() } nt.commands <- req - nt.show <- "tasks" + nt.show <- ShowRequest{Screen: "tasks"} nt.clearForm() } +func (nt *NewTask) Cancel() { + nt.show <- ShowRequest{Screen: "tasks"} +} + func (nt *NewTask) clearForm() { for _, f := range nt.fields { f.SetValue("") @@ -89,6 +91,6 @@ func (nt *NewTask) Hide() { nt.root.Hide() } -func (nt *NewTask) Show() { +func (nt *NewTask) Show(_ Task) { nt.root.Show() } diff --git a/cmd/android-app/screen/request.go b/cmd/android-app/screen/request.go index cda4d38..3f1adcd 100644 --- a/cmd/android-app/screen/request.go +++ b/cmd/android-app/screen/request.go @@ -13,3 +13,8 @@ type SyncTasksRequest struct{} type MarkTaskDoneRequest struct { ID string } + +type UpdateTaskRequest struct { + ID string + Due string +} diff --git a/cmd/android-app/screen/screen.go b/cmd/android-app/screen/screen.go index f133287..a323761 100644 --- a/cmd/android-app/screen/screen.go +++ b/cmd/android-app/screen/screen.go @@ -14,6 +14,11 @@ type Task struct { Action string } +type ShowRequest struct { + Screen string + Task Task +} + type State struct { Status string CurrentScreen string @@ -26,12 +31,12 @@ type Screen interface { Content() *fyne.Container Refresh(state State) Hide() - Show() + Show(Task) } type ScreenSet struct { current string - show chan string + show chan ShowRequest status binding.String menu *fyne.Container screens map[string]Screen @@ -40,16 +45,16 @@ type ScreenSet struct { func NewScreenSet(requests chan interface{}) *ScreenSet { status := binding.NewString() - show := make(chan string) + show := make(chan ShowRequest) tasksButton := widget.NewButton("tasks", func() { - show <- "tasks" + show <- ShowRequest{Screen: "tasks"} }) configButton := widget.NewButton("config", func() { - show <- "config" + show <- ShowRequest{Screen: "config"} }) logsButton := widget.NewButton("logs", func() { - show <- "logs" + show <- ShowRequest{Screen: "logs"} }) statusLabel := widget.NewLabel("> init...") statusLabel.Bind(status) @@ -61,6 +66,7 @@ func NewScreenSet(requests chan interface{}) *ScreenSet { "logs": NewLog(), "config": NewConfig(requests, show), "new": NewNewTask(requests, show), + "update": NewUpdateTask(requests, show), } cs := []fyne.CanvasObject{} @@ -68,7 +74,7 @@ func NewScreenSet(requests chan interface{}) *ScreenSet { s.Hide() cs = append(cs, s.Content()) } - screens["tasks"].Show() + screens["tasks"].Show(Task{}) root := container.NewBorder(menu, nil, nil, nil, cs...) @@ -83,10 +89,10 @@ func NewScreenSet(requests chan interface{}) *ScreenSet { func (ss *ScreenSet) Run() { for s := range ss.show { - if s != ss.current { + if s.Screen != ss.current { ss.screens[ss.current].Hide() - ss.screens[s].Show() - ss.current = s + ss.screens[s.Screen].Show(s.Task) + ss.current = s.Screen ss.root.Refresh() } diff --git a/cmd/android-app/screen/tasks.go b/cmd/android-app/screen/tasks.go index 7cb000d..66cbb81 100644 --- a/cmd/android-app/screen/tasks.go +++ b/cmd/android-app/screen/tasks.go @@ -12,19 +12,20 @@ import ( type Tasks struct { tasks []Task taskLabels binding.StringList - selectedTask string + selectedTask Task list *widget.List commands chan interface{} - show chan string + show chan ShowRequest root *fyne.Container } -func NewTasks(commands chan interface{}, show chan string) *Tasks { +func NewTasks(commands chan interface{}, show chan ShowRequest) *Tasks { tasks := &Tasks{ - tasks: []Task{}, - taskLabels: binding.NewStringList(), - commands: commands, - show: show, + tasks: []Task{}, + taskLabels: binding.NewStringList(), + commands: commands, + show: show, + selectedTask: Task{}, } tasks.Init() @@ -41,14 +42,17 @@ func (t *Tasks) Refresh(state State) { tls = append(tls, t.Action) } t.taskLabels.Set(tls) - if t.selectedTask == "" { + if t.selectedTask.ID == "" { t.list.UnselectAll() } } func (t *Tasks) Init() { newButton := widget.NewButton("new", func() { - t.show <- "new" + t.show <- ShowRequest{Screen: "new"} + }) + updateButton := widget.NewButton("update", func() { + t.updateTask() }) doneButton := widget.NewButton("done", func() { t.markDone() @@ -66,7 +70,7 @@ func (t *Tasks) Init() { t.root = container.NewBorder( newButton, - doneButton, + container.NewVBox(updateButton, doneButton), nil, nil, t.list, @@ -81,7 +85,7 @@ func (t *Tasks) Hide() { t.root.Hide() } -func (t *Tasks) Show() { +func (t *Tasks) Show(_ Task) { t.root.Show() } @@ -91,15 +95,26 @@ func (t *Tasks) selectItem(lid widget.ListItemID) { return } - t.selectedTask = t.tasks[id].ID + t.selectedTask = t.tasks[id] } func (t *Tasks) markDone() { - if t.selectedTask == "" { + if t.selectedTask.ID == "" { return } t.commands <- MarkTaskDoneRequest{ - ID: t.selectedTask, + ID: t.selectedTask.ID, + } + t.selectedTask = Task{} +} + +func (t *Tasks) updateTask() { + if t.selectedTask.ID == "" { + return + } + + t.show <- ShowRequest{ + Screen: "update", + Task: t.selectedTask, } - t.selectedTask = "" } diff --git a/cmd/android-app/screen/update.go b/cmd/android-app/screen/update.go new file mode 100644 index 0000000..6065f27 --- /dev/null +++ b/cmd/android-app/screen/update.go @@ -0,0 +1,88 @@ +package screen + +import ( + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/data/binding" + "fyne.io/fyne/v2/widget" +) + +type UpdateTask struct { + field *FormField + action binding.String + taskID string + commands chan interface{} + show chan ShowRequest + root *fyne.Container +} + +func NewUpdateTask(commands chan interface{}, show chan ShowRequest) *UpdateTask { + newUpdate := &UpdateTask{ + field: NewFormField("new due", "due"), + action: binding.NewString(), + commands: commands, + show: show, + } + newUpdate.Init() + + return newUpdate +} + +func (ut *UpdateTask) Init() { + actionLabel := widget.NewLabel("") + actionLabel.Bind(ut.action) + updateForm := widget.NewForm() + dueEntry := widget.NewEntry() + dueEntry.Bind(ut.field.Value) + updateForm.Append(ut.field.Label, dueEntry) + + updateForm.SubmitText = "save" + updateForm.OnSubmit = ut.Save + updateForm.CancelText = "cancel" + updateForm.OnCancel = ut.Cancel + updateForm.Enable() + ut.clearForm() + + ut.root = container.NewBorder( + actionLabel, + nil, + nil, + nil, + updateForm, + ) +} + +func (ut *UpdateTask) Save() { + ut.commands <- UpdateTaskRequest{ + ID: ut.taskID, + Due: ut.field.GetValue(), + } + ut.show <- ShowRequest{Screen: "tasks"} +} + +func (ut *UpdateTask) Cancel() { + ut.clearForm() + ut.show <- ShowRequest{Screen: "tasks"} +} + +func (ut *UpdateTask) clearForm() { + ut.field.SetValue("") + ut.action.Set("") + ut.taskID = "" +} + +func (ut *UpdateTask) Refresh(_ State) {} + +func (ut *UpdateTask) Content() *fyne.Container { + return ut.root +} + +func (ut *UpdateTask) Hide() { + ut.root.Hide() +} + +func (ut *UpdateTask) Show(task Task) { + ut.taskID = task.ID + ut.action.Set(task.Action) + ut.root.Show() +}