Commit 4e2fac11 by Demid Merzlyakov

Notifications screen (work in progress).

parent 33617db7
...@@ -120,6 +120,10 @@ ...@@ -120,6 +120,10 @@
CDF9BF8E26133D050037847D /* LocationSearchCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */; }; CDF9BF8E26133D050037847D /* LocationSearchCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */; };
CE04561F26282325003D252B /* NWSCurrentEventsReponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE04561E26282325003D252B /* NWSCurrentEventsReponse.swift */; }; CE04561F26282325003D252B /* NWSCurrentEventsReponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE04561E26282325003D252B /* NWSCurrentEventsReponse.swift */; };
CE0456242629C04C003D252B /* NWSAlertsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE0456232629C04C003D252B /* NWSAlertsManager.swift */; }; CE0456242629C04C003D252B /* NWSAlertsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE0456232629C04C003D252B /* NWSAlertsManager.swift */; };
CE0457902632B3BC00B3C19A /* NotificationsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE04578F2632B3BC00B3C19A /* NotificationsViewModel.swift */; };
CE0457932632B3DD00B3C19A /* NotificationsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE0457922632B3DD00B3C19A /* NotificationsViewController.swift */; };
CE0457952632B3F900B3C19A /* NotificationsCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE0457942632B3F900B3C19A /* NotificationsCoordinator.swift */; };
CE0457982632E47B00B3C19A /* NWSAlertCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE0457972632E47B00B3C19A /* NWSAlertCell.swift */; };
CE13B72826245CE2007CBD4D /* AirQuality.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72726245CE2007CBD4D /* AirQuality.swift */; }; CE13B72826245CE2007CBD4D /* AirQuality.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72726245CE2007CBD4D /* AirQuality.swift */; };
CE13B72B26245D0D007CBD4D /* HealthStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72A26245D0D007CBD4D /* HealthStatus.swift */; }; CE13B72B26245D0D007CBD4D /* HealthStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72A26245D0D007CBD4D /* HealthStatus.swift */; };
CE13B72E26245D42007CBD4D /* Pollutant.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72D26245D42007CBD4D /* Pollutant.swift */; }; CE13B72E26245D42007CBD4D /* Pollutant.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE13B72D26245D42007CBD4D /* Pollutant.swift */; };
...@@ -358,6 +362,10 @@ ...@@ -358,6 +362,10 @@
CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchCoordinator.swift; sourceTree = "<group>"; }; CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchCoordinator.swift; sourceTree = "<group>"; };
CE04561E26282325003D252B /* NWSCurrentEventsReponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWSCurrentEventsReponse.swift; sourceTree = "<group>"; }; CE04561E26282325003D252B /* NWSCurrentEventsReponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWSCurrentEventsReponse.swift; sourceTree = "<group>"; };
CE0456232629C04C003D252B /* NWSAlertsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWSAlertsManager.swift; sourceTree = "<group>"; }; CE0456232629C04C003D252B /* NWSAlertsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWSAlertsManager.swift; sourceTree = "<group>"; };
CE04578F2632B3BC00B3C19A /* NotificationsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsViewModel.swift; sourceTree = "<group>"; };
CE0457922632B3DD00B3C19A /* NotificationsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsViewController.swift; sourceTree = "<group>"; };
CE0457942632B3F900B3C19A /* NotificationsCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsCoordinator.swift; sourceTree = "<group>"; };
CE0457972632E47B00B3C19A /* NWSAlertCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NWSAlertCell.swift; sourceTree = "<group>"; };
CE13B72726245CE2007CBD4D /* AirQuality.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirQuality.swift; sourceTree = "<group>"; }; CE13B72726245CE2007CBD4D /* AirQuality.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirQuality.swift; sourceTree = "<group>"; };
CE13B72A26245D0D007CBD4D /* HealthStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthStatus.swift; sourceTree = "<group>"; }; CE13B72A26245D0D007CBD4D /* HealthStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthStatus.swift; sourceTree = "<group>"; };
CE13B72D26245D42007CBD4D /* Pollutant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pollutant.swift; sourceTree = "<group>"; }; CE13B72D26245D42007CBD4D /* Pollutant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pollutant.swift; sourceTree = "<group>"; };
...@@ -610,6 +618,7 @@ ...@@ -610,6 +618,7 @@
CD37D3F2260DF4FB002669D6 /* SettingsCoordinator.swift */, CD37D3F2260DF4FB002669D6 /* SettingsCoordinator.swift */,
CD866A75260F77C500E96A5C /* SettingsDetailsCoordinator.swift */, CD866A75260F77C500E96A5C /* SettingsDetailsCoordinator.swift */,
CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */, CDF9BF8D26133D050037847D /* LocationSearchCoordinator.swift */,
CE0457942632B3F900B3C19A /* NotificationsCoordinator.swift */,
); );
path = Coordinators; path = Coordinators;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -712,6 +721,7 @@ ...@@ -712,6 +721,7 @@
CD32CE07260C743B00235081 /* MenuViewModel.swift */, CD32CE07260C743B00235081 /* MenuViewModel.swift */,
CD37D3F5260DF5BA002669D6 /* SettingsViewModel.swift */, CD37D3F5260DF5BA002669D6 /* SettingsViewModel.swift */,
CD866A6E260F67F200E96A5C /* SettingsDetailsViewModel.swift */, CD866A6E260F67F200E96A5C /* SettingsDetailsViewModel.swift */,
CE04578F2632B3BC00B3C19A /* NotificationsViewModel.swift */,
); );
path = ViewModels; path = ViewModels;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -719,6 +729,7 @@ ...@@ -719,6 +729,7 @@
CD6B3038257267E2004B34B3 /* View controllers */ = { CD6B3038257267E2004B34B3 /* View controllers */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
CE0457912632B3D000B3C19A /* Notifications */,
CE578FE025FB415E00E8B85D /* Locations */, CE578FE025FB415E00E8B85D /* Locations */,
CD17C5F425D15B3400EE884E /* Today */, CD17C5F425D15B3400EE884E /* Today */,
CDE18DCF25D166DD00C80ED9 /* Forecast */, CDE18DCF25D166DD00C80ED9 /* Forecast */,
...@@ -900,6 +911,23 @@ ...@@ -900,6 +911,23 @@
path = Forecast; path = Forecast;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
CE0457912632B3D000B3C19A /* Notifications */ = {
isa = PBXGroup;
children = (
CE0457962632E47100B3C19A /* Cells */,
CE0457922632B3DD00B3C19A /* NotificationsViewController.swift */,
);
path = Notifications;
sourceTree = "<group>";
};
CE0457962632E47100B3C19A /* Cells */ = {
isa = PBXGroup;
children = (
CE0457972632E47B00B3C19A /* NWSAlertCell.swift */,
);
path = Cells;
sourceTree = "<group>";
};
CE13B76126246743007CBD4D /* CCPA */ = { CE13B76126246743007CBD4D /* CCPA */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
...@@ -1482,6 +1510,7 @@ ...@@ -1482,6 +1510,7 @@
CEC526FD25E795F700DA58A5 /* WdtWeatherSource.swift in Sources */, CEC526FD25E795F700DA58A5 /* WdtWeatherSource.swift in Sources */,
CD32CE08260C743B00235081 /* MenuViewModel.swift in Sources */, CD32CE08260C743B00235081 /* MenuViewModel.swift in Sources */,
CD866A76260F77C500E96A5C /* SettingsDetailsCoordinator.swift in Sources */, CD866A76260F77C500E96A5C /* SettingsDetailsCoordinator.swift in Sources */,
CE0457902632B3BC00B3C19A /* NotificationsViewModel.swift in Sources */,
CEAFF09225DFC71D00DF4EBF /* HelperTypes.swift in Sources */, CEAFF09225DFC71D00DF4EBF /* HelperTypes.swift in Sources */,
CE13B81A262480B3007CBD4D /* AdManager.swift in Sources */, CE13B81A262480B3007CBD4D /* AdManager.swift in Sources */,
CE13B76326246743007CBD4D /* CCPAHelper.swift in Sources */, CE13B76326246743007CBD4D /* CCPAHelper.swift in Sources */,
...@@ -1571,6 +1600,7 @@ ...@@ -1571,6 +1600,7 @@
CDE18DD125D166F900C80ED9 /* ForecastViewController.swift in Sources */, CDE18DD125D166F900C80ED9 /* ForecastViewController.swift in Sources */,
CE8962AC26175DF500CA274A /* CoreCurrentWeather.swift in Sources */, CE8962AC26175DF500CA274A /* CoreCurrentWeather.swift in Sources */,
CE9F01BB261B31A6009BA500 /* CoreDataError.swift in Sources */, CE9F01BB261B31A6009BA500 /* CoreDataError.swift in Sources */,
CE0457932632B3DD00B3C19A /* NotificationsViewController.swift in Sources */,
CD39F2F525DE9571009FE398 /* ArrowButton.swift in Sources */, CD39F2F525DE9571009FE398 /* ArrowButton.swift in Sources */,
CE13B72826245CE2007CBD4D /* AirQuality.swift in Sources */, CE13B72826245CE2007CBD4D /* AirQuality.swift in Sources */,
CE8962A526175DF500CA274A /* _CoreHourlyWeather.swift in Sources */, CE8962A526175DF500CA274A /* _CoreHourlyWeather.swift in Sources */,
...@@ -1585,6 +1615,7 @@ ...@@ -1585,6 +1615,7 @@
CE13B81B262480B3007CBD4D /* AdView.swift in Sources */, CE13B81B262480B3007CBD4D /* AdView.swift in Sources */,
CD251EDC26036E5400ED7A65 /* DayTimePrecipitationView.swift in Sources */, CD251EDC26036E5400ED7A65 /* DayTimePrecipitationView.swift in Sources */,
CD32CE04260C742300235081 /* MenuViewController.swift in Sources */, CD32CE04260C742300235081 /* MenuViewController.swift in Sources */,
CE0457952632B3F900B3C19A /* NotificationsCoordinator.swift in Sources */,
CD86246925E672A20097F3FB /* PrecipButton.swift in Sources */, CD86246925E672A20097F3FB /* PrecipButton.swift in Sources */,
CD3F6E6C25FA5A90002DB99B /* PeriodButtonProtocol.swift in Sources */, CD3F6E6C25FA5A90002DB99B /* PeriodButtonProtocol.swift in Sources */,
CEDE4E8225EEFD56007457E9 /* WdtWeatherCode.swift in Sources */, CEDE4E8225EEFD56007457E9 /* WdtWeatherCode.swift in Sources */,
...@@ -1665,6 +1696,7 @@ ...@@ -1665,6 +1696,7 @@
CDA5542D25EF7C9700A2E08C /* ReusableCellProtocol.swift in Sources */, CDA5542D25EF7C9700A2E08C /* ReusableCellProtocol.swift in Sources */,
CD647D0625ED08050034578B /* ViewModelProtocol.swift in Sources */, CD647D0625ED08050034578B /* ViewModelProtocol.swift in Sources */,
CD8E041225F8F775001785B6 /* ForecastViewModel.swift in Sources */, CD8E041225F8F775001785B6 /* ForecastViewModel.swift in Sources */,
CE0457982632E47B00B3C19A /* NWSAlertCell.swift in Sources */,
CD32CE12260C77B400235081 /* MenuBuyButton.swift in Sources */, CD32CE12260C77B400235081 /* MenuBuyButton.swift in Sources */,
CD86246C25E6826A0097F3FB /* InnerShadowLayer.swift in Sources */, CD86246C25E6826A0097F3FB /* InnerShadowLayer.swift in Sources */,
CE578FE525FB415F00E8B85D /* CityCell.swift in Sources */, CE578FE525FB415F00E8B85D /* CityCell.swift in Sources */,
......
//
// NotificationsCoordinator.swift
// 1Weather
//
// Created by Demid Merzlyakov on 23.04.2021.
//
import UIKit
class NotificationsCoordinator: Coordinator {
private let parentViewController:UIViewController
public var childCoordinators = [Coordinator]()
public var parentCoordinator: Coordinator?
init(parentViewController:UIViewController) {
self.parentViewController = parentViewController
}
public func start() {
let notificationController = NotificationsViewController(coordinator: self)
let navigationController = UINavigationController(rootViewController: notificationController)
self.parentViewController.present(navigationController, animated: true)
}
public func openNWSAlert() {
#warning("Not implemented!")
//TODO: Implement!
}
func viewControllerDidEnd(controller: UIViewController) {
parentCoordinator?.childDidFinish(child: self)
}
}
...@@ -32,7 +32,13 @@ class TodayCoordinator: Coordinator { ...@@ -32,7 +32,13 @@ class TodayCoordinator: Coordinator {
searchCoordinator.start() searchCoordinator.start()
} }
func viewControllerDidEnd(controller: UIViewController) { public func openNotificationsScreen() {
// let notificationsCoordinator = NotificationsCoordinator(parentViewController: navigationController)
notificationsCoordinator.parentCoordinator = self
notificationsCoordinator.start()
}
public func viewControllerDidEnd(controller: UIViewController) {
} }
} }
...@@ -19,6 +19,7 @@ public struct NWSAlert: Codable, Equatable, Hashable { ...@@ -19,6 +19,7 @@ public struct NWSAlert: Codable, Equatable, Hashable {
public var weatherMessage: String? = nil public var weatherMessage: String? = nil
/// This property is set by NWSAlertManager after decoding the response. /// This property is set by NWSAlertManager after decoding the response.
public var city: String = "" public var city: String = ""
public var timeZone: TimeZone!
public var extendedInfo: NWSAlertExtendedInfo? public var extendedInfo: NWSAlertExtendedInfo?
......
...@@ -138,6 +138,7 @@ public class NWSAlertsManager { ...@@ -138,6 +138,7 @@ public class NWSAlertsManager {
let eventsWithLocation: [NWSAlert] = events.map { (alert) -> NWSAlert in let eventsWithLocation: [NWSAlert] = events.map { (alert) -> NWSAlert in
var updated: NWSAlert = alert var updated: NWSAlert = alert
updated.city = location.cityName ?? "" updated.city = location.cityName ?? ""
updated.timeZone = location.timeZone
return updated return updated
} }
self.merge(alerts: eventsWithLocation) self.merge(alerts: eventsWithLocation)
......
...@@ -17,6 +17,13 @@ ...@@ -17,6 +17,13 @@
"today.alert.infoTemplate.overTwoAlerts" = "#ALERT_DESCRIPTION and #REMAINING_ALERTS_COUNT more alerts"; "today.alert.infoTemplate.overTwoAlerts" = "#ALERT_DESCRIPTION and #REMAINING_ALERTS_COUNT more alerts";
"today.alert.timeAgoTemplate" = "#TIME_AGO ago"; "today.alert.timeAgoTemplate" = "#TIME_AGO ago";
//Notifications
"notifications.title" = "Notifications";
"notifications.nwsCell.active" = "Active";
"notifications.nwsCell.expired" = "Expired";
"notifications.nwsCell.staticTitle" = "NWS ALERT";
"notifications.nwsCell.remainsInEffectUntil" = "remains in effect until";
//Forecast //Forecast
"forecast.sunny" = "Sunny"; "forecast.sunny" = "Sunny";
"forecast.cloudy" = "Cloudy"; "forecast.cloudy" = "Cloudy";
......
//
// NWSAlertCell.swift
// 1Weather
//
// Created by Demid Merzlyakov on 23.04.2021.
//
import UIKit
class NWSAlertCell: UITableViewCell {
// TODO: we don't have an image for this one?
// private let headerImage = UIImageView()
private let alertIconImageView = UIImageView(image: UIImage(named: "notifications_alert_icon"))
private let headerActiveExpiredLabel = UILabel()
private let alertTextLabel = UILabel()
private let alertLocationLabel = UILabel()
public private(set) var alert: NWSAlert?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
prepareHeader()
prepareAlertTextLabel()
prepareFooter()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private static let expiryDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full
return dateFormatter
}()
private static let issueDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
return dateFormatter
}()
private func expiryDateFormatter(timeZone: TimeZone) -> DateFormatter {
NWSAlertCell.expiryDateFormatter.timeZone = timeZone
return NWSAlertCell.expiryDateFormatter // it's always used from the same thread, so it's OK.
}
private func issueDateFormatter(timeZone: TimeZone) -> DateFormatter {
NWSAlertCell.issueDateFormatter.timeZone = timeZone
return NWSAlertCell.issueDateFormatter // it's always used from the same thread, so it's OK.
}
public func configure(with alert: NWSAlert) {
self.alert = alert
if alert.expired {
headerActiveExpiredLabel.text = "notifications.nwsCell.expired".localized()
headerActiveExpiredLabel.backgroundColor = UIColor(named: "nws_cell_expired")
}
else {
headerActiveExpiredLabel.text = "notifications.nwsCell.active".localized()
headerActiveExpiredLabel.backgroundColor = UIColor(named: "nws_cell_active")
}
headerActiveExpiredLabel.isHidden = false
let expiryString = expiryDateFormatter(timeZone: alert.timeZone).string(from: alert.expires)
alertTextLabel.text = "\(alert.description) \("notifications.nwsCell.remainsInEffectUntil".localized()) \(expiryString)"
if let issueDate = alert.extendedInfo?.issueDate {
let issueDateString = issueDateFormatter(timeZone: alert.timeZone).string(from: issueDate)
alertLocationLabel.text = "\(alert.city) - \(issueDateString)"
}
else {
alertLocationLabel.text = alert.city
}
}
}
private extension NWSAlertCell {
var primaryTextColor: UIColor? {
UIColor(named: "notifications_primary_text")
}
var offsetToSuperview: CGFloat {
return 18
}
func prepareHeader() {
let staticTitleLabel = UILabel()
staticTitleLabel.text = "notifications.nwsCell.staticTitle".localized()
staticTitleLabel.font = AppFont.SFPro.semibold(size: 14)
staticTitleLabel.textColor = primaryTextColor
headerActiveExpiredLabel.text = ""
headerActiveExpiredLabel.textColor = UIColor.white
headerActiveExpiredLabel.font = AppFont.SFPro.semibold(size: 12)
headerActiveExpiredLabel.clipsToBounds = true
headerActiveExpiredLabel.layer.cornerRadius = 2
headerActiveExpiredLabel.isHidden = true
addSubview(alertIconImageView)
addSubview(staticTitleLabel)
addSubview(headerActiveExpiredLabel)
alertIconImageView.snp.makeConstraints { (make) in
make.width.height.equalTo(18)
make.top.equalToSuperview().offset(offsetToSuperview)
make.left.equalToSuperview().offset(offsetToSuperview)
}
staticTitleLabel.snp.makeConstraints { (make) in
make.left.equalTo(alertIconImageView.snp.right).offset(6)
make.bottom.equalTo(alertIconImageView.snp.bottom)
}
headerActiveExpiredLabel.snp.makeConstraints { (make) in
make.left.equalTo(staticTitleLabel.snp.right).offset(6)
make.centerY.equalTo(staticTitleLabel.snp.centerY)
}
}
func prepareAlertTextLabel() {
alertTextLabel.text = ""
alertTextLabel.textColor = primaryTextColor
alertTextLabel.font = AppFont.SFPro.regular(size: 14)
alertTextLabel.numberOfLines = 0
addSubview(alertTextLabel)
alertTextLabel.snp.makeConstraints { (make) in
make.left.equalToSuperview().offset(offsetToSuperview)
make.top.equalTo(alertIconImageView.snp.bottom).offset(4)
make.right.equalToSuperview().inset(offsetToSuperview)
}
}
func prepareFooter() {
let locationIconImageView = UIImageView(image: UIImage(named: "notifications_alert_location_icon"))
alertLocationLabel.font = AppFont.SFPro.regular(size: 12)
alertLocationLabel.textColor = primaryTextColor
alertLocationLabel.numberOfLines = 0
addSubview(locationIconImageView)
addSubview(alertLocationLabel)
locationIconImageView.snp.makeConstraints { (make) in
make.left.equalToSuperview().offset(offsetToSuperview)
make.bottom.equalTo(alertLocationLabel)
make.width.equalTo(8)
make.height.equalTo(10)
}
alertLocationLabel.snp.makeConstraints { (make) in
make.top.equalTo(alertTextLabel.snp.bottom)
make.left.equalTo(locationIconImageView.snp.right).offset(6)
make.right.equalToSuperview().inset(offsetToSuperview)
make.bottom.equalToSuperview().inset(offsetToSuperview)
}
}
}
//
// NotificationsViewController.swift
// 1Weather
//
// Created by Demid Merzlyakov on 23.04.2021.
//
import UIKit
class NotificationsViewController: UIViewController {
private let coordinator: NotificationsCoordinator
private let viewModel = NotificationsViewModel()
private var localizationObserver:Any?
private let log = Logger(componentName: "NotificationsViewController")
private let tableView = UITableView()
init(coordinator: NotificationsCoordinator) {
self.coordinator = coordinator
super.init(nibName: nil, bundle: nil)
self.title = ""
}
@available(*, unavailable)
required init?(coder: NSCoder) { return nil }
override func viewDidLoad() {
prepareViewController()
prepareNavigationBar()
prepareTableView()
viewModel.delegate = self
tableView.reloadData()
}
deinit {
if let observer = localizationObserver {
NotificationCenter.default.removeObserver(observer)
}
}
private func close() {
self.navigationController?.dismiss(animated: true)
}
@objc private func handleCloseButton() {
close()
}
}
private extension NotificationsViewController {
func prepareViewController() {
view.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
let closeButton = UIBarButtonItem(title: "general.close".localized().capitalized,
style: .done,
target: self,
action: #selector(handleCloseButton))
navigationItem.leftBarButtonItem = closeButton
localizationObserver = NotificationCenter.default.addObserver(forName: .localizationChange, object: nil, queue: .main, using: { [weak self] _ in
self?.tableView.reloadData()
self?.prepareNavigationBar()
})
}
func prepareNavigationBar() {
title = "notifications.title".localized()
}
func prepareTableView() {
view.addSubview(tableView)
tableView.register(NWSAlertCell.self, forCellReuseIdentifier: NWSAlertCell.self.kIdentifier)
tableView.delegate = self
tableView.dataSource = self
tableView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
}
}
extension NotificationsViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
viewModel.alerts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: NWSAlertCell.self.kIdentifier, for: indexPath) as? NWSAlertCell else {
fatalError("Should never happen")
#if !DEBUG
return UITableViewCell()
#endif
}
cell.configure(with: viewModel.alerts[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
UITableView.automaticDimension
}
}
extension NotificationsViewController: ViewModelDelegate {
func viewModelDidChange<P: ViewModelProtocol>(model:P) {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
...@@ -58,7 +58,7 @@ class TodayViewController: UIViewController { ...@@ -58,7 +58,7 @@ class TodayViewController: UIViewController {
} }
@objc private func handleNotificationButton() { @objc private func handleNotificationButton() {
print("Handle notification button") self.coordinator.openNotificationsScreen()
} }
} }
......
//
// NotificationsViewModel.swift
// 1Weather
//
// Created by Demid Merzlyakov on 23.04.2021.
//
import Foundation
class NotificationsViewModel: ViewModelProtocol {
public weak var delegate:ViewModelDelegate?
private let alertsManager: NWSAlertsManager = LocationManager.shared.nwsAlertsManager
public var alerts: [NWSAlert] {
alertsManager.alerts
}
}
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