Skip to content

Commit

Permalink
refactor(app): file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 16, 2024
1 parent 84901a4 commit 0f1b93c
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 333 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

struct CountdownView: View {
typealias CustomFormatFunctionType = (_ formattedTime: String) -> String

let targetDate: Date
let customFunction: CustomFormatFunctionType
@State private var timeRemaining: TimeInterval = 0
private let timer = Timer.publish(
every: 0.1,
on: .main,
in: .common
)
.autoconnect()

init(targetDate: Date, customFunction: CustomFormatFunctionType? = nil) {
self.targetDate = targetDate
self.customFunction = customFunction ?? { $0 }
updateRemainingTime()
}

var body: some View {
Text(
customFunction(
getRemainingTime(
timeRemaining
)
)
)
.onReceive(timer) { _ in
updateRemainingTime()
}
}

private func updateRemainingTime() {
timeRemaining = targetDate.timeIntervalSinceNow
}
}

#Preview {
VStack {
CountdownView(targetDate: .now)
CountdownView(targetDate: .now + 60)
CountdownView(targetDate: .now + 10 * 60)
CountdownView(targetDate: .now + 2 * 60 * 60)
CountdownView(targetDate: .now + 10 * 60) { "Also in \($0)" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// metro-now
// https://github.com/krystxf/metro-now

import Foundation

func getRemainingTime(_ remainingSeconds: TimeInterval) -> String {
let remainingSecondsAbs = abs(remainingSeconds)

let hours = Int(remainingSecondsAbs) / 3600
let minutes = Int(remainingSecondsAbs) % 3600 / 60
let seconds = Int(remainingSecondsAbs) % 60
let isNegative = Bool(remainingSeconds < 0)

var res = isNegative ? "-" : ""

if hours > 0 {
res += "\(hours)h \(minutes)m"
} else if minutes > 0 {
res += "\(minutes)m \(seconds)s"
} else {
res += "\(seconds)s"
}

return res
}
54 changes: 0 additions & 54 deletions apps/mobile/metro-now/common/components/countdown.view.swift

This file was deleted.

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

import SwiftUI

func getRouteType(_ routeName: String?) -> RouteType {
guard let routeName else {
return RouteType.fallback
}

// metro
if let metroLine = MetroLine(rawValue: routeName.uppercased()) {
return RouteType.metro(metroLine)
}
// train
else if routeName.hasPrefix("S") || routeName.hasPrefix("R") {
return RouteType.train
}
// ferry
else if routeName.hasPrefix("P") {
return RouteType.ferry
}
// funicular
else if routeName.hasPrefix("LD") {
return RouteType.funicular
}
// bus or tram
else if let routeNumber = Int(routeName) {
// tram
if routeNumber < 90 {
return RouteType.tram
}
// night tram
else if routeNumber < 100 {
return RouteType.night
}
// bus
else if routeNumber < 900 {
return RouteType.bus
}
// night bus
else if routeNumber < 1000 {
return RouteType.night
}
// fallback
else {
return RouteType.bus
}
}

return RouteType.fallback
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,85 +22,85 @@ struct RouteNameIconView: View {
HStack {
RouteNameIconView(
label: "a",
background: getColorByRouteName("a")
background: getRouteType("a").color
)

RouteNameIconView(
label: "b",
background: getColorByRouteName("b")
background: getRouteType("b").color
)

RouteNameIconView(
label: "c",
background: getColorByRouteName("c")
background: getRouteType("c").color
)
}

HStack {
RouteNameIconView(
label: "2",
background: getColorByRouteName("28")
background: getRouteType("28").color
)
RouteNameIconView(
label: "23",
background: getColorByRouteName("28")
background: getRouteType("28").color
)
RouteNameIconView(
label: "28",
background: getColorByRouteName("28")
background: getRouteType("28").color
)
RouteNameIconView(
label: "99",
background: getColorByRouteName("99")
background: getRouteType("99").color
)
}

HStack {
RouteNameIconView(
label: "149",
background: getColorByRouteName("149")
background: getRouteType("149").color
)

RouteNameIconView(
label: "912",
background: getColorByRouteName("912")
background: getRouteType("912").color
)
}

HStack {
RouteNameIconView(
label: "P2",
background: getColorByRouteName("P2")
background: getRouteType("P2").color
)
RouteNameIconView(
label: "P4",
background: getColorByRouteName("P2")
background: getRouteType("P2").color
)
RouteNameIconView(
label: "P6",
background: getColorByRouteName("P2")
background: getRouteType("P2").color
)
}

HStack {
RouteNameIconView(
label: "S9",
background: getColorByRouteName("S49")
background: getRouteType("S49").color
)
RouteNameIconView(
label: "S88",
background: getColorByRouteName("S49")
background: getRouteType("S49").color
)
RouteNameIconView(
label: "R19",
background: getColorByRouteName("S49")
background: getRouteType("S49").color
)
}

HStack {
RouteNameIconView(
label: "LD",
background: getColorByRouteName("LD")
background: getRouteType("LD").color
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// metro-now
// https://github.com/krystxf/metro-now

import SwiftUI

enum MetroLine: String {
case A
case B
case C

var color: Color {
switch self {
case .A: .green
case .B: .yellow
case .C: .red
}
}
}

let METRO_LINES = [
MetroLine.A.rawValue,
MetroLine.B.rawValue,
MetroLine.C.rawValue,
]

enum RouteType {
case fallback
case metro(MetroLine)
case night
case bus
case tram
case ferry
case funicular
case train

var color: Color {
switch self {
case .fallback: .black
case let .metro(line): line.color
case .night: .black
case .bus: .blue
case .tram: .indigo
case .ferry: .cyan
case .funicular: .brown
case .train: .gray
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
// https://github.com/krystxf/metro-now

let ENDPOINT: String = "https://api.metronow.dev"
// let ENDPOINT: String = "http://localhost:3001"
8 changes: 0 additions & 8 deletions apps/mobile/metro-now/common/types/metro-line.swift

This file was deleted.

Loading

0 comments on commit 0f1b93c

Please sign in to comment.