-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
338 additions
and
333 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
apps/mobile/metro-now/common/components/countdown-view/countdown.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
apps/mobile/metro-now/common/components/countdown-view/countdown.utils.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
apps/mobile/metro-now/common/components/countdown.view.swift
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
apps/mobile/metro-now/common/components/route-label-view/get-color-by-route-name.utils.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
apps/mobile/metro-now/common/components/route-label-view/route-type.enum.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.