Centralize app state ownership in controller
This commit is contained in:
@@ -75,6 +75,35 @@ type State struct {
|
||||
SearchQuery string
|
||||
SelectedEntryID string
|
||||
Dirty bool
|
||||
StatusMessage string
|
||||
ErrorMessage string
|
||||
}
|
||||
|
||||
func (s *State) ShowSection(section Section) {
|
||||
s.Section = section
|
||||
s.CurrentPath = nil
|
||||
s.SelectedEntryID = ""
|
||||
}
|
||||
|
||||
func (s *State) SetSearchQuery(query string) {
|
||||
s.SearchQuery = query
|
||||
}
|
||||
|
||||
func (s *State) BeginNewEntry() {
|
||||
s.SelectedEntryID = ""
|
||||
s.StatusMessage = "new entry form ready"
|
||||
s.ErrorMessage = ""
|
||||
}
|
||||
|
||||
func (s *State) SetActionResult(label string, err error) {
|
||||
if err != nil {
|
||||
s.ErrorMessage = err.Error()
|
||||
s.StatusMessage = ""
|
||||
return
|
||||
}
|
||||
|
||||
s.ErrorMessage = ""
|
||||
s.StatusMessage = label + " complete"
|
||||
}
|
||||
|
||||
func (s *State) VisibleEntries() ([]vault.Entry, error) {
|
||||
@@ -467,6 +496,28 @@ func (s *State) NavigateToPath(path []string) {
|
||||
s.SelectedEntryID = ""
|
||||
}
|
||||
|
||||
func (s *State) DeleteCurrentGroup() error {
|
||||
session, ok := s.Session.(MutableSession)
|
||||
if !ok {
|
||||
return fmt.Errorf("session is not mutable")
|
||||
}
|
||||
|
||||
model, err := session.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := model.DeleteGroup(s.CurrentPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
session.Replace(model)
|
||||
if len(s.CurrentPath) > 0 {
|
||||
s.CurrentPath = append([]string(nil), s.CurrentPath[:len(s.CurrentPath)-1]...)
|
||||
}
|
||||
s.Dirty = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *State) Save() error {
|
||||
session, ok := s.Session.(SaveableSession)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user