Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Refactored isRelevant implementation and added forecasts attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaximilian committed Feb 11, 2021
1 parent c9d1b96 commit 02c64d0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Sources/Weather/Models/Weather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public struct Weather: Decodable {
extension Weather {
/// - Returns: Whether or not any relevant forecasts are available
public var isRelevant: Bool {
let now = Date()
return timeSeries.contains { forecast -> Bool in
forecast.validTime > now
}
return forecasts.count > 0
}

public var forecast: Forecast? {
Expand All @@ -58,10 +55,17 @@ extension Weather {
return forecast.unsafelyUnwrapped
}

public var forecasts: [Forecast] {
let now = Date()
return timeSeries.sorted(by: { $0.validTime < $1.validTime })
.filter { forecast -> Bool in
forecast.validTime >= now
}
}

/// - Returns: The most relevant `Forecast`
public func get(by date: Date = .init()) -> Forecast? {
return timeSeries.sorted(by: { $0.validTime < $1.validTime })
.first { forecast -> Bool in
return forecasts.first { forecast -> Bool in
forecast.validTime >= date
}
}
Expand Down

0 comments on commit 02c64d0

Please sign in to comment.