Skip to content

Commit

Permalink
Added confirmation dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Oct 14, 2024
1 parent e530080 commit 752f0b6
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@ package com.d4rk.cleaner.data.model.ui.screens
import java.io.File

data class UiHomeModel(
val storageUsageProgress : Float = 0f ,
val usedStorageFormatted : String = "" ,
val totalStorageFormatted : String = "" ,
var storageUsageProgress : Float = 0f ,
var usedStorageFormatted : String = "" ,
var totalStorageFormatted : String = "" ,
var analyzeState : UiAnalyzeModel = UiAnalyzeModel() ,
val isRescanDialogVisible : Boolean = false ,
var isRescanDialogVisible : Boolean = false ,
)

data class UiAnalyzeModel(
val isAnalyzeScreenVisible : Boolean = false ,
val scannedFileList : List<File> = emptyList() ,
val emptyFolderList : List<File> = emptyList() ,
val areAllFilesSelected : Boolean = false ,
val fileSelectionMap : Map<File , Boolean> = emptyMap() ,
val selectedFilesCount : Int = 0 ,
val isFileScanEmpty : Boolean = false ,
var isAnalyzeScreenVisible : Boolean = false ,
var scannedFileList : List<File> = emptyList() ,
var emptyFolderList : List<File> = emptyList() ,
var areAllFilesSelected : Boolean = false ,
var fileSelectionMap : Map<File , Boolean> = emptyMap() ,
var selectedFilesCount : Int = 0 ,
var isFileScanEmpty : Boolean = false ,
var fileTypesData : FileTypesData = FileTypesData() ,
var isDeleteForeverConfirmationDialogVisible: Boolean = false,
var isMoveToTrashConfirmationDialogVisible: Boolean = false,
)

data class FileTypesData(
val apkExtensions : List<String> = emptyList() ,
val imageExtensions : List<String> = emptyList() ,
val videoExtensions : List<String> = emptyList() ,
val audioExtensions : List<String> = emptyList() ,
val archiveExtensions : List<String> = emptyList() ,
val fileTypesTitles : List<String> = emptyList() ,
var apkExtensions : List<String> = emptyList() ,
var imageExtensions : List<String> = emptyList() ,
var videoExtensions : List<String> = emptyList() ,
var audioExtensions : List<String> = emptyList() ,
var archiveExtensions : List<String> = emptyList() ,
var fileTypesTitles : List<String> = emptyList() ,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class AppManagerViewModel(application: Application) : BaseViewModel(application) {
Expand Down Expand Up @@ -63,14 +64,16 @@ class AppManagerViewModel(application: Application) : BaseViewModel(application)
repository.getApkFilesFromStorage { files ->
apkFiles = files
}
apkFiles
return@async apkFiles
}
val apkFiles: List<ApkInfo> = apkFilesDeferred.await()
_uiState.value = UiAppManagerModel(installedApps, apkFiles)
_uiState.update { it.copy(
installedApps = installedApps,
apkFiles = apkFiles
) }
}
}
}

fun installApk(apkPath: String) {
viewModelScope.launch(coroutineExceptionHandler) {
repository.installApk(apkPath, onSuccess = {})
Expand Down
122 changes: 79 additions & 43 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/AnalyzeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import com.d4rk.cleaner.ui.components.NonLazyGrid
import com.d4rk.cleaner.ui.components.TwoRowButtons
import com.d4rk.cleaner.ui.components.animations.bounceClick
import com.d4rk.cleaner.ui.components.animations.hapticPagerSwipe
import com.d4rk.cleaner.ui.components.dialogs.ConfirmationAlertDialog
import com.d4rk.cleaner.utils.TimeHelper
import com.d4rk.cleaner.utils.cleaning.getFileIcon
import com.google.common.io.Files.getFileExtension
Expand All @@ -92,7 +93,7 @@ fun AnalyzeScreen(
imageLoader : ImageLoader ,
view : View ,
viewModel : HomeViewModel ,
data : UiHomeModel
data : UiHomeModel ,
) {
val coroutineScope : CoroutineScope = rememberCoroutineScope()
val enabled = data.analyzeState.selectedFilesCount > 0
Expand All @@ -107,25 +108,30 @@ fun AnalyzeScreen(
val emptyFoldersString = stringResource(R.string.empty_folders)

val groupedFiles = remember(
data.analyzeState,
data.analyzeState ,
) {
val filesMap = data.analyzeState.scannedFileList.groupBy { file ->
when (file.extension.lowercase()) {
in imageExtensions -> {
return@groupBy filesTypesTitles[0]
}

in audioExtensions -> {
return@groupBy filesTypesTitles[1]
}

in videoExtensions -> {
return@groupBy filesTypesTitles[2]
}

in apkExtensions -> {
return@groupBy filesTypesTitles[3]
}

in archiveExtensions -> {
return@groupBy filesTypesTitles[4]
}

else -> {
return@groupBy filesTypesTitles[6]
}
Expand Down Expand Up @@ -299,22 +305,58 @@ fun AnalyzeScreen(
SelectAllComposable(viewModel = viewModel , view = view)
}

TwoRowButtons(
modifier = Modifier ,
enabled = enabled ,
onStartButtonClick = {
viewModel.moveToTrash()
} ,
onStartButtonIcon = Icons.Outlined.Delete ,
onStartButtonText = R.string.move_to_trash ,
TwoRowButtons(modifier = Modifier ,
enabled = enabled ,
onStartButtonClick = {
viewModel.setMoveToTrashConfirmationDialogVisibility(true)
} ,
onStartButtonIcon = Icons.Outlined.Delete ,
onStartButtonText = R.string.move_to_trash ,

onEndButtonClick = {
viewModel.setDeleteForeverConfirmationDialogVisibility(
true
)
} ,
onEndButtonIcon = Icons.Outlined.DeleteForever ,
onEndButtonText = R.string.delete_forever ,
view = view)

if (data.analyzeState.isMoveToTrashConfirmationDialogVisible) {
ConfirmationAlertDialog(confirmationTitle = stringResource(R.string.delete_forever_title) ,
confirmationMessage = stringResource(R.string.delete_forever_message) ,
confirmationConfirmButtonText = stringResource(android.R.string.ok) ,
confirmationDismissButtonText = stringResource(android.R.string.cancel) ,
onConfirm = {
viewModel.clean()
viewModel.setDeleteForeverConfirmationDialogVisibility(
false
)
} ,
onDismiss = {
viewModel.setDeleteForeverConfirmationDialogVisibility(
false
)
})
}

onEndButtonClick = {
viewModel.clean()
} ,
onEndButtonIcon = Icons.Outlined.DeleteForever ,
onEndButtonText = R.string.delete_forever ,
view = view ,
)
if (data.analyzeState.isMoveToTrashConfirmationDialogVisible) {
ConfirmationAlertDialog(confirmationTitle = stringResource(R.string.move_to_trash_title) ,
confirmationMessage = stringResource(R.string.move_to_trash_message) ,
confirmationConfirmButtonText = stringResource(android.R.string.ok) ,
confirmationDismissButtonText = stringResource(android.R.string.cancel) ,
onConfirm = {
viewModel.moveToTrash()
viewModel.setMoveToTrashConfirmationDialogVisibility(
false
)
} ,
onDismiss = {
viewModel.setMoveToTrashConfirmationDialogVisibility(
false
)
})
}
}
}
}
Expand Down Expand Up @@ -403,13 +445,11 @@ fun FilesGrid(
columns = 3 , itemCount = files.size , modifier = Modifier.padding(horizontal = 8.dp)
) { index ->
val file = files[index]
FileCard(
file = file ,
imageLoader = imageLoader ,
isChecked = fileSelectionStates[file] == true ,
onCheckedChange = { isChecked -> onFileSelectionChange(file , isChecked) } ,
view = view
)
FileCard(file = file ,
imageLoader = imageLoader ,
isChecked = fileSelectionStates[file] == true ,
onCheckedChange = { isChecked -> onFileSelectionChange(file , isChecked) } ,
view = view)
}
}
}
Expand Down Expand Up @@ -472,19 +512,17 @@ fun FileCard(
}

in videoExtensions -> {
AsyncImage(
model = remember(file) {
ImageRequest.Builder(context).data(file)
.decoderFactory { result , options , _ ->
VideoFrameDecoder(result.source , options)
}.videoFramePercent(framePercent = 0.5)
.crossfade(enable = true).build()
} ,
imageLoader = imageLoader ,
contentDescription = file.name ,
contentScale = ContentScale.Crop ,
modifier = Modifier.fillMaxSize()
)
AsyncImage(model = remember(file) {
ImageRequest.Builder(context).data(file)
.decoderFactory { result , options , _ ->
VideoFrameDecoder(result.source , options)
}.videoFramePercent(framePercent = 0.5).crossfade(enable = true)
.build()
} ,
imageLoader = imageLoader ,
contentDescription = file.name ,
contentScale = ContentScale.Crop ,
modifier = Modifier.fillMaxSize())
}

else -> {
Expand All @@ -504,12 +542,10 @@ fun FileCard(
}
}

Checkbox(
checked = isChecked , onCheckedChange = { checked ->
view.playSoundEffect(SoundEffectConstants.CLICK)
onCheckedChange(checked)
} , modifier = Modifier.align(Alignment.TopEnd)
)
Checkbox(checked = isChecked , onCheckedChange = { checked ->
view.playSoundEffect(SoundEffectConstants.CLICK)
onCheckedChange(checked)
} , modifier = Modifier.align(Alignment.TopEnd))

Text(
text = file.name ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ fun HomeScreen() {
val context : Context = LocalContext.current
val view : View = LocalView.current
val viewModel : HomeViewModel = viewModel()


val uiState : UiHomeModel by viewModel.uiState.collectAsState()
val uiErrorModel : UiErrorModel by viewModel.uiErrorModel.collectAsState()
val imageLoader : ImageLoader = remember {
Expand All @@ -50,9 +48,6 @@ fun HomeScreen() {
}

LaunchedEffect(Unit) {
// viewModel.populateFileTypesData()

// Permissions
if (! PermissionsUtils.hasStoragePermissions(context)) {
PermissionsUtils.requestStoragePermissions(context as Activity)
}
Expand Down
Loading

0 comments on commit 752f0b6

Please sign in to comment.