Skip to content

Commit

Permalink
Finished trash screen and functionality + string improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Oct 5, 2024
1 parent 8fc8738 commit e7bb6a0
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/components/Buttons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.d4rk.cleaner.ui.components.animations.bounceClick

@Composable
fun TwoRowButtons(enabled : Boolean ,
onStartButtonClick: () -> Unit ,
onStartButtonIcon : ImageVector ,
onEndButtonClick: () -> Unit ,
onEndButtonIcon : ImageVector) {
fun TwoRowButtons(
modifier : Modifier ,
enabled : Boolean ,
onStartButtonClick : () -> Unit ,
onStartButtonIcon : ImageVector ,
onStartButtonText : Int ,
onEndButtonClick : () -> Unit ,
onEndButtonIcon : ImageVector ,
onEndButtonText : Int ,
) {
Row(
modifier = Modifier.fillMaxWidth() , horizontalArrangement = Arrangement.SpaceAround
modifier = modifier.fillMaxWidth() , horizontalArrangement = Arrangement.SpaceAround
) {
OutlinedButton(enabled = enabled ,
onClick = {
Expand All @@ -41,14 +47,14 @@ fun TwoRowButtons(enabled : Boolean ,
modifier = Modifier.size(ButtonDefaults.IconSize)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(text = "Move to trash")
Text(text = stringResource(id = onStartButtonText))
}

Spacer(Modifier.width(8.dp))

Button(enabled = enabled ,
onClick = {
onEndButtonClick()
onEndButtonClick()
} ,
modifier = Modifier
.weight(1f)
Expand All @@ -60,7 +66,7 @@ fun TwoRowButtons(enabled : Boolean ,
modifier = Modifier.size(ButtonDefaults.IconSize)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(text = "Delete forever")
Text(text = stringResource(id = onEndButtonText))
}
}
}
31 changes: 21 additions & 10 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
Expand Down Expand Up @@ -304,6 +305,7 @@ fun AnalyzeComposable(imageLoader : ImageLoader , context : Context) {
}

FilesByDateSection(
modifier = Modifier,
filesByDate = filesByDate ,
fileSelectionStates = uiState.fileSelectionStates ,
imageLoader = imageLoader ,
Expand All @@ -319,10 +321,9 @@ fun AnalyzeComposable(imageLoader : ImageLoader , context : Context) {
verticalAlignment = Alignment.CenterVertically ,
horizontalArrangement = Arrangement.SpaceBetween ,
) {
val statusText : String = if (uiState.selectedFileCount > 0) {
stringResource(id = R.string.status_selected_files , uiState.selectedFileCount)
}
else {
val statusText: String = if (uiState.selectedFileCount > 0) {
pluralStringResource(id = R.plurals.status_selected_files, count = uiState.selectedFileCount, uiState.selectedFileCount)
} else {
stringResource(id = R.string.status_no_files_selected)
}
val statusColor : Color by animateColorAsState(
Expand All @@ -342,24 +343,34 @@ fun AnalyzeComposable(imageLoader : ImageLoader , context : Context) {
SelectAllComposable(viewModel)
}

TwoRowButtons(enabled = enabled , onStartButtonClick = {
viewModel.moveToTrash()
} , onStartButtonIcon = Icons.Outlined.Delete , onEndButtonClick = {
viewModel.clean()
} , onEndButtonIcon = Icons.Outlined.DeleteForever)
TwoRowButtons(
modifier = Modifier,
enabled = enabled ,
onStartButtonClick = {
viewModel.moveToTrash()
} ,
onStartButtonIcon = Icons.Outlined.Delete ,
onStartButtonText = R.string.move_to_trash ,

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

@Composable
fun FilesByDateSection(
modifier : Modifier,
filesByDate : Map<String , List<File>> ,
fileSelectionStates : Map<File , Boolean> ,
imageLoader : ImageLoader ,
onFileSelectionChange : (File , Boolean) -> Unit ,
) {
LazyColumn(
modifier = Modifier.fillMaxSize()
modifier = modifier.fillMaxSize()
) {
val sortedDates = filesByDate.keys.sortedByDescending { dateString ->
SimpleDateFormat("yyyy-MM-dd" , Locale.getDefault()).parse(dateString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class HomeRepository(

suspend fun moveToTrash(filesToMove : List<File> , onSuccess : () -> Unit) {
withContext(Dispatchers.IO) {
moveToTrash(filesToMove) // Call the updated function below
moveToTrash(filesToMove)
withContext(Dispatchers.Main) {
onSuccess()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,28 @@ abstract class HomeRepositoryImplementation(val application : Application) {
}
}

fun restoreFromTrash(filesToRestore : Set<File>) {
fun restoreFromTrash(filesToRestore: Set<File>) {
filesToRestore.forEach { file ->
val originalPath = file.absolutePath.replace("/trash/" , "/")
val destination = File(originalPath)
val trashDir = File(application.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "Trash")

val relativePathInTrash = file.relativeTo(trashDir).path
val originalFile = File(application.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), relativePathInTrash)

val destinationParent = originalFile.parentFile

if (destinationParent != null && !destinationParent.exists()) {
destinationParent.mkdirs()
}

if (file.exists()) {
file.renameTo(destination)
if (file.renameTo(originalFile)) {
MediaScannerConnection.scanFile(
application,
arrayOf(originalFile.absolutePath, file.absolutePath),
null,
null
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.d4rk.cleaner.ui.screens.trash

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.DeleteForever
import androidx.compose.material.icons.outlined.Restore
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -17,6 +16,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.ImageLoader
import coil.disk.DiskCache
Expand Down Expand Up @@ -72,23 +74,48 @@ fun TrashScreen(activity : TrashActivity) {
}
}
else {
Column(
ConstraintLayout(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
val (list , buttons) = createRefs()

TrashItemsList(
modifier = Modifier.constrainAs(list) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(buttons.top)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
} ,
trashFiles = uiState.trashFiles ,
imageLoader ,
uiState = uiState ,
viewModel = viewModel
)

TwoRowButtons(enabled = enabled , onStartButtonClick = {
viewModel.restoreFromTrash()
} , onStartButtonIcon = Icons.Outlined.Delete , onEndButtonClick = {
// TODO: for restore
} , onEndButtonIcon = Icons.Outlined.DeleteForever)
TwoRowButtons(modifier = Modifier
.padding(16.dp)
.constrainAs(buttons) {
bottom.linkTo(parent.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
width = Dimension.fillToConstraints
} ,
enabled = enabled ,
onStartButtonClick = {
viewModel.restoreFromTrash()
} ,
onStartButtonIcon = Icons.Outlined.Restore ,
onStartButtonText = R.string.restore ,

onEndButtonClick = {
viewModel.clean()
} ,
onEndButtonIcon = Icons.Outlined.DeleteForever ,
onEndButtonText = R.string.delete_forever)
}
}
}
Expand All @@ -97,6 +124,7 @@ fun TrashScreen(activity : TrashActivity) {

@Composable
fun TrashItemsList(
modifier : Modifier ,
trashFiles : List<File> ,
imageLoader : ImageLoader ,
uiState : UiTrashModel ,
Expand All @@ -109,6 +137,7 @@ fun TrashItemsList(
}

FilesByDateSection(
modifier = modifier ,
filesByDate = filesByDate ,
fileSelectionStates = uiState.fileSelectionStates ,
imageLoader = imageLoader ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.d4rk.cleaner.data.datastore.DataStore
import com.d4rk.cleaner.data.model.ui.screens.UiTrashModel
import com.d4rk.cleaner.ui.screens.home.repository.HomeRepository
import com.d4rk.cleaner.ui.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -48,4 +49,13 @@ class TrashViewModel(application : Application) : BaseViewModel(application) {
hideLoading()
}
}

fun clean() {
viewModelScope.launch(context = Dispatchers.Default + coroutineExceptionHandler) {
val filesToDelete = _uiState.value.fileSelectionStates.filter { it.value }.keys
showLoading()
repository.deleteFiles(filesToDelete)
hideLoading()
}
}
}
5 changes: 4 additions & 1 deletion app/src/main/res/values-bg-rBG/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<string name="videos">Видеоклипове</string>
<string name="audios">Аудио файлове</string>
<string name="other">Други</string>
<string name="status_selected_files">Статус: Избрани %1$d файла</string>
<plurals name="status_selected_files">
<item quantity="one">Състояние: Избран %1$d файл</item>
<item quantity="other">Състояние: Избрани %1$d файла</item>
</plurals>
<string name="status_no_files_selected">Статус: Не са избрани файлове</string>
<string name="select_all">Избиране на всички</string>
<string name="rescan_title">Сканиране отново?</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<string name="videos">Videos</string>
<string name="audios">Audios</string>
<string name="other">Andere</string>
<string name="status_selected_files">Status: %1$d Dateien ausgewählt</string>
<plurals name="status_selected_files">
<item quantity="one">Status: %1$d Datei ausgewählt</item>
<item quantity="other">Status: %1$d Dateien ausgewählt</item>
</plurals>
<string name="status_no_files_selected">Status: Keine Dateien ausgewählt</string>
<string name="select_all">Alle auswählen</string>
<string name="rescan_title">Neu scannen?</string>
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values-es-rGQ/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
<string name="videos">Vídeos</string>
<string name="audios">Audios</string>
<string name="other">Otros</string>
<string name="status_selected_files">Estado: Se seleccionaron %1$d archivos</string>
<plurals name="status_selected_files">
<item quantity="one">Estado: %1$d archivo seleccionado</item>
<item quantity="other">Estado: %1$d archivos seleccionados</item>
<item quantity="many">Estado: %1$d archivos seleccionados</item>
</plurals>
<string name="status_no_files_selected">Estado: No se seleccionaron archivos</string>
<string name="select_all">Seleccionar todo</string>
<string name="rescan_title">¿Volver a escanear?</string>
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values-fr-rFR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
<string name="videos">Vidéos</string>
<string name="audios">Audios</string>
<string name="other">Autres</string>
<string name="status_selected_files">Statut: %1$d fichiers sélectionnés</string>
<plurals name="status_selected_files">
<item quantity="one">Statut : %1$d fichier sélectionné</item>
<item quantity="other">Statut : %1$d fichiers sélectionnés</item>
<item quantity="many">Statut : %1$d fichiers sélectionnés</item>
</plurals>
<string name="status_no_files_selected">Statut: Aucun fichier sélectionné</string>
<string name="select_all">Tout sélectionner</string>
<string name="rescan_title">Re-scanner?</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-hi-rIN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<string name="videos">वीडियो</string>
<string name="audios">ऑडियो</string>
<string name="other">अन्य</string>
<string name="status_selected_files">स्थिति: %1$d फ़ाइलें चयनित</string>
<plurals name="status_selected_files">
<item quantity="one">स्थिति: %1$d फ़ाइल चयनित</item>
<item quantity="other">स्थिति: %1$d फ़ाइलें चयनित</item>
</plurals>
<string name="status_no_files_selected">स्थिति: कोई फ़ाइलें चयनित नहीं हैं</string>
<string name="select_all">सबका चयन करें</string>
<string name="rescan_title">पुनः स्कैन करें?</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-hu-rHU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<string name="videos">Videók</string>
<string name="audios">Hangfájlok</string>
<string name="other">Egyéb</string>
<string name="status_selected_files">Állapot: Kiválasztva %1$d fájl</string>
<plurals name="status_selected_files">
<item quantity="one">Állapot: %1$d fájl kiválasztva</item>
<item quantity="other">Állapot: %1$d fájlok kiválasztva</item>
</plurals>
<string name="status_no_files_selected">Állapot: Nincsenek kiválasztva fájlok</string>
<string name="select_all">Összes kijelölése</string>
<string name="rescan_title">Újraellenőrzés?</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-in-rID/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
<string name="videos">Video</string>
<string name="audios">Audio</string>
<string name="other">Lainnya</string>
<string name="status_selected_files">Status: %1$d file dipilih</string>
<plurals name="status_selected_files">
<item quantity="other">Status: %1$d berkas dipilih</item>
</plurals>
<string name="status_no_files_selected">Status: Tidak ada file yang dipilih</string>
<string name="select_all">Pilih Semua</string>
<string name="rescan_title">Pindai ulang?</string>
Expand Down
Loading

0 comments on commit e7bb6a0

Please sign in to comment.