Code: Select all
import SwiftUI
import PhotosUI
import SwiftData
// Dummy Model Definitions
@Model
class Bus {
var id: UUID = UUID()
var date: Date = Date()
//More stuff
var images: [BusImage] = []
}
@Model
class BusImage {
var id: UUID = UUID()
var data: Data
var bus: Bus?
init(data: Data, bus: Bus? = nil) {
self.data = data
self.bus = bus
}
}
struct HeroView: View {
let bus: Bus
@Binding var selectedBus: Bus?
@Namespace private var ns
var body: some View {
NavigationLink(destination: CreateReportDetailsView(bus: bus, selectedBus: $selectedBus)
.navigationTransition(.zoom(sourceID: bus, in: ns))) {
Text("Create Report")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.simultaneousGesture(TapGesture().onEnded {
print("HeroView tap recognized.")
})
}
}
// CreateReportDetailsView that contains a TabView and a PhotoPickerView.
struct CreateReportDetailsView: View {
let bus: Bus
@Binding var selectedBus: Bus?
@State private var selectedTabIndex: Int = 0
var body: some View {
VStack {
TabView(selection: $selectedTabIndex) {
BusPageView(bus: bus)
.tag(0)
Text("page")
.tag(1)
}
.tabViewStyle(.page)
}
.navigationTitle(selectedTabIndex == 0 ? "Bus Info" : "Issues")
}
}
// BusInfoPageView that embeds my PhotoPickerView.
struct BusPageView: View {
let bus: Bus
@State private var photosPickerItems: [PhotosPickerItem] = []
var body: some View {
VStack {
// Other Bus info content here...
PhotoPickerView(photosPickerItems: $photosPickerItems, bus: $bus)
.padding()
}
}
}
// PhotoPickerView with a high-priority tap gesture.
struct PhotoPickerView: View {
@Binding var photosPickerItems: [PhotosPickerItem]
let bus: Bus
@Environment(\.modelContext) private var modelContext
var body: some View {
VStack {
PhotosPicker("Select Photos", selection: $photosPickerItems, maxSelectionCount: 2, selectionBehavior: .ordered)
.frame(width: 120, height: 120)
.buttonStyle(.borderless)
.background(Color(.systemFill))
.clipShape(RoundedRectangle(cornerRadius: 20))
.contentShape(RoundedRectangle(cornerRadius: 20))
.simultaneousGesture(
TapGesture().onEnded {
print("Tap gesture recognized on PhotosPicker")
}
)
}
.onChange(of: photosPickerItems) { _, _ in
Task {
for item in photosPickerItems {
if let data = try? await item.loadTransferable(type: Data.self) {
let newImage = BusImage(data: data, bus: bus)
bus.images.append(newImage)
}
}
photosPickerItems = []
do {
try modelContext.save()
} catch {
print("Error saving model: \(error.localizedDescription)")
}
}
}
}
}