Skip to content

Commit

Permalink
start header
Browse files Browse the repository at this point in the history
  • Loading branch information
ualch9 committed Mar 28, 2023
1 parent 0ab639c commit c225881
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
25 changes: 25 additions & 0 deletions OBAKit/Mapping/MapSnapshotter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions OBAKit/Mapping/MapSnapshotterEnvironmentKey.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
}
45 changes: 45 additions & 0 deletions OBAKit/Stops/StopArrivalHeaderView.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c225881

Please sign in to comment.