Fixed showing previous selected tab after choosing an option from 'New ...' ActionSheet

This commit is contained in:
dankito 2020-07-29 20:49:24 +02:00
parent 1f66bdfc63
commit 20cd16e509
3 changed files with 91 additions and 20 deletions

View File

@ -4,33 +4,48 @@ import BankingUiSwift
struct ContentView: View { struct ContentView: View {
static private let OverlayTabIndex = 1
@ObservedObject var data: AppData = AppData() @ObservedObject var data: AppData = AppData()
@State private var selection = 0 @State private var previousSelectedTab: Int = 0
@State private var selectedTab = 0
private var selectedTabBinding: Binding<Int> {
Binding<Int>(
get: { self.selectedTab },
set: {
if $0 == Self.OverlayTabIndex {
self.previousSelectedTab = self.selectedTab
self.showNewOptionsActionSheet = true
}
self.selectedTab = $0
})
}
@State private var navigationBarTitle = "" @State private var navigationBarTitle = ""
@State private var leadingNavigationBarItem: AnyView? = nil @State private var leadingNavigationBarItem: AnyView? = nil
@State private var showTransferMoneyOptionsActionSheet = false @State private var showNewOptionsActionSheet = false
@State private var selectedTransferMoneyOption: Int? = 0
@State private var selectedNewOption: Int? = nil
@Inject private var presenter: BankingPresenterSwift @Inject private var presenter: BankingPresenterSwift
var body: some View { var body: some View {
TabView(selection: $selection) { TabView(selection: selectedTabBinding) {
/* First tab: Accounts */ /* First tab: Accounts */
AccountsTab(data: data) AccountsTab(data: data)
.onAppear { .onAppear {
// due to a SwiftUI bug this cannot be set in AccountsTab directly, so i have to do it here self.savelySetAccountsTabNavigationBar()
self.navigationBarTitle = "Accounts"
self.leadingNavigationBarItem = AnyView(UpdateButton { _ in
self.presenter.updateAccountsTransactionsAsync { _ in }
})
} }
.onDisappear { .onDisappear {
self.navigationBarTitle = "" self.navigationBarTitle = ""
@ -47,17 +62,16 @@ struct ContentView: View {
/* Second tab: 'New' action sheet button */ /* Second tab: 'New' action sheet button */
VStack { VStack {
NavigationLink(destination: TransferMoneyDialog(), tag: 1, selection: self.$selectedNewOption.didSet(self.selectedNewOptionChanged)) {
NavigationLink(destination: TransferMoneyDialog(), tag: 1, selection: $selectedTransferMoneyOption) {
EmptyView() EmptyView()
} }
SheetPresenter(presentingSheet: $showTransferMoneyOptionsActionSheet, content: SheetPresenter(presentingSheet: $showNewOptionsActionSheet, content:
ActionSheet( ActionSheet(
title: Text("New ..."), title: Text("New ..."),
buttons: [ buttons: [
.default(Text("Show transfer money dialog")) { self.selectedTransferMoneyOption = 1 }, .default(Text("Show transfer money dialog")) { self.selectedNewOption = 1 },
.cancel() .cancel { self.showPreviousSelectedTab() }
] ]
) )
) )
@ -68,13 +82,42 @@ struct ContentView: View {
Text("New") Text("New")
} }
} }
.tag(1) .tag(Self.OverlayTabIndex)
} }
.navigationBarHidden(false) .navigationBarHidden(false)
.navigationBarTitle(navigationBarTitle) .navigationBarTitle(navigationBarTitle)
.navigationBarItems(leading: leadingNavigationBarItem) .navigationBarItems(leading: leadingNavigationBarItem)
} }
private func selectedNewOptionChanged(oldValue: Int?, newValue: Int?) {
if newValue == nil && oldValue != nil {
showPreviousSelectedTab()
}
}
private func showPreviousSelectedTab() {
self.selectedTab = self.previousSelectedTab
}
private func savelySetAccountsTabNavigationBar() {
setAccountsTabNavigationBar()
DispatchQueue.main.async { // when pressing 'Cancel' on ActionSheet navigation bar has to be set asynchronously (why, SwiftUI?)
self.setAccountsTabNavigationBar()
}
}
private func setAccountsTabNavigationBar() {
// due to a SwiftUI bug this cannot be set in AccountsTab directly, so i have to do it here
self.navigationBarTitle = "Accounts"
self.leadingNavigationBarItem = AnyView(UpdateButton { _ in
self.presenter.updateAccountsTransactionsAsync { _ in }
})
}
} }
struct ContentView_Previews: PreviewProvider { struct ContentView_Previews: PreviewProvider {

View File

@ -11,10 +11,5 @@ struct SheetPresenter: View {
var body: some View { var body: some View {
Text("") Text("")
.actionSheet(isPresented: self.$presentingSheet, content: { self.content }) .actionSheet(isPresented: self.$presentingSheet, content: { self.content })
.onAppear {
DispatchQueue.main.async {
self.presentingSheet = true
}
}
} }
} }

View File

@ -84,3 +84,36 @@ extension Alert.Button {
} }
} }
extension Binding {
func willSet(_ execute: @escaping (_ currentValue: Value, _ nextValue: Value) ->Void) -> Binding {
return Binding(
get: {
return self.wrappedValue
},
set: {
execute(self.wrappedValue, $0)
self.wrappedValue = $0
}
)
}
func didSet(_ execute: @escaping (_ oldValue: Value, _ newValue: Value) ->Void) -> Binding {
return Binding(
get: {
return self.wrappedValue
},
set: {
let oldValue = self.wrappedValue
self.wrappedValue = $0
execute(oldValue, $0)
}
)
}
}