Skip to content
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

iOS-2456 Membership | Drop membership cache upon login #1087

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct MembershipCoordinator: View {
subtitle: Loc.Error.Common.message,
actionText: Loc.Error.Common.tryAgain
) {
model.loadTiers(noCache: false)
model.loadTiers()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class MembershipCoordinatorModel: ObservableObject {

init() {
membershipStatusStorage.status.assign(to: &$userMembership)
loadTiers(noCache: false)
loadTiers()
}

func onTierSelected(tier: MembershipTier) {
Expand All @@ -47,7 +47,7 @@ final class MembershipCoordinatorModel: ObservableObject {
func onSuccessfulValidation(data: EmailVerificationData) {
emailVerificationData = nil
showTier = nil
loadTiers(noCache: true)
loadTiers()

// https://linear.app/anytype/issue/IOS-2434/bottom-sheet-nesting
Task {
Expand All @@ -56,10 +56,10 @@ final class MembershipCoordinatorModel: ObservableObject {
}
}

func loadTiers(noCache: Bool) {
func loadTiers() {
Task {
do {
tiers = try await membershipService.getTiers(noCache: noCache)
tiers = try await membershipService.getTiers()
showTiersLoadingError = false
} catch {
showTiersLoadingError = true
Expand Down
4 changes: 4 additions & 0 deletions Anytype/Sources/ServiceLayer/Auth/LoginStateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class LoginStateService: LoginStateServiceProtocol {
private var middlewareConfigurationProvider: MiddlewareConfigurationProviderProtocol
@Injected(\.blockWidgetExpandedService)
private var blockWidgetExpandedService: BlockWidgetExpandedServiceProtocol
@Injected(\.membershipService)
private var membershipService: MembershipServiceProtocol
@Injected(\.relationDetailsStorage)
private var relationDetailsStorage: RelationDetailsStorageProtocol
@Injected(\.workspaceStorage)
Expand All @@ -37,6 +39,8 @@ final class LoginStateService: LoginStateServiceProtocol {

func setupStateAfterLoginOrAuth(account: AccountData) async {
middlewareConfigurationProvider.setupConfiguration(account: account)
try? await membershipService.dropTiersCache()

await startSubscriptions()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public protocol MembershipServiceProtocol {
func makeStatusFromMiddlewareModel(membership: MiddlewareMemberhsipStatus) async throws -> MembershipStatus

func getTiers(noCache: Bool) async throws -> [MembershipTier]
func dropTiersCache() async throws


func getVerificationEmail(data: EmailVerificationData) async throws
Expand All @@ -24,6 +25,12 @@ public protocol MembershipServiceProtocol {
func validateName(name: String, tierType: MembershipTierType) async throws
}

public extension MembershipServiceProtocol {
func getTiers() async throws -> [MembershipTier] {
try await getTiers(noCache: false)
}
}

final class MembershipService: MembershipServiceProtocol {

public func getStatus() async throws -> MembershipStatus {
Expand All @@ -32,19 +39,14 @@ final class MembershipService: MembershipServiceProtocol {
}

public func makeStatusFromMiddlewareModel(membership: MiddlewareMemberhsipStatus) async throws -> MembershipStatus {
let cachedTier = try await getTiers(noCache: false).first { $0.type.id == membership.tier }
let tier = try await getTiers().first { $0.type.id == membership.tier }

if let tier = cachedTier {
return convertMiddlewareMembership(membership: membership, tier: tier)
guard let tier else {
anytypeAssertionFailure("Not found tier info for \(membership)")
throw MembershipServiceError.tierNotFound
}

let middlewareTier = try await getTiers(noCache: true).first { $0.type.id == membership.tier }
if let tier = middlewareTier {
return convertMiddlewareMembership(membership: membership, tier: tier)
}

anytypeAssertionFailure("Not found tier info for \(membership)")
throw MembershipServiceError.tierNotFound
return convertMiddlewareMembership(membership: membership, tier: tier)
}

public func getTiers(noCache: Bool) async throws -> [MembershipTier] {
Expand All @@ -55,6 +57,10 @@ final class MembershipService: MembershipServiceProtocol {
.invoke().tiers.filter { !$0.isTest }.compactMap { $0.asModel() }
}

func dropTiersCache() async throws {
_ = try await getTiers(noCache: true)
}

public func getVerificationEmail(data: EmailVerificationData) async throws {
try await ClientCommands.membershipGetVerificationEmail(.with {
$0.email = data.email
Expand Down
Loading