diff --git a/data/stations.json b/app/metro-now/metro-now-data/metro-stations.json similarity index 100% rename from data/stations.json rename to app/metro-now/metro-now-data/metro-stations.json diff --git a/app/metro-now/metro-now-tests/getStationsFromJSONTest.swift b/app/metro-now/metro-now-tests/jsonUtilsTests.swift similarity index 53% rename from app/metro-now/metro-now-tests/getStationsFromJSONTest.swift rename to app/metro-now/metro-now-tests/jsonUtilsTests.swift index 610f1337..abe1ffdf 100644 --- a/app/metro-now/metro-now-tests/getStationsFromJSONTest.swift +++ b/app/metro-now/metro-now-tests/jsonUtilsTests.swift @@ -8,10 +8,12 @@ @testable import metro_now import XCTest -final class getStationsFromJSONTest: XCTestCase { +final class jsonUtilsTests: XCTestCase { // MARK: - check if JSON data loads successfully - func testIfGetStationsFromJSONFails() { - let _ = parseStationsJSON() + func testStationsJSON() { + let result :[Station]? = getParsedJSONFile(.METRO_STATIONS_FILE) + + XCTAssertNotNil(result) } } diff --git a/app/metro-now/metro-now-tests/getMetroLineColorTests.swift b/app/metro-now/metro-now-tests/metroUtilsTests.swift similarity index 94% rename from app/metro-now/metro-now-tests/getMetroLineColorTests.swift rename to app/metro-now/metro-now-tests/metroUtilsTests.swift index 6e4a4f59..0dc39904 100644 --- a/app/metro-now/metro-now-tests/getMetroLineColorTests.swift +++ b/app/metro-now/metro-now-tests/metroUtilsTests.swift @@ -9,12 +9,12 @@ import SwiftUI import XCTest -final class getMetroLineColorTests: XCTestCase { +final class metroUtilsTests: XCTestCase { // MARK: - test if function returns correct color /// should show positive and negative or zero seconds without any issues - func testGetMetroLineColorOutput() { + func getMetroLineColorOutputTests() { /// init variables for testing var lineLetter: String var result: Color diff --git a/app/metro-now/metro-now-tests/formatTimeTests.swift b/app/metro-now/metro-now-tests/timeUtilsTests.swift similarity index 98% rename from app/metro-now/metro-now-tests/formatTimeTests.swift rename to app/metro-now/metro-now-tests/timeUtilsTests.swift index d41589de..ab721297 100644 --- a/app/metro-now/metro-now-tests/formatTimeTests.swift +++ b/app/metro-now/metro-now-tests/timeUtilsTests.swift @@ -8,12 +8,12 @@ @testable import metro_now import XCTest -final class formatTimeTests: XCTestCase { +final class timeUtilsTests: XCTestCase { // MARK: - outputs with seconds /// should show positive and negative or zero seconds without any issues - func testFormatTimePositiveSeconds() { + func formatTimePositiveSecondsTests() { /// init variables for testing var seconds: Int var result: String @@ -41,7 +41,7 @@ final class formatTimeTests: XCTestCase { XCTAssertEqual(result, expectedResult) } - func testFormatTimeNegativeSeconds() { + func formatTimeNegativeSecondsTests() { /// init variables for testing var seconds: Int var result: String @@ -76,7 +76,7 @@ final class formatTimeTests: XCTestCase { XCTAssertEqual(result, expectedResult) } - func testFormatTimeZeroSeconds() { + func formatTimeZeroSecondsTests() { /// init variables for testing var seconds: Int var result: String diff --git a/app/metro-now/metro-now.xcodeproj/project.pbxproj b/app/metro-now/metro-now.xcodeproj/project.pbxproj index 30598f8c..deed473a 100644 --- a/app/metro-now/metro-now.xcodeproj/project.pbxproj +++ b/app/metro-now/metro-now.xcodeproj/project.pbxproj @@ -7,24 +7,25 @@ objects = { /* Begin PBXBuildFile section */ + 2D05EC612BF65E0B003D9581 /* fileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D05EC602BF65E0B003D9581 /* fileUtils.swift */; }; 2DC639DC2BF3CCBA00A72C7F /* metro_nowApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639DB2BF3CCBA00A72C7F /* metro_nowApp.swift */; }; 2DC639DE2BF3CCBA00A72C7F /* PlatformListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639DD2BF3CCBA00A72C7F /* PlatformListView.swift */; }; 2DC639E02BF3CCBC00A72C7F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2DC639DF2BF3CCBC00A72C7F /* Assets.xcassets */; }; 2DC639E32BF3CCBC00A72C7F /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2DC639E22BF3CCBC00A72C7F /* Preview Assets.xcassets */; }; 2DC639ED2BF3CFCF00A72C7F /* PlatformListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639EC2BF3CFCF00A72C7F /* PlatformListItem.swift */; }; - 2DC639F02BF4B02B00A72C7F /* formatTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639EF2BF4B02B00A72C7F /* formatTime.swift */; }; + 2DC639F02BF4B02B00A72C7F /* timeUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639EF2BF4B02B00A72C7F /* timeUtils.swift */; }; 2DC63A002BF4B1E300A72C7F /* PlatformDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC639FF2BF4B1E300A72C7F /* PlatformDetailView.swift */; }; 2DC63A022BF4B20E00A72C7F /* PlatformDetailDepartureListView:.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A012BF4B20E00A72C7F /* PlatformDetailDepartureListView:.swift */; }; 2DC63A042BF4C1E200A72C7F /* MainTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A032BF4C1E200A72C7F /* MainTabView.swift */; }; 2DC63A082BF4C25B00A72C7F /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A072BF4C25B00A72C7F /* MapView.swift */; }; 2DC63A0B2BF4C5B900A72C7F /* LocationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A0A2BF4C5B900A72C7F /* LocationModel.swift */; }; 2DC63A0D2BF4CD9900A72C7F /* mapUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A0C2BF4CD9900A72C7F /* mapUtils.swift */; }; - 2DC63A0F2BF4D13200A72C7F /* stationsJSONManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */; }; - 2DC63A112BF4D3F800A72C7F /* stations.json in Resources */ = {isa = PBXBuildFile; fileRef = 2DC63A102BF4D3F800A72C7F /* stations.json */; }; - 2DC63A132BF4D98F00A72C7F /* getMetroLineColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */; }; - 2DC63A222BF50EDD00A72C7F /* formatTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */; }; - 2DC63A242BF5266700A72C7F /* getMetroLineColorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A232BF5266700A72C7F /* getMetroLineColorTests.swift */; }; - 2DC63A262BF5280F00A72C7F /* getStationsFromJSONTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A252BF5280F00A72C7F /* getStationsFromJSONTest.swift */; }; + 2DC63A0F2BF4D13200A72C7F /* jsonUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A0E2BF4D13200A72C7F /* jsonUtils.swift */; }; + 2DC63A112BF4D3F800A72C7F /* metro-stations.json in Resources */ = {isa = PBXBuildFile; fileRef = 2DC63A102BF4D3F800A72C7F /* metro-stations.json */; }; + 2DC63A132BF4D98F00A72C7F /* metroUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A122BF4D98F00A72C7F /* metroUtils.swift */; }; + 2DC63A222BF50EDD00A72C7F /* timeUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A212BF50EDD00A72C7F /* timeUtilsTests.swift */; }; + 2DC63A242BF5266700A72C7F /* metroUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A232BF5266700A72C7F /* metroUtilsTests.swift */; }; + 2DC63A262BF5280F00A72C7F /* jsonUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A252BF5280F00A72C7F /* jsonUtilsTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -38,26 +39,27 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 2D05EC602BF65E0B003D9581 /* fileUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = fileUtils.swift; sourceTree = ""; }; 2DC639D82BF3CCBA00A72C7F /* metro-now.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "metro-now.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 2DC639DB2BF3CCBA00A72C7F /* metro_nowApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metro_nowApp.swift; sourceTree = ""; }; 2DC639DD2BF3CCBA00A72C7F /* PlatformListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformListView.swift; sourceTree = ""; }; 2DC639DF2BF3CCBC00A72C7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 2DC639E22BF3CCBC00A72C7F /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 2DC639EC2BF3CFCF00A72C7F /* PlatformListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformListItem.swift; sourceTree = ""; }; - 2DC639EF2BF4B02B00A72C7F /* formatTime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = formatTime.swift; sourceTree = ""; }; + 2DC639EF2BF4B02B00A72C7F /* timeUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = timeUtils.swift; sourceTree = ""; }; 2DC639FF2BF4B1E300A72C7F /* PlatformDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformDetailView.swift; sourceTree = ""; }; 2DC63A012BF4B20E00A72C7F /* PlatformDetailDepartureListView:.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PlatformDetailDepartureListView:.swift"; sourceTree = ""; }; 2DC63A032BF4C1E200A72C7F /* MainTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabView.swift; sourceTree = ""; }; 2DC63A072BF4C25B00A72C7F /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = ""; }; 2DC63A0A2BF4C5B900A72C7F /* LocationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationModel.swift; sourceTree = ""; }; 2DC63A0C2BF4CD9900A72C7F /* mapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = mapUtils.swift; sourceTree = ""; }; - 2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = stationsJSONManager.swift; sourceTree = ""; }; - 2DC63A102BF4D3F800A72C7F /* stations.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; name = stations.json; path = ../../../../data/stations.json; sourceTree = ""; }; - 2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getMetroLineColor.swift; sourceTree = ""; }; + 2DC63A0E2BF4D13200A72C7F /* jsonUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = jsonUtils.swift; sourceTree = ""; }; + 2DC63A102BF4D3F800A72C7F /* metro-stations.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "metro-stations.json"; sourceTree = ""; }; + 2DC63A122BF4D98F00A72C7F /* metroUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metroUtils.swift; sourceTree = ""; }; 2DC63A182BF50E8F00A72C7F /* metro-now-tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "metro-now-tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; - 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = formatTimeTests.swift; sourceTree = ""; }; - 2DC63A232BF5266700A72C7F /* getMetroLineColorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getMetroLineColorTests.swift; sourceTree = ""; }; - 2DC63A252BF5280F00A72C7F /* getStationsFromJSONTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getStationsFromJSONTest.swift; sourceTree = ""; }; + 2DC63A212BF50EDD00A72C7F /* timeUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = timeUtilsTests.swift; sourceTree = ""; }; + 2DC63A232BF5266700A72C7F /* metroUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metroUtilsTests.swift; sourceTree = ""; }; + 2DC63A252BF5280F00A72C7F /* jsonUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = jsonUtilsTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -78,10 +80,19 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 2D2B0F222BF67B7C00EF9D95 /* metro-now-data */ = { + isa = PBXGroup; + children = ( + 2DC63A102BF4D3F800A72C7F /* metro-stations.json */, + ); + path = "metro-now-data"; + sourceTree = ""; + }; 2DC639CF2BF3CCBA00A72C7F = { isa = PBXGroup; children = ( 2DC639DA2BF3CCBA00A72C7F /* metro-now */, + 2D2B0F222BF67B7C00EF9D95 /* metro-now-data */, 2DC63A192BF50E8F00A72C7F /* metro-now-tests */, 2DC639D92BF3CCBA00A72C7F /* Products */, ); @@ -140,11 +151,11 @@ 2DC639EE2BF4B00A00A72C7F /* Utils */ = { isa = PBXGroup; children = ( - 2DC639EF2BF4B02B00A72C7F /* formatTime.swift */, - 2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */, - 2DC63A102BF4D3F800A72C7F /* stations.json */, + 2DC639EF2BF4B02B00A72C7F /* timeUtils.swift */, + 2DC63A0E2BF4D13200A72C7F /* jsonUtils.swift */, 2DC63A0C2BF4CD9900A72C7F /* mapUtils.swift */, - 2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */, + 2DC63A122BF4D98F00A72C7F /* metroUtils.swift */, + 2D05EC602BF65E0B003D9581 /* fileUtils.swift */, ); path = Utils; sourceTree = ""; @@ -185,9 +196,9 @@ 2DC63A192BF50E8F00A72C7F /* metro-now-tests */ = { isa = PBXGroup; children = ( - 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */, - 2DC63A232BF5266700A72C7F /* getMetroLineColorTests.swift */, - 2DC63A252BF5280F00A72C7F /* getStationsFromJSONTest.swift */, + 2DC63A212BF50EDD00A72C7F /* timeUtilsTests.swift */, + 2DC63A232BF5266700A72C7F /* metroUtilsTests.swift */, + 2DC63A252BF5280F00A72C7F /* jsonUtilsTests.swift */, ); path = "metro-now-tests"; sourceTree = ""; @@ -275,7 +286,7 @@ files = ( 2DC639E32BF3CCBC00A72C7F /* Preview Assets.xcassets in Resources */, 2DC639E02BF3CCBC00A72C7F /* Assets.xcassets in Resources */, - 2DC63A112BF4D3F800A72C7F /* stations.json in Resources */, + 2DC63A112BF4D3F800A72C7F /* metro-stations.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -295,16 +306,17 @@ files = ( 2DC639DE2BF3CCBA00A72C7F /* PlatformListView.swift in Sources */, 2DC63A082BF4C25B00A72C7F /* MapView.swift in Sources */, - 2DC639F02BF4B02B00A72C7F /* formatTime.swift in Sources */, + 2DC639F02BF4B02B00A72C7F /* timeUtils.swift in Sources */, 2DC63A042BF4C1E200A72C7F /* MainTabView.swift in Sources */, 2DC63A0B2BF4C5B900A72C7F /* LocationModel.swift in Sources */, 2DC639DC2BF3CCBA00A72C7F /* metro_nowApp.swift in Sources */, 2DC63A002BF4B1E300A72C7F /* PlatformDetailView.swift in Sources */, + 2D05EC612BF65E0B003D9581 /* fileUtils.swift in Sources */, 2DC63A022BF4B20E00A72C7F /* PlatformDetailDepartureListView:.swift in Sources */, - 2DC63A132BF4D98F00A72C7F /* getMetroLineColor.swift in Sources */, + 2DC63A132BF4D98F00A72C7F /* metroUtils.swift in Sources */, 2DC63A0D2BF4CD9900A72C7F /* mapUtils.swift in Sources */, 2DC639ED2BF3CFCF00A72C7F /* PlatformListItem.swift in Sources */, - 2DC63A0F2BF4D13200A72C7F /* stationsJSONManager.swift in Sources */, + 2DC63A0F2BF4D13200A72C7F /* jsonUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -312,9 +324,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 2DC63A242BF5266700A72C7F /* getMetroLineColorTests.swift in Sources */, - 2DC63A262BF5280F00A72C7F /* getStationsFromJSONTest.swift in Sources */, - 2DC63A222BF50EDD00A72C7F /* formatTimeTests.swift in Sources */, + 2DC63A242BF5266700A72C7F /* metroUtilsTests.swift in Sources */, + 2DC63A262BF5280F00A72C7F /* jsonUtilsTests.swift in Sources */, + 2DC63A222BF50EDD00A72C7F /* timeUtilsTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/app/metro-now/metro-now/Core/Map/MapView.swift b/app/metro-now/metro-now/Core/Map/MapView.swift index c514f568..b435f28d 100644 --- a/app/metro-now/metro-now/Core/Map/MapView.swift +++ b/app/metro-now/metro-now/Core/Map/MapView.swift @@ -9,8 +9,8 @@ import MapKit import SwiftUI struct MapView: View { - let stations: [Station] = parseStationsJSON() ?? [] - + let stations: [Station]! = getParsedJSONFile(.METRO_STATIONS_FILE) + var body: some View { Map { ForEach(stations, id: \.name) { station in diff --git a/app/metro-now/metro-now/Utils/fileUtils.swift b/app/metro-now/metro-now/Utils/fileUtils.swift new file mode 100644 index 00000000..cae38f33 --- /dev/null +++ b/app/metro-now/metro-now/Utils/fileUtils.swift @@ -0,0 +1,14 @@ +// +// getNearestStation.swift +// metro-now +// +// Created by Kryštof Krátký on 16.05.2024. +// + +import Foundation + +enum FileName: String { + case METRO_STATIONS_FILE = "metro-stations" +} + + diff --git a/app/metro-now/metro-now/Utils/formatTime.swift b/app/metro-now/metro-now/Utils/formatTime.swift deleted file mode 100644 index 78faea21..00000000 --- a/app/metro-now/metro-now/Utils/formatTime.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// formatTime.swift -// metro-now -// -// Created by Kryštof Krátký on 15.05.2024. -// - -import Foundation - -let twelveHoursSeconds = 12 * 60 * 60 - -func formatTime(seconds: Int) -> String { - let isNegative = seconds < 0 - - if isNegative, seconds < -twelveHoursSeconds { - return "- >12h" - } else if !isNegative, seconds > twelveHoursSeconds { - return ">12h" - } - - let positiveSeconds = isNegative ? -seconds : seconds - - let hours = positiveSeconds / 3600 - let minutes = (positiveSeconds % 3600) / 60 - let remainingSeconds = positiveSeconds % 60 - - var output: String = isNegative ? "-" : "" - if hours > 0 { - if minutes == 0 { - output += String(format: "%dh", hours) - } else { - output += String(format: "%dh %dm", hours, minutes) - } - } else if minutes > 0 { - if remainingSeconds == 0 { - output += String(format: "%dm", minutes) - } else { - output += String(format: "%dm %ds", minutes, remainingSeconds) - } - } else { - output += String(format: "%ds", remainingSeconds) - } - - return output -} diff --git a/app/metro-now/metro-now/Utils/jsonUtils.swift b/app/metro-now/metro-now/Utils/jsonUtils.swift new file mode 100644 index 00000000..63b52597 --- /dev/null +++ b/app/metro-now/metro-now/Utils/jsonUtils.swift @@ -0,0 +1,48 @@ +// +// stationsJSONManager.swift +// metro-now +// +// Created by Kryštof Krátký on 15.05.2024. +// + +import Foundation + +struct Station: Codable { + let name: String + let avgLat, avgLon: Double + let platforms: [Platform] +} + +struct Platform: Codable { + let gtfsID, name, direction: String? + + enum CodingKeys: String, CodingKey { + case gtfsID + case name, direction + } +} + +func getParsedJSONFile(_ filename: FileName) -> T? { + let path = Bundle.main.path(forResource: filename.rawValue, ofType: "json") + + guard let path else { + print("File not found") + return nil + } + + do { + let data = try Data( + contentsOf: URL(fileURLWithPath: path) + ) + let stations = try JSONDecoder().decode( + T.self, + from: data + ) + return stations + } catch { + print("Error parsing JSON: \(error)") + return nil + } +} + + diff --git a/app/metro-now/metro-now/Utils/getMetroLineColor.swift b/app/metro-now/metro-now/Utils/metroUtils.swift similarity index 100% rename from app/metro-now/metro-now/Utils/getMetroLineColor.swift rename to app/metro-now/metro-now/Utils/metroUtils.swift diff --git a/app/metro-now/metro-now/Utils/stationsJSONManager.swift b/app/metro-now/metro-now/Utils/stationsJSONManager.swift deleted file mode 100644 index eab03d9e..00000000 --- a/app/metro-now/metro-now/Utils/stationsJSONManager.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// stationsJSONManager.swift -// metro-now -// -// Created by Kryštof Krátký on 15.05.2024. -// - -import Foundation - -struct Station: Codable { - let name: String - let avgLat, avgLon: Double - let platforms: [Platform] -} - -struct Platform: Codable { - let gtfsID, name, direction: String? - - enum CodingKeys: String, CodingKey { - case gtfsID - case name, direction - } -} - -func parseStationsJSON() -> [Station]? { - if let path = Bundle.main.path(forResource: "stations", ofType: "json") { - do { - let data = try Data(contentsOf: URL(fileURLWithPath: path)) - let stations = try JSONDecoder().decode([Station].self, from: data) - return stations - } catch { - print("Error parsing JSON: \(error)") - } - } else { - print("File not found") - } - return nil -} diff --git a/app/metro-now/metro-now/Utils/timeUtils.swift b/app/metro-now/metro-now/Utils/timeUtils.swift new file mode 100644 index 00000000..0b817acd --- /dev/null +++ b/app/metro-now/metro-now/Utils/timeUtils.swift @@ -0,0 +1,49 @@ +// +// formatTime.swift +// metro-now +// +// Created by Kryštof Krátký on 15.05.2024. +// + +import Foundation + +let SECONDS_IN_MINUTE = 60 +let SECONDS_IN_HOUR = 60 * SECONDS_IN_MINUTE +let SECONDS_IN_TWELVE_HOURS = 12 * SECONDS_IN_HOUR + +func formatTime(seconds: Int) -> String { + // (-1 * Int.min) > Int.max + // => this has to be separate check before seconds is converted to positive number + let isMoreThan12Hours = seconds > SECONDS_IN_TWELVE_HOURS + guard !isMoreThan12Hours else { + return ">12h" + } + let isLessThanMinus12Hours = seconds < -SECONDS_IN_TWELVE_HOURS + guard !isLessThanMinus12Hours else { + return "- >12h" + } + + let output = seconds < 0 ? "-" : "" + let positiveSeconds = abs(seconds) + + let hours = positiveSeconds / SECONDS_IN_HOUR + let minutes = (positiveSeconds % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE + let remainingSeconds = positiveSeconds % SECONDS_IN_MINUTE + + guard hours == 0 else { + if minutes == 0 { + return String(format: "\(output)%dh", hours) + } else { + return String(format: "\(output)%dh %dm", hours, minutes) + } + } + guard minutes == 0 else { + if remainingSeconds == 0 { + return String(format: "\(output)%dm", minutes) + } else { + return String(format: "\(output)%dm %ds", minutes, remainingSeconds) + } + } + + return String(format: "\(output)%ds", remainingSeconds) +}