Skip to content

Commit

Permalink
Refactor use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Maruchin1 committed Apr 16, 2023
1 parent dd31419 commit 850055d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package com.maruchin.domaindrivenandroid.domain.coupon
import com.maruchin.domaindrivenandroid.data.account.AccountRepository
import com.maruchin.domaindrivenandroid.data.coupon.CouponsRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetAllCollectableCouponsUseCase @Inject constructor(
Expand All @@ -14,14 +13,12 @@ class GetAllCollectableCouponsUseCase @Inject constructor(
) {

operator fun invoke(): Flow<List<CollectableCoupon>> {
return accountRepository.getLoggedInAccount().filterNotNull().flatMapLatest { account ->
couponsRepository.getAllCoupons().map { allCoupons ->
allCoupons.map { coupon ->
CollectableCoupon(
coupon = coupon,
canCollect = account.canPayFor(coupon)
)
}
return combine(
accountRepository.getLoggedInAccount().filterNotNull(),
couponsRepository.getAllCoupons()
) { account, allCoupons ->
allCoupons.map { coupon ->
CollectableCoupon(coupon = coupon, canCollect = account.canPayFor(coupon))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import com.maruchin.domaindrivenandroid.data.account.AccountRepository
import com.maruchin.domaindrivenandroid.data.coupon.CouponsRepository
import com.maruchin.domaindrivenandroid.data.units.ID
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetCollectableCouponUseCase @Inject constructor(
Expand All @@ -15,13 +14,14 @@ class GetCollectableCouponUseCase @Inject constructor(
) {

operator fun invoke(couponId: ID): Flow<CollectableCoupon?> {
return accountRepository.getLoggedInAccount().filterNotNull().flatMapLatest { account ->
couponsRepository.getCoupon(couponId).filterNotNull().map { coupon ->
CollectableCoupon(
coupon = coupon,
canCollect = account.canPayFor(coupon)
)
}
return combine(
accountRepository.getLoggedInAccount().filterNotNull(),
couponsRepository.getCoupon(couponId).filterNotNull(),
) { account, coupon ->
CollectableCoupon(
coupon = coupon,
canCollect = account.canPayFor(coupon)
)
}
}
}

0 comments on commit 850055d

Please sign in to comment.