From c2258813af7021c085de0447b41e8e95f0331b41 Mon Sep 17 00:00:00 2001 From: Alan Chu Date: Tue, 7 Mar 2023 11:00:36 +0100 Subject: [PATCH] start header --- OBAKit/Mapping/MapSnapshotter.swift | 25 +++++++++++ .../MapSnapshotterEnvironmentKey.swift | 22 +++++++++ OBAKit/Stops/StopArrivalHeaderView.swift | 45 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 OBAKit/Mapping/MapSnapshotterEnvironmentKey.swift create mode 100644 OBAKit/Stops/StopArrivalHeaderView.swift diff --git a/OBAKit/Mapping/MapSnapshotter.swift b/OBAKit/Mapping/MapSnapshotter.swift index 491623709..82a432ead 100644 --- a/OBAKit/Mapping/MapSnapshotter.swift +++ b/OBAKit/Mapping/MapSnapshotter.swift @@ -62,6 +62,31 @@ class MapSnapshotter: NSObject { return options } + public func snapshot(stop: Stop, traitCollection: UITraitCollection) async throws -> UIImage { + let options = snapshotOptions(stop: stop, traitCollection: traitCollection) + + let snapshotter = MKMapSnapshotter(options: options) + let snapshot = try await snapshotter.start() + + // Generate the stop icon. + let stopIcon = self.stopIconFactory.buildIcon(for: stop, isBookmarked: false, traits: traitCollection) + + // Calculate the point at which to draw the stop icon. + // It needs to be shifted up by 1/2 the stop icon height + // in order to draw it at the proper location. + var point = snapshot.point(for: stop.coordinate) + point.y -= (stopIcon.size.height / 2.0) + + // Render the composited image. + var annotatedImage = UIImage.draw(image: stopIcon, onto: snapshot.image, at: point) + + if traitCollection.userInterfaceStyle == .light { + annotatedImage = annotatedImage.darkened() + } + + return annotatedImage + } + public func snapshot(stop: Stop, traitCollection: UITraitCollection, completion: @escaping MapSnapshotterCompletionHandler) { let options = snapshotOptions(stop: stop, traitCollection: traitCollection) diff --git a/OBAKit/Mapping/MapSnapshotterEnvironmentKey.swift b/OBAKit/Mapping/MapSnapshotterEnvironmentKey.swift new file mode 100644 index 000000000..d29d5f781 --- /dev/null +++ b/OBAKit/Mapping/MapSnapshotterEnvironmentKey.swift @@ -0,0 +1,22 @@ +// +// MapSnapshotterEnvironmentKey.swift +// OBAKit +// +// Created by Alan Chu on 2/22/23. +// + +import SwiftUI +import OBAKitCore + +private struct StopIconFactoryKey: EnvironmentKey { + static public let defaultValue: StopIconFactory = { + return StopIconFactory(iconSize: ThemeMetrics.defaultMapAnnotationSize, themeColors: ThemeColors.shared) + }() +} + +extension EnvironmentValues { + var stopIconFactory: StopIconFactory { + get { self[StopIconFactoryKey.self] } + set { self[StopIconFactoryKey.self] = newValue } + } +} diff --git a/OBAKit/Stops/StopArrivalHeaderView.swift b/OBAKit/Stops/StopArrivalHeaderView.swift new file mode 100644 index 000000000..11c44575c --- /dev/null +++ b/OBAKit/Stops/StopArrivalHeaderView.swift @@ -0,0 +1,45 @@ +// +// StopArrivalHeaderView.swift +// OBAKit +// +// Created by Alan Chu on 2/22/23. +// + +import MapKit +import SwiftUI + +struct StopArrivalHeaderView: View { + @Environment(\.stopIconFactory) var stopIconFactory + + @State var region: MKCoordinateRegion + @State var mapImage: UIImage? + + var body: some View { + ZStack { + Map(coordinateRegion: $region, interactionModes: []) + Text("asdf") + + if let mapImage { + Image(uiImage: mapImage) + } + } + .task(priority: .high) { +// stopIconFactory.buildIcon(for: <#T##Stop#>, isBookmarked: <#T##Bool#>, traits: <#T##UITraitCollection#>) +// let snapshot = MapSnapshotter(size: , stopIconFactory: <#T##StopIconFactory#>) + } + } +} + +struct StopArrivalHeaderView_Previews: PreviewProvider { + static var previews: some View { + StopArrivalHeaderView( + region: MapHelpers.coordinateRegionWith( + center: CLLocationCoordinate2D(latitude: 47.62217, longitude: -122.32090), + zoomLevel: 16, + size: CGSize(width: 375, height: 250) + ) + ) + .frame(width: 375, height: 250, alignment: .center) + .previewLayout(.sizeThatFits) + } +}