Skip to content

Commit

Permalink
Merge branch 'master' into renovate/coil
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigoriym authored Jan 15, 2024
2 parents 1797f1c + ed65a18 commit a5e678d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class DataCleanerImplTest {
private val dataCleaner: DataCleaner = DataCleanerImpl(
fileUtils = fileUtils,
productsRepository = productsRepository,
transactionController = transactionController,
databaseDao = databaseDao,
databaseWrapper = databaseWrapper,
ioDispatcher = UnconfinedTestDispatcher()
Expand Down Expand Up @@ -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<suspend () -> Unit>().invoke()
}
every { fileUtils.clearMainFolder() } just Runs
every { fileUtils.clearMainFolder() } returns true

dataCleaner.clearAllData()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ class FileUtils @Inject constructor(
)
}

fun clearMainFolder() {
getMainFolder("").deleteRecursively()
}
fun clearMainFolder() : Boolean = getMainFolder("").deleteRecursively()

fun getMainFolder(
child: String
Expand Down

0 comments on commit a5e678d

Please sign in to comment.