diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-30 13:08:09 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | 8ea7b7a62e5a09314bbe7a446a45e649d66b524d (patch) | |
tree | 4f2e2fe927b5f8380249e51eef30820383d145e2 /pkg/gui/filetree | |
parent | 09dc160da98a0a4fe976d3d174cc93af57c53892 (diff) |
migrate files context to new structure
Diffstat (limited to 'pkg/gui/filetree')
-rw-r--r-- | pkg/gui/filetree/file_node_test.go | 4 | ||||
-rw-r--r-- | pkg/gui/filetree/file_tree_view_model.go | 23 | ||||
-rw-r--r-- | pkg/gui/filetree/file_tree_view_model_test.go | 2 |
3 files changed, 10 insertions, 19 deletions
diff --git a/pkg/gui/filetree/file_node_test.go b/pkg/gui/filetree/file_node_test.go index 8961015ac..02ae84f1d 100644 --- a/pkg/gui/filetree/file_node_test.go +++ b/pkg/gui/filetree/file_node_test.go @@ -142,13 +142,13 @@ func TestGetFile(t *testing.T) { }{ { name: "valid case", - viewModel: NewFileTreeViewModel([]*models.File{{Name: "blah/one"}, {Name: "blah/two"}}, nil, false), + viewModel: NewFileTreeViewModel(func() []*models.File { return []*models.File{{Name: "blah/one"}, {Name: "blah/two"}} }, nil, false), path: "blah/two", expected: &models.File{Name: "blah/two"}, }, { name: "not found", - viewModel: NewFileTreeViewModel([]*models.File{{Name: "blah/one"}, {Name: "blah/two"}}, nil, false), + viewModel: NewFileTreeViewModel(func() []*models.File { return []*models.File{{Name: "blah/one"}, {Name: "blah/two"}} }, nil, false), path: "blah/three", expected: nil, }, diff --git a/pkg/gui/filetree/file_tree_view_model.go b/pkg/gui/filetree/file_tree_view_model.go index 01eb751e3..e3d01ef7d 100644 --- a/pkg/gui/filetree/file_tree_view_model.go +++ b/pkg/gui/filetree/file_tree_view_model.go @@ -19,7 +19,7 @@ const ( ) type FileTreeViewModel struct { - files []*models.File + getFiles func() []*models.File tree *FileNode showTree bool log *logrus.Entry @@ -28,8 +28,9 @@ type FileTreeViewModel struct { sync.RWMutex } -func NewFileTreeViewModel(files []*models.File, log *logrus.Entry, showTree bool) *FileTreeViewModel { +func NewFileTreeViewModel(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTreeViewModel { viewModel := &FileTreeViewModel{ + getFiles: getFiles, log: log, showTree: showTree, filter: DisplayAll, @@ -37,8 +38,6 @@ func NewFileTreeViewModel(files []*models.File, log *logrus.Entry, showTree bool RWMutex: sync.RWMutex{}, } - viewModel.SetFiles(files) - return viewModel } @@ -51,11 +50,9 @@ func (self *FileTreeViewModel) ExpandToPath(path string) { } func (self *FileTreeViewModel) GetFilesForDisplay() []*models.File { - files := self.files - switch self.filter { case DisplayAll: - return files + return self.getFiles() case DisplayStaged: return self.FilterFiles(func(file *models.File) bool { return file.HasStagedChanges }) case DisplayUnstaged: @@ -69,7 +66,7 @@ func (self *FileTreeViewModel) GetFilesForDisplay() []*models.File { func (self *FileTreeViewModel) FilterFiles(test func(*models.File) bool) []*models.File { result := make([]*models.File, 0) - for _, file := range self.files { + for _, file := range self.getFiles() { if test(file) { result = append(result, file) } @@ -93,7 +90,7 @@ func (self *FileTreeViewModel) GetItemAtIndex(index int) *FileNode { } func (self *FileTreeViewModel) GetFile(path string) *models.File { - for _, file := range self.files { + for _, file := range self.getFiles() { if file.Name == path { return file } @@ -120,13 +117,7 @@ func (self *FileTreeViewModel) GetItemsLength() int { } func (self *FileTreeViewModel) GetAllFiles() []*models.File { - return self.files -} - -func (self *FileTreeViewModel) SetFiles(files []*models.File) { - self.files = files - - self.SetTree() + return self.getFiles() } func (self *FileTreeViewModel) SetTree() { diff --git a/pkg/gui/filetree/file_tree_view_model_test.go b/pkg/gui/filetree/file_tree_view_model_test.go index 89b8e74df..a168573ea 100644 --- a/pkg/gui/filetree/file_tree_view_model_test.go +++ b/pkg/gui/filetree/file_tree_view_model_test.go @@ -73,7 +73,7 @@ func TestFilterAction(t *testing.T) { for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) { - mngr := &FileTreeViewModel{files: s.files, filter: s.filter} + mngr := &FileTreeViewModel{getFiles: s.files, filter: s.filter} result := mngr.GetFilesForDisplay() assert.EqualValues(t, s.expected, result) }) |