Skip to content

Commit

Permalink
feat(ios): utils
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 15, 2024
1 parent ce45be0 commit df0c598
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apps/mobile/metro-now/common/utils/array.utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// metro-now
// https://github.com/krystxf/metro-now

func uniqueBy<T, E: Hashable>(array: [T], by keySelector: (T) -> E) -> [T] {
var seen = Set<E>()
return array.filter { element in
let key = keySelector(element)
return seen.insert(key).inserted
}
}
29 changes: 29 additions & 0 deletions apps/mobile/metro-now/metro-now/utils/find-closest-stop.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// metro-now
// https://github.com/krystxf/metro-now

import CoreLocation
import SwiftUI

func findClosestStop(to location: CLLocation, stops: [ApiStop]) -> ApiStop? {
var closestStop: ApiStop?
var closestDistance: CLLocationDistance?

for stop in stops {
let stopLocation = CLLocation(latitude: stop.avgLatitude, longitude: stop.avgLongitude)

let distance = location.distance(from: stopLocation)

guard closestDistance != nil else {
closestStop = stop
closestDistance = distance
continue
}

if distance < closestDistance! {
closestStop = stop
closestDistance = distance
}
}

return closestStop
}
10 changes: 10 additions & 0 deletions apps/mobile/metro-now/metro-now/utils/get-platform-label.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// metro-now
// https://github.com/krystxf/metro-now

func getPlatformLabel(_ platform: ApiPlatform) -> String {
if let code = platform.code {
return "\(platform.name) \(code)"
}

return platform.name
}

0 comments on commit df0c598

Please sign in to comment.