Skip to content

Commit

Permalink
feat(app): vehicles annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Jul 26, 2024
1 parent 1dd205c commit 908ad6b
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 78 deletions.
200 changes: 200 additions & 0 deletions app/Common/Components/MapAnnotation/BusAnnotation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@

import MapKit
import SwiftUI

struct StopAnnotation: View {
let routes: [String]
var stopIcon: String
let isMetro: Bool

init(routes: [String]) {
isMetro = routes.allSatisfy { METRO_LINES.contains($0) }
self.routes = routes

guard !isMetro else {
stopIcon = "tram.circle.fill"
return
}

let transportTypes = Set(routes.map {
getVehicleType($0)
})

if transportTypes.contains(.bus) {
stopIcon = "bus"
} else {
stopIcon = transportTypes.first!.rawValue
}
}

var body: some View {
if isMetro {
MetroAnnotationStack(metroLines: routes)
} else {
Image(
systemName: stopIcon
)
.imageScale(.small)
.padding(3)
.font(.system(size: 16))
.foregroundStyle(.white)
.background(
LinearGradient(
gradient: Gradient(colors: getBgColors(routes)

),
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.clipShape(.rect(cornerRadius: 4))
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(.white, lineWidth: 2)
)
}
}
}

#Preview("Metro") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["A"]
)
}
}
}

#Preview("Metro Stack") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["A", "C"]
)
}
}
}

#Preview("Tram") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["78"]
)
}
}
}

#Preview("Night Tram") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["90"]
)
}
}
}

#Preview("Bus") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["700"]
)
}
}
}

#Preview("Bus & Tram") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["60", "700"]
)
}
}
}

#Preview("Night Bus") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["900"]
)
}
}
}

#Preview("Detour") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["X700"]
)
}
}
}

#Preview("Ferry") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["P4"]
)
}
}
}

#Preview("Cable car") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["LD"]
)
}
}
}

#Preview("Train") {
Map {
Annotation(
"Random place on map", coordinate: CLLocationCoordinate2D(
latitude: 50.113680, longitude: 14.449520)
) {
StopAnnotation(
routes: ["S42"]
)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,3 @@ struct MetroAnnotationStack: View {
}
}
}

#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"]
)
}
}
}
33 changes: 33 additions & 0 deletions app/Common/Utils/Vehicle/get-vehicle-bg-colors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import SwiftUI

func getBgColors(_ routes: [String]) -> [Color] {
let vehicleTypes: [VehicleType] = routes.map { getVehicleType($0) }

if vehicleTypes.count == 1, vehicleTypes[0] == .metro {
return [getMetroLineColor(routes[0])]
}

if routes.allSatisfy({ isNightService($0) }) {
return [.gray]
} else if routes.allSatisfy({ $0.starts(with: "X") }) {
return [.orange]
}

var res: [Color] = []
if vehicleTypes.contains(.bus) {
res.append(.blue)
}

if vehicleTypes.contains(.tram) {
res.append(.indigo)
} else if vehicleTypes.contains(.lightrail) {
res.append(.gray)
} else if vehicleTypes.contains(.cablecar) {
res.append(.brown)
} else if vehicleTypes.contains(.ferry) {
res.append(.mint)
}

return res
}
35 changes: 35 additions & 0 deletions app/Common/Utils/Vehicle/get-vehicle-type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// https://pid.cz/jizdni-rady-podle-linek/metro/
func getVehicleType(_ nameAnyCased: String) -> VehicleType {
let name = nameAnyCased.uppercased()

if name.hasPrefix("LD") {
return .cablecar
}

if name.hasPrefix("R") ||
name.hasPrefix("S") ||
name.hasPrefix("T") ||
name.hasPrefix("U")
{
return .lightrail
}

if name.hasPrefix("P") {
return .ferry
}

let number = Int(name)
guard let number else {
return .bus
}

// trolley bus
if number == 58 || number == 59 {
return .bus
} else if number < 100 {
return .tram
}

return .bus
}
10 changes: 10 additions & 0 deletions app/Common/Utils/Vehicle/is-night-service.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


func isNightService(_ name: String) -> Bool {
let number = Int(name)
guard let number else {
return false
}

return number >= 900 || (number >= 90 && number < 100)
}
9 changes: 9 additions & 0 deletions app/Common/Utils/Vehicle/vehicle-type-enum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

enum VehicleType: String {
case bus
case tram
case cablecar
case ferry
case metro
case lightrail
}
20 changes: 19 additions & 1 deletion app/Common/Utils/metroUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation
import MapKit
import SwiftUI

let METRO_LINES: [String] = ["A", "B", "C"]

enum MetroLine: String {
case A
case B
Expand Down Expand Up @@ -73,4 +75,20 @@ func getClosestStationFromGeoJSON(location: CLLocation) -> MetroStationsGeoJSONF
return stations.features[closestStationIndex]
}

func getSortedStationsByDistance() {}
func shortenStopName(_ name: String) -> String {
if name == "Depo Hostivař" {
return "D. Hostivař"
} else if name == "Nemocnice Motol" {
return "N. Motol"
} else if name == "Pražského povstání" {
return "P. povstání"
}

let shorten: String = name
.replacingOccurrences(of: "Náměstí", with: "Nám.")
.replacingOccurrences(of: "náměstí", with: "Nád.")
.replacingOccurrences(of: "Nádraží", with: "Nád.")
.replacingOccurrences(of: "nádraží", with: "nád.")

return shorten
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct SmallWidgetView: View {
ForEach(0 ..< 2) { index in
if entry.departures.count > index {
DepartureView(
direction: entry.departures[index].direction,
direction: shortenStopName(entry.departures[index].direction),
departureDate: entry.departures[index].departureDate,
metroLine: entry.departures[index].metroLine
)
Expand Down
Loading

0 comments on commit 908ad6b

Please sign in to comment.