From 02c64d0a72b817e2f6190bd872e3f73784110473 Mon Sep 17 00:00:00 2001 From: Maximilian Wendel Date: Thu, 11 Feb 2021 16:52:31 +0100 Subject: [PATCH] Refactored isRelevant implementation and added forecasts attribute --- Sources/Weather/Models/Weather.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sources/Weather/Models/Weather.swift b/Sources/Weather/Models/Weather.swift index 87e387f..ef6182f 100644 --- a/Sources/Weather/Models/Weather.swift +++ b/Sources/Weather/Models/Weather.swift @@ -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? { @@ -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 } }