Commit 5e0ea178 by Demid Merzlyakov

IOS-22: Smart text: approaching precipitation.

parent 4a0e1f92
......@@ -11,6 +11,23 @@ private enum Macro: String {
case feelsLikeTemp = "#FEELS_LIKE_TEMP#"
case weatherType = "#WEATHER_TYPE#"
case weatherTypeChangeHour = "#WEATHER_TYPE_CHANGE_HOUR#"
case expectedPrecipitationHour = "#EXPECTED_PRECIPITATION_HOUR#"
case expectedPrecipitationProbability = "#EXPECTED_PRECIPITATION_PROBABILITY#"
case expectedPrecipitationWeatherType = "#EXPECTED_PRECIPITATION_WEATHER_TYPE#"
static let hourFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h a"
return dateFormatter
}()
static func expectedPrecipitation(for location: Location) -> HourlyWeather? {
location.hourly.first { (hourly) -> Bool in
hourly.date > Date()
&& hourly.date < Date().addingTimeInterval(6 * 3600)
&& precipitationWeatherTypes.contains(hourly.type)
}
}
func string(from location: Location) -> String? {
switch self {
......@@ -31,14 +48,33 @@ private enum Macro: String {
&& hourly.date > Date()
&& hourly.date < Date().addingTimeInterval(6 * 3600)
}) {
let dateFormatter = DateFormatter()
let dateFormatter = Macro.hourFormatter
dateFormatter.timeZone = location.timeZone
dateFormatter.dateFormat = "h a"
return dateFormatter.string(from: firstDifferentHour.date)
}
else {
return nil
}
case .expectedPrecipitationHour:
guard let expectedPrecipitationHour = Macro.expectedPrecipitation(for: location) else {
return nil
}
let dateFormatter = Macro.hourFormatter
dateFormatter.timeZone = location.timeZone
return dateFormatter.string(from: expectedPrecipitationHour.date)
case .expectedPrecipitationProbability:
guard let expectedPrecipitationHour = Macro.expectedPrecipitation(for: location) else {
return nil
}
guard let precipitationProbability = expectedPrecipitationHour.precipitationProbability else {
return nil
}
return "\(precipitationProbability)"
case .expectedPrecipitationWeatherType:
guard let expectedPrecipitationHour = Macro.expectedPrecipitation(for: location) else {
return nil
}
return expectedPrecipitationHour.type.localized(isDay: expectedPrecipitationHour.isDay)
}
}
}
......@@ -90,11 +126,25 @@ fileprivate struct OngoingPrecipitationSmartText: SmartText {
}
}
fileprivate struct ApproachingPrecipitationSmartText: SmartText {
let templateKey: String = "today.smart.approachingPrecipitation"
let requiredMacros: [Macro] = [.expectedPrecipitationHour, .expectedPrecipitationProbability, .expectedPrecipitationWeatherType]
func applicable(to location: Location) -> Bool {
guard let today = location.today else {
return false
}
guard !precipitationWeatherTypes.contains(today.type) else {
return false
}
return Macro.expectedPrecipitation(for: location) != nil
}
}
class SmartTextProvider {
private var prioritizedSmartTexts: [SmartText] = [
OngoingPrecipitationSmartText(),
ApproachingPrecipitationSmartText(),
//Continue adding here.
DefaultSmartText()]
......
......@@ -38,6 +38,7 @@
//Today Smart Text
"today.smart.default" = "Feels like #FEELS_LIKE_TEMP#.";
"today.smart.ongoingPrecipitation" = "#WEATHER_TYPE# until #WEATHER_TYPE_CHANGE_HOUR#.";
"today.smart.approachingPrecipitation" = "#EXPECTED_PRECIPITATION_PROBABILITY#% chance of #EXPECTED_PRECIPITATION_WEATHER_TYPE# by #EXPECTED_PRECIPITATION_HOUR#.";
//Forecast
"forecast.sunny" = "Sunny";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment