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 /widgets/dirlist.go | |
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>
Diffstat (limited to 'widgets/dirlist.go')
-rw-r--r-- | widgets/dirlist.go | 41 |
1 files changed, 19 insertions, 22 deletions
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() { |