Skip to content

Commit

Permalink
refactor(ios): map annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Jul 13, 2024
1 parent 18fbbf2 commit df3be95
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 71 deletions.
Empty file removed app/Common/Components/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@


import SwiftUI
import MapKit


struct MetroAnnotationStack: View {
let metroLines: [String]

var body: some View {
ZStack {
ForEach(Array(metroLines.enumerated()), id: \.0) {
index, metroLine in
let offset: CGFloat = index == 0 ? 0 : (-16 * CGFloat(index))

MetroStationAnnotation(metroLine: metroLine)
.offset(x: offset, y: offset)
}
}
}
}

#Preview("One station annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MetroAnnotationStack(
metroLines: ["A"]
)
}
}
}

#Preview("Two stations annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MetroAnnotationStack(
metroLines: ["A", "B"]
)
}
}
}

// this is not very valid for Prague, but might be useful in the future
#Preview("Multiple stations annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MetroAnnotationStack(
metroLines: ["A", "B", "C", "A", "B", "C"]
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import SwiftUI
import MapKit

struct BusStationAnnotation: View {
var body: some View {
Image(
systemName:"bus"
)
.imageScale(.medium)
.padding(5)
.foregroundStyle(.white)
.background(.blue)
.clipShape(.rect(cornerRadius: 6))
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(.white, lineWidth: 2)
)
}
}

#Preview("Bus station annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
BusStationAnnotation(

)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
//
// metro-now
//
// Created by Kryštof Krátký on 20.05.2024.
//

import MapKit

import SwiftUI

struct MetroStationIcon: View {
struct MetroStationAnnotation: View {
let metroLine: String

var body: some View {
Expand All @@ -26,59 +20,3 @@ struct MetroStationIcon: View {
)
}
}

struct MapMetroStationView: View {
let metroLines: [String]

var body: some View {
ZStack {
ForEach(Array(metroLines.enumerated()), id: \.0) {
index, metroLine in
let offset: CGFloat = index == 0 ? 0 : (-16 * CGFloat(index))

MetroStationIcon(metroLine: metroLine)
.offset(x: offset, y: offset)
}
}
}
}

#Preview("One station annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MapMetroStationView(
metroLines: ["A"]
)
}
}
}

#Preview("Two stations annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MapMetroStationView(
metroLines: ["A", "B"]
)
}
}
}

// this is not very valid for Prague, but might be useful in the future
#Preview("Multiple stations annotation") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
MapMetroStationView(
metroLines: ["A", "B", "C", "A", "B", "C"]
)
}
}
}
2 changes: 2 additions & 0 deletions app/metro-now.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@
2D5BA5EB2C382FEA0055F12A /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
AnnotationStack/MetroAnnotationStack.swift,
BusAnnotation/BusAnnotation.swift,
MetroAnnotation/MetroStationAnnotation.swift,
);
target = 2DC639D72BF3CCBA00A72C7F /* metro-now */;
Expand Down
22 changes: 15 additions & 7 deletions app/metro-now/Core/Map/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import MapKit
import SwiftUI

private struct MetroStationAnnotation {
private struct MetroStationAnnotationType {
var name: String
let coordinate: CLLocationCoordinate2D
let metroLines: [String] // A | B | C
let metroLines: [String] // A | B | C
}

struct MapView: View {
@State private var metroStationAnnotations: [MetroStationAnnotation] = []
@State private var metroStationAnnotations: [MetroStationAnnotationType] = []

var body: some View {
NavigationStack {
Expand All @@ -21,22 +21,30 @@ struct MapView: View {

ForEach(metroStationAnnotations, id: \.name) { station in
Annotation(station.name, coordinate: station.coordinate) {
NavigationLink(destination: StationDetailView(stationName: station.name, showMap: true, showDirection: true)) {
MapMetroStationView(metroLines: station.metroLines)
NavigationLink(
destination: StationDetailView(
stationName: station.name,
showMap: true,
showDirection: true
)
) {
MetroAnnotationStack(metroLines: station.metroLines)
}
}
}
}
}
.task {
let metroStationsGeoJSON: MetroStationsGeoJSON! = getParsedJSONFile(.METRO_STATIONS_FILE)
let metroStationsGeoJSON: MetroStationsGeoJSON! = getParsedJSONFile(
.METRO_STATIONS_FILE
)
guard metroStationsGeoJSON != nil, metroStationsGeoJSON?.features != nil else {
return
}

metroStationAnnotations = metroStationsGeoJSON.features.map { feature in

MetroStationAnnotation(
MetroStationAnnotationType(
name: feature.properties.name,
coordinate: CLLocationCoordinate2D(
latitude: feature.geometry.coordinates[1],
Expand Down

0 comments on commit df3be95

Please sign in to comment.