From e4d62eba94c9ec4c4d74e8c4d241a8c083bb7e22 Mon Sep 17 00:00:00 2001 From: D4rK7355608 Date: Mon, 8 Jul 2024 11:31:09 +0300 Subject: [PATCH] Added rescan dialog --- .../ui/dialogs/RescanDialogComposable.kt | 30 +++++++++++++++++++ .../d4rk/cleaner/ui/home/HomeComposable.kt | 17 +++++++++-- .../com/d4rk/cleaner/ui/home/HomeViewModel.kt | 18 ++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/d4rk/cleaner/ui/dialogs/RescanDialogComposable.kt diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/dialogs/RescanDialogComposable.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/dialogs/RescanDialogComposable.kt new file mode 100644 index 0000000..4c7440b --- /dev/null +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/dialogs/RescanDialogComposable.kt @@ -0,0 +1,30 @@ +package com.d4rk.cleaner.ui.dialogs + +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource + + +@Composable +fun RescanAlertDialog( + onYes: () -> Unit, + onDismiss: () -> Unit +) { + AlertDialog( + onDismissRequest = onDismiss, + title = { Text("Rescan?") }, + text = { Text("Are you sure you want to scan again?") }, + confirmButton = { + TextButton(onClick = onYes) { + Text(stringResource(android.R.string.ok)) + } + }, + dismissButton = { + TextButton(onClick = onDismiss) { + Text(stringResource(android.R.string.cancel)) + } + } + ) +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeComposable.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeComposable.kt index 66c8014..9ffef6b 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeComposable.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeComposable.kt @@ -68,6 +68,7 @@ import coil.disk.DiskCache import coil.memory.MemoryCache import coil.request.ImageRequest import com.d4rk.cleaner.R +import com.d4rk.cleaner.ui.dialogs.RescanAlertDialog import com.d4rk.cleaner.utils.CircularDeterminateIndicator import com.d4rk.cleaner.utils.bounceClick import com.d4rk.cleaner.utils.getFileIcon @@ -102,6 +103,18 @@ fun HomeComposable() { val launchScanningKey = remember { mutableStateOf(false) } + if (viewModel.showRescanDialog.value) { + RescanAlertDialog( + onYes = { + viewModel.rescan( + context as Activity + ) + viewModel.showRescanDialog.value = false + }, + onDismiss = { viewModel.showRescanDialog.value = false } + ) + } + Column( modifier = Modifier.fillMaxSize() ) { @@ -195,9 +208,7 @@ fun HomeComposable() { .animateContentSize() .padding(start = if (showCleaningComposable) 8.dp else 16.dp, end = 16.dp) .bounceClick(), onClick = { - if (!showCleaningComposable) { - viewModel.analyze(activity = context as Activity) - } + viewModel.analyze(activity = context as Activity) }, shape = MaterialTheme.shapes.medium ) { Column( diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeViewModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeViewModel.kt index 1ea63c1..4a9ff2c 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeViewModel.kt @@ -43,6 +43,9 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { private val dataStoreInstance: DataStore = DataStore(application) val showCleaningComposable = MutableLiveData(false) val isAnalyzing = MutableLiveData(false) + var showRescanDialog = mutableStateOf(false) + private var hasScanned = mutableStateOf(false) + private var isUserConfirmedRescan = mutableStateOf(false) val _selectedFileCount = MutableStateFlow(0) val selectedFileCount: StateFlow = _selectedFileCount.asStateFlow() @@ -124,17 +127,30 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { requestPermissions(activity) return } + + if (hasScanned.value && !isUserConfirmedRescan.value) { + showRescanDialog.value = true + } + + isUserConfirmedRescan.value = false isAnalyzing.value = true showCleaningComposable.value = true viewModelScope.launch { fileScanner.startScanning() withContext(Dispatchers.Main) { scannedFiles.value = fileScanner.getFilteredFiles() - isAnalyzing.value =false + isAnalyzing.value = false + hasScanned.value = true } } } + fun rescan(activity: Activity) { + showRescanDialog.value = false + scannedFiles.value = emptyList() + analyze(activity) + } + /** * Initiates the cleaning process if the required permissions are granted. *