Commit 224238c5 by Dmitriy Stepanets

Changed architrecture of TodayViewController

parent 5255d67c
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<key>1Weather.xcscheme_^#shared#^_</key> <key>1Weather.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>6</integer> <integer>5</integer>
</dict> </dict>
<key>PG (Playground) 1.xcscheme</key> <key>PG (Playground) 1.xcscheme</key>
<dict> <dict>
......
...@@ -9,7 +9,6 @@ import UIKit ...@@ -9,7 +9,6 @@ import UIKit
class TodayCoordinator:Coordinator { class TodayCoordinator:Coordinator {
//Private //Private
private let todayViewModel = TodayViewModel(locationManager: LocationManager.shared) // TODO: get rid of singleton maybe?
private let navigationController = UINavigationController(nibName: nil, bundle: nil) private let navigationController = UINavigationController(nibName: nil, bundle: nil)
private var tabBarController:UITabBarController? private var tabBarController:UITabBarController?
...@@ -22,7 +21,7 @@ class TodayCoordinator:Coordinator { ...@@ -22,7 +21,7 @@ class TodayCoordinator:Coordinator {
} }
func start() { func start() {
let todayViewController = TodayViewController(viewModel: todayViewModel) let todayViewController = TodayViewController()
navigationController.viewControllers = [todayViewController] navigationController.viewControllers = [todayViewController]
tabBarController?.add(viewController: navigationController) tabBarController?.add(viewController: navigationController)
} }
......
...@@ -7,8 +7,9 @@ ...@@ -7,8 +7,9 @@
import UIKit import UIKit
private enum TodayCellType:Int, CaseIterable { private enum TodayCellType:Int {
case forecast = 0 case alert = 0
case forecast
case ad case ad
case conditions case conditions
case forecastPeriod case forecastPeriod
...@@ -18,17 +19,32 @@ private enum TodayCellType:Int, CaseIterable { ...@@ -18,17 +19,32 @@ private enum TodayCellType:Int, CaseIterable {
case moon case moon
} }
private struct TodaySection {
var rows:[TodayCellType]
}
class TodayCellFactory: CellFactoryProtocol { class TodayCellFactory: CellFactoryProtocol {
public var onGetLocation:(() -> Location?)? //Private
private let todayViewModel:TodayViewModel
private var todaySection = TodaySection(rows: [/*.alert,*/ .forecast, .ad,
.conditions, .forecastPeriod, .precipitation,
.dayTime, .sun, .moon])
//Public
init(viewModel: TodayViewModel) {
self.todayViewModel = viewModel
}
public var numberOfSections: Int { public var numberOfSections: Int {
return 1 return 1
} }
public func numberOfRows(inSection section: Int) -> Int { public func numberOfRows(inSection section: Int) -> Int {
return TodayCellType.allCases.count return todaySection.rows.count
} }
public func registerCells(on tableView:UITableView) { public func registerCells(on tableView:UITableView) {
registerCell(type: TodayAlertCell.self, tableView: tableView)
registerCell(type: TodayForecastCell.self, tableView: tableView) registerCell(type: TodayForecastCell.self, tableView: tableView)
registerCell(type: TodayAdCell.self, tableView: tableView) registerCell(type: TodayAdCell.self, tableView: tableView)
registerCell(type: TodayConditionsCell.self, tableView: tableView) registerCell(type: TodayConditionsCell.self, tableView: tableView)
...@@ -41,15 +57,14 @@ class TodayCellFactory: CellFactoryProtocol { ...@@ -41,15 +57,14 @@ class TodayCellFactory: CellFactoryProtocol {
} }
public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell { public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell {
guard let cellType = TodayCellType(rawValue: indexPath.row) else { let cellType = todaySection.rows[indexPath.row]
return UITableViewCell() guard let loc = self.todayViewModel.location else {
}
guard let loc = self.onGetLocation?() else {
return UITableViewCell() return UITableViewCell()
} }
switch cellType { switch cellType {
case .alert:
return dequeueReusableCell(type: TodayAlertCell.self, tableView: tableView, indexPath: indexPath)
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)
......
...@@ -10,8 +10,9 @@ import CoreLocation ...@@ -10,8 +10,9 @@ import CoreLocation
class TodayViewController: UIViewController { class TodayViewController: UIViewController {
//Private //Private
private let viewModel = TodayViewModel()
private let todayCellFactory:TodayCellFactory
private let cityButton = NavigationCityButton() private let cityButton = NavigationCityButton()
private let viewModel:TodayViewModel
private let tableView = UITableView() private let tableView = UITableView()
private var localizationObserver:Any? private var localizationObserver:Any?
...@@ -21,8 +22,8 @@ class TodayViewController: UIViewController { ...@@ -21,8 +22,8 @@ class TodayViewController: UIViewController {
} }
} }
init(viewModel: TodayViewModel) { init() {
self.viewModel = viewModel self.todayCellFactory = TodayCellFactory(viewModel: viewModel)
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
} }
...@@ -90,7 +91,7 @@ private extension TodayViewController { ...@@ -90,7 +91,7 @@ private extension TodayViewController {
} }
func prepareTableView() { func prepareTableView() {
viewModel.todayCellFactory.registerCells(on: tableView) todayCellFactory.registerCells(on: tableView)
tableView.contentInset = .init(top: 0, left: 0, bottom: 15, right: 0) tableView.contentInset = .init(top: 0, left: 0, bottom: 15, right: 0)
tableView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor tableView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
tableView.separatorStyle = .none tableView.separatorStyle = .none
...@@ -110,11 +111,11 @@ private extension TodayViewController { ...@@ -110,11 +111,11 @@ private extension TodayViewController {
//MARK:- UITableView Data Source //MARK:- UITableView Data Source
extension TodayViewController: UITableViewDataSource { extension TodayViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.todayCellFactory.numberOfRows(inSection: section) return todayCellFactory.numberOfRows(inSection: section)
} }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return viewModel.todayCellFactory.cellFromTableView(tableView: tableView, indexPath: indexPath) return todayCellFactory.cellFromTableView(tableView: tableView, indexPath: indexPath)
} }
} }
...@@ -127,13 +128,14 @@ extension TodayViewController: UITableViewDelegate { ...@@ -127,13 +128,14 @@ extension TodayViewController: UITableViewDelegate {
} }
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
viewModel.todayCellFactory.willDisplay(cell: cell) todayCellFactory.willDisplay(cell: cell)
} }
} }
//MARK:- ViewModel Delegate //MARK:- ViewModel Delegate
extension TodayViewController: ViewModelDelegate { extension TodayViewController: ViewModelDelegate {
func viewModelDidChange<P>(model: P) where P : ViewModelProtocol { func viewModelDidChange<P>(model: P) where P : ViewModelProtocol {
print("TodayViewModel did change")
cityButton.configure(with: viewModel.location) cityButton.configure(with: viewModel.location)
cityButton.isHidden = false cityButton.isHidden = false
tableView.reloadData() tableView.reloadData()
......
...@@ -9,31 +9,22 @@ import UIKit ...@@ -9,31 +9,22 @@ import UIKit
class TodayViewModel: ViewModelProtocol { class TodayViewModel: ViewModelProtocol {
//Public //Public
public let todayCellFactory = TodayCellFactory()
public weak var delegate:ViewModelDelegate? public weak var delegate:ViewModelDelegate?
public private(set) var location: Location? private(set) var location:Location?
//Private
private var locationManager: LocationManager
deinit { deinit {
self.locationManager.remove(delegate: self) LocationManager.shared.remove(delegate: self)
Settings.shared.delegate.remove(delegate: self) Settings.shared.delegate.remove(delegate: self)
} }
public init(locationManager: LocationManager) { public init() {
self.locationManager = locationManager self.location = LocationManager.shared.currentLocation
locationManager.add(delegate: self) LocationManager.shared.add(delegate: self)
Settings.shared.delegate.add(delegate: self) Settings.shared.delegate.add(delegate: self)
//Setup factory callback
todayCellFactory.onGetLocation = {[weak self] in
return self?.location
}
} }
public func updateWeather() { public func updateWeather() {
locationManager.updateWeather() LocationManager.shared.updateWeather()
} }
} }
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
<key>XMLCoder.xcscheme_^#shared#^_</key> <key>XMLCoder.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>5</integer> <integer>6</integer>
</dict> </dict>
</dict> </dict>
<key>SuppressBuildableAutocreation</key> <key>SuppressBuildableAutocreation</key>
......
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