Skip to content

Commit 73ae6a7

Browse files
committed
refactor: status window entity list
1 parent 3bbefe2 commit 73ae6a7

File tree

1 file changed

+182
-120
lines changed

1 file changed

+182
-120
lines changed

internal/app/statuswindow/statuswindow.go

Lines changed: 182 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type statusWindow struct {
8181
widget.BaseWidget
8282

8383
charactersTop *widget.Label
84-
details *updateStatusDetail
84+
details *sectionDetails
8585
detailsTop *widget.Label
8686
entities fyne.CanvasObject
8787
entityList *widget.List
@@ -106,7 +106,7 @@ type statusWindow struct {
106106
func newStatusWindow(s ui, w fyne.Window) *statusWindow {
107107
a := &statusWindow{
108108
charactersTop: awidget.NewLabelWithWrapping(""),
109-
details: newUpdateStatusDetail(),
109+
details: newSectionDetails(),
110110
detailsTop: awidget.NewLabelWithWrapping(""),
111111
sb: xwidget.NewSnackbar(w),
112112
sectionsTop: awidget.NewLabelWithWrapping(""),
@@ -230,80 +230,23 @@ func (a *statusWindow) CreateRenderer() fyne.WidgetRenderer {
230230
}
231231

232232
func (a *statusWindow) makeEntityList() *widget.List {
233+
isOfflineMode := a.u.IsOfflineMode()
233234
list := widget.NewList(
234235
func() int {
235236
return len(a.sectionEntities)
236237
},
237238
func() fyne.CanvasObject {
238-
icon := xwidget.NewImageFromResource(icons.BlankSvg, fyne.NewSquareSize(app.IconUnitSize))
239-
name := widget.NewLabel("Template")
240-
status := widget.NewLabel("Template")
241-
spinner := widget.NewActivity()
242-
spinner.Stop()
243-
row := container.NewHBox(icon, name, spinner, layout.NewSpacer(), status)
244-
return row
239+
return newEntityItem(
240+
isOfflineMode,
241+
a.u.EVEImage().CharacterPortraitAsync,
242+
a.u.EVEImage().CorporationLogoAsync,
243+
)
245244
},
246245
func(id widget.ListItemID, co fyne.CanvasObject) {
247246
if id >= len(a.sectionEntities) {
248247
return
249248
}
250-
c := a.sectionEntities[id]
251-
row := co.(*fyne.Container).Objects
252-
name := row[1].(*widget.Label)
253-
icon := row[0].(*canvas.Image)
254-
spinner := row[2].(*widget.Activity)
255-
status := row[4].(*widget.Label)
256-
257-
name.Text = c.name
258-
switch c.category {
259-
case sectionGeneral:
260-
name.TextStyle.Bold = false
261-
name.Refresh()
262-
icon.Resource = eveicon.FromName(eveicon.StarMap)
263-
icon.Refresh()
264-
case sectionCharacter, sectionCorporation:
265-
name.TextStyle.Bold = false
266-
name.Refresh()
267-
switch c.category {
268-
case sectionCharacter:
269-
a.u.EVEImage().CharacterPortraitAsync(c.id, app.IconPixelSize, func(r fyne.Resource) {
270-
icon.Resource = r
271-
icon.Refresh()
272-
})
273-
case sectionCorporation:
274-
a.u.EVEImage().CorporationLogoAsync(c.id, app.IconPixelSize, func(r fyne.Resource) {
275-
icon.Resource = r
276-
icon.Refresh()
277-
})
278-
default:
279-
icon.Resource = icons.BlankSvg
280-
icon.Refresh()
281-
}
282-
case sectionHeader:
283-
name.TextStyle.Bold = true
284-
name.Refresh()
285-
icon.Hide()
286-
spinner.Hide()
287-
status.Hide()
288-
return
289-
}
290-
291-
if c.ss.IsRunning && !a.u.IsOfflineMode() {
292-
spinner.Start()
293-
spinner.Show()
294-
} else {
295-
spinner.Stop()
296-
spinner.Hide()
297-
}
298-
299-
t := c.ss.Display()
300-
i := c.ss.Status().ToImportance2()
301-
status.Text = t
302-
status.Importance = i
303-
status.Refresh()
304-
status.Show()
305-
306-
icon.Show()
249+
co.(*entityItem).set(a.sectionEntities[id])
307250
})
308251

309252
list.OnSelected = func(id widget.ListItemID) {
@@ -406,42 +349,19 @@ func (a *statusWindow) updateEntityList(_ context.Context) ([]sectionEntity, int
406349
}
407350

408351
func (a *statusWindow) makeSectionList() *widget.List {
352+
isOfflineMode := a.u.IsOfflineMode()
409353
l := widget.NewList(
410354
func() int {
411355
return len(a.sections)
412356
},
413357
func() fyne.CanvasObject {
414-
spinner := widget.NewActivity()
415-
spinner.Stop()
416-
return container.NewHBox(
417-
widget.NewLabel("Section name long"),
418-
spinner,
419-
layout.NewSpacer(),
420-
widget.NewLabel("Status XXXX"),
421-
)
358+
return newSectionItem(isOfflineMode)
422359
},
423360
func(id widget.GridWrapItemID, co fyne.CanvasObject) {
424361
if id >= len(a.sections) {
425362
return
426363
}
427-
cs := a.sections[id]
428-
hbox := co.(*fyne.Container).Objects
429-
name := hbox[0].(*widget.Label)
430-
status := hbox[3].(*widget.Label)
431-
name.SetText(cs.SectionName)
432-
s, i := cs.Display()
433-
status.Text = s
434-
status.Importance = i
435-
status.Refresh()
436-
437-
spinner := hbox[1].(*widget.Activity)
438-
if cs.IsRunning() && !a.u.IsOfflineMode() {
439-
spinner.Start()
440-
spinner.Show()
441-
} else {
442-
spinner.Stop()
443-
spinner.Hide()
444-
}
364+
co.(*sectionItem).set(a.sections[id])
445365
},
446366
)
447367
l.OnSelected = func(id widget.ListItemID) {
@@ -521,7 +441,154 @@ func (a *statusWindow) makeUpdateSectionAction(entityID int64, sectionID string)
521441
}
522442
}
523443

524-
type updateStatusDetail struct {
444+
type loadFuncAsync func(int64, int, func(fyne.Resource))
445+
446+
type entityItem struct {
447+
widget.BaseWidget
448+
449+
icon *canvas.Image
450+
isOfflineMode bool
451+
loadCharacterIcon loadFuncAsync
452+
loadCorporationIcon loadFuncAsync
453+
name *widget.Label
454+
spinner *widget.Activity
455+
status *widget.Label
456+
thief *xwidget.HooverThief
457+
}
458+
459+
func newEntityItem(isOfflineMode bool, loadCharacterIcon, loadCorporationIcon loadFuncAsync) *entityItem {
460+
icon := xwidget.NewImageFromResource(icons.BlankSvg, fyne.NewSquareSize(app.IconUnitSize))
461+
name := widget.NewLabel("Template")
462+
status := widget.NewLabel("Template")
463+
spinner := widget.NewActivity()
464+
w := &entityItem{
465+
icon: icon,
466+
isOfflineMode: isOfflineMode,
467+
loadCharacterIcon: loadCharacterIcon,
468+
loadCorporationIcon: loadCorporationIcon,
469+
name: name,
470+
spinner: spinner,
471+
status: status,
472+
thief: xwidget.NewHooverThief(),
473+
}
474+
w.ExtendBaseWidget(w)
475+
return w
476+
}
477+
478+
func (w *entityItem) CreateRenderer() fyne.WidgetRenderer {
479+
c := container.NewStack(
480+
container.NewHBox(w.icon, w.name, w.spinner, layout.NewSpacer(), w.status),
481+
w.thief,
482+
)
483+
return widget.NewSimpleRenderer(c)
484+
}
485+
486+
func (w *entityItem) set(r sectionEntity) {
487+
w.name.Text = r.name
488+
489+
switch r.category {
490+
case sectionGeneral:
491+
w.name.TextStyle.Bold = false
492+
w.name.Refresh()
493+
w.icon.Resource = eveicon.FromName(eveicon.StarMap)
494+
w.icon.Refresh()
495+
case sectionCharacter, sectionCorporation:
496+
w.name.TextStyle.Bold = false
497+
w.name.Refresh()
498+
switch r.category {
499+
case sectionCharacter:
500+
w.loadCharacterIcon(r.id, app.IconPixelSize, func(r fyne.Resource) {
501+
w.icon.Resource = r
502+
w.icon.Refresh()
503+
})
504+
case sectionCorporation:
505+
w.loadCorporationIcon(r.id, app.IconPixelSize, func(r fyne.Resource) {
506+
w.icon.Resource = r
507+
w.icon.Refresh()
508+
})
509+
default:
510+
w.icon.Resource = icons.BlankSvg
511+
w.icon.Refresh()
512+
}
513+
case sectionHeader:
514+
w.name.TextStyle.Bold = true
515+
w.name.Refresh()
516+
w.icon.Hide()
517+
w.spinner.Hide()
518+
w.status.Hide()
519+
w.thief.Show()
520+
return
521+
}
522+
523+
w.thief.Hide()
524+
if r.ss.IsRunning && !w.isOfflineMode {
525+
w.spinner.Start()
526+
w.spinner.Show()
527+
} else {
528+
w.spinner.Stop()
529+
w.spinner.Hide()
530+
}
531+
532+
w.icon.Show()
533+
534+
t := r.ss.Display()
535+
i := r.ss.Status().ToImportance2()
536+
w.status.Text = t
537+
w.status.Importance = i
538+
w.status.Refresh()
539+
w.status.Show()
540+
}
541+
542+
type sectionItem struct {
543+
widget.BaseWidget
544+
545+
isOfflineMode bool
546+
name *widget.Label
547+
spinner *widget.Activity
548+
status *widget.Label
549+
}
550+
551+
func newSectionItem(isOfflineMode bool) *sectionItem {
552+
name := widget.NewLabel("")
553+
status := widget.NewLabel("")
554+
spinner := widget.NewActivity()
555+
w := &sectionItem{
556+
name: name,
557+
spinner: spinner,
558+
status: status,
559+
isOfflineMode: isOfflineMode,
560+
}
561+
w.ExtendBaseWidget(w)
562+
return w
563+
}
564+
565+
func (w *sectionItem) CreateRenderer() fyne.WidgetRenderer {
566+
c := container.NewHBox(
567+
w.name,
568+
w.spinner,
569+
layout.NewSpacer(),
570+
w.status,
571+
)
572+
return widget.NewSimpleRenderer(c)
573+
}
574+
575+
func (w *sectionItem) set(r app.CacheSectionStatus) {
576+
w.name.SetText(r.SectionName)
577+
s, i := r.Display()
578+
w.status.Text = s
579+
w.status.Importance = i
580+
w.status.Refresh()
581+
582+
if r.IsRunning() && !w.isOfflineMode {
583+
w.spinner.Start()
584+
w.spinner.Show()
585+
} else {
586+
w.spinner.Stop()
587+
w.spinner.Hide()
588+
}
589+
}
590+
591+
type sectionDetails struct {
525592
widget.BaseWidget
526593

527594
completedAt *widget.Label
@@ -532,25 +599,33 @@ type updateStatusDetail struct {
532599
timeout *widget.Label
533600
}
534601

535-
func newUpdateStatusDetail() *updateStatusDetail {
536-
makeLabel := func() *widget.Label {
537-
l := widget.NewLabel("")
538-
l.Wrapping = fyne.TextWrapWord
539-
return l
540-
}
541-
w := &updateStatusDetail{
542-
completedAt: makeLabel(),
543-
issue: makeLabel(),
544-
nextUpdate: makeLabel(),
545-
startedAt: makeLabel(),
546-
status: makeLabel(),
547-
timeout: makeLabel(),
602+
func newSectionDetails() *sectionDetails {
603+
w := &sectionDetails{
604+
completedAt: awidget.NewLabelWithWrapping(""),
605+
issue: awidget.NewLabelWithWrapping(""),
606+
nextUpdate: awidget.NewLabelWithWrapping(""),
607+
startedAt: awidget.NewLabelWithWrapping(""),
608+
status: awidget.NewLabelWithWrapping(""),
609+
timeout: awidget.NewLabelWithWrapping(""),
548610
}
549611
w.ExtendBaseWidget(w)
550612
return w
551613
}
552614

553-
func (w *updateStatusDetail) set(ss app.CacheSectionStatus) {
615+
func (w *sectionDetails) CreateRenderer() fyne.WidgetRenderer {
616+
layout := kxlayout.NewColumns(100)
617+
c := container.NewVScroll(container.NewVBox(
618+
container.New(layout, widget.NewLabel("Status"), w.status),
619+
container.New(layout, widget.NewLabel("Started"), w.startedAt),
620+
container.New(layout, widget.NewLabel("Completed"), w.completedAt),
621+
container.New(layout, widget.NewLabel("Timeout"), w.timeout),
622+
container.New(layout, widget.NewLabel("Next update"), w.nextUpdate),
623+
container.New(layout, widget.NewLabel("Issue"), w.issue),
624+
))
625+
return widget.NewSimpleRenderer(c)
626+
}
627+
628+
func (w *sectionDetails) set(ss app.CacheSectionStatus) {
554629
w.status.Text, w.status.Importance = ss.Display()
555630
w.status.Refresh()
556631

@@ -573,16 +648,3 @@ func (w *updateStatusDetail) set(ss app.CacheSectionStatus) {
573648
w.timeout.SetText(humanize.RelTime(now.Add(ss.Timeout), now, "", ""))
574649
w.nextUpdate.SetText(humanize.RelTime(now, ss.CompletedAt.Add(ss.Timeout), "", ""))
575650
}
576-
577-
func (w *updateStatusDetail) CreateRenderer() fyne.WidgetRenderer {
578-
layout := kxlayout.NewColumns(100)
579-
c := container.NewVScroll(container.NewVBox(
580-
container.New(layout, widget.NewLabel("Status"), w.status),
581-
container.New(layout, widget.NewLabel("Started"), w.startedAt),
582-
container.New(layout, widget.NewLabel("Completed"), w.completedAt),
583-
container.New(layout, widget.NewLabel("Timeout"), w.timeout),
584-
container.New(layout, widget.NewLabel("Next update"), w.nextUpdate),
585-
container.New(layout, widget.NewLabel("Issue"), w.issue),
586-
))
587-
return widget.NewSimpleRenderer(c)
588-
}

0 commit comments

Comments
 (0)