Fix bug where the Edit and + buttons sometimes weren't visible on iOS 14. Along the way, I ran into more SwiftUI problems in iOS 14 so I had to put in a bit of hack for adding destinations by queuing them to the main queue 0.1 seconds later on add. Without that hack, the popup would not dismiss.

This commit is contained in:
Jeremy Rand 2022-03-25 22:18:30 -04:00
parent 7279d7ecb1
commit cdd4a8ea82
2 changed files with 51 additions and 32 deletions

View File

@ -7,6 +7,47 @@
import SwiftUI
struct PopOverView : View {
@State private var destination = ""
@Binding var showPopover: Bool
var destinations : GSDestinations
var body: some View {
VStack {
Text("Enter the hostname or IP address of your GS:")
.font(.title2)
TextField("New destination", text: self.$destination) { isEditing in
} onCommit: {
self.showPopover = false
onAdd()
}
.padding()
HStack {
Button("Cancel") {
self.showPopover = false
}
.padding()
Button("Add") {
self.showPopover = false
onAdd()
}
.padding()
}
}.padding()
}
func onAdd() {
let newDestination = destination
destination = ""
// This schedules the add of the destination for 0.1 s from now. Under iOS 14.x, it seems like there is
// a bug such that if I synchronously add the destination here, the popup will not dismiss. Some kind of
// swiftui bug I think. No issue in iOS 15 as far as I can tell. So, this queuing of the add is only
// necessary as a workaround.
OperationQueue.main.schedule(after: OperationQueue.SchedulerTimeType(Date(timeIntervalSinceNow: 0.1))) {
destinations.onAdd(ipAddress: newDestination)
}
}
}
struct DestinationsView: View {
@State private var editMode = EditMode.inactive
@ -25,8 +66,15 @@ struct DestinationsView: View {
.onDelete(perform: onDelete)
.onMove(perform: onMove)
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
EditButton()
}
ToolbarItem(placement: .navigationBarTrailing) {
addButton
}
}
.navigationBarTitle("GS Destinations")
.navigationBarItems(leading: EditButton(), trailing: addButton)
.environment(\.editMode, $editMode)
}
@ -37,35 +85,12 @@ struct DestinationsView: View {
.popover(
isPresented: self.$showPopover,
arrowEdge: .bottom
) { addPopover } )
) { PopOverView(showPopover: self.$showPopover, destinations: destinations) } )
default:
return AnyView(EmptyView())
}
}
private var addPopover: some View {
VStack {
Text("Enter the hostname or IP address of your GS:")
.font(.title2)
TextField("New destination", text: self.$newDestination) { isEditing in
} onCommit: {
onAdd()
}
.padding()
HStack {
Button("Cancel") {
self.showPopover = false
editMode = EditMode.inactive
}
.padding()
Button("Add") {
onAdd()
}
.padding()
}
}.padding()
}
private func onDelete(offsets: IndexSet) {
destinations.onDelete(offsets: offsets)
}
@ -77,12 +102,6 @@ struct DestinationsView: View {
func showAdd() {
self.showPopover = true;
}
func onAdd() {
destinations.onAdd(ipAddress: self.newDestination)
newDestination = ""
showPopover = false;
}
}
struct DestinationsView_Previews: PreviewProvider {

View File

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>759</string>
<string>780</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSRequiresIPhoneOS</key>