Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit af67507

Browse files
committed
Completing consignment importing
1 parent adc1568 commit af67507

File tree

10 files changed

+162
-73
lines changed

10 files changed

+162
-73
lines changed

Shared/AppView.swift

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88
import SwiftUI
99
import CitadelKit
1010

11-
enum Sheet {
12-
case addAccount
13-
case addKeyring
14-
case importAsset
15-
case importAnything
16-
}
17-
1811
extension View {
1912
func conditional(closure: (Self) -> AnyView) -> AnyView {
2013
return closure(self)
@@ -37,9 +30,9 @@ struct AppView: View {
3730
@State private var invoice: Invoice? = nil
3831
@State private var selection: String? = nil
3932
@State private var showingSheet = false
40-
@State private var activeSheet = Sheet.addAccount
4133
@State private var errorSheet = ErrorSheetConfig()
4234
@State private var scannedString: String = ""
35+
@State private var presentedSheet: PresentedSheet? = nil
4336

4437
var body: some View {
4538
List(selection: isEditing ? nil : $selection) {
@@ -173,20 +166,18 @@ struct AppView: View {
173166
}
174167
#endif
175168
})
176-
.sheet(isPresented: $showingSheet, onDismiss: reloadData, content: sheetContent)
169+
.sheet(item: $presentedSheet, onDismiss: reloadData) { sheet in
170+
switch sheet {
171+
case .addAccount: SelectContract()
172+
case .addKeyring: AddKeyringSheet()
173+
case .scan(let name, let category):
174+
Import(importName: name, category: category, invoice: $invoice, dataString: $scannedString, presentedSheet: $presentedSheet)
175+
default: let _ = ""
176+
}
177+
}
177178
.alert(isPresented: $errorSheet.presented, content: errorSheet.content)
178179
.onAppear(perform: reloadData)
179180
}
180-
181-
@ViewBuilder
182-
private func sheetContent() -> some View {
183-
switch activeSheet {
184-
case .addAccount: SelectContract()
185-
case .addKeyring: AddKeyringSheet()
186-
case .importAsset: Import(importName: "asset", category: .genesis, invoice: $invoice, dataString: $scannedString)
187-
case .importAnything: Import(importName: "anything", category: .all, invoice: $invoice, dataString: $scannedString)
188-
}
189-
}
190181

191182
private func reloadData() {
192183
do {
@@ -210,22 +201,22 @@ struct AppView: View {
210201
}
211202

212203
private func createWallet() {
213-
activeSheet = .addAccount
204+
presentedSheet = .addAccount
214205
showingSheet = true
215206
}
216207

217208
private func createKeyring() {
218-
activeSheet = .addKeyring
209+
presentedSheet = .addKeyring
219210
showingSheet = true
220211
}
221212

222213
private func importAsset() {
223-
activeSheet = .importAsset
214+
presentedSheet = .scan("asset", .genesis)
224215
showingSheet = true
225216
}
226217

227218
private func importAnything() {
228-
activeSheet = .importAnything
219+
presentedSheet = .scan("anything", .all)
229220
showingSheet = true
230221
}
231222

Shared/Asset/AssetsView.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ struct AssetsView: View {
1717
@State private var scannedString: String = ""
1818

1919
private var assetList: AssetList {
20-
AssetList(showingSheet: $showingImportSheet, errorSheet: $errorSheet)
20+
AssetList(presentedSheet: $presentedSheet, errorSheet: $errorSheet)
2121
}
2222

23-
@State private var showingImportSheet: Bool = false
23+
@State private var presentedSheet: PresentedSheet?
2424
@State private var errorSheet = ErrorSheetConfig()
2525

2626
#if os(iOS)
@@ -93,9 +93,13 @@ struct AssetsView: View {
9393
#endif
9494
}
9595
.alert(isPresented: $errorSheet.presented, content: errorSheet.content)
96-
.sheet(isPresented: $showingImportSheet) {
97-
Import(importName: "asset", category: .genesis, invoice: $invoice, dataString: $scannedString)
98-
.onDisappear(perform: self.reloadAssets)
96+
.sheet(item: $presentedSheet) {sheet in
97+
switch sheet {
98+
case .scan(let importName, let category):
99+
Import(importName: importName, category: category, invoice: $invoice, dataString: $scannedString, presentedSheet: $presentedSheet)
100+
.onDisappear(perform: self.reloadAssets)
101+
default: let _ = ""
102+
}
99103
}
100104
}
101105

@@ -104,7 +108,7 @@ struct AssetsView: View {
104108
}
105109

106110
private func importAsset() {
107-
showingImportSheet = true
111+
presentedSheet = .scan("asset", .genesis)
108112
}
109113
}
110114

@@ -122,7 +126,7 @@ struct AssetList: View {
122126

123127
@StateObject private var citadel = CitadelVault.embedded
124128

125-
@Binding var showingSheet: Bool
129+
@Binding var presentedSheet: PresentedSheet?
126130
@Binding var errorSheet: ErrorSheetConfig
127131

128132
var body: some View {
@@ -156,7 +160,7 @@ struct AssetList: View {
156160
}
157161

158162
public func importAsset() {
159-
showingSheet = true
163+
presentedSheet = nil
160164
}
161165

162166
private func deleteAsset(indexSet: IndexSet) {

Shared/Components/DisplayExtensions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,27 @@ extension WitnessVersion {
344344
}
345345
}
346346
}
347+
348+
extension ValidationStatus {
349+
var localizedName: String {
350+
switch self.validity {
351+
case .valid: return "Success"
352+
case .unresolvedTransactions: return "Danger"
353+
case .invalid: return "Failure"
354+
}
355+
}
356+
var systemImage: String {
357+
switch self.validity {
358+
case .valid: return "checkmark.shield.fill"
359+
case .unresolvedTransactions: return "exclamationmark.triangle.fill"
360+
case .invalid: return "xmark.octagon.fill"
361+
}
362+
}
363+
var color: Color {
364+
switch self.validity {
365+
case .valid: return .green
366+
case .unresolvedTransactions: return .yellow
367+
case .invalid: return .red
368+
}
369+
}
370+
}

Shared/Components/SheetConfig.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum PresentedSheet {
1313
case scan(String, Import.Category)
1414
case pay(WalletContract, Invoice)
1515
case walletDetails(WalletContract)
16+
case addAccount
17+
case addKeyring
1618
}
1719

1820
extension PresentedSheet: Identifiable {
@@ -22,6 +24,8 @@ extension PresentedSheet: Identifiable {
2224
case .scan(_, _): return 1
2325
case .pay(_, _): return 2
2426
case .walletDetails(_): return 3
27+
case .addAccount: return 4
28+
case .addKeyring: return 5
2529
}
2630
}
2731
}

Shared/Create/CurrentContract.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ struct CurrentContract: View {
173173
.disabled(scripting != .publicKey || name.isEmpty)
174174
}
175175

176+
/*
176177
ToolbarItem(placement: .cancellationAction) {
177178
Button(action: {
178179
self.presentationMode.wrappedValue.dismiss()
179180
}) {
180181
Text("Cancel")
181182
}
182-
}
183+
}*/
183184
}
184185
.alert(isPresented: $errorSheet.presented, content: errorSheet.content)
185186
}

Shared/Import/ConsignmentView.swift

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,65 @@
88
import SwiftUI
99
import CitadelKit
1010

11+
extension ConsignmentView.AcceptanceStatus {
12+
var localizedName: String {
13+
switch self {
14+
case .validationStatus(let status): return status.localizedName
15+
case .error(_): return "Unable to process"
16+
}
17+
}
18+
var systemImage: String {
19+
switch self {
20+
case .validationStatus(let status): return status.systemImage
21+
case .error(_): return "clear.fill"
22+
}
23+
}
24+
var color: Color {
25+
switch self {
26+
case .validationStatus(let status): return status.color
27+
case .error(_): return Color.secondary
28+
}
29+
}
30+
var failures: [String] {
31+
switch self {
32+
case .validationStatus(let status): return status.failures
33+
case .error(let err): return [err]
34+
}
35+
}
36+
var warnings: [String] {
37+
switch self {
38+
case .validationStatus(let status): return status.warnings
39+
case .error(_): return []
40+
}
41+
}
42+
var info: [String] {
43+
switch self {
44+
case .validationStatus(let status): return status.info
45+
case .error(_): return []
46+
}
47+
}
48+
}
49+
1150
struct ConsignmentView: View {
1251
@Environment(\.presentationMode) var presentationMode
1352

1453
var consignment: String
1554

16-
private enum AcceptanceStatus {
17-
case message(String)
55+
fileprivate enum AcceptanceStatus {
56+
case validationStatus(ValidationStatus)
1857
case error(String)
1958
}
2059
private var status: AcceptanceStatus
21-
22-
init(consignment: String) {
60+
61+
@Binding var presentedSheet: PresentedSheet?
62+
63+
init(consignment: String, presentedSheet: Binding<PresentedSheet?>) {
64+
self._presentedSheet = presentedSheet
2365
self.consignment = consignment
66+
2467
do {
25-
let message = try CitadelVault.embedded.contracts.first!.accept(consignment: consignment)
26-
status = .message(message)
68+
let validationStatus = try CitadelVault.embedded.contracts.first!.accept(consignment: consignment)
69+
status = .validationStatus(validationStatus)
2770
} catch let error where error is CitadelError {
2871
status = .error((error as! CitadelError).description)
2972
} catch {
@@ -33,37 +76,69 @@ struct ConsignmentView: View {
3376

3477
var body: some View {
3578
List {
36-
switch status {
37-
case .message(let message):
38-
Text(message)
39-
case .error(let message):
40-
Section(header:
41-
Text("Error accepting payment:")
42-
.padding(.top)
43-
) {
44-
Text(message).font(.subheadline).foregroundColor(.red).italic()
79+
resultView
80+
81+
if !status.failures.isEmpty {
82+
Section(header: Text("Failures:")) {
83+
ForEach(status.failures, id: \.self) { message in
84+
Text(message)
85+
.foregroundColor(.red)
86+
}
87+
}
88+
}
89+
90+
if !status.warnings.isEmpty {
91+
Section(header: Text("Warnings:")) {
92+
ForEach(status.warnings, id: \.self) { message in
93+
Text(message)
94+
}
95+
}
96+
}
97+
98+
if !status.info.isEmpty {
99+
Section(header: Text("Additional info:")) {
100+
ForEach(status.info, id: \.self) { message in
101+
Text(message)
102+
}
45103
}
46104
}
47105
}
48-
.listStyle(GroupedListStyle())
49-
.navigationTitle("Incoming payment")
50-
/*.toolbar {
106+
.navigationTitle("Accepting payment")
107+
.toolbar {
51108
ToolbarItem(placement: .confirmationAction) {
52109
Button("Dismiss", action: dissmiss)
53110
}
54-
}*/
111+
}
112+
}
113+
114+
var resultView: some View {
115+
HStack {
116+
Spacer()
117+
VStack(alignment: .center) {
118+
Image(systemName: status.systemImage)
119+
.resizable()
120+
.scaledToFit()
121+
.frame(width: 200, height: 200, alignment: .center)
122+
.opacity(0.666)
123+
.foregroundColor(status.color)
124+
Text(status.localizedName)
125+
.font(.largeTitle)
126+
.padding(.top)
127+
}
128+
Spacer()
129+
}.padding([.top, .bottom], 50)
55130
}
56131

57132
private func dissmiss() {
58-
presentationMode.wrappedValue.dismiss()
59-
presentationMode.wrappedValue.dismiss()
133+
presentedSheet = nil
60134
}
61135
}
62136

63137
struct ConsignmentView_Previews: PreviewProvider {
138+
@State static private var presentedSheet: PresentedSheet? = nil
64139
static var previews: some View {
65140
NavigationView {
66-
ConsignmentView(consignment: "")
141+
ConsignmentView(consignment: "", presentedSheet: $presentedSheet)
67142
}
68143
}
69144
}

0 commit comments

Comments
 (0)