From d3abe56a304ef21e84ff3fc76f84a1e034f4aa40 Mon Sep 17 00:00:00 2001 From: Mihai-Cristian Condrea Date: Fri, 27 Sep 2024 22:25:28 +0300 Subject: [PATCH] Improved the date format and made the selection of files by date functional. --- .../com/d4rk/cleaner/ui/home/HomeScreen.kt | 39 ++++++++++++------- .../com/d4rk/cleaner/ui/home/HomeViewModel.kt | 2 +- .../ui/home/repository/HomeRepository.kt | 2 +- .../com/d4rk/cleaner/utils/TimeHelper.kt | 39 +++++++++++++++++++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 app/src/main/kotlin/com/d4rk/cleaner/utils/TimeHelper.kt diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeScreen.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeScreen.kt index 12083ae..96a3920 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeScreen.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeScreen.kt @@ -66,6 +66,7 @@ import com.d4rk.cleaner.data.model.ui.screens.UiHomeModel import com.d4rk.cleaner.ui.dialogs.ErrorAlertDialog import com.d4rk.cleaner.ui.dialogs.RescanAlertDialog import com.d4rk.cleaner.utils.PermissionsUtils +import com.d4rk.cleaner.utils.TimeHelper import com.d4rk.cleaner.utils.cleaning.getFileIcon import com.d4rk.cleaner.utils.compose.NonLazyGrid import com.d4rk.cleaner.utils.compose.bounceClick @@ -165,12 +166,24 @@ fun AnalyzeComposable(imageLoader: ImageLoader) { ) { uiState.scannedFiles.groupBy { file -> when (file.extension.lowercase()) { - in apkExtensions -> "APKs" - in imageExtensions -> "Images" - in videoExtensions -> "Videos" - in audioExtensions -> "Audios" - in archiveExtensions -> "Archives" - else -> "Others" + in apkExtensions -> { + return@groupBy "APKs" + } + in imageExtensions -> { + return@groupBy "Images" + } + in videoExtensions -> { + return@groupBy "Videos" + } + in audioExtensions -> { + return@groupBy "Audios" + } + in archiveExtensions -> { + return@groupBy "Archives" + } + else -> { + return@groupBy "Others" + } } } } @@ -239,25 +252,25 @@ fun AnalyzeComposable(imageLoader: ImageLoader) { LazyColumn( modifier = Modifier - .fillMaxSize(), + .fillMaxSize(), ) { filesByDate.forEach { (date, files) -> item(key = date) { Row( modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 8.dp, vertical = 4.dp), + .fillMaxWidth() + .padding(horizontal = 8.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { - Text(modifier = Modifier.padding(start = 8.dp), text = date) + Text(modifier = Modifier.padding(start = 8.dp), text = TimeHelper.formatDate(Date(files[0].lastModified()))) val allFilesForDateSelected = - files.all { uiState.fileSelectionStates[it] == true } + files.all { uiState.fileSelectionStates[it] == true } Checkbox( checked = allFilesForDateSelected, - onCheckedChange = { checked -> + onCheckedChange = { isChecked -> files.forEach { file -> - // viewModel.selectFile(file, checked) + viewModel.onFileSelectionChange(file, isChecked) } } ) 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 522a6c8..a2b6234 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 @@ -52,7 +52,7 @@ class HomeViewModel(application: Application) : BaseViewModel(application) { val thumbnailFile = File(context.cacheDir, "thumbnail_${filePath.hashCode()}.png") val savedSuccessfully = repository.saveBitmapToFile(bitmap, thumbnailFile) if (savedSuccessfully) { - callback(thumbnailFile) // Update state only once + callback(thumbnailFile) } else { callback(null) } diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/repository/HomeRepository.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/repository/HomeRepository.kt index 4481817..1436635 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/home/repository/HomeRepository.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/home/repository/HomeRepository.kt @@ -27,7 +27,7 @@ class HomeRepository( withContext(Dispatchers.IO) { fileScanner.startScanning() val filteredFiles = fileScanner.getFilteredFiles() - withContext(Dispatchers.Main) { // Switch to Main for callback + withContext(Dispatchers.Main) { onSuccess(filteredFiles) } } diff --git a/app/src/main/kotlin/com/d4rk/cleaner/utils/TimeHelper.kt b/app/src/main/kotlin/com/d4rk/cleaner/utils/TimeHelper.kt new file mode 100644 index 0000000..746d79d --- /dev/null +++ b/app/src/main/kotlin/com/d4rk/cleaner/utils/TimeHelper.kt @@ -0,0 +1,39 @@ +package com.d4rk.cleaner.utils + +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Date +import java.util.Locale + +object TimeHelper { + + fun formatDate(date: Date): String { + val today = Calendar.getInstance() + val yesterday = Calendar.getInstance() + yesterday.add(Calendar.DAY_OF_YEAR, -1) + + val calendar = Calendar.getInstance() + calendar.time = date + + return when { + calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && + calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) -> { + "Today" + } + calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) && + calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR) -> { + "Yesterday" + } + calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) -> { + SimpleDateFormat("EEE, MMM d", Locale.getDefault()).format(date) + } + calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) - 1 -> { + "a year ago" + } + else -> { + val yearsAgo = today.get(Calendar.YEAR) - calendar.get(Calendar.YEAR) + "$yearsAgo years ago" + } + } + } +} \ No newline at end of file