From ed65a187f2de11a8bf70c4672045071dbc48bc2b Mon Sep 17 00:00:00 2001 From: Mikhalchuk Grigoriy <31949421+Grigoriym@users.noreply.github.com> Date: Mon, 15 Jan 2024 12:29:07 +0100 Subject: [PATCH] Fix db cleaning not being finished (#26) --- .../ui/screens/settings/SettingsViewModel.kt | 20 +++++++++++++++---- .../data/cleanerimpl/DataCleanerImpl.kt | 10 +++------- .../data/cleanerimpl/DataCleanerImplTest.kt | 5 ++--- .../grappim/hateitorrateit/utils/FileUtils.kt | 4 +--- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/grappim/hateitorrateit/ui/screens/settings/SettingsViewModel.kt b/app/src/main/java/com/grappim/hateitorrateit/ui/screens/settings/SettingsViewModel.kt index 045e96ef..30145560 100644 --- a/app/src/main/java/com/grappim/hateitorrateit/ui/screens/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/grappim/hateitorrateit/ui/screens/settings/SettingsViewModel.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject @HiltViewModel @@ -80,10 +81,21 @@ class SettingsViewModel @Inject constructor( _viewState.update { it.safeCopy(isLoading = true, showAlertDialog = false) } - dataCleaner.clearAllData() - _viewState.update { - it.safeCopy(isLoading = false) - } + runCatching { + dataCleaner.clearAllData() + }.fold( + onSuccess = { + _viewState.update { + it.safeCopy(isLoading = false) + } + }, + onFailure = { throwable -> + Timber.e(throwable) + _viewState.update { + it.safeCopy(isLoading = false) + } + } + ) } } } diff --git a/data/cleaner-impl/src/main/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImpl.kt b/data/cleaner-impl/src/main/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImpl.kt index bd99be02..f2d70c3e 100644 --- a/data/cleaner-impl/src/main/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImpl.kt +++ b/data/cleaner-impl/src/main/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImpl.kt @@ -3,7 +3,6 @@ package com.grappim.hateitorrateit.data.cleanerimpl import com.grappim.hateitorrateit.commons.IoDispatcher import com.grappim.hateitorrateit.data.cleanerapi.DataCleaner import com.grappim.hateitorrateit.data.db.dao.DatabaseDao -import com.grappim.hateitorrateit.data.db.utils.TransactionController import com.grappim.hateitorrateit.data.db.wrapper.DatabaseWrapper import com.grappim.hateitorrateit.data.repoapi.ProductsRepository import com.grappim.hateitorrateit.domain.DraftProduct @@ -21,7 +20,6 @@ import javax.inject.Inject class DataCleanerImpl @Inject constructor( private val fileUtils: FileUtils, private val productsRepository: ProductsRepository, - private val transactionController: TransactionController, private val databaseDao: DatabaseDao, private val databaseWrapper: DatabaseWrapper, @IoDispatcher private val ioDispatcher: CoroutineDispatcher, @@ -69,16 +67,14 @@ class DataCleanerImpl @Inject constructor( productsRepository.removeProductById(draftProduct.id) } - override suspend fun clearAllData() = withContext(ioDispatcher) { + override suspend fun clearAllData() { clearDatabaseData() clearFileSystemData() } private suspend fun clearDatabaseData() { - transactionController.runInTransaction { - databaseWrapper.clearAllTables() - databaseDao.clearPrimaryKeyIndex() - } + databaseWrapper.clearAllTables() + databaseDao.clearPrimaryKeyIndex() } private fun clearFileSystemData() { diff --git a/data/cleaner-impl/src/test/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImplTest.kt b/data/cleaner-impl/src/test/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImplTest.kt index 8b4460f8..bfe88b25 100644 --- a/data/cleaner-impl/src/test/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImplTest.kt +++ b/data/cleaner-impl/src/test/java/com/grappim/hateitorrateit/data/cleanerimpl/DataCleanerImplTest.kt @@ -34,7 +34,6 @@ class DataCleanerImplTest { private val dataCleaner: DataCleaner = DataCleanerImpl( fileUtils = fileUtils, productsRepository = productsRepository, - transactionController = transactionController, databaseDao = databaseDao, databaseWrapper = databaseWrapper, ioDispatcher = UnconfinedTestDispatcher() @@ -152,10 +151,10 @@ class DataCleanerImplTest { fun `on clearAllData, should call the needed functions`() = runTest { coEvery { databaseWrapper.clearAllTables() } just Runs coEvery { databaseDao.clearPrimaryKeyIndex() } just Runs - coEvery { transactionController.runInTransaction(any()) } coAnswers { + coEvery { transactionController.runInTransaction(any()) } coAnswers { firstArg Unit>().invoke() } - every { fileUtils.clearMainFolder() } just Runs + every { fileUtils.clearMainFolder() } returns true dataCleaner.clearAllData() diff --git a/utils/src/main/java/com/grappim/hateitorrateit/utils/FileUtils.kt b/utils/src/main/java/com/grappim/hateitorrateit/utils/FileUtils.kt index 500d9466..ed050ac1 100644 --- a/utils/src/main/java/com/grappim/hateitorrateit/utils/FileUtils.kt +++ b/utils/src/main/java/com/grappim/hateitorrateit/utils/FileUtils.kt @@ -104,9 +104,7 @@ class FileUtils @Inject constructor( ) } - fun clearMainFolder() { - getMainFolder("").deleteRecursively() - } + fun clearMainFolder() : Boolean = getMainFolder("").deleteRecursively() fun getMainFolder( child: String