Skip to content

Commit

Permalink
refactor(app): utils
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed May 16, 2024
1 parent 25f56bc commit 5d502c1
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 138 deletions.
File renamed without changes.
17 changes: 0 additions & 17 deletions app/metro-now/metro-now-tests/getStationsFromJSONTest.swift

This file was deleted.

19 changes: 19 additions & 0 deletions app/metro-now/metro-now-tests/jsonUtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// jsonUtilsTests.swift
// metro-now-tests
//
// Created by Kryštof Krátký on 15.05.2024.
//

@testable import metro_now
import XCTest

final class jsonUtilsTests: XCTestCase {
// MARK: - check if JSON data loads successfully

func testStationsJSON() {
let result: [Station]? = getParsedJSONFile(.METRO_STATIONS_FILE)

XCTAssertNotNil(result)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// getMetroLineColorTests.swift
// metroUtilsTests.swift
// metro-now-tests
//
// Created by Kryštof Krátký on 15.05.2024.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// formatTimeTests.swift
// timeUtilsTests.swift
// metro-now-tests
//
// Created by Kryštof Krátký on 15.05.2024.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
68 changes: 40 additions & 28 deletions app/metro-now/metro-now.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -38,26 +39,27 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
2D05EC602BF65E0B003D9581 /* fileUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = fileUtils.swift; sourceTree = "<group>"; };
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 = "<group>"; };
2DC639DD2BF3CCBA00A72C7F /* PlatformListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformListView.swift; sourceTree = "<group>"; };
2DC639DF2BF3CCBC00A72C7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2DC639E22BF3CCBC00A72C7F /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
2DC639EC2BF3CFCF00A72C7F /* PlatformListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformListItem.swift; sourceTree = "<group>"; };
2DC639EF2BF4B02B00A72C7F /* formatTime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = formatTime.swift; sourceTree = "<group>"; };
2DC639EF2BF4B02B00A72C7F /* timeUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = timeUtils.swift; sourceTree = "<group>"; };
2DC639FF2BF4B1E300A72C7F /* PlatformDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlatformDetailView.swift; sourceTree = "<group>"; };
2DC63A012BF4B20E00A72C7F /* PlatformDetailDepartureListView:.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PlatformDetailDepartureListView:.swift"; sourceTree = "<group>"; };
2DC63A032BF4C1E200A72C7F /* MainTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabView.swift; sourceTree = "<group>"; };
2DC63A072BF4C25B00A72C7F /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = "<group>"; };
2DC63A0A2BF4C5B900A72C7F /* LocationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationModel.swift; sourceTree = "<group>"; };
2DC63A0C2BF4CD9900A72C7F /* mapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = mapUtils.swift; sourceTree = "<group>"; };
2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = stationsJSONManager.swift; sourceTree = "<group>"; };
2DC63A102BF4D3F800A72C7F /* stations.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; name = stations.json; path = ../../../../data/stations.json; sourceTree = "<group>"; };
2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getMetroLineColor.swift; sourceTree = "<group>"; };
2DC63A0E2BF4D13200A72C7F /* jsonUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = jsonUtils.swift; sourceTree = "<group>"; };
2DC63A102BF4D3F800A72C7F /* metro-stations.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "metro-stations.json"; sourceTree = "<group>"; };
2DC63A122BF4D98F00A72C7F /* metroUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metroUtils.swift; sourceTree = "<group>"; };
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 = "<group>"; };
2DC63A232BF5266700A72C7F /* getMetroLineColorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getMetroLineColorTests.swift; sourceTree = "<group>"; };
2DC63A252BF5280F00A72C7F /* getStationsFromJSONTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getStationsFromJSONTest.swift; sourceTree = "<group>"; };
2DC63A212BF50EDD00A72C7F /* timeUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = timeUtilsTests.swift; sourceTree = "<group>"; };
2DC63A232BF5266700A72C7F /* metroUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metroUtilsTests.swift; sourceTree = "<group>"; };
2DC63A252BF5280F00A72C7F /* jsonUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = jsonUtilsTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -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 = "<group>";
};
2DC639CF2BF3CCBA00A72C7F = {
isa = PBXGroup;
children = (
2DC639DA2BF3CCBA00A72C7F /* metro-now */,
2D2B0F222BF67B7C00EF9D95 /* metro-now-data */,
2DC63A192BF50E8F00A72C7F /* metro-now-tests */,
2DC639D92BF3CCBA00A72C7F /* Products */,
);
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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;
};
Expand All @@ -295,26 +306,27 @@
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;
};
2DC63A142BF50E8F00A72C7F /* Sources */ = {
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;
};
Expand Down
2 changes: 1 addition & 1 deletion app/metro-now/metro-now/Core/Map/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import MapKit
import SwiftUI

struct MapView: View {
let stations: [Station] = parseStationsJSON() ?? []
let stations: [Station]! = getParsedJSONFile(.METRO_STATIONS_FILE)

var body: some View {
Map {
Expand Down
12 changes: 12 additions & 0 deletions app/metro-now/metro-now/Utils/fileUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// fileUtils.swift
// metro-now
//
// Created by Kryštof Krátký on 16.05.2024.
//

import Foundation

enum FileName: String {
case METRO_STATIONS_FILE = "metro-stations"
}
45 changes: 0 additions & 45 deletions app/metro-now/metro-now/Utils/formatTime.swift

This file was deleted.

46 changes: 46 additions & 0 deletions app/metro-now/metro-now/Utils/jsonUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// jsonUtils.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<T: Decodable>(_ 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
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// getMetroLineColor.swift
// metroUtils.swift
// metro-now
//
// Created by Kryštof Krátký on 15.05.2024.
Expand All @@ -8,6 +8,8 @@
import Foundation
import SwiftUI

let metroStations: [Station]! = getParsedJSONFile(.METRO_STATIONS_FILE)

func getMetroLineColor(_ letter: String) -> Color {
switch letter.uppercased() {
case "A":
Expand Down
Loading

0 comments on commit 5d502c1

Please sign in to comment.