Skip to content

Commit

Permalink
feat(app): previews
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed May 17, 2024
1 parent 40a46b9 commit ba994fc
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/metro-now/metro-now-types/metroStationsTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct MetroStationsGeoJSONFeatureGeometryProperties: Codable {
let platforms: [MetroStationsGeoJSONFeatureGeometryPropertiesPlatform]
}

struct MetroStationsGeoJSONFeatureGeometryPropertiesPlatform: Codable {
struct MetroStationsGeoJSONFeatureGeometryPropertiesPlatform: Codable, Hashable {
let gtfsID: String?
let name: String?
let direction: String?
Expand Down
8 changes: 8 additions & 0 deletions app/metro-now/metro-now-utils/metroUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func getMetroLineColor(_ letter: MetroLine) -> Color {
}
}

func getMetroLineIcon(_ letter: String) -> String {
"\(letter.lowercased()).circle.fill"
}

func getMetroLineIcon(_ letter: MetroLine) -> String {
getMetroLineIcon(letter.rawValue)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct PlatformDetailView: View {
var direction: String
let direction: String

var body: some View {
ZStack {
Expand Down
68 changes: 59 additions & 9 deletions app/metro-now/metro-now/Core/PlatformList/PlatformListItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,83 @@ import SwiftUI
struct PlatformListItemView: View {
var direction: String
var departure: String
var metroLine: MetroLine
var nextDeparture: String?

var body: some View {
HStack {
Text(direction)
.bold()
Label(
title: { Text(direction) },
icon: { Image(systemName: getMetroLineIcon(metroLine)) }
)
.fontWeight(.bold)
.font(.headline)
.foregroundStyle(.white)

Spacer()

VStack {
Text(departure).bold()
Text(departure)
.fontWeight(.bold)
.foregroundStyle(.white)
.foregroundStyle(.white)
if let nextDeparture {
Text("Next in \(nextDeparture)")
.font(.caption2)
.foregroundStyle(.gray)
Text(
"Also in \(nextDeparture)"
)
.font(.caption2)
.fontWeight(.bold)
.foregroundStyle(.white)
.opacity(0.9)
}
}
}
.padding(.horizontal, 20)
.padding(.vertical, 10)
.background(
LinearGradient(colors: [
getMetroLineColor(metroLine),
getMetroLineColor(metroLine).opacity(0.8),
],
startPoint: .topLeading,
endPoint: .bottomTrailing)
)

.clipShape(.rect(cornerRadius: 15))
}
}

#Preview {
#Preview("Last train") {
PlatformListItemView(
direction: "Nemocnice Motol",
departure: formatTime(seconds: 20),
metroLine: MetroLine.A
)
}

#Preview("Line A") {
PlatformListItemView(
direction: "Nemocnice Motol",
departure: formatTime(seconds: 20),
metroLine: MetroLine.A,
nextDeparture: formatTime(seconds: 220)
)
}

#Preview("Line B") {
PlatformListItemView(
direction: "Černý Most",
departure: formatTime(seconds: 20),
metroLine: MetroLine.B,
nextDeparture: formatTime(seconds: 220)
)
}

#Preview("Line C") {
PlatformListItemView(
direction: "Háje",
departure: "20s",
nextDeparture: "2m 20s"
departure: formatTime(seconds: 20),
metroLine: MetroLine.C,
nextDeparture: formatTime(seconds: 220)
)
}
78 changes: 67 additions & 11 deletions app/metro-now/metro-now/Core/PlatformList/PlatformListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,93 @@
// PlatformListView.swift
// metro-now
//
// Created by Kryštof Krátký on 14.05.2024.
//

import CoreLocation
import SwiftUI

struct PlatformsListView: View {
var station: MetroStationsGeoJSONFeature?

var body: some View {
NavigationStack {
ScrollView {
LazyVStack(spacing: 10) {
ForEach(0 ... 4, id: \.self) { platform in
NavigationLink(value: platform) {
PlatformListItemView(
direction: "Háje",
departure: formatTime(seconds: 20)
)
if let station {
ForEach(station.properties.platforms, id: \.self) { platform in
NavigationLink(value: platform) {
PlatformListItemView(
direction: platform.direction!,
departure: formatTime(seconds: 20),
metroLine: MetroLine(rawValue: platform.name!)!,
nextDeparture: formatTime(seconds: 200)
)
}
}
}
}
.padding(20)
.padding(10)
}
.navigationDestination(for: Int.self) {
_ in
PlatformDetailView(
direction: "Háje"
)
}
.navigationTitle(station?.properties.name ?? "")
}
}
}

#Preview {
PlatformsListView()
#Preview("Muzeum") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.078453,
longitude: 14.430676
)
)!)
}

#Preview("Florenc") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.090583,
longitude: 14.438805
)
)!)
}

#Preview("Můstek") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.083956,
longitude: 14.423844
)
)!)
}

#Preview("Dejvická") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.100485,
longitude: 14.393898
)
)!)
}

#Preview("Hlavní nádraží") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.082637,
longitude: 14.434300
)
)!)
}

#Preview("Černý Most") {
PlatformsListView(station: getClosestStationFromGeoJSON(
location: CLLocation(
latitude: 50.111485,
longitude: 14.587877
)
)!)
}
22 changes: 17 additions & 5 deletions app/metro-now/metro-now/Core/TabBar/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import SwiftUI

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

var body: some View {
TabView {
PlatformsListView()
PlatformsListView(
station: closestStation)
.tabItem {
Label("Near me", systemImage: "location.circle")
}
Expand All @@ -25,14 +27,24 @@ struct MainTabView: View {
}
}
.onReceive(locationModel.$location) { location in

guard let location else {
print("Unknown location")
return
}
print("User's location: \(location)")

let res = getClosestStationFromGeoJSON(location: location)

guard let res else {
print("Unknown closest station")
return
}

closestStation = res
}
.onAppear {
locationModel.checkLocationServicesEnabled()
}
}
}

#Preview {
MainTabView()
}

0 comments on commit ba994fc

Please sign in to comment.