Commit dd049516 by Demid Merzlyakov

Added an ability to hide rows on the Today screen that are not needed.

parent dcd92a0e
......@@ -30,14 +30,30 @@ private struct CellsToUpdate:OptionSet {
}
private struct TodaySection {
var rows:[TodayCellType]
var rows: [TodayCellType]
var hiddenRows = Set<TodayCellType>() {
didSet {
var filteredRows = allRows
if !hiddenRows.isEmpty {
filteredRows.removeAll(where: { hiddenRows.contains($0) })
}
rows = filteredRows
}
}
let allRows: [TodayCellType]
init(allRows: [TodayCellType]) {
self.allRows = allRows
self.rows = allRows
}
}
class TodayCellFactory: CellFactoryProtocol {
//Private
private var cellsToUpdate:CellsToUpdate = [.condition, .timePeriod, .precipitation, .dayTime]
private let todayViewModel:TodayViewModel
private var todaySection = TodaySection(rows: [.alert, .forecast, .ad,
private var todaySection = TodaySection(allRows: [.alert, .forecast, .ad,
.conditions, .forecastPeriod, .precipitation,
.airQuality, .dayTime, .sun, .moon])
......@@ -142,6 +158,7 @@ class TodayCellFactory: CellFactoryProtocol {
public func setNeedsUpdate() {
cellsToUpdate = [.condition, .timePeriod, .precipitation, .dayTime]
setupHiddenRows()
}
public func willDisplay(cell: UITableViewCell) {
......@@ -165,4 +182,23 @@ class TodayCellFactory: CellFactoryProtocol {
break
}
}
private func setupHiddenRows() {
var rowsToHide = Set<TodayCellType>()
if !AdConfigManager.shared.adConfig.adsEnabled {
rowsToHide.insert(.ad)
}
let location = self.todayViewModel.location
if location?.notifications?.nwsAlers.count ?? 0 == 0 {
rowsToHide.insert(.alert)
}
if location?.health?.airQuality == nil {
rowsToHide.insert(.airQuality)
}
todaySection.hiddenRows = rowsToHide
}
}
......@@ -12,7 +12,7 @@ class TodayViewController: UIViewController {
//Private
private let coordinator:TodayCoordinator
private let viewModel = TodayViewModel()
private let todayCellFactory:TodayCellFactory
private let notificationButton = UIButton()
private let cityButton = NavigationCityButton()
private let tableView = UITableView()
......@@ -26,7 +26,6 @@ class TodayViewController: UIViewController {
init(coordinator:TodayCoordinator) {
self.coordinator = coordinator
self.todayCellFactory = TodayCellFactory(viewModel: viewModel)
super.init(nibName: nil, bundle: nil)
}
......@@ -93,7 +92,7 @@ private extension TodayViewController {
}
func prepareTableView() {
todayCellFactory.registerCells(on: tableView)
viewModel.todayCellFactory.registerCells(on: tableView)
tableView.isHidden = true
tableView.contentInset = .init(top: 0, left: 0, bottom: 15, right: 0)
tableView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
......@@ -114,11 +113,11 @@ private extension TodayViewController {
//MARK:- UITableView Data Source
extension TodayViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todayCellFactory.numberOfRows(inSection: section)
return viewModel.todayCellFactory.numberOfRows(inSection: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return todayCellFactory.cellFromTableView(tableView: tableView, indexPath: indexPath)
return viewModel.todayCellFactory.cellFromTableView(tableView: tableView, indexPath: indexPath)
}
}
......@@ -129,11 +128,11 @@ extension TodayViewController: UITableViewDelegate {
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
todayCellFactory.willDisplay(cell: cell)
viewModel.todayCellFactory.willDisplay(cell: cell)
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
todayCellFactory.didHide(cell: cell)
viewModel.todayCellFactory.didHide(cell: cell)
}
}
......@@ -143,7 +142,6 @@ extension TodayViewController: ViewModelDelegate {
cityButton.configure(with: viewModel.location)
cityButton.isHidden = false
tableView.isHidden = false
todayCellFactory.setNeedsUpdate()
tableView.reloadData()
}
}
......@@ -13,6 +13,10 @@ class TodayViewModel: ViewModelProtocol {
private let locationManager = LocationManager.shared
private(set) var location:Location?
public lazy var todayCellFactory:TodayCellFactory = {
TodayCellFactory(viewModel: self)
}()
deinit {
locationManager.remove(delegate: self)
Settings.shared.delegate.remove(delegate: self)
......@@ -34,6 +38,7 @@ extension TodayViewModel: LocationManagerDelegate {
func locationManager(_ locationManager: LocationManager, changedSelectedLocation newLocation: Location?) {
DispatchQueue.main.async {
self.location = newLocation
self.todayCellFactory.setNeedsUpdate()
self.delegate?.viewModelDidChange(model: self)
}
}
......
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