Skip to content

Commit

Permalink
MBL-1198: Reorder rewards so that unavailable ones show last (#1956)
Browse files Browse the repository at this point in the history
* sort rewards

* Sort rewards, testing

* cleanup
  • Loading branch information
leighdouglas authored Feb 24, 2024
1 parent bb46ce7 commit ac5ce6a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RewardsAdapter(private val delegate: Delegate) : KSAdapter() {

val rewards = projectData.project().rewards()

if (rewards != null) {
if (!rewards.isNullOrEmpty()) {
addSection(
Observable.from(rewards)
.map { reward -> Pair.create(projectData, reward) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class RewardsFragmentViewModel {
.addToDisposable(disposables)

this.projectDataInput
.filter { filterOutNotStartedRewards(it).isNotNull() }
.map { filterOutNotStartedRewards(it) }
.filter { sortAndFilterRewards(it).isNotNull() }
.map { sortAndFilterRewards(it) }
.subscribe { this.projectData.onNext(it) }
.addToDisposable(disposables)

Expand Down Expand Up @@ -246,9 +246,14 @@ class RewardsFragmentViewModel {
.addToDisposable(disposables)
}

private fun filterOutNotStartedRewards(pData: ProjectData): ProjectData {
val rewards = pData.project().rewards()?.filter { RewardUtils.hasStarted(it) }
val modifiedProject = pData.project().toBuilder().rewards(rewards).build()
private fun sortAndFilterRewards(pData: ProjectData): ProjectData {
val startedRewards = pData.project().rewards()?.filter { RewardUtils.hasStarted(it) }
val sortedRewards = startedRewards?.filter { RewardUtils.isAvailable(pData.project(), it) }?.toMutableList() ?: mutableListOf()
val unavailableRewards = startedRewards?.filter { !RewardUtils.isAvailable(pData.project(), it) }?.toMutableList()

unavailableRewards?.let { sortedRewards.addAll(it) }

val modifiedProject = pData.project().toBuilder().rewards(sortedRewards).build()
return pData.toBuilder()
.project(modifiedProject)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class RewardsFragmentViewModelTest : KSRobolectricTestCase() {
}

@Test
fun testFilterOutRewards_whenRewardNotStarted() {
fun testFilterOutRewards_whenRewardNotStarted_filtersOutReward() {
val rwNotLimitedStart = RewardFactory.reward()
val rwLimitedStartNotStartedYet = rwNotLimitedStart.toBuilder().startsAt(DateTime.now().plusDays(1)).build()
val rwLimitedStartStarted = rwNotLimitedStart.toBuilder().startsAt(DateTime.now()).build()
Expand All @@ -301,6 +301,39 @@ class RewardsFragmentViewModelTest : KSRobolectricTestCase() {
this.rewardsCount.assertValue(2)
}

@Test
fun testFilterAndSortRewards_whenRewardUnavailable_sortsRewardToEnd() {
val rwNotLimitedStart = RewardFactory.reward()
val rwLimitedStartNotStartedYet = rwNotLimitedStart.toBuilder().startsAt(DateTime.now().plusDays(1)).build()
val rwLimitedStartStarted = rwNotLimitedStart.toBuilder().startsAt(DateTime.now()).build()
val limited = RewardFactory.reward().toBuilder().startsAt(DateTime.now()).limit(5).build()
val noRemaining = RewardFactory.limitReached()
val expired = RewardFactory.ended()

val rewards = listOf<Reward>(
rwNotLimitedStart,
rwLimitedStartNotStartedYet,
noRemaining,
limited,
expired,
rwLimitedStartStarted
)

val project = ProjectFactory.project().toBuilder().rewards(rewards).build()

setUpEnvironment(environment())
// - We configure the viewModel with a project that has rewards not started yet
this.vm.inputs.configureWith(ProjectDataFactory.project(project))

val filteredList = listOf(rwNotLimitedStart, limited, rwLimitedStartStarted, noRemaining, expired)
val projWithFilteredRewards = project.toBuilder().rewards(filteredList).build()
val modifiedPData = ProjectData.builder().project(projWithFilteredRewards).build()

// - We check that the viewModel has filtered out the rewards not started yet
this.projectData.assertValue(modifiedPData)
this.rewardsCount.assertValue(5)
}

@Test
fun testRewardsCount() {
val project = ProjectFactory.project()
Expand Down

0 comments on commit ac5ce6a

Please sign in to comment.