diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2023-04-16 09:53:40 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-04-22 22:40:12 +0200 |
commit | 8ced001d82b59f353c33ad34ca0c79313eef41af (patch) | |
tree | 1044ba34ffd9f8b66a04cac16c83de56bd363e34 | |
parent | 2fbb7ce4cbf67538fb7e3416ba0820a22607d452 (diff) | |
download | aerc-8ced001d82b59f353c33ad34ca0c79313eef41af.tar.gz |
listDirectories: refactor listdirectories handling
ListDirectories is called when connecting, reconnecting, and
creation/deletion of a directory. The code is not in the same style as
other areas of aerc. Refactor to match coding style of the rest of aerc
by creating an Update function which handles necessary updates in the
dirlist. This style does not use a callback, making it clearer what is
happening in the message flow, and operates similar to how the msgstore
receives updates.
Use a map in the dirstore to reduce duplicate storage of directory
names. Directly add or remove directories from the map when created /
deleted to prevent a new ListDirectories message, and a flash of the UI.
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r-- | lib/dirstore.go | 16 | ||||
-rw-r--r-- | widgets/account.go | 48 | ||||
-rw-r--r-- | widgets/dirlist.go | 41 | ||||
-rw-r--r-- | widgets/dirtree.go | 23 |
4 files changed, 69 insertions, 59 deletions
diff --git a/lib/dirstore.go b/lib/dirstore.go index bb58a9dc..cc06d579 100644 --- a/lib/dirstore.go +++ b/lib/dirstore.go @@ -1,7 +1,6 @@ package lib type DirStore struct { - dirs []string msgStores map[string]*MessageStore } @@ -10,13 +9,12 @@ func NewDirStore() *DirStore { return &DirStore{msgStores: msgStores} } -func (store *DirStore) Update(dirs []string) { - store.dirs = make([]string, len(dirs)) - copy(store.dirs, dirs) -} - func (store *DirStore) List() []string { - return store.dirs + dirs := []string{} + for dir := range store.msgStores { + dirs = append(dirs, dir) + } + return dirs } func (store *DirStore) MessageStore(dirname string) (*MessageStore, bool) { @@ -27,3 +25,7 @@ func (store *DirStore) MessageStore(dirname string) (*MessageStore, bool) { func (store *DirStore) SetMessageStore(name string, msgStore *MessageStore) { store.msgStores[name] = msgStore } + +func (store *DirStore) Remove(name string) { + delete(store.msgStores, name) +} diff --git a/widgets/account.go b/widgets/account.go index 3ba511de..ad9d200a 100644 --- a/widgets/account.go +++ b/widgets/account.go @@ -231,26 +231,10 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) { switch msg.InResponseTo().(type) { case *types.Connect, *types.Reconnect: acct.SetStatus(state.ConnectionActivity("Listing mailboxes...")) + log.Infof("[%s] connected.", acct.acct.Name) + acct.SetStatus(state.SetConnected(true)) log.Tracef("Listing mailboxes...") - acct.dirlist.UpdateList(func(dirs []string) { - var dir string - for _, _dir := range dirs { - if _dir == acct.acct.Default { - dir = _dir - break - } - } - if dir == "" && len(dirs) > 0 { - dir = dirs[0] - } - if dir != "" { - acct.dirlist.Select(dir) - } - acct.msglist.SetInitDone() - log.Infof("[%s] connected.", acct.acct.Name) - acct.SetStatus(state.SetConnected(true)) - acct.newConn = true - }) + acct.worker.PostAction(&types.ListDirectories{}, nil) case *types.Disconnect: acct.dirlist.ClearList() acct.msglist.SetStore(nil) @@ -268,13 +252,35 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) { acct.msglist.SetStore(nil) } case *types.CreateDirectory: - acct.dirlist.UpdateList(nil) + acct.dirlist.Update(msg) case *types.RemoveDirectory: - acct.dirlist.UpdateList(nil) + acct.dirlist.Update(msg) case *types.FetchMessageHeaders: if acct.newConn { acct.checkMailOnStartup() } + case *types.ListDirectories: + acct.dirlist.Update(msg) + if acct.dirlist.Selected() != "" { + return + } + // Nothing selected, select based on config + dirs := acct.dirlist.List() + var dir string + for _, _dir := range dirs { + if _dir == acct.acct.Default { + dir = _dir + break + } + } + if dir == "" && len(dirs) > 0 { + dir = dirs[0] + } + if dir != "" { + acct.dirlist.Select(dir) + } + acct.msglist.SetInitDone() + acct.newConn = true } case *types.Directory: name := msg.Dir.Name diff --git a/widgets/dirlist.go b/widgets/dirlist.go index 986bd662..a548b7b7 100644 --- a/widgets/dirlist.go +++ b/widgets/dirlist.go @@ -28,7 +28,7 @@ type DirectoryLister interface { Selected() string Select(string) - UpdateList(func([]string)) + Update(types.WorkerMessage) List() []string ClearList() @@ -91,33 +91,30 @@ func (dirlist *DirectoryList) UiConfig(dir string) *config.UIConfig { } func (dirlist *DirectoryList) List() []string { - return dirlist.store.List() + return dirlist.dirs } func (dirlist *DirectoryList) ClearList() { dirlist.dirs = []string{} } -func (dirlist *DirectoryList) UpdateList(done func(dirs []string)) { - // TODO: move this logic into dirstore - var dirs []string - dirlist.worker.PostAction( - &types.ListDirectories{}, func(msg types.WorkerMessage) { - switch msg := msg.(type) { - case *types.Directory: - dirs = append(dirs, msg.Dir.Name) - case *types.Done: - dirlist.store.Update(dirs) - dirlist.filterDirsByFoldersConfig() - dirlist.sortDirsByFoldersSortConfig() - dirlist.store.Update(dirlist.dirs) - dirlist.spinner.Stop() - dirlist.Invalidate() - if done != nil { - done(dirlist.dirs) - } - } - }) +func (dirlist *DirectoryList) Update(msg types.WorkerMessage) { + switch msg := msg.(type) { + case *types.Done: + switch msg := msg.InResponseTo().(type) { + case *types.ListDirectories: + dirlist.filterDirsByFoldersConfig() + dirlist.sortDirsByFoldersSortConfig() + dirlist.spinner.Stop() + dirlist.Invalidate() + case *types.RemoveDirectory: + dirlist.store.Remove(msg.Directory) + dirlist.filterDirsByFoldersConfig() + dirlist.sortDirsByFoldersSortConfig() + } + default: + return + } } func (dirlist *DirectoryList) CollapseFolder() { diff --git a/widgets/dirtree.go b/widgets/dirtree.go index 0c7f090a..88e930ec 100644 --- a/widgets/dirtree.go +++ b/widgets/dirtree.go @@ -37,18 +37,23 @@ func NewDirectoryTree(dirlist *DirectoryList, pathSeparator string) DirectoryLis func (dt *DirectoryTree) ClearList() { dt.list = make([]*types.Thread, 0) + dt.selected = "" } -func (dt *DirectoryTree) UpdateList(done func([]string)) { - dt.DirectoryList.UpdateList(func(dirs []string) { - if done != nil { - done(dirs) +func (dt *DirectoryTree) Update(msg types.WorkerMessage) { + switch msg := msg.(type) { + + case *types.Done: + switch msg.InResponseTo().(type) { + case *types.RemoveDirectory, *types.ListDirectories, *types.CreateDirectory: + dt.DirectoryList.Update(msg) + dt.buildTree() + default: + dt.DirectoryList.Update(msg) } - dt.buildTree() - dt.listIdx = findString(dt.dirs, dt.selecting) - dt.Select(dt.selecting) - dt.Scrollable = Scrollable{} - }) + default: + dt.DirectoryList.Update(msg) + } } func (dt *DirectoryTree) Draw(ctx *ui.Context) { |