diff --git a/app/src/main/kotlin/com/d4rk/cleaner/data/model/ui/screens/UiHomeModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/data/model/ui/screens/UiHomeModel.kt index 8d958bc..8978496 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/data/model/ui/screens/UiHomeModel.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/data/model/ui/screens/UiHomeModel.kt @@ -7,7 +7,6 @@ data class UiHomeModel( val storageUsed: String = "", val storageTotal: String = "", val showCleaningComposable: Boolean = false, - val isAnalyzing: Boolean = false, val scannedFiles: List = emptyList(), val allFilesSelected: Boolean = false, val fileSelectionStates: Map = emptyMap(), diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeScreen.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeScreen.kt index 2c12425..41ebb2e 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeScreen.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeScreen.kt @@ -160,7 +160,8 @@ fun AnalyzeComposable(imageLoader : ImageLoader) { val context = LocalContext.current val activity = LocalContext.current as Activity val coroutineScope : CoroutineScope = rememberCoroutineScope() - + val isLoading: Boolean by viewModel.isLoading.collectAsState() + val enabled = uiState.selectedFileCount > 0 val apkExtensions = remember { context.resources.getStringArray(R.array.apk_extensions) } val imageExtensions = remember { context.resources.getStringArray(R.array.image_extensions) } val videoExtensions = remember { context.resources.getStringArray(R.array.video_extensions) } @@ -218,7 +219,7 @@ fun AnalyzeComposable(imageLoader : ImageLoader) { .fillMaxWidth(), ) { when { - uiState.isAnalyzing && uiState.scannedFiles.isEmpty() -> { + isLoading && uiState.scannedFiles.isEmpty() -> { Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { CircularProgressIndicator() } @@ -243,7 +244,7 @@ fun AnalyzeComposable(imageLoader : ImageLoader) { OutlinedButton ( modifier = Modifier.bounceClick(), onClick = { - // TODO: Add close action + viewModel.rescanFiles() } ) { Icon(modifier = Modifier.size(ButtonDefaults.IconSize) , imageVector = Icons.Outlined.Refresh , contentDescription = "Close") @@ -402,6 +403,7 @@ fun AnalyzeComposable(imageLoader : ImageLoader) { horizontalArrangement = Arrangement.SpaceAround ) { OutlinedButton( + enabled = enabled, onClick = { // TODO: add trash }, @@ -414,12 +416,13 @@ fun AnalyzeComposable(imageLoader : ImageLoader) { modifier = Modifier.size(ButtonDefaults.IconSize) ) Spacer(Modifier.size(ButtonDefaults.IconSpacing)) - Text("Move to trash") + Text(text = "Move to trash") } Spacer(Modifier.width(8.dp)) Button( + enabled = enabled, onClick = { viewModel.clean(activity) }, diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeViewModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeViewModel.kt index be85c52..807144e 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeViewModel.kt @@ -34,14 +34,28 @@ class HomeViewModel(application : Application) : BaseViewModel(application) { fun analyze() { viewModelScope.launch(context = Dispatchers.Default + coroutineExceptionHandler) { - _uiState.value = _uiState.value.copy(isAnalyzing = true) + showLoading() repository.analyzeFiles { filteredFiles -> _uiState.value = _uiState.value.copy( scannedFiles = filteredFiles , - isAnalyzing = false , showCleaningComposable = true ) } + hideLoading() + } + } + + fun rescanFiles() { + viewModelScope.launch(context = Dispatchers.Default + coroutineExceptionHandler) { + showLoading() + _uiState.value = _uiState.value.copy(scannedFiles = emptyList()) + repository.rescanFiles { filteredFiles -> + _uiState.value = _uiState.value.copy( + scannedFiles = filteredFiles, + showCleaningComposable = true + ) + } + hideLoading() } } diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt index b04bda8..844d970 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt @@ -33,6 +33,17 @@ class HomeRepository( } } + suspend fun rescanFiles(onSuccess: (List) -> Unit) { + withContext(Dispatchers.IO) { + fileScanner.reset() + fileScanner.startScanning() + val filteredFiles = fileScanner.getFilteredFiles() + withContext(Dispatchers.Main) { + onSuccess(filteredFiles) + } + } + } + suspend fun getVideoThumbnail(filePath: String , context: Context , onSuccess: (File?) -> Unit) { withContext(Dispatchers.IO) { val thumbnailFile = getVideoThumbnail(filePath, context) diff --git a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileScanner.kt b/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileScanner.kt index c149a48..8894e09 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileScanner.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/utils/cleaning/FileScanner.kt @@ -137,4 +137,8 @@ class FileScanner(private val dataStore : DataStore , private val resources : Re fun getFilteredFiles() : List { return filteredFiles } + + fun reset() { + filteredFiles = emptyList() + } } \ No newline at end of file