2022-10-18 16:58:12 +02:00
|
|
|
package screen
|
|
|
|
|
|
|
|
import (
|
2022-10-20 13:29:13 +02:00
|
|
|
"sort"
|
|
|
|
|
2022-10-18 16:58:12 +02:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/data/binding"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tasks struct {
|
2022-10-20 10:40:46 +02:00
|
|
|
tasks []Task
|
|
|
|
taskLabels binding.StringList
|
2022-10-31 15:59:58 +01:00
|
|
|
selectedTask Task
|
2022-10-23 12:45:21 +02:00
|
|
|
list *widget.List
|
2022-10-24 15:57:55 +02:00
|
|
|
commands chan interface{}
|
2022-10-31 15:59:58 +01:00
|
|
|
show chan ShowRequest
|
2022-10-24 15:57:55 +02:00
|
|
|
root *fyne.Container
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 15:59:58 +01:00
|
|
|
func NewTasks(commands chan interface{}, show chan ShowRequest) *Tasks {
|
2022-10-24 15:57:55 +02:00
|
|
|
tasks := &Tasks{
|
2022-10-31 15:59:58 +01:00
|
|
|
tasks: []Task{},
|
|
|
|
taskLabels: binding.NewStringList(),
|
|
|
|
commands: commands,
|
|
|
|
show: show,
|
|
|
|
selectedTask: Task{},
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
2022-10-24 15:57:55 +02:00
|
|
|
tasks.Init()
|
|
|
|
|
|
|
|
return tasks
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tasks) Refresh(state State) {
|
2022-10-20 10:40:46 +02:00
|
|
|
t.tasks = state.Tasks
|
2022-10-20 13:58:48 +02:00
|
|
|
sort.Slice(t.tasks, func(i, j int) bool {
|
|
|
|
return t.tasks[i].Action < t.tasks[j].Action
|
|
|
|
})
|
2022-10-20 10:40:46 +02:00
|
|
|
tls := []string{}
|
|
|
|
for _, t := range t.tasks {
|
|
|
|
tls = append(tls, t.Action)
|
|
|
|
}
|
|
|
|
t.taskLabels.Set(tls)
|
2022-10-31 15:59:58 +01:00
|
|
|
if t.selectedTask.ID == "" {
|
2022-10-23 12:45:21 +02:00
|
|
|
t.list.UnselectAll()
|
|
|
|
}
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
func (t *Tasks) Init() {
|
2022-10-31 09:18:36 +01:00
|
|
|
newButton := widget.NewButton("new", func() {
|
2022-10-31 15:59:58 +01:00
|
|
|
t.show <- ShowRequest{Screen: "new"}
|
|
|
|
})
|
|
|
|
updateButton := widget.NewButton("update", func() {
|
|
|
|
t.updateTask()
|
2022-10-31 09:18:36 +01:00
|
|
|
})
|
2022-10-20 10:40:46 +02:00
|
|
|
doneButton := widget.NewButton("done", func() {
|
|
|
|
t.markDone()
|
|
|
|
})
|
2022-10-23 12:45:21 +02:00
|
|
|
t.list = widget.NewListWithData(
|
2022-10-20 10:40:46 +02:00
|
|
|
t.taskLabels,
|
2022-10-18 16:58:12 +02:00
|
|
|
func() fyne.CanvasObject {
|
|
|
|
return widget.NewLabel("template")
|
|
|
|
},
|
|
|
|
func(i binding.DataItem, o fyne.CanvasObject) {
|
|
|
|
o.(*widget.Label).Bind(i.(binding.String))
|
|
|
|
},
|
|
|
|
)
|
2022-10-23 12:45:21 +02:00
|
|
|
t.list.OnSelected = t.selectItem
|
2022-10-18 16:58:12 +02:00
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
t.root = container.NewBorder(
|
2022-10-31 09:18:36 +01:00
|
|
|
newButton,
|
2022-10-31 15:59:58 +01:00
|
|
|
container.NewVBox(updateButton, doneButton),
|
2022-10-18 16:58:12 +02:00
|
|
|
nil,
|
|
|
|
nil,
|
2022-10-23 12:45:21 +02:00
|
|
|
t.list,
|
2022-10-18 16:58:12 +02:00
|
|
|
)
|
|
|
|
}
|
2022-10-20 10:40:46 +02:00
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
func (t *Tasks) Content() *fyne.Container {
|
|
|
|
return t.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tasks) Hide() {
|
|
|
|
t.root.Hide()
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:59:58 +01:00
|
|
|
func (t *Tasks) Show(_ Task) {
|
2022-10-24 15:57:55 +02:00
|
|
|
t.root.Show()
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:40:46 +02:00
|
|
|
func (t *Tasks) selectItem(lid widget.ListItemID) {
|
|
|
|
id := int(lid)
|
|
|
|
if id < 0 || id >= len(t.tasks) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:59:58 +01:00
|
|
|
t.selectedTask = t.tasks[id]
|
2022-10-20 10:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tasks) markDone() {
|
2022-10-31 15:59:58 +01:00
|
|
|
if t.selectedTask.ID == "" {
|
2022-10-20 10:40:46 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-24 15:57:55 +02:00
|
|
|
t.commands <- MarkTaskDoneRequest{
|
2022-10-31 15:59:58 +01:00
|
|
|
ID: t.selectedTask.ID,
|
|
|
|
}
|
|
|
|
t.selectedTask = Task{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tasks) updateTask() {
|
|
|
|
if t.selectedTask.ID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.show <- ShowRequest{
|
|
|
|
Screen: "update",
|
|
|
|
Task: t.selectedTask,
|
2022-10-20 10:40:46 +02:00
|
|
|
}
|
|
|
|
}
|