Skip to content

Commit ba994fc

Browse files
committed
feat(app): previews
1 parent 40a46b9 commit ba994fc

File tree

6 files changed

+153
-27
lines changed

6 files changed

+153
-27
lines changed

app/metro-now/metro-now-types/metroStationsTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct MetroStationsGeoJSONFeatureGeometryProperties: Codable {
2525
let platforms: [MetroStationsGeoJSONFeatureGeometryPropertiesPlatform]
2626
}
2727

28-
struct MetroStationsGeoJSONFeatureGeometryPropertiesPlatform: Codable {
28+
struct MetroStationsGeoJSONFeatureGeometryPropertiesPlatform: Codable, Hashable {
2929
let gtfsID: String?
3030
let name: String?
3131
let direction: String?

app/metro-now/metro-now-utils/metroUtils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ func getMetroLineColor(_ letter: MetroLine) -> Color {
4040
}
4141
}
4242

43+
func getMetroLineIcon(_ letter: String) -> String {
44+
"\(letter.lowercased()).circle.fill"
45+
}
46+
47+
func getMetroLineIcon(_ letter: MetroLine) -> String {
48+
getMetroLineIcon(letter.rawValue)
49+
}
50+
4351
func getClosestStationFromGeoJSON(location: CLLocation) -> MetroStationsGeoJSONFeature? {
4452
let stations: MetroStationsGeoJSON? = getParsedJSONFile(.METRO_STATIONS_FILE)
4553

app/metro-now/metro-now/Core/PlatformDetail/PlatformDetailView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
struct PlatformDetailView: View {
11-
var direction: String
11+
let direction: String
1212

1313
var body: some View {
1414
ZStack {

app/metro-now/metro-now/Core/PlatformList/PlatformListItem.swift

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,83 @@ import SwiftUI
1010
struct PlatformListItemView: View {
1111
var direction: String
1212
var departure: String
13+
var metroLine: MetroLine
1314
var nextDeparture: String?
1415

1516
var body: some View {
1617
HStack {
17-
Text(direction)
18-
.bold()
18+
Label(
19+
title: { Text(direction) },
20+
icon: { Image(systemName: getMetroLineIcon(metroLine)) }
21+
)
22+
.fontWeight(.bold)
23+
.font(.headline)
24+
.foregroundStyle(.white)
1925

2026
Spacer()
2127

2228
VStack {
23-
Text(departure).bold()
29+
Text(departure)
30+
.fontWeight(.bold)
31+
.foregroundStyle(.white)
32+
.foregroundStyle(.white)
2433
if let nextDeparture {
25-
Text("Next in \(nextDeparture)")
26-
.font(.caption2)
27-
.foregroundStyle(.gray)
34+
Text(
35+
"Also in \(nextDeparture)"
36+
)
37+
.font(.caption2)
38+
.fontWeight(.bold)
39+
.foregroundStyle(.white)
40+
.opacity(0.9)
2841
}
2942
}
3043
}
3144
.padding(.horizontal, 20)
3245
.padding(.vertical, 10)
46+
.background(
47+
LinearGradient(colors: [
48+
getMetroLineColor(metroLine),
49+
getMetroLineColor(metroLine).opacity(0.8),
50+
],
51+
startPoint: .topLeading,
52+
endPoint: .bottomTrailing)
53+
)
54+
55+
.clipShape(.rect(cornerRadius: 15))
3356
}
3457
}
3558

36-
#Preview {
59+
#Preview("Last train") {
60+
PlatformListItemView(
61+
direction: "Nemocnice Motol",
62+
departure: formatTime(seconds: 20),
63+
metroLine: MetroLine.A
64+
)
65+
}
66+
67+
#Preview("Line A") {
68+
PlatformListItemView(
69+
direction: "Nemocnice Motol",
70+
departure: formatTime(seconds: 20),
71+
metroLine: MetroLine.A,
72+
nextDeparture: formatTime(seconds: 220)
73+
)
74+
}
75+
76+
#Preview("Line B") {
77+
PlatformListItemView(
78+
direction: "Černý Most",
79+
departure: formatTime(seconds: 20),
80+
metroLine: MetroLine.B,
81+
nextDeparture: formatTime(seconds: 220)
82+
)
83+
}
84+
85+
#Preview("Line C") {
3786
PlatformListItemView(
3887
direction: "Háje",
39-
departure: "20s",
40-
nextDeparture: "2m 20s"
88+
departure: formatTime(seconds: 20),
89+
metroLine: MetroLine.C,
90+
nextDeparture: formatTime(seconds: 220)
4191
)
4292
}

app/metro-now/metro-now/Core/PlatformList/PlatformListView.swift

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,93 @@
22
// PlatformListView.swift
33
// metro-now
44
//
5-
// Created by Kryštof Krátký on 14.05.2024.
6-
//
75

6+
import CoreLocation
87
import SwiftUI
98

109
struct PlatformsListView: View {
10+
var station: MetroStationsGeoJSONFeature?
11+
1112
var body: some View {
1213
NavigationStack {
1314
ScrollView {
1415
LazyVStack(spacing: 10) {
15-
ForEach(0 ... 4, id: \.self) { platform in
16-
NavigationLink(value: platform) {
17-
PlatformListItemView(
18-
direction: "Háje",
19-
departure: formatTime(seconds: 20)
20-
)
16+
if let station {
17+
ForEach(station.properties.platforms, id: \.self) { platform in
18+
NavigationLink(value: platform) {
19+
PlatformListItemView(
20+
direction: platform.direction!,
21+
departure: formatTime(seconds: 20),
22+
metroLine: MetroLine(rawValue: platform.name!)!,
23+
nextDeparture: formatTime(seconds: 200)
24+
)
25+
}
2126
}
2227
}
2328
}
24-
.padding(20)
29+
.padding(10)
2530
}
2631
.navigationDestination(for: Int.self) {
2732
_ in
2833
PlatformDetailView(
2934
direction: "Háje"
3035
)
3136
}
37+
.navigationTitle(station?.properties.name ?? "")
3238
}
3339
}
3440
}
3541

36-
#Preview {
37-
PlatformsListView()
42+
#Preview("Muzeum") {
43+
PlatformsListView(station: getClosestStationFromGeoJSON(
44+
location: CLLocation(
45+
latitude: 50.078453,
46+
longitude: 14.430676
47+
)
48+
)!)
49+
}
50+
51+
#Preview("Florenc") {
52+
PlatformsListView(station: getClosestStationFromGeoJSON(
53+
location: CLLocation(
54+
latitude: 50.090583,
55+
longitude: 14.438805
56+
)
57+
)!)
58+
}
59+
60+
#Preview("Můstek") {
61+
PlatformsListView(station: getClosestStationFromGeoJSON(
62+
location: CLLocation(
63+
latitude: 50.083956,
64+
longitude: 14.423844
65+
)
66+
)!)
67+
}
68+
69+
#Preview("Dejvická") {
70+
PlatformsListView(station: getClosestStationFromGeoJSON(
71+
location: CLLocation(
72+
latitude: 50.100485,
73+
longitude: 14.393898
74+
)
75+
)!)
76+
}
77+
78+
#Preview("Hlavní nádraží") {
79+
PlatformsListView(station: getClosestStationFromGeoJSON(
80+
location: CLLocation(
81+
latitude: 50.082637,
82+
longitude: 14.434300
83+
)
84+
)!)
85+
}
86+
87+
#Preview("Černý Most") {
88+
PlatformsListView(station: getClosestStationFromGeoJSON(
89+
location: CLLocation(
90+
latitude: 50.111485,
91+
longitude: 14.587877
92+
)
93+
)!)
3894
}

app/metro-now/metro-now/Core/TabBar/MainTabView.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import SwiftUI
1010

1111
struct MainTabView: View {
1212
@StateObject private var locationModel = LocationModel()
13+
@State var closestStation: MetroStationsGeoJSONFeature?
1314
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
1415

1516
var body: some View {
1617
TabView {
17-
PlatformsListView()
18+
PlatformsListView(
19+
station: closestStation)
1820
.tabItem {
1921
Label("Near me", systemImage: "location.circle")
2022
}
@@ -25,14 +27,24 @@ struct MainTabView: View {
2527
}
2628
}
2729
.onReceive(locationModel.$location) { location in
30+
31+
guard let location else {
32+
print("Unknown location")
33+
return
34+
}
2835
print("User's location: \(location)")
36+
37+
let res = getClosestStationFromGeoJSON(location: location)
38+
39+
guard let res else {
40+
print("Unknown closest station")
41+
return
42+
}
43+
44+
closestStation = res
2945
}
3046
.onAppear {
3147
locationModel.checkLocationServicesEnabled()
3248
}
3349
}
3450
}
35-
36-
#Preview {
37-
MainTabView()
38-
}

0 commit comments

Comments
 (0)