Skip to content

Commit ff3e70e

Browse files
authored
ui(send): use navigation stack instead of navigation view
1 parent 92ae5f6 commit ff3e70e

File tree

5 files changed

+77
-48
lines changed

5 files changed

+77
-48
lines changed

BDKSwiftExampleWallet/View/AddressView.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUI
1212
struct AddressView: View {
1313
let amount: String
1414
@State var address: String = ""
15-
@Binding var rootIsActive: Bool
15+
@Binding var navigationPath: NavigationPath
1616
let pasteboard = UIPasteboard.general
1717
@State private var isShowingScanner = false
1818
@State private var isShowingAlert = false
@@ -99,22 +99,17 @@ struct AddressView: View {
9999

100100
Spacer()
101101

102-
NavigationLink(
103-
destination:
104-
FeeView(
105-
amount: amount,
106-
address: address,
107-
viewModel: .init(),
108-
rootIsActive: self.$rootIsActive
109-
)
110-
) {
102+
Button {
103+
navigationPath.append(
104+
NavigationDestination.fee(amount: amount, address: address)
105+
)
106+
} label: {
111107
Label(
112108
title: { Text("Next") },
113109
icon: { Image(systemName: "arrow.right") }
114110
)
115111
.labelStyle(.iconOnly)
116112
}
117-
.isDetailLink(false)
118113
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))
119114

120115
}
@@ -155,14 +150,14 @@ extension AddressView {
155150
AddressView(
156151
amount: "200",
157152
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
158-
rootIsActive: .constant(false)
153+
navigationPath: .constant(NavigationPath())
159154
)
160155
}
161156
#Preview {
162157
AddressView(
163158
amount: "200",
164159
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
165-
rootIsActive: .constant(false)
160+
navigationPath: .constant(NavigationPath())
166161
)
167162
.environment(\.dynamicTypeSize, .accessibility5)
168163
}

BDKSwiftExampleWallet/View/AmountView.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111
struct AmountView: View {
1212
@Bindable var viewModel: AmountViewModel
1313
@State var numpadAmount = "0"
14-
@State var isActive: Bool = false
14+
@Binding var navigationPath: NavigationPath
1515

1616
var body: some View {
1717

@@ -60,8 +60,11 @@ struct AmountView: View {
6060
Spacer()
6161

6262
VStack {
63+
6364
Button {
64-
isActive = true
65+
navigationPath.append(
66+
NavigationDestination.address(amount: numpadAmount)
67+
)
6568
} label: {
6669
Label(
6770
title: { Text("Next") },
@@ -70,13 +73,7 @@ struct AmountView: View {
7073
.labelStyle(.iconOnly)
7174
}
7275
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))
73-
NavigationLink(
74-
destination: AddressView(amount: numpadAmount, rootIsActive: $isActive),
75-
isActive: $isActive
76-
) {
77-
EmptyView()
78-
}
79-
.hidden()
76+
8077
}
8178

8279
}
@@ -85,8 +82,8 @@ struct AmountView: View {
8582
viewModel.getBalance()
8683
}
8784
}
88-
.onChange(of: isActive) {
89-
if !isActive {
85+
.onChange(of: navigationPath) { oldPath, newPath in
86+
if newPath.isEmpty {
9087
numpadAmount = "0"
9188
}
9289
}
@@ -145,11 +142,18 @@ struct NumpadButton: View {
145142

146143
#if DEBUG
147144
#Preview {
148-
AmountView(viewModel: .init(bdkClient: .mock))
145+
AmountView(
146+
viewModel: .init(bdkClient: .mock),
147+
navigationPath: .constant(NavigationPath())
148+
)
149149
}
150150

151151
#Preview {
152-
AmountView(viewModel: .init(bdkClient: .mock))
153-
.environment(\.dynamicTypeSize, .accessibility5)
152+
AmountView(
153+
viewModel: .init(bdkClient: .mock),
154+
navigationPath: .constant(NavigationPath())
155+
156+
)
157+
.environment(\.dynamicTypeSize, .accessibility5)
154158
}
155159
#endif

BDKSwiftExampleWallet/View/BuildTransactionView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct BuildTransactionView: View {
1616
@Bindable var viewModel: BuildTransactionViewModel
1717
@State var isSent: Bool = false
1818
@State var isError: Bool = false
19-
@Binding var shouldPopToRootView: Bool
19+
@Binding var navigationPath: NavigationPath
2020
@State private var isCopied = false
2121
@State private var showCheckmark = false
2222

@@ -86,7 +86,7 @@ struct BuildTransactionView: View {
8686
if self.viewModel.buildTransactionViewError == nil {
8787
self.isSent = true
8888
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
89-
self.shouldPopToRootView = false
89+
self.navigationPath.removeLast(self.navigationPath.count)
9090
}
9191
} else {
9292
self.isSent = false
@@ -186,7 +186,7 @@ struct BuildTransactionView: View {
186186
viewModel: .init(
187187
bdkClient: .mock
188188
),
189-
shouldPopToRootView: .constant(false)
189+
navigationPath: .constant(NavigationPath())
190190
)
191191
}
192192

@@ -198,7 +198,7 @@ struct BuildTransactionView: View {
198198
viewModel: .init(
199199
bdkClient: .mock
200200
),
201-
shouldPopToRootView: .constant(false)
201+
navigationPath: .constant(NavigationPath())
202202
)
203203
.environment(\.dynamicTypeSize, .accessibility5)
204204
}

BDKSwiftExampleWallet/View/FeeView.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct FeeView: View {
1313
let amount: String
1414
let address: String
1515
@Bindable var viewModel: FeeViewModel
16-
@Binding var rootIsActive: Bool
16+
@Binding var navigationPath: NavigationPath
1717

1818
var body: some View {
1919

@@ -66,23 +66,21 @@ struct FeeView: View {
6666

6767
Spacer()
6868

69-
NavigationLink(
70-
destination: BuildTransactionView(
71-
amount: amount,
72-
address: address,
73-
fee: viewModel.selectedFee ?? 1,
74-
viewModel: .init(),
75-
shouldPopToRootView: self.$rootIsActive
76-
69+
Button {
70+
navigationPath.append(
71+
NavigationDestination.buildTransaction(
72+
amount: amount,
73+
address: address,
74+
fee: viewModel.selectedFee ?? 1
75+
)
7776
)
78-
) {
77+
} label: {
7978
Label(
8079
title: { Text("Next") },
8180
icon: { Image(systemName: "arrow.right") }
8281
)
8382
.labelStyle(.iconOnly)
8483
}
85-
.isDetailLink(false)
8684
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))
8785

8886
}
@@ -113,7 +111,7 @@ struct FeeView: View {
113111
amount: "50",
114112
address: "tb1pxg0lakl0x4jee73f38m334qsma7mn2yv764x9an5ylht6tx8ccdsxtktrt",
115113
viewModel: .init(feeClient: .mock, bdkClient: .mock),
116-
rootIsActive: .constant(false)
114+
navigationPath: .constant(NavigationPath())
117115
)
118116
}
119117

@@ -122,7 +120,7 @@ struct FeeView: View {
122120
amount: "50",
123121
address: "tb1pxg0lakl0x4jee73f38m334qsma7mn2yv764x9an5ylht6tx8ccdsxtktrt",
124122
viewModel: .init(feeClient: .mock, bdkClient: .mock),
125-
rootIsActive: .constant(false)
123+
navigationPath: .constant(NavigationPath())
126124
)
127125
.environment(\.dynamicTypeSize, .accessibility5)
128126
}

BDKSwiftExampleWallet/View/TabHomeView.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import SwiftUI
99

1010
struct TabHomeView: View {
1111
@Bindable var viewModel: TabHomeViewModel
12+
@State private var sendNavigationPath = NavigationPath()
1213

1314
var body: some View {
1415

@@ -29,10 +30,35 @@ struct TabHomeView: View {
2930
.tabItem {
3031
Image(systemName: "arrow.down")
3132
}
32-
AmountView(viewModel: .init())
33-
.tabItem {
34-
Image(systemName: "arrow.up")
35-
}
33+
34+
NavigationStack(path: $sendNavigationPath) {
35+
AmountView(viewModel: .init(), navigationPath: $sendNavigationPath)
36+
.navigationDestination(for: NavigationDestination.self) { destination in
37+
switch destination {
38+
case .address(let amount):
39+
AddressView(amount: amount, navigationPath: $sendNavigationPath)
40+
case .fee(let amount, let address):
41+
FeeView(
42+
amount: amount,
43+
address: address,
44+
viewModel: .init(),
45+
navigationPath: $sendNavigationPath
46+
)
47+
case .buildTransaction(let amount, let address, let fee):
48+
BuildTransactionView(
49+
amount: amount,
50+
address: address,
51+
fee: fee,
52+
viewModel: .init(),
53+
navigationPath: $sendNavigationPath
54+
)
55+
}
56+
}
57+
}
58+
.tabItem {
59+
Image(systemName: "arrow.up")
60+
}
61+
3662
SettingsView(viewModel: .init())
3763
.tabItem {
3864
Image(systemName: "gear")
@@ -58,6 +84,12 @@ struct TabHomeView: View {
5884

5985
}
6086

87+
enum NavigationDestination: Hashable {
88+
case address(amount: String)
89+
case fee(amount: String, address: String)
90+
case buildTransaction(amount: String, address: String, fee: Int)
91+
}
92+
6193
#if DEBUG
6294
#Preview {
6395
TabHomeView(viewModel: .init(bdkClient: .mock))

0 commit comments

Comments
 (0)