Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Remove CategoryDao WriteCategoryDao from BackupDataUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyatanu committed Sep 5, 2024
1 parent ac81760 commit 13be8a0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import com.ivy.data.DataObserver
import com.ivy.data.db.IvyRoomDatabase
import com.ivy.data.file.FileSystem
import com.ivy.data.repository.AccountRepository
import com.ivy.data.repository.CategoryRepository
import com.ivy.data.repository.CurrencyRepository
import com.ivy.data.repository.fake.fakeRepositoryMemoFactory
import com.ivy.data.repository.mapper.AccountMapper
import com.ivy.data.repository.mapper.CategoryMapper
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.ints.shouldBeGreaterThan
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -47,7 +49,6 @@ class BackupDataUseCaseAndroidTest {
useCase = BackupDataUseCase(
accountDao = db.accountDao,
budgetDao = db.budgetDao,
categoryDao = db.categoryDao,
loanRecordDao = db.loanRecordDao,
loanDao = db.loanDao,
plannedPaymentRuleDao = db.plannedPaymentRuleDao,
Expand All @@ -63,12 +64,19 @@ class BackupDataUseCaseAndroidTest {
memoFactory = fakeRepositoryMemoFactory(),
),
accountMapper = accountMapper,
categoryWriter = db.writeCategoryDao,
settingsWriter = db.writeSettingsDao,
budgetWriter = db.writeBudgetDao,
loanWriter = db.writeLoanDao,
loanRecordWriter = db.writeLoanRecordDao,
plannedPaymentRuleWriter = db.writePlannedPaymentRuleDao,
categoryMapper = CategoryMapper(),
categoryRepository = CategoryRepository(
mapper = CategoryMapper(),
categoryDao = db.categoryDao,
writeCategoryDao = db.writeCategoryDao,
dispatchersProvider = TestDispatchersProvider,
memoFactory = fakeRepositoryMemoFactory(),
),
context = appContext,
json = KotlinxSerializationModule.provideJson(),
dispatchersProvider = TestDispatchersProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ivy.data.backup
import android.content.Context
import android.net.Uri
import androidx.core.net.toUri
import arrow.core.Either
import com.ivy.base.legacy.SharedPrefs
import com.ivy.base.legacy.unzip
import com.ivy.base.legacy.zip
Expand All @@ -11,22 +12,23 @@ import com.ivy.data.DataObserver
import com.ivy.data.DataWriteEvent
import com.ivy.data.db.dao.read.AccountDao
import com.ivy.data.db.dao.read.BudgetDao
import com.ivy.data.db.dao.read.CategoryDao
import com.ivy.data.db.dao.read.LoanDao
import com.ivy.data.db.dao.read.LoanRecordDao
import com.ivy.data.db.dao.read.PlannedPaymentRuleDao
import com.ivy.data.db.dao.read.SettingsDao
import com.ivy.data.db.dao.read.TransactionDao
import com.ivy.data.db.dao.write.WriteBudgetDao
import com.ivy.data.db.dao.write.WriteCategoryDao
import com.ivy.data.db.dao.write.WriteLoanDao
import com.ivy.data.db.dao.write.WriteLoanRecordDao
import com.ivy.data.db.dao.write.WritePlannedPaymentRuleDao
import com.ivy.data.db.dao.write.WriteSettingsDao
import com.ivy.data.db.dao.write.WriteTransactionDao
import com.ivy.data.file.FileSystem
import com.ivy.data.model.Category
import com.ivy.data.repository.AccountRepository
import com.ivy.data.repository.CategoryRepository
import com.ivy.data.repository.mapper.AccountMapper
import com.ivy.data.repository.mapper.CategoryMapper
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.async
Expand All @@ -43,7 +45,6 @@ import javax.inject.Inject
class BackupDataUseCase @Inject constructor(
private val accountDao: AccountDao,
private val budgetDao: BudgetDao,
private val categoryDao: CategoryDao,
private val loanRecordDao: LoanRecordDao,
private val loanDao: LoanDao,
private val plannedPaymentRuleDao: PlannedPaymentRuleDao,
Expand All @@ -53,12 +54,13 @@ class BackupDataUseCase @Inject constructor(
private val sharedPrefs: SharedPrefs,
private val accountRepository: AccountRepository,
private val accountMapper: AccountMapper,
private val categoryWriter: WriteCategoryDao,
private val settingsWriter: WriteSettingsDao,
private val budgetWriter: WriteBudgetDao,
private val loanWriter: WriteLoanDao,
private val loanRecordWriter: WriteLoanRecordDao,
private val plannedPaymentRuleWriter: WritePlannedPaymentRuleDao,
private val categoryMapper: CategoryMapper,
private val categoryRepository: CategoryRepository,
@ApplicationContext
private val context: Context,
private val json: Json,
Expand Down Expand Up @@ -90,7 +92,7 @@ class BackupDataUseCase @Inject constructor(
return withContext(dispatchersProvider.io) {
val accounts = async { accountDao.findAll() }
val budgets = async { budgetDao.findAll() }
val categories = async { categoryDao.findAll() }
val categories = async { categoryRepository.findAll() }
val loanRecords = async { loanRecordDao.findAll() }
val loans = async { loanDao.findAll() }
val plannedPaymentRules =
Expand All @@ -102,7 +104,11 @@ class BackupDataUseCase @Inject constructor(
val completeData = IvyWalletCompleteData(
accounts = accounts.await(),
budgets = budgets.await(),
categories = categories.await(),
categories = categories.await().map {
with(categoryMapper){
it.toEntity()
}
},
loanRecords = loanRecords.await(),
loans = loans.await(),
plannedPaymentRules = plannedPaymentRules.await(),
Expand Down Expand Up @@ -247,7 +253,20 @@ class BackupDataUseCase @Inject constructor(
}
val budgets = async { budgetWriter.saveMany(completeData.budgets) }
val categories =
async { categoryWriter.saveMany(completeData.categories) }
async {
with(categoryMapper) {
val categoryList = completeData.categories.map { it.toDomain() }
val list = mutableListOf<Category>()
categoryList.filter { either ->
when(either){
is Either.Left -> { error("Failed to insert data to db")}
is Either.Right -> { list.add(either.value)}
}

}
categoryRepository.saveMany(list)
}
}
accounts.await()
budgets.await()
categories.await()
Expand Down Expand Up @@ -308,10 +327,16 @@ class BackupDataUseCase @Inject constructor(
private suspend fun getReplacementPairs(
completeData: IvyWalletCompleteData
): List<Pair<UUID, UUID>> {


return withContext(dispatchersProvider.io) {
val existingAccountsList = accountDao.findAll()
val existingCategoryList = categoryDao.findAll()

val categoryList = categoryRepository.findAll()
val existingCategoryList = categoryList.map {
with(categoryMapper){
it.toEntity()
}
}
val backupAccountsList = completeData.accounts
val backupCategoryList = completeData.categories

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 13be8a0

Please sign in to comment.