Skip to content

Commit

Permalink
Fix: Merge multiple instances of same asset in one portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrlzy committed Nov 11, 2024
1 parent 21417f8 commit 55b6b76
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.arkbuilders.rate.data.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Upsert
import dev.arkbuilders.rate.data.db.entity.RoomAsset
Expand All @@ -12,6 +14,9 @@ interface PortfolioDao {
@Upsert
suspend fun insert(asset: RoomAsset)

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertList(list: List<RoomAsset>)

@Query("SELECT * FROM RoomAsset")
suspend fun getAll(): List<RoomAsset>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.arkbuilders.rate.data.repo
import dev.arkbuilders.rate.data.db.dao.PortfolioDao
import dev.arkbuilders.rate.data.db.entity.RoomAsset
import dev.arkbuilders.rate.domain.model.Asset
import dev.arkbuilders.rate.domain.model.CurrencyCode
import dev.arkbuilders.rate.domain.repo.PortfolioRepo
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -23,19 +24,12 @@ class PortfolioRepoImpl @Inject constructor(

override suspend fun getById(id: Long) = dao.getById(id)?.toAsset()

override suspend fun setAsset(asset: Asset) {
val roomAsset =
dao.getAllByCode(asset.code).find {
it.group == asset.group
}?.toAsset()
val mergedAsset =
roomAsset?.let {
roomAsset.copy(value = asset.value + roomAsset.value)
} ?: asset
dao.insert(mergedAsset.toRoom())
}

override suspend fun setAssetsList(list: List<Asset>) = list.forEach { setAsset(it) }
override suspend fun getAllByCode(code: CurrencyCode) =
dao.getAllByCode(code).map { it.toAsset() }

override suspend fun setAsset(asset: Asset) = dao.insert(asset.toRoom())

override suspend fun setAssetsList(list: List<Asset>) = dao.insertList(list.map { it.toRoom() })

override suspend fun removeAsset(id: Long) = dao.delete(id) > 0
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.arkbuilders.rate.domain.repo

import dev.arkbuilders.rate.domain.model.Asset
import dev.arkbuilders.rate.domain.model.CurrencyCode
import kotlinx.coroutines.flow.Flow

interface PortfolioRepo {
Expand All @@ -10,6 +11,8 @@ interface PortfolioRepo {

suspend fun getById(id: Long): Asset?

suspend fun getAllByCode(code: CurrencyCode): List<Asset>

suspend fun setAsset(asset: Asset)

suspend fun setAssetsList(list: List<Asset>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.arkbuilders.rate.domain.usecase

import dev.arkbuilders.rate.domain.model.Asset
import dev.arkbuilders.rate.domain.repo.PortfolioRepo
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class AddNewAssetsUseCase @Inject constructor(
private val portfolioRepo: PortfolioRepo,
) {
suspend operator fun invoke(assets: List<Asset>) {
val mergedAssets =
assets.map { asset ->
val roomAsset =
portfolioRepo.getAllByCode(asset.code).find {
it.group == asset.group
}
val mergedAsset =
roomAsset?.let {
roomAsset.copy(value = asset.value + roomAsset.value)
} ?: asset
mergedAsset
}
portfolioRepo.setAssetsList(mergedAssets)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.arkbuilders.rate.domain.repo.AnalyticsManager
import dev.arkbuilders.rate.domain.repo.CodeUseStatRepo
import dev.arkbuilders.rate.domain.repo.CurrencyRepo
import dev.arkbuilders.rate.domain.repo.PortfolioRepo
import dev.arkbuilders.rate.domain.usecase.AddNewAssetsUseCase
import dev.arkbuilders.rate.presentation.shared.AppSharedFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -42,6 +43,7 @@ class AddAssetViewModel(
private val currencyRepo: CurrencyRepo,
private val codeUseStatRepo: CodeUseStatRepo,
private val analyticsManager: AnalyticsManager,
private val addNewAssetsUseCase: AddNewAssetsUseCase,
) : ViewModel(), ContainerHost<AddAssetState, AddAssetSideEffect> {
override val container: Container<AddAssetState, AddAssetSideEffect> =
container(AddAssetState())
Expand Down Expand Up @@ -123,7 +125,7 @@ class AddAssetViewModel(
group = state.group,
)
}
assetsRepo.setAssetsList(currencies)
addNewAssetsUseCase(currencies)
codeUseStatRepo.codesUsed(*currencies.map { it.code }.toTypedArray())
postSideEffect(AddAssetSideEffect.NotifyAssetAdded(currencies))
postSideEffect(AddAssetSideEffect.NavigateBack)
Expand All @@ -136,13 +138,15 @@ class AddAssetViewModelFactory @Inject constructor(
private val currencyRepo: CurrencyRepo,
private val codeUseStatRepo: CodeUseStatRepo,
private val analyticsManager: AnalyticsManager,
private val addNewAssetsUseCase: AddNewAssetsUseCase,
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return AddAssetViewModel(
assetsRepo,
currencyRepo,
codeUseStatRepo,
analyticsManager,
addNewAssetsUseCase,
) as T
}
}

0 comments on commit 55b6b76

Please sign in to comment.