Commit b24d371c by Demid Merzlyakov

Alert on Today is filled with data.

parent dd049516
...@@ -7,13 +7,34 @@ ...@@ -7,13 +7,34 @@
import Foundation import Foundation
public enum NWSSeverityLevel: String, Codable { public enum NWSSeverityLevel: String, Codable, Comparable {
case warning = "1" case warning = "1"
case watch = "2" case watch = "2"
case advisory = "3" case advisory = "3"
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs != rhs &&
(
(lhs == .warning) ||
(lhs == .watch && rhs == .advisory)
)
}
public static func <= (lhs: Self, rhs: Self) -> Bool {
lhs == rhs || lhs < rhs
}
public static func >= (lhs: Self, rhs: Self) -> Bool {
lhs == rhs || lhs > rhs
}
public static func > (lhs: Self, rhs: Self) -> Bool {
lhs != rhs && !(lhs < rhs)
}
} }
public struct NWSAlert: Codable, Equatable, Hashable { public struct NWSAlert: Codable, Equatable, Hashable {
public var issueDate: Date?
public let weatherID: String public let weatherID: String
public let messageID: String public let messageID: String
public let messageURL: String public let messageURL: String
...@@ -37,7 +58,7 @@ public struct NWSAlert: Codable, Equatable, Hashable { ...@@ -37,7 +58,7 @@ public struct NWSAlert: Codable, Equatable, Hashable {
} }
public var expired: Bool { public var expired: Bool {
return Date() < expires return Date() > expires
} }
// MARK: Equatable implementation // MARK: Equatable implementation
......
...@@ -11,6 +11,12 @@ ...@@ -11,6 +11,12 @@
"general.more" = "more"; "general.more" = "more";
"general.search" = "search"; "general.search" = "search";
//Alert
"today.alert.infoTemplate.oneAlert" = "#ALERT_DESCRIPTION";
"today.alert.infoTemplate.twoAlerts" = "#ALERT_DESCRIPTION and 1 more alert";
"today.alert.infoTemplate.overTwoAlerts" = "#ALERT_DESCRIPTION and #REMAINING_ALERTS_COUNT more alerts";
"today.alert.timeAgoTemplate" = "#TIME_AGO ago";
//Forecast //Forecast
"forecast.sunny" = "Sunny"; "forecast.sunny" = "Sunny";
"forecast.cloudy" = "Cloudy"; "forecast.cloudy" = "Cloudy";
......
...@@ -55,6 +55,50 @@ class TodayAlertCell: UITableViewCell { ...@@ -55,6 +55,50 @@ class TodayAlertCell: UITableViewCell {
container.layer.shadowOpacity = 1.0 container.layer.shadowOpacity = 1.0
} }
} }
public func configure(location: Location) {
infoLabel.text = ""
timeAgoLabel.text = ""
if let alerts = location.notifications?.nwsAlers.filter( { !$0.expired }) {
if let firstAlert = alerts.first {
var mostSevereAlert = firstAlert
for alert in alerts {
if alert.severityLevel > mostSevereAlert.severityLevel {
mostSevereAlert = alert
}
}
let infoTemplateAlertDescriptionKey = "#ALERT_DESCRIPTION"
let infoTemplateRemainingAlertsCountKey = "#REMAINING_ALERTS_COUNT"
var infoTemplate = "today.alert.infoTemplate.overTwoAlerts".localized()
if alerts.count == 1 {
infoTemplate = "today.alert.infoTemplate.oneAlert".localized()
}
else if alerts.count == 2 {
infoTemplate = "today.alert.infoTemplate.twoAlerts".localized()
}
infoTemplate = infoTemplate.replacingOccurrences(of: infoTemplateAlertDescriptionKey, with: mostSevereAlert.description)
infoTemplate = infoTemplate.replacingOccurrences(of: infoTemplateRemainingAlertsCountKey, with: "\(alerts.count - 1)")
infoLabel.text = infoTemplate
if let issueDate = mostSevereAlert.issueDate {
let secondsAgo = Date().timeIntervalSince(issueDate)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .short
formatter.maximumUnitCount = 1
let timeAgoTemplateTimeAgoKey = "#TIME_AGO"
if let formattedTime = formatter.string(from: secondsAgo) {
timeAgoLabel.text = "today.alert.timeAgoTemplate".localized().replacingOccurrences(of: timeAgoTemplateTimeAgoKey, with: formattedTime)
}
}
}
}
}
} }
//MARK:- Prepare //MARK:- Prepare
...@@ -88,11 +132,11 @@ private extension TodayAlertCell { ...@@ -88,11 +132,11 @@ private extension TodayAlertCell {
infoLabel.numberOfLines = 2 infoLabel.numberOfLines = 2
infoLabel.lineBreakMode = .byWordWrapping infoLabel.lineBreakMode = .byWordWrapping
infoLabel.font = AppFont.SFPro.regular(size: 16) infoLabel.font = AppFont.SFPro.regular(size: 16)
infoLabel.text = "Frost Advisory and 2 more alerts" infoLabel.text = ""// E.g. "Frost Advisory and 2 more alerts"
container.addSubview(infoLabel) container.addSubview(infoLabel)
timeAgoLabel.font = AppFont.SFPro.regular(size: 14) timeAgoLabel.font = AppFont.SFPro.regular(size: 14)
timeAgoLabel.text = "5 mins ago" timeAgoLabel.text = ""// E.g. "5 mins ago"
timeAgoLabel.textAlignment = .right timeAgoLabel.textAlignment = .right
container.addSubview(timeAgoLabel) container.addSubview(timeAgoLabel)
......
...@@ -104,7 +104,9 @@ class TodayCellFactory: CellFactoryProtocol { ...@@ -104,7 +104,9 @@ class TodayCellFactory: CellFactoryProtocol {
switch cellType { switch cellType {
case .alert: case .alert:
return dequeueReusableCell(type: TodayAlertCell.self, tableView: tableView, indexPath: indexPath) let cell = dequeueReusableCell(type: TodayAlertCell.self, tableView: tableView, indexPath: indexPath)
cell.configure(location: loc)
return cell
case .forecast: case .forecast:
let cell = dequeueReusableCell(type: TodayForecastCell.self, tableView: tableView, indexPath: indexPath) let cell = dequeueReusableCell(type: TodayForecastCell.self, tableView: tableView, indexPath: indexPath)
cell.configure(with: loc) cell.configure(with: loc)
...@@ -191,7 +193,7 @@ class TodayCellFactory: CellFactoryProtocol { ...@@ -191,7 +193,7 @@ class TodayCellFactory: CellFactoryProtocol {
} }
let location = self.todayViewModel.location let location = self.todayViewModel.location
if location?.notifications?.nwsAlers.count ?? 0 == 0 { if location?.notifications?.nwsAlers.filter({ !$0.expired }).count ?? 0 == 0 {
rowsToHide.insert(.alert) rowsToHide.insert(.alert)
} }
......
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