Skip to content

Commit

Permalink
[MBL-702] Update Project Watch Count and List on Dashboard for Prelau…
Browse files Browse the repository at this point in the history
…nch Projects (#1814)

* Fixed the count of saved projects and added a debounce just so we're not spamming calls at a rate faster than 1 per sec.

* added corrections to existing tests - now the saved button title gets its saved projects count from the graphql call.
  • Loading branch information
msadoon committed Apr 20, 2023
1 parent 6e20524 commit dcd3724
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 27 additions & 2 deletions Library/ViewModels/BackerDashboardViewModel.swift
Expand Up @@ -108,7 +108,8 @@ public final class BackerDashboardViewModel: BackerDashboardViewModelType, Backe
.map { (.backed, DiscoveryParams.Sort.endingSoon) }

let fetchedUserEvent = Signal.merge(
self.projectSavedProperty.signal.ignoreValues(),
self.projectSavedProperty.signal.ignoreValues()
.ksr_debounce(.seconds(1), on: AppEnvironment.current.scheduler),
self.viewWillAppearProperty.signal.ignoreValues()
)
.switchMap { _ in
Expand All @@ -118,9 +119,33 @@ public final class BackerDashboardViewModel: BackerDashboardViewModelType, Backe
.materialize()
}

let graphUserEvent = Signal.merge(
self.projectSavedProperty.signal.ignoreValues()
.ksr_debounce(.seconds(1), on: AppEnvironment.current.scheduler),
self.viewWillAppearProperty.signal.ignoreValues()
)
.switchMap { _ in
AppEnvironment.current
.apiService.fetchGraphUserSelf()
.ksr_delay(AppEnvironment.current.apiDelayInterval, on: AppEnvironment.current.scheduler)
.materialize()
}

let user = fetchedUserEvent.values()

let updatedUserWithSavedProjectsCount = graphUserEvent.values()
.map { $0.me }

self.updateCurrentUserInEnvironment = user.skip(first: 1)
.takePairWhen(updatedUserWithSavedProjectsCount)
.map { user, graphEventUser in
var updatedUserWithStarredProjectsCount = user

updatedUserWithStarredProjectsCount.stats.starredProjectsCount = graphEventUser.stats
.starredProjectsCount

return updatedUserWithStarredProjectsCount
}

self.postNotification = self.currentUserUpdatedInEnvironmentProperty.signal
.mapConst(Notification(name: .ksr_userUpdated, object: nil))
Expand All @@ -133,7 +158,7 @@ public final class BackerDashboardViewModel: BackerDashboardViewModelType, Backe

self.backerNameText = user.map { $0.name }

self.savedButtonTitleText = user.map { user in
self.savedButtonTitleText = updatedUserWithSavedProjectsCount.map { user in
Strings.projects_count_newline_saved(projects_count: user.stats.starredProjectsCount ?? 0)
}

Expand Down
5 changes: 4 additions & 1 deletion Library/ViewModels/BackerDashboardViewModelTests.swift
Expand Up @@ -59,7 +59,10 @@ internal final class BackerDashboardViewModelTests: TestCase {
|> \.stats.starredProjectsCount .~ 58
|> \.avatar.large .~ "http://cats.com/furball.jpg"

let userEnvelope = UserEnvelope(me: user)

withEnvironment(apiService: MockService(
fetchGraphUserSelfResult: .success(userEnvelope),
fetchUserSelfResponse: user
)) {
AppEnvironment.login(AccessTokenEnvelope(accessToken: "deadbeef", user: user))
Expand All @@ -84,7 +87,7 @@ internal final class BackerDashboardViewModelTests: TestCase {
self.avatarURL.assertValues(["http://cats.com/furball.jpg", "http://cats.com/furball.jpg"])
self.backedButtonTitleText.assertValues(["45\nbacked", "45\nbacked"])
self.backerNameText.assertValues(["Princess Vespa", "Princess Vespa"])
self.savedButtonTitleText.assertValues(["58\nsaved", "58\nsaved"])
self.savedButtonTitleText.assertValues(["58\nsaved"])
self.setSelectedButton.assertValues([.backed])
self.sortBarIsHidden.assertValues([true])
self.embeddedViewTopConstraintConstant.assertValues([0.0])
Expand Down

0 comments on commit dcd3724

Please sign in to comment.