From f87c132fd00203811d852ba396044f36f79758de Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Wed, 8 Jun 2022 14:01:22 +0200 Subject: [PATCH] Implement MyIdentityView --- .../MyIdentityFeature/MyIdentityView.swift | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/Example/example-app/Sources/MyIdentityFeature/MyIdentityView.swift b/Example/example-app/Sources/MyIdentityFeature/MyIdentityView.swift index 62cea2bb..61e09c13 100644 --- a/Example/example-app/Sources/MyIdentityFeature/MyIdentityView.swift +++ b/Example/example-app/Sources/MyIdentityFeature/MyIdentityView.swift @@ -1,4 +1,7 @@ import ComposableArchitecture +import ComposablePresentation +import ElixxirDAppsSDK +import ErrorFeature import SwiftUI public struct MyIdentityView: View { @@ -9,15 +12,71 @@ public struct MyIdentityView: View { let store: Store<MyIdentityState, MyIdentityAction> struct ViewState: Equatable { - init(state: MyIdentityState) {} + let identity: Identity? + let isMakingIdentity: Bool + + init(state: MyIdentityState) { + identity = state.identity + isMakingIdentity = state.isMakingIdentity + } + + var isLoading: Bool { + isMakingIdentity + } } public var body: some View { WithViewStore(store.scope(state: ViewState.init)) { viewStore in - Text("MyIdentityView") - .task { - viewStore.send(.viewDidLoad) + Form { + Section { + Text(string(for: viewStore.identity)) + .textSelection(.enabled) + } + + Section { + Button { + viewStore.send(.makeIdentity) + } label: { + HStack { + Text("Make new identity") + Spacer() + if viewStore.isMakingIdentity { + ProgressView() + } + } + } } + .disabled(viewStore.isLoading) + } + .navigationTitle("My identity") + .navigationBarBackButtonHidden(viewStore.isLoading) + .task { + viewStore.send(.viewDidLoad) + } + .sheet( + store.scope( + state: \.error, + action: MyIdentityAction.error + ), + onDismiss: { + viewStore.send(.didDismissError) + }, + content: ErrorView.init(store:) + ) + } + } + + func string(for identity: Identity?) -> String { + guard let identity = identity else { + return "No identity" + } + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + do { + let data = try encoder.encode(identity) + return String(data: data, encoding: .utf8) ?? "Decoding error" + } catch { + return "Decoding error: \(error)" } } } -- GitLab