From d1ee5398e4a7d04175fb93ae2734fa8c5bedadb0 Mon Sep 17 00:00:00 2001 From: Mihai-Cristian Condrea Date: Tue, 22 Oct 2024 10:56:18 +0300 Subject: [PATCH] Fixed trash size --- .../com/d4rk/cleaner/data/datastore/DataStore.kt | 7 +++++++ .../ui/screens/home/repository/HomeRepository.kt | 14 +++++++++----- .../cleaner/ui/screens/trash/TrashViewModel.kt | 2 ++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/d4rk/cleaner/data/datastore/DataStore.kt b/app/src/main/kotlin/com/d4rk/cleaner/data/datastore/DataStore.kt index 5e4098f..c09c4b4 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/data/datastore/DataStore.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/data/datastore/DataStore.kt @@ -225,6 +225,13 @@ class DataStore(context : Context) { } } + suspend fun subtractTrashSize(size: Long) { + dataStore.edit { settings -> + val currentSize = settings[trashSizeKey] ?: 0L + settings[trashSizeKey] = (currentSize - size).coerceAtLeast(0L) + } + } + private val genericFilterKey = booleanPreferencesKey(name = DataStoreNamesConstants.DATA_STORE_GENERIC_FILTER) val genericFilter : Flow = dataStore.data.map { preferences -> diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt index 29723f1..d0496db 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/home/repository/HomeRepository.kt @@ -36,7 +36,7 @@ class HomeRepository( } } - suspend fun getLastScanInfo(onSuccess: (Int) -> Unit) { + suspend fun getLastScanInfo(onSuccess : (Int) -> Unit) { withContext(Dispatchers.IO) { dataStore.lastScanTimestamp.firstOrNull()?.let { timestamp -> val daysFromLastScan = calculateDaysSince(timestamp) @@ -140,10 +140,6 @@ class HomeRepository( } } - suspend fun addTrashSize(size: Long) { - dataStore.addTrashSize(size) - } - /** * Restores the specified files from the trash directory. * @param filesToRestore The set of files to restore from trash. @@ -159,4 +155,12 @@ class HomeRepository( } } } + + suspend fun addTrashSize(size : Long) { + dataStore.addTrashSize(size) + } + + suspend fun subtractTrashSize(size : Long) { + dataStore.subtractTrashSize(size) + } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/trash/TrashViewModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/trash/TrashViewModel.kt index 1cbc4ea..de75d47 100644 --- a/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/trash/TrashViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/screens/trash/TrashViewModel.kt @@ -51,6 +51,7 @@ class TrashViewModel(application : Application) : BaseViewModel(application) { repository.restoreFromTrash(filesToRestore) { loadTrashItems() } + repository.subtractTrashSize(filesToRestore.sumOf { it.length() }) hideLoading() } } @@ -64,6 +65,7 @@ class TrashViewModel(application : Application) : BaseViewModel(application) { repository.deleteFiles(filesToDelete) { loadTrashItems() } + repository.subtractTrashSize(filesToDelete.sumOf { it.length() }) hideLoading() println("Cleaner for Android -> Hiding loading indicator") }