From b133540f7f940a29ad44b78b2580af679e4dad68 Mon Sep 17 00:00:00 2001 From: matiasbzurovski <164921079+matiasbzurovski@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:36:12 +0200 Subject: [PATCH] Small improvement to Personas loading (#1169) --- .../Child/List/PersonaList+Reducer.swift | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/RadixWallet/Features/DappsAndPersonas/Personas/Child/List/PersonaList+Reducer.swift b/RadixWallet/Features/DappsAndPersonas/Personas/Child/List/PersonaList+Reducer.swift index 30bad61a3c..f5f3b64b0c 100644 --- a/RadixWallet/Features/DappsAndPersonas/Personas/Child/List/PersonaList+Reducer.swift +++ b/RadixWallet/Features/DappsAndPersonas/Personas/Child/List/PersonaList+Reducer.swift @@ -50,7 +50,7 @@ public struct PersonaList: Sendable, FeatureReducer { } public enum InternalAction: Sendable, Equatable { - case personasLoaded(IdentifiedArrayOf) + case setPersonas([Persona]) case setSecurityProblems([SecurityProblem]) } @@ -80,27 +80,12 @@ public struct PersonaList: Sendable, FeatureReducer { case personasMissingFromClient(OrderedSet) } - /// Returns the ids of personas to include under the given strategy. nil means that all ids should be included - private func personaIDs(_ strategy: State.ReloadingStrategy) async throws -> OrderedSet? { - switch strategy { - case .all: - return nil - case let .ids(ids): - return ids - case let .dApp(dAppID): - guard let dApp = try? await authorizedDappsClient.getDetailedDapp(dAppID) else { return [] } - return OrderedSet(dApp.detailedAuthorizedPersonas.map(\.id)) - } - } - public func reduce(into state: inout State, internalAction: InternalAction) -> Effect { switch internalAction { - case var .personasLoaded(personas): - personas.mutateAll { persona in - persona.securityProblemsConfig.update(problems: state.problems) - } - + case let .setPersonas(personas): state.personas = personas + .map { PersonaFeature.State(persona: $0, problems: state.problems) } + .asIdentified() return .none case let .setSecurityProblems(problems): @@ -133,15 +118,15 @@ public struct PersonaList: Sendable, FeatureReducer { } private func personasEffect(state: State) -> Effect { - .run { [strategy = state.strategy, problems = state.problems] send in + .run { [strategy = state.strategy] send in for try await personas in await personasClient.personas() { guard !Task.isCancelled else { return } let ids = try await personaIDs(strategy) ?? OrderedSet(validating: personas.ids) - let result = ids.compactMap { personas[id: $0] }.map { PersonaFeature.State(persona: $0, problems: problems) } + let result = ids.compactMap { personas[id: $0] } guard result.count == ids.count else { throw UpdatePersonaError.personasMissingFromClient(ids.subtracting(result.map(\.id))) } - await send(.internal(.personasLoaded(result.asIdentified()))) + await send(.internal(.setPersonas(result))) } } catch: { error, _ in loggerGlobal.error("Failed to update personas from client, error: \(error)") @@ -157,4 +142,17 @@ public struct PersonaList: Sendable, FeatureReducer { } } } + + /// Returns the ids of personas to include under the given strategy. nil means that all ids should be included + private func personaIDs(_ strategy: State.ReloadingStrategy) async throws -> OrderedSet? { + switch strategy { + case .all: + return nil + case let .ids(ids): + return ids + case let .dApp(dAppID): + guard let dApp = try? await authorizedDappsClient.getDetailedDapp(dAppID) else { return [] } + return OrderedSet(dApp.detailedAuthorizedPersonas.map(\.id)) + } + } }