-
Notifications
You must be signed in to change notification settings - Fork 66
IOS-5365 implement the async membership handling #4111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,24 +14,75 @@ final class MembershipCoordinatorModel: ObservableObject { | |
| @Published var fireConfetti = false | ||
| @Published var emailUrl: URL? | ||
|
|
||
| @Injected(\.membershipService) | ||
| private var membershipService: any MembershipServiceProtocol | ||
| @Injected(\.membershipStatusStorage) | ||
| private var membershipStatusStorage: any MembershipStatusStorageProtocol | ||
| @Injected(\.accountManager) | ||
| private var accountManager: any AccountManagerProtocol | ||
|
|
||
| private let initialTierId: Int? | ||
|
|
||
| private var statusTask: Task<Void, Never>? | ||
| private var tiersTask: Task<Void, Never>? | ||
|
|
||
| init(initialTierId: Int?) { | ||
| self.initialTierId = initialTierId | ||
| membershipStatusStorage.statusPublisher.receiveOnMain().assign(to: &$userMembership) | ||
|
|
||
| statusTask = Task { [weak self] in | ||
| guard let self else { return } | ||
| for await status in membershipStatusStorage.statusStream() { | ||
| self.userMembership = status | ||
| } | ||
| } | ||
|
|
||
| tiersTask = Task { [weak self] in | ||
| guard let self else { return } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for await (status, allTiers) in self.combinedStream() { | ||
| let currentTierId = status.tier?.type.id ?? 0 | ||
| self.tiers = allTiers | ||
| .filter { FeatureFlags.membershipTestTiers || !$0.isTest } | ||
| .filter { !$0.iosProductID.isEmpty || $0.type.id == currentTierId } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| statusTask?.cancel() | ||
| tiersTask?.cancel() | ||
| } | ||
|
|
||
| private func combinedStream() -> AsyncStream<(MembershipStatus, [MembershipTier])> { | ||
| let storage = membershipStatusStorage | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use combineLastes in AsyncAlgoritms |
||
| return AsyncStream { continuation in | ||
| let task = Task { | ||
| var currentStatus = await storage.currentStatus() | ||
| var currentTiers = await storage.currentTiers() | ||
|
|
||
| continuation.yield((currentStatus, currentTiers)) | ||
|
|
||
| await withTaskGroup(of: Void.self) { group in | ||
| group.addTask { | ||
| for await status in storage.statusStream() { | ||
| currentStatus = status | ||
| continuation.yield((currentStatus, currentTiers)) | ||
| } | ||
| } | ||
|
|
||
| group.addTask { | ||
| for await tiers in storage.tiersStream() { | ||
| currentTiers = tiers | ||
| continuation.yield((currentStatus, currentTiers)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| continuation.onTermination = { _ in | ||
| task.cancel() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func onAppear() { | ||
| Task { | ||
| await loadTiers() | ||
|
|
||
| guard let initialTierId else { return } | ||
| guard let initialTier = tiers.first(where: { $0.type.id == initialTierId }) else { | ||
| anytypeAssertionFailure("Not found initial id for Memberhsip coordinator", info: ["tierId": String(initialTierId)]) | ||
|
|
@@ -40,17 +91,15 @@ final class MembershipCoordinatorModel: ObservableObject { | |
| onTierSelected(tier: initialTier) | ||
| } | ||
| } | ||
|
|
||
| func loadTiers(noCache: Bool = false) { | ||
| Task { await loadTiers(noCache: noCache) } | ||
| } | ||
|
|
||
| private func loadTiers(noCache: Bool = false) async { | ||
| do { | ||
| tiers = try await membershipService.getTiers(noCache: noCache) | ||
| showTiersLoadingError = false | ||
| } catch { | ||
| showTiersLoadingError = true | ||
|
|
||
| func retryLoadTiers() { | ||
| Task { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EmptyStateView support async throws methods ->
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try not to write |
||
| do { | ||
| try await membershipStatusStorage.refreshMembership() | ||
| showTiersLoadingError = false | ||
| } catch { | ||
| showTiersLoadingError = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -64,14 +113,14 @@ final class MembershipCoordinatorModel: ObservableObject { | |
|
|
||
| private func showSuccessScreen(tier: MembershipTier) { | ||
| showTier = nil | ||
| loadTiers(noCache: true) | ||
|
|
||
| Task { | ||
| try? await membershipStatusStorage.refreshMembership() | ||
|
|
||
| // https://linear.app/anytype/issue/IOS-2434/bottom-sheet-nesting | ||
| try await Task.sleep(seconds: 0.5) | ||
| showSuccess = tier | ||
|
|
||
| try await Task.sleep(seconds:0.5) | ||
| try await Task.sleep(seconds: 0.5) | ||
| UINotificationFeedbackGenerator().notificationOccurred(.success) | ||
| fireConfetti = true | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,10 @@ final class MembershipUpgradeViewModifierModel: ObservableObject { | |
|
|
||
| nonisolated init() { } | ||
|
|
||
| func updateState(reason: MembershipUpgradeReason?) { | ||
| func updateState(reason: MembershipUpgradeReason?) async { | ||
| guard let reason else { return } | ||
| guard let currentTier = statusStorage.currentStatus.tier else { return } | ||
|
|
||
| guard let currentTier = await statusStorage.currentStatus().tier else { return } | ||
| if accountManager.account.allowMembership && currentTier.isPossibleToUpgrade(reason: reason) { | ||
| showMembershipScreen = true | ||
| } else { | ||
|
|
@@ -56,11 +56,13 @@ struct MembershipUpgradeViewModifier: ViewModifier { | |
| } | ||
| }) | ||
|
|
||
| .onAppear { | ||
| model.updateState(reason: reason) | ||
| .task { | ||
| await model.updateState(reason: reason) | ||
| } | ||
| .onChange(of: reason) { _, reason in | ||
| model.updateState(reason: reason) | ||
| Task { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make |
||
| await model.updateState(reason: reason) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,12 +19,12 @@ struct MembershipPricingView: View { | |
| AnytypeText(info.localizedPeriod ?? "", style: .caption1Regular) | ||
| .foregroundColor(.Text.primary) | ||
| case nil: | ||
| Rectangle().hidden().onAppear { | ||
| Rectangle().hidden().task { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why task here? |
||
| anytypeAssertionFailure( | ||
| "No pricing view for empty payment info", | ||
| info: [ | ||
| "Tier": String(reflecting: tier), | ||
| "Status": String(reflecting: Container.shared.membershipStatusStorage.resolve().currentStatus) | ||
| "Status": String(reflecting: await Container.shared.membershipStatusStorage.resolve().currentStatus()) | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ final class MembershipTierViewModel: ObservableObject { | |
|
|
||
| @Injected(\.membershipMetadataProvider) | ||
| private var tierMetadataProvider: any MembershipMetadataProviderProtocol | ||
|
|
||
| private var statusTask: Task<Void, Never>? | ||
|
|
||
| init( | ||
| tierToDisplay: MembershipTier, | ||
| onTap: @escaping () -> Void | ||
|
|
@@ -22,9 +23,18 @@ final class MembershipTierViewModel: ObservableObject { | |
| self.onTap = onTap | ||
|
|
||
| let storage = Container.shared.membershipStatusStorage.resolve() | ||
| storage.statusPublisher.receiveOnMain().assign(to: &$userMembership) | ||
| statusTask = Task { [weak self] in | ||
| guard let self else { return } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for await status in storage.statusStream() { | ||
| self.userMembership = status | ||
| } | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| statusTask?.cancel() | ||
| } | ||
|
|
||
| func updateState() { | ||
| Task { | ||
| state = await tierMetadataProvider.owningState(tier: tierToDisplay) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Retain cycle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use view.task.
Look example