Skip to content

Commit

Permalink
feat(app): get closest metro station
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed May 17, 2024
1 parent a394f88 commit 40a46b9
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 7 deletions.
115 changes: 108 additions & 7 deletions app/metro-now/metro-now-tests/metroUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Kryštof Krátký on 15.05.2024.
//

import CoreLocation
@testable import metro_now
import SwiftUI
import XCTest
Expand Down Expand Up @@ -53,27 +54,23 @@ final class metroUtilsTests: XCTestCase {

func testsGetMetroLineColorOutputEnum() {
/// init variables for testing
var lineLetter: String
var result: Color
var expectedResult: Color

/// test 1
lineLetter = MetroLine.A.rawValue
result = getMetroLineColor(lineLetter)
result = getMetroLineColor(.A)
expectedResult = Color.green

XCTAssertEqual(result, expectedResult)

/// test 2
lineLetter = MetroLine.B.rawValue
result = getMetroLineColor(lineLetter)
result = getMetroLineColor(.B)
expectedResult = Color.yellow

XCTAssertEqual(result, expectedResult)

/// test 3
lineLetter = MetroLine.C.rawValue
result = getMetroLineColor(lineLetter)
result = getMetroLineColor(.C)
expectedResult = Color.red

XCTAssertEqual(result, expectedResult)
Expand Down Expand Up @@ -106,4 +103,108 @@ final class metroUtilsTests: XCTestCase {

XCTAssertEqual(result, expectedResult)
}

func testsGetClosestStationFromGeoJSON() {
/// init variables for testing
var location: CLLocation
var result: MetroStationsGeoJSONFeature?

/// test 1
location = CLLocation(
latitude: 50.078453,
longitude: 14.430676
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Muzeum"
)

/// test 2
location = CLLocation(
latitude: 50.079591,
longitude: 14.430883
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Muzeum"
)

/// test 3
location = CLLocation(
latitude: 50.080094,
longitude: 14.429663
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Muzeum"
)

/// test 4
location = CLLocation(
latitude: 50.074929,
longitude: 14.430177
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"I. P. Pavlova"
)

/// test 5
location = CLLocation(
latitude: 50.075499,
longitude: 14.322204
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Nemocnice Motol"
)

/// test 6
location = CLLocation(
latitude: 50.047485,
longitude: 14.460385
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Kačerov"
)

/// test 7
location = CLLocation(
latitude: 50.040648,
longitude: 14.456518
)
result = getClosestStationFromGeoJSON(
location: location
)

XCTAssertEqual(
result?.properties.name,
"Kačerov"
)
}
}
44 changes: 44 additions & 0 deletions app/metro-now/metro-now-utils/metroUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Kryštof Krátký on 15.05.2024.
//

import CoreLocation
import Foundation
import MapKit
import SwiftUI

enum MetroLine: String {
Expand All @@ -26,3 +28,45 @@ func getMetroLineColor(_ letter: String) -> Color {
.purple
}
}

func getMetroLineColor(_ letter: MetroLine) -> Color {
switch letter {
case .A:
.green
case .B:
.yellow
case .C:
.red
}
}

func getClosestStationFromGeoJSON(location: CLLocation) -> MetroStationsGeoJSONFeature? {
let stations: MetroStationsGeoJSON? = getParsedJSONFile(.METRO_STATIONS_FILE)

guard let stations else {
return nil
}
guard stations.features.count > 0 else {
return nil
}

var closestStationIndex = 0
var closestStationDistance: Double = location.distance(from: CLLocation(
latitude: stations.features[0].geometry.coordinates[1], longitude: stations.features[0].geometry.coordinates[0]
))

for (index, station) in stations.features.enumerated() {
let distance = location.distance(from: CLLocation(
latitude: station.geometry.coordinates[1], longitude: station.geometry.coordinates[0]
))

if closestStationDistance > distance {
closestStationIndex = index
closestStationDistance = distance
}
}

return stations.features[closestStationIndex]
}

func getSortedStationsByDistance() {}

0 comments on commit 40a46b9

Please sign in to comment.