Skip to content

Commit

Permalink
Improved the date format and made the selection of files by date func…
Browse files Browse the repository at this point in the history
…tional.
  • Loading branch information
Mihai-Cristian Condrea committed Sep 27, 2024
1 parent d470803 commit d3abe56
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
39 changes: 26 additions & 13 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
}
}
}
Expand Down Expand Up @@ -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)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/utils/TimeHelper.kt
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit d3abe56

Please sign in to comment.