Commit 224238c5 by Dmitriy Stepanets

Changed architrecture of TodayViewController

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