-
Notifications
You must be signed in to change notification settings - Fork 9
/
DappInteractionLoading.swift
174 lines (154 loc) · 4.89 KB
/
DappInteractionLoading.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import ComposableArchitecture
import SwiftUI
// MARK: - DappInteractionLoading
struct DappInteractionLoading: Sendable, FeatureReducer {
struct State: Sendable, Hashable {
let interaction: DappToWalletInteraction
var isLoading: Bool = false
@PresentationState
var errorAlert: AlertState<ViewAction.ErrorAlertAction>?
init(interaction: DappToWalletInteraction) {
self.interaction = interaction
}
}
enum ViewAction: Sendable, Equatable {
case appeared
case errorAlert(PresentationAction<ErrorAlertAction>)
case dismissButtonTapped
enum ErrorAlertAction: Sendable, Equatable {
case retryButtonTapped
case cancelButtonTapped
}
}
enum InternalAction: Sendable, Equatable {
case dappMetadataLoadingResult(TaskResult<DappMetadata>)
}
enum DelegateAction: Sendable, Equatable {
case dappMetadataLoaded(DappMetadata)
case dismiss
}
@Dependency(\.gatewayAPIClient) var gatewayAPIClient
@Dependency(\.cacheClient) var cacheClient
@Dependency(\.appPreferencesClient) var appPreferencesClient
var body: some ReducerOf<Self> {
Reduce(core)
.ifLet(\.$errorAlert, action: /Action.view .. ViewAction.errorAlert)
}
func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .appeared:
metadataLoadingEffect(with: &state)
case let .errorAlert(.presented(action)):
switch action {
case .retryButtonTapped:
metadataLoadingEffect(with: &state)
case .cancelButtonTapped:
.send(.delegate(.dismiss))
}
case .errorAlert:
.none
case .dismissButtonTapped:
.send(.delegate(.dismiss))
}
}
func metadataLoadingEffect(with state: inout State) -> Effect<Action> {
state.isLoading = true
if state.interaction.metadata.origin == DappOrigin.wallet {
return .send(.internal(.dappMetadataLoadingResult(.success(.wallet(.init())))))
}
return .run { [request = state.interaction.metadata] send in
let result: TaskResult<DappMetadata> = await {
let isDeveloperModeEnabled = await appPreferencesClient.getPreferences().security.isDeveloperModeEnabled
let dappDefinitionAddress = request.dappDefinitionAddress
do {
let cachedMetadata = try await cacheClient.withCaching(
cacheEntry: .dAppRequestMetadata(dappDefinitionAddress.address),
invalidateCached: { (cached: DappMetadata.Ledger) in
guard
cached.name != nil,
cached.description != nil,
cached.thumbnail != nil
else {
/// Some of these fields were not set, fetch and see if they
/// have been updated since last time...
return .cachedIsInvalid
}
// All relevant fields are set, the cached metadata is valid.
return .cachedIsValid
},
request: {
let entityMetadataForDapp = try await gatewayAPIClient.getEntityMetadata(dappDefinitionAddress.address, .dappMetadataKeys)
return DappMetadata.Ledger(
entityMetadataForDapp: entityMetadataForDapp,
dAppDefinintionAddress: dappDefinitionAddress,
origin: request.origin
)
}
)
return .success(.ledger(cachedMetadata))
} catch {
guard isDeveloperModeEnabled else {
return .failure(error)
}
loggerGlobal.warning("Failed to fetch Dapps metadata, but since 'isDeveloperModeEnabled' is enabled we surpress the error and allow continuation. Error: \(error)")
return .success(.request(request))
}
}()
await send(.internal(.dappMetadataLoadingResult(result)))
}
}
func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .dappMetadataLoadingResult(.success(dappMetadata)):
state.isLoading = false
return .send(.delegate(.dappMetadataLoaded(dappMetadata)))
case let .dappMetadataLoadingResult(.failure(error)):
state.errorAlert = .init(
title: { TextState(L10n.Common.errorAlertTitle) },
actions: {
ButtonState(action: .send(.retryButtonTapped)) {
TextState(L10n.Common.retry)
}
ButtonState(role: .cancel, action: .send(.cancelButtonTapped)) {
TextState(L10n.Common.cancel)
}
},
message: {
TextState(
L10n.DAppRequest.MetadataLoadingAlert.message + {
#if DEBUG
"\n\n" + error.legibleLocalizedDescription
#else
""
#endif
}()
)
}
)
return .none
}
}
}
extension DappMetadata.Ledger {
init(
entityMetadataForDapp: GatewayAPI.EntityMetadataCollection,
dAppDefinintionAddress: AccountAddress,
origin: DappOrigin
) {
let items = entityMetadataForDapp.items
let maybeName: String? = items[.name]?.value.asString
let name: NonEmptyString? = {
guard let name = maybeName else {
return nil
}
return NonEmptyString(rawValue: name)
}()
self.init(
origin: origin,
dAppDefinintionAddress: dAppDefinintionAddress,
name: name,
description: items[.description]?.value.asString,
thumbnail: items[.iconURL]?.value.asURL
)
}
}