github.com/wtfutil/wtf@v0.43.0/modules/todo_plus/widget.go (about) 1 package todo_plus 2 3 import ( 4 "log" 5 6 "github.com/rivo/tview" 7 "github.com/wtfutil/wtf/modules/todo_plus/backend" 8 "github.com/wtfutil/wtf/view" 9 ) 10 11 // A Widget represents a Todoist widget 12 type Widget struct { 13 view.MultiSourceWidget 14 view.ScrollableWidget 15 16 projects []*backend.Project 17 settings *Settings 18 backend backend.Backend 19 } 20 21 // NewWidget creates a new instance of a widget 22 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 23 widget := Widget{ 24 MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "project", "projects"), 25 ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common), 26 27 settings: settings, 28 } 29 30 widget.backend = getBackend(settings.backendType) 31 widget.backend.Setup(settings.backendSettings) 32 widget.CommonSettings().Title = widget.backend.Title() 33 34 widget.SetRenderFunction(widget.display) 35 widget.initializeKeyboardControls() 36 widget.SetDisplayFunction(widget.display) 37 38 return &widget 39 } 40 41 func getBackend(backendType string) backend.Backend { 42 switch backendType { 43 case "trello": 44 backend := &backend.Trello{} 45 return backend 46 case "todoist": 47 backend := &backend.Todoist{} 48 return backend 49 default: 50 log.Fatal(backendType + " is not a supported backend") 51 return nil 52 } 53 54 } 55 56 /* -------------------- Exported Functions -------------------- */ 57 58 func (widget *Widget) CurrentProject() *backend.Project { 59 return widget.ProjectAt(widget.Idx) 60 } 61 62 func (widget *Widget) ProjectAt(idx int) *backend.Project { 63 if len(widget.projects) == 0 { 64 return nil 65 } 66 67 return widget.projects[idx] 68 } 69 70 func (widget *Widget) Refresh() { 71 if widget.Disabled() { 72 return 73 } 74 75 widget.projects = widget.backend.BuildProjects() 76 widget.Sources = widget.backend.Sources() 77 widget.SetItemCount(len(widget.CurrentProject().Tasks)) 78 widget.display() 79 } 80 81 func (widget *Widget) NextSource() { 82 widget.MultiSourceWidget.NextSource() 83 widget.Selected = widget.CurrentProject().Index 84 widget.SetItemCount(len(widget.CurrentProject().Tasks)) 85 widget.RenderFunction() 86 } 87 88 func (widget *Widget) PrevSource() { 89 widget.MultiSourceWidget.PrevSource() 90 widget.Selected = widget.CurrentProject().Index 91 widget.SetItemCount(len(widget.CurrentProject().Tasks)) 92 widget.RenderFunction() 93 } 94 95 func (widget *Widget) Prev() { 96 widget.ScrollableWidget.Prev() 97 widget.CurrentProject().Index = widget.Selected 98 } 99 100 func (widget *Widget) Next() { 101 widget.ScrollableWidget.Next() 102 widget.CurrentProject().Index = widget.Selected 103 } 104 105 func (widget *Widget) Unselect() { 106 widget.ScrollableWidget.Unselect() 107 widget.CurrentProject().Index = -1 108 widget.RenderFunction() 109 } 110 111 /* -------------------- Keyboard Movement -------------------- */ 112 113 // Close closes the currently-selected task in the currently-selected project 114 func (w *Widget) Close() { 115 w.CurrentProject().CloseSelectedTask() 116 w.SetItemCount(len(w.CurrentProject().Tasks)) 117 118 if w.CurrentProject().IsLast() { 119 w.Prev() 120 return 121 } 122 w.CurrentProject().Index = w.Selected 123 w.RenderFunction() 124 } 125 126 // Delete deletes the currently-selected task in the currently-selected project 127 func (w *Widget) Delete() { 128 w.CurrentProject().DeleteSelectedTask() 129 w.SetItemCount(len(w.CurrentProject().Tasks)) 130 131 if w.CurrentProject().IsLast() { 132 w.Prev() 133 } 134 w.CurrentProject().Index = w.Selected 135 w.RenderFunction() 136 }