github.com/wtfutil/wtf@v0.43.0/modules/pivotal/widget.go (about) 1 package pivotal 2 3 import ( 4 "github.com/rivo/tview" 5 "github.com/wtfutil/wtf/view" 6 ) 7 8 // A Widget represents a Todoist widget 9 type Widget struct { 10 view.MultiSourceWidget 11 view.ScrollableWidget 12 settings *Settings 13 14 client *PivotalClient 15 projectClient map[string]*PivotalClient 16 sources []*PivotalSource 17 } 18 19 // NewWidget creates a new instance of a widget 20 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 21 22 widget := Widget{ 23 MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "customQuery", "customQueries"), 24 ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common), 25 26 settings: settings, 27 client: NewPivotalClient(settings.apiToken, settings.projectId), 28 projectClient: make(map[string]*PivotalClient), 29 } 30 31 widget.loadSources() 32 33 // Add the client to projectClient list 34 widget.projectClient[widget.settings.projectId] = widget.client 35 36 //Build the Souce lists 37 widget.sources = widget.buildPivotalSources() 38 39 widget.SetRenderFunction(widget.display) 40 widget.initializeKeyboardControls() 41 widget.SetDisplayFunction(widget.display) 42 43 return &widget 44 } 45 46 func (widget *Widget) loadSources() { 47 var queries []string 48 for _, query := range widget.settings.customQueries { 49 queries = append(queries, query.title) 50 } 51 widget.Sources = queries 52 } 53 54 func (widget *Widget) buildPivotalSources() []*PivotalSource { 55 var sources []*PivotalSource 56 57 for _, query := range widget.settings.customQueries { 58 client := widget.client 59 // Make sure that we have a viable Pivotal Client 60 if query.project != "" && query.project != widget.client.projectId { 61 nclient, ok := widget.projectClient[query.project] 62 if !ok { 63 nclient = NewPivotalClient(widget.settings.apiToken, query.project) 64 } 65 client = nclient 66 } 67 68 sources = append(sources, 69 NewPivotalSource(query.title, query.filter, client, widget)) 70 } 71 72 return sources 73 } 74 75 /* -------------------- Exported Functions -------------------- */ 76 77 func (widget *Widget) CurrentSource() *PivotalSource { 78 if len(widget.sources) == 0 { 79 return nil 80 } 81 82 return widget.sources[widget.Idx] 83 84 } 85 86 func (widget *Widget) Refresh() { 87 if widget.Disabled() { 88 return 89 } 90 widget.SetItemCount(widget.CurrentSource().getItemCount()) 91 widget.display() 92 } 93 94 /* -------------------- Exported Functions -------------------- */ 95 96 func (widget *Widget) Open() { 97 widget.CurrentSource().Open() 98 } 99 func (widget *Widget) OpenPulls() { 100 widget.CurrentSource().OpenPulls() 101 }