From fd60ea4811efbe1b2af210fb77872367f519e64c Mon Sep 17 00:00:00 2001 From: Simon Pham Date: Sat, 10 Aug 2019 20:44:13 +0700 Subject: [PATCH] Refactor select all & deselect all logic --- .../mcp/ui/filepicker/FileBrowserFragment.kt | 15 ++------------- .../mcp/ui/filepicker/FileBrowserViewModel.kt | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserFragment.kt b/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserFragment.kt index bed9ae8..be45f5d 100644 --- a/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserFragment.kt +++ b/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserFragment.kt @@ -143,14 +143,7 @@ class FileBrowserFragment : BaseFragment() { fun selectAllFilesInCurrentFolder() { if (getCallbacks()?.allowChangeSelectedFile() != false) { - viewModel.getFileModels().value.orEmpty().forEach { model -> - val isFull = viewModel.getSelectedFiles().size == limitSelectCount - if (model.type == TYPE_FILE - && !isFull - && !model.selected) { - viewModel.selectFile(model.path) - } - } + viewModel.selectAllFilesInCurrentFolder(limitSelectCount) refreshOnSelectedFilesChanged() } else { showFileSelectionLockedMessage() @@ -159,11 +152,7 @@ class FileBrowserFragment : BaseFragment() { fun deselectAllFilesInCurrentFolder() { if (getCallbacks()?.allowChangeSelectedFile() != false) { - viewModel.getFileModels().value.orEmpty().forEach { model -> - if (model.type == TYPE_FILE && model.selected) { - viewModel.unselectedFile(model.path) - } - } + viewModel.deselectAllFilesInCurrentFolder() refreshOnSelectedFilesChanged() } else { showFileSelectionLockedMessage() diff --git a/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserViewModel.kt b/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserViewModel.kt index b98d8e2..8920267 100644 --- a/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserViewModel.kt +++ b/app/src/main/java/com/github/khangnt/mcp/ui/filepicker/FileBrowserViewModel.kt @@ -123,6 +123,25 @@ class FileBrowserViewModel : ViewModel() { fun getFileModels(): LiveData> = fileModelsLiveData + fun selectAllFilesInCurrentFolder(limitSelectCount: Int = Int.MAX_VALUE) { + fileModelsLiveData.value.orEmpty().forEach { model -> + val isFull = selectedFilesReadOnly.size == limitSelectCount + if (model.type == TYPE_FILE + && !isFull + && !model.selected) { + selectFile(model.path) + } + } + } + + fun deselectAllFilesInCurrentFolder() { + fileModelsLiveData.value.orEmpty().forEach { model -> + if (model.type == TYPE_FILE && model.selected) { + unselectedFile(model.path) + } + } + } + @MainThread fun selectFile(file: File) { selectedFiles.add(file)