Skip to content

Commit

Permalink
Added rescan dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 8, 2024
1 parent e119ab6 commit e4d62eb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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))
}
}
)
}
17 changes: 14 additions & 3 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeComposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
) {
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = _selectedFileCount.asStateFlow()

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit e4d62eb

Please sign in to comment.