Centralize app state ownership in controller

This commit is contained in:
Joe Julian
2026-03-29 11:22:17 -07:00
parent 01559a3a2b
commit b56401b5c6
5 changed files with 186 additions and 40 deletions
+18 -35
View File
@@ -150,8 +150,6 @@ type ui struct {
copyIcon *widget.Icon
clipboardWriter clipboard.Writer
loadingMessage string
statusMessage string
errorMessage string
keyboardFocus focusID
}
@@ -240,8 +238,7 @@ func newUIWithState(mode string, sess appstate.CurrentSession) *ui {
}
func (u *ui) filter() {
u.state.SearchQuery = u.search.Text()
u.syncCurrentPath()
u.state.SetSearchQuery(u.search.Text())
visible, err := u.state.VisibleEntries()
if err != nil {
u.visible = nil
@@ -289,26 +286,22 @@ func (u *ui) selectedAttachmentNames() []string {
}
func (u *ui) showEntriesSection() {
u.state.Section = appstate.SectionEntries
u.setCurrentPath(nil)
u.state.ShowSection(appstate.SectionEntries)
u.filter()
}
func (u *ui) showTemplatesSection() {
u.state.Section = appstate.SectionTemplates
u.setCurrentPath(nil)
u.state.ShowSection(appstate.SectionTemplates)
u.filter()
}
func (u *ui) showRecycleBinSection() {
u.state.Section = appstate.SectionRecycleBin
u.setCurrentPath(nil)
u.state.ShowSection(appstate.SectionRecycleBin)
u.filter()
}
func (u *ui) childGroups() []string {
u.state.SearchQuery = u.search.Text()
u.syncCurrentPath()
u.state.SetSearchQuery(u.search.Text())
groups, err := u.state.ChildGroups()
if err != nil {
return nil
@@ -328,14 +321,6 @@ func (u *ui) filteredTitles() []string {
return titles
}
func (u *ui) visiblePathContexts() []string {
paths := make([]string, 0, len(u.visible))
for _, item := range u.visible {
paths = append(paths, u.state.SearchPathContext(item))
}
return paths
}
func (u *ui) selectedEntry() (entry, bool) {
for _, item := range u.visible {
if item.ID == u.state.SelectedEntryID {
@@ -517,13 +502,13 @@ func (u *ui) runAction(label string, action func() error) {
u.loadingMessage = actionLoadingLabel(label)
if err := action(); err != nil {
u.loadingMessage = ""
u.errorMessage = u.describeActionError(label, err)
u.statusMessage = ""
u.state.ErrorMessage = u.describeActionError(label, err)
u.state.StatusMessage = ""
return
}
u.loadingMessage = ""
u.errorMessage = ""
u.statusMessage = label + " complete"
u.state.ErrorMessage = ""
u.state.StatusMessage = label + " complete"
}
func actionLoadingLabel(label string) string {
@@ -553,10 +538,10 @@ func (u *ui) bannerSurface() uiBanner {
switch {
case strings.TrimSpace(u.loadingMessage) != "":
return uiBanner{Kind: bannerLoading, Message: strings.TrimSpace(u.loadingMessage)}
case strings.TrimSpace(u.errorMessage) != "":
return uiBanner{Kind: bannerError, Message: strings.TrimSpace(u.errorMessage)}
case strings.TrimSpace(u.statusMessage) != "":
return uiBanner{Kind: bannerStatus, Message: strings.TrimSpace(u.statusMessage)}
case strings.TrimSpace(u.state.ErrorMessage) != "":
return uiBanner{Kind: bannerError, Message: strings.TrimSpace(u.state.ErrorMessage)}
case strings.TrimSpace(u.state.StatusMessage) != "":
return uiBanner{Kind: bannerStatus, Message: strings.TrimSpace(u.state.StatusMessage)}
default:
return uiBanner{}
}
@@ -690,11 +675,9 @@ func (u *ui) layout(gtx layout.Context) layout.Dimensions {
u.runAction("lock vault", u.lockAction)
}
for u.addEntry.Clicked(gtx) {
u.state.SelectedEntryID = ""
u.state.BeginNewEntry()
u.loadSelectedEntryIntoEditor()
u.entryPath.SetText(strings.Join(u.state.CurrentPath, " / "))
u.statusMessage = "new entry form ready"
u.errorMessage = ""
}
for u.saveEntry.Clicked(gtx) {
u.runAction("save entry", u.saveEntryAction)
@@ -977,7 +960,7 @@ func (u *ui) entryRow(gtx layout.Context, click *widget.Clickable, idx int, item
if strings.TrimSpace(u.search.Text()) == "" {
return layout.Dimensions{}
}
lbl := material.Label(u.theme, unit.Sp(11), u.state.SearchPathContext(item))
lbl := material.Label(u.theme, unit.Sp(11), strings.Join(item.Path, " / "))
lbl.Color = mutedColor
return lbl.Layout(gtx)
}),
@@ -1403,11 +1386,11 @@ func detailLine(th *material.Theme, label, value string) layout.Widget {
}
func (u *ui) feedbackBanner(gtx layout.Context) layout.Dimensions {
message := u.statusMessage
message := u.state.StatusMessage
tone := color.NRGBA{R: 231, G: 239, B: 235, A: 255}
textColor := accentColor
if u.errorMessage != "" {
message = u.errorMessage
if u.state.ErrorMessage != "" {
message = u.state.ErrorMessage
tone = color.NRGBA{R: 248, G: 226, B: 223, A: 255}
textColor = color.NRGBA{R: 140, G: 46, B: 34, A: 255}
}