Skip to content

ui(send): use navigation stack instead of navigation view #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions BDKSwiftExampleWallet/View/AddressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftUI
struct AddressView: View {
let amount: String
@State var address: String = ""
@Binding var rootIsActive: Bool
@Binding var navigationPath: NavigationPath
let pasteboard = UIPasteboard.general
@State private var isShowingScanner = false
@State private var isShowingAlert = false
Expand Down Expand Up @@ -99,22 +99,17 @@ struct AddressView: View {

Spacer()

NavigationLink(
destination:
FeeView(
amount: amount,
address: address,
viewModel: .init(),
rootIsActive: self.$rootIsActive
)
) {
Button {
navigationPath.append(
NavigationDestination.fee(amount: amount, address: address)
)
} label: {
Label(
title: { Text("Next") },
icon: { Image(systemName: "arrow.right") }
)
.labelStyle(.iconOnly)
}
.isDetailLink(false)
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))

}
Expand Down Expand Up @@ -155,14 +150,14 @@ extension AddressView {
AddressView(
amount: "200",
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
rootIsActive: .constant(false)
navigationPath: .constant(NavigationPath())
)
}
#Preview {
AddressView(
amount: "200",
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
rootIsActive: .constant(false)
navigationPath: .constant(NavigationPath())
)
.environment(\.dynamicTypeSize, .accessibility5)
}
Expand Down
32 changes: 18 additions & 14 deletions BDKSwiftExampleWallet/View/AmountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct AmountView: View {
@Bindable var viewModel: AmountViewModel
@State var numpadAmount = "0"
@State var isActive: Bool = false
@Binding var navigationPath: NavigationPath

var body: some View {

Expand Down Expand Up @@ -60,8 +60,11 @@ struct AmountView: View {
Spacer()

VStack {

Button {
isActive = true
navigationPath.append(
NavigationDestination.address(amount: numpadAmount)
)
} label: {
Label(
title: { Text("Next") },
Expand All @@ -70,13 +73,7 @@ struct AmountView: View {
.labelStyle(.iconOnly)
}
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))
NavigationLink(
destination: AddressView(amount: numpadAmount, rootIsActive: $isActive),
isActive: $isActive
) {
EmptyView()
}
.hidden()

}

}
Expand All @@ -85,8 +82,8 @@ struct AmountView: View {
viewModel.getBalance()
}
}
.onChange(of: isActive) {
if !isActive {
.onChange(of: navigationPath) { oldPath, newPath in
if newPath.isEmpty {
numpadAmount = "0"
}
}
Expand Down Expand Up @@ -145,11 +142,18 @@ struct NumpadButton: View {

#if DEBUG
#Preview {
AmountView(viewModel: .init(bdkClient: .mock))
AmountView(
viewModel: .init(bdkClient: .mock),
navigationPath: .constant(NavigationPath())
)
}

#Preview {
AmountView(viewModel: .init(bdkClient: .mock))
.environment(\.dynamicTypeSize, .accessibility5)
AmountView(
viewModel: .init(bdkClient: .mock),
navigationPath: .constant(NavigationPath())

)
.environment(\.dynamicTypeSize, .accessibility5)
}
#endif
8 changes: 4 additions & 4 deletions BDKSwiftExampleWallet/View/BuildTransactionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct BuildTransactionView: View {
@Bindable var viewModel: BuildTransactionViewModel
@State var isSent: Bool = false
@State var isError: Bool = false
@Binding var shouldPopToRootView: Bool
@Binding var navigationPath: NavigationPath
@State private var isCopied = false
@State private var showCheckmark = false

Expand Down Expand Up @@ -86,7 +86,7 @@ struct BuildTransactionView: View {
if self.viewModel.buildTransactionViewError == nil {
self.isSent = true
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.shouldPopToRootView = false
self.navigationPath.removeLast(self.navigationPath.count)
}
} else {
self.isSent = false
Expand Down Expand Up @@ -186,7 +186,7 @@ struct BuildTransactionView: View {
viewModel: .init(
bdkClient: .mock
),
shouldPopToRootView: .constant(false)
navigationPath: .constant(NavigationPath())
)
}

Expand All @@ -198,7 +198,7 @@ struct BuildTransactionView: View {
viewModel: .init(
bdkClient: .mock
),
shouldPopToRootView: .constant(false)
navigationPath: .constant(NavigationPath())
)
.environment(\.dynamicTypeSize, .accessibility5)
}
Expand Down
24 changes: 11 additions & 13 deletions BDKSwiftExampleWallet/View/FeeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct FeeView: View {
let amount: String
let address: String
@Bindable var viewModel: FeeViewModel
@Binding var rootIsActive: Bool
@Binding var navigationPath: NavigationPath

var body: some View {

Expand Down Expand Up @@ -66,23 +66,21 @@ struct FeeView: View {

Spacer()

NavigationLink(
destination: BuildTransactionView(
amount: amount,
address: address,
fee: viewModel.selectedFee ?? 1,
viewModel: .init(),
shouldPopToRootView: self.$rootIsActive

Button {
navigationPath.append(
NavigationDestination.buildTransaction(
amount: amount,
address: address,
fee: viewModel.selectedFee ?? 1
)
)
) {
} label: {
Label(
title: { Text("Next") },
icon: { Image(systemName: "arrow.right") }
)
.labelStyle(.iconOnly)
}
.isDetailLink(false)
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))

}
Expand Down Expand Up @@ -113,7 +111,7 @@ struct FeeView: View {
amount: "50",
address: "tb1pxg0lakl0x4jee73f38m334qsma7mn2yv764x9an5ylht6tx8ccdsxtktrt",
viewModel: .init(feeClient: .mock, bdkClient: .mock),
rootIsActive: .constant(false)
navigationPath: .constant(NavigationPath())
)
}

Expand All @@ -122,7 +120,7 @@ struct FeeView: View {
amount: "50",
address: "tb1pxg0lakl0x4jee73f38m334qsma7mn2yv764x9an5ylht6tx8ccdsxtktrt",
viewModel: .init(feeClient: .mock, bdkClient: .mock),
rootIsActive: .constant(false)
navigationPath: .constant(NavigationPath())
)
.environment(\.dynamicTypeSize, .accessibility5)
}
Expand Down
40 changes: 36 additions & 4 deletions BDKSwiftExampleWallet/View/TabHomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct TabHomeView: View {
@Bindable var viewModel: TabHomeViewModel
@State private var sendNavigationPath = NavigationPath()

var body: some View {

Expand All @@ -29,10 +30,35 @@ struct TabHomeView: View {
.tabItem {
Image(systemName: "arrow.down")
}
AmountView(viewModel: .init())
.tabItem {
Image(systemName: "arrow.up")
}

NavigationStack(path: $sendNavigationPath) {
AmountView(viewModel: .init(), navigationPath: $sendNavigationPath)
.navigationDestination(for: NavigationDestination.self) { destination in
switch destination {
case .address(let amount):
AddressView(amount: amount, navigationPath: $sendNavigationPath)
case .fee(let amount, let address):
FeeView(
amount: amount,
address: address,
viewModel: .init(),
navigationPath: $sendNavigationPath
)
case .buildTransaction(let amount, let address, let fee):
BuildTransactionView(
amount: amount,
address: address,
fee: fee,
viewModel: .init(),
navigationPath: $sendNavigationPath
)
}
}
}
.tabItem {
Image(systemName: "arrow.up")
}

SettingsView(viewModel: .init())
.tabItem {
Image(systemName: "gear")
Expand All @@ -58,6 +84,12 @@ struct TabHomeView: View {

}

enum NavigationDestination: Hashable {
case address(amount: String)
case fee(amount: String, address: String)
case buildTransaction(amount: String, address: String, fee: Int)
}

#if DEBUG
#Preview {
TabHomeView(viewModel: .init(bdkClient: .mock))
Expand Down
Loading