2024-04-10 23:13:47 +00:00
|
|
|
//
|
|
|
|
// AsyncPicker.swift
|
|
|
|
// TuskerComponents
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/9/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
public struct AsyncPicker<V: Hashable, Content: View>: View {
|
|
|
|
let titleKey: LocalizedStringKey
|
2024-04-15 13:49:42 +00:00
|
|
|
#if !os(visionOS)
|
2024-04-10 23:13:47 +00:00
|
|
|
@available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent")
|
|
|
|
let labelHidden: Bool
|
2024-04-15 13:49:42 +00:00
|
|
|
#endif
|
2024-04-10 23:13:47 +00:00
|
|
|
let alignment: Alignment
|
|
|
|
@Binding var value: V
|
|
|
|
let onChange: (V) async -> Bool
|
|
|
|
let content: Content
|
|
|
|
@State private var isLoading = false
|
|
|
|
|
|
|
|
public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, alignment: Alignment = .center, value: Binding<V>, onChange: @escaping (V) async -> Bool, @ViewBuilder content: () -> Content) {
|
|
|
|
self.titleKey = titleKey
|
2024-04-15 13:49:42 +00:00
|
|
|
#if !os(visionOS)
|
2024-04-10 23:13:47 +00:00
|
|
|
self.labelHidden = labelHidden
|
2024-04-15 13:49:42 +00:00
|
|
|
#endif
|
2024-04-10 23:13:47 +00:00
|
|
|
self.alignment = alignment
|
|
|
|
self._value = value
|
|
|
|
self.onChange = onChange
|
|
|
|
self.content = content()
|
|
|
|
}
|
|
|
|
|
|
|
|
public var body: some View {
|
2024-04-15 13:49:42 +00:00
|
|
|
#if os(visionOS)
|
|
|
|
LabeledContent(titleKey) {
|
|
|
|
picker
|
|
|
|
}
|
|
|
|
#else
|
2024-04-10 23:13:47 +00:00
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
LabeledContent(titleKey) {
|
|
|
|
picker
|
|
|
|
}
|
|
|
|
} else if labelHidden {
|
|
|
|
picker
|
|
|
|
} else {
|
|
|
|
HStack {
|
|
|
|
Text(titleKey)
|
|
|
|
Spacer()
|
|
|
|
picker
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 13:49:42 +00:00
|
|
|
#endif
|
2024-04-10 23:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var picker: some View {
|
|
|
|
ZStack(alignment: alignment) {
|
|
|
|
Picker(titleKey, selection: Binding(get: {
|
|
|
|
value
|
|
|
|
}, set: { newValue in
|
|
|
|
let oldValue = value
|
|
|
|
value = newValue
|
|
|
|
isLoading = true
|
|
|
|
Task {
|
|
|
|
let operationCompleted = await onChange(newValue)
|
|
|
|
if !operationCompleted {
|
|
|
|
value = oldValue
|
|
|
|
}
|
|
|
|
isLoading = false
|
|
|
|
}
|
|
|
|
})) {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
.labelsHidden()
|
|
|
|
.opacity(isLoading ? 0 : 1)
|
|
|
|
|
|
|
|
if isLoading {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
@State var value = 0
|
|
|
|
return AsyncPicker("", value: $value) { _ in
|
|
|
|
try! await Task.sleep(nanoseconds: NSEC_PER_SEC)
|
|
|
|
return true
|
|
|
|
} content: {
|
|
|
|
ForEach(0..<10) {
|
|
|
|
Text("\($0)").tag($0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|