Skip to content

Commit

Permalink
feat(watch): mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 5, 2024
1 parent 49ad155 commit 3097fa6
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 128 deletions.
10 changes: 4 additions & 6 deletions apps/mobile/metro-now/metro-now Watch App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ struct ContentView: View {

var body: some View {
VStack {
if
let location = locationManager.location,
let stops,
let closestStop = findClosestStop(to: location, stops: stops)
if let location = locationManager.location,
let stops,
let closestStop = findClosestStop(to: location, stops: stops)
{
MainPage(
StopDeparturesView(
title: closestStop.name,
platforms: closestStop.platforms.map {
platform in
Expand All @@ -31,7 +30,6 @@ struct ContentView: View {
)
}
)

} else {
ProgressView()
}
Expand Down

This file was deleted.

78 changes: 0 additions & 78 deletions apps/mobile/metro-now/metro-now Watch App/pages/main-page.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

struct PlatformDetailNextDepartureView: View {
let headsign: String
let departure: Date
let nextHeadsign: String?
let nextDeparture: Date?

var body: some View {
VStack(spacing: 40) {
VStack {
Spacer()
Label(
shortenStopName(headsign),
systemImage: "arrowshape.forward"
).font(.title2)
.fontWeight(.semibold)
.symbolVariant(.fill)
.imageScale(.small)
.foregroundStyle(.secondary)

CountdownView(
targetDate: departure
)
.font(.largeTitle)
.foregroundStyle(.primary)
}

if let nextHeadsign, let nextDeparture {
if headsign == nextHeadsign {
CountdownView(
targetDate: nextDeparture
) {
"Also in \($0)"
}.foregroundStyle(.tertiary)
} else {
VStack {
Text(
shortenStopName(nextHeadsign)
)

CountdownView(
targetDate: nextDeparture
)
}.foregroundStyle(.secondary)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

struct PlatformDetailDeparturesListView: View {
let departures: [ApiDeparture]

var body: some View { List(departures, id: \.departure.predicted) { departure in
HStack {
Text(departure.headsign)
Spacer()
CountdownView(targetDate: departure.departure.predicted)
}
}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

struct PlatformDetailView: View {
let platformId: String
let metroLine: MetroLine?
@State var departures: [ApiDeparture]? = nil
private let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()

init(
platformId: String,
metroLine: MetroLine? = nil,
departures: [ApiDeparture]? = nil

) {
self.platformId = platformId
self.departures = departures
self.metroLine = metroLine
}

var body: some View {
TabView {
if let departures, departures.count > 0 {
let backgroundColor = getMetroLineColor(
metroLine ?? MetroLine(rawValue: departures[0].route)
) ?? .clear

PlatformDetailNextDepartureView(
headsign: departures[0].headsign,
departure: departures[0].departure.scheduled,
nextHeadsign: departures[1].headsign,
nextDeparture: departures[1].departure.predicted
)
.containerBackground(backgroundColor.gradient, for: .tabView)

PlatformDetailDeparturesListView(departures: departures)
.containerBackground(backgroundColor.gradient, for: .tabView)

} else {
ProgressView()
}
}

.toolbar {
if let metroLineName = metroLine?.rawValue {
ToolbarItem(
placement: .confirmationAction)
{
Text(metroLineName)
.overlay(
Circle()
.size(width: 32, height: 32, anchor: .center)
.stroke(.white.opacity(0.6), lineWidth: 3)
)
.fontWeight(.semibold)
.foregroundStyle(.white.opacity(0.6))
}

}
}

.tabViewStyle(.verticalPage(transitionStyle: .identity))
.onAppear {
getDepartures()
}
.onReceive(timer) { _ in
getDepartures()
}
}

func getDepartures() {
NetworkManager.shared
.getDepartures(stopIds: [], platformIds: [platformId]) { result in
DispatchQueue.main.async {
switch result {
case let .success(departures):

self.departures = departures
print(departures)

case let .failure(error):
print(error.localizedDescription)
}
}
}
}
}

#Preview {
NavigationStack {
PlatformDetailView(
platformId: "U1040Z101P",
metroLine: MetroLine.B
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

struct StopDepartureListItemPlaceholderView: View {
let color: Color

init(color: Color?
) {
self.color = color ?? Color.gray.opacity(0.3)
}

var body: some View {
StopDepartureListItemView(
color: color,
headsign: "Loading",
departure: .now,
nextHeadsign: "Loading",
nextDeparture: .now
)
.redacted(reason: .placeholder)
}
}

#Preview {
ScrollView {
StopDepartureListItemPlaceholderView(
color: .red
)
StopDepartureListItemPlaceholderView(
color: .red
)
StopDepartureListItemPlaceholderView(
color: .green
)
StopDepartureListItemPlaceholderView(
color: .green
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import SwiftUI

struct DepartureListItem: View {
struct StopDepartureListItemView: View {
let color: Color

let headsign: String
Expand Down Expand Up @@ -45,13 +45,20 @@ struct DepartureListItem: View {
CountdownView(targetDate: nextDeparture) {
nextHeadsign == headsign ? "also in \($0)" : $0
}

}.font(.system(size: 12))
}
}
.padding(.vertical, 10)
.padding(.horizontal, 5)
.background(color)
.clipShape(.rect(cornerRadius: 10))
.clipShape(.rect(cornerRadius: 14))
.listRowInsets(
EdgeInsets(
top: 0,
leading: 0,
bottom: 0,
trailing: 0
)
)
}
}
Loading

0 comments on commit 3097fa6

Please sign in to comment.