Skip to content

Commit

Permalink
Added reset if no files found and state for buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Sep 28, 2024
1 parent 9640fa2 commit bf74333
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ data class UiHomeModel(
val storageUsed: String = "",
val storageTotal: String = "",
val showCleaningComposable: Boolean = false,
val isAnalyzing: Boolean = false,
val scannedFiles: List<File> = emptyList(),
val allFilesSelected: Boolean = false,
val fileSelectionStates: Map<File, Boolean> = emptyMap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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()
}
Expand All @@ -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")
Expand Down Expand Up @@ -402,6 +403,7 @@ fun AnalyzeComposable(imageLoader : ImageLoader) {
horizontalArrangement = Arrangement.SpaceAround
) {
OutlinedButton(
enabled = enabled,
onClick = {
// TODO: add trash
},
Expand All @@ -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)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ class HomeRepository(
}
}

suspend fun rescanFiles(onSuccess: (List<File>) -> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,8 @@ class FileScanner(private val dataStore : DataStore , private val resources : Re
fun getFilteredFiles() : List<File> {
return filteredFiles
}

fun reset() {
filteredFiles = emptyList()
}
}

0 comments on commit bf74333

Please sign in to comment.