Commit 9243ce65 by Demid Merzlyakov

IOS-73: add ads to the Forecast creen.

parent 993157e4
...@@ -10,6 +10,7 @@ import UIKit ...@@ -10,6 +10,7 @@ import UIKit
private enum DailyForecastCellType:Int, CaseIterable { private enum DailyForecastCellType:Int, CaseIterable {
case forecast = 0 case forecast = 0
case forecastInfo case forecastInfo
case ad
// case forecast // case forecast
// case conditions // case conditions
// case precipitation // case precipitation
...@@ -21,6 +22,7 @@ private enum DailyForecastCellType:Int, CaseIterable { ...@@ -21,6 +22,7 @@ private enum DailyForecastCellType:Int, CaseIterable {
private enum HourlyForecastCellType: Int, CaseIterable { private enum HourlyForecastCellType: Int, CaseIterable {
case day case day
case forecastHourly case forecastHourly
case ad
case precipitation case precipitation
case wind case wind
} }
...@@ -39,10 +41,11 @@ class ForecastCellFactory: CellFactoryProtocol { ...@@ -39,10 +41,11 @@ class ForecastCellFactory: CellFactoryProtocol {
//Private //Private
private var cellsToUpdate:CellsToUpdate = [.dailyTimePeriod, .hourlyTimePeriod, .dailyForecastInfoCell, .precipitation, .wind] private var cellsToUpdate:CellsToUpdate = [.dailyTimePeriod, .hourlyTimePeriod, .dailyForecastInfoCell, .precipitation, .wind]
private let forecastViewModel:ForecastViewModel private let forecastViewModel:ForecastViewModel
public var timePeriod = TimePeriod.daily private var adViewCache = [TimePeriod: [IndexPath: AdView]]()
//Public //Public
var numberOfSections: Int { public var timePeriod = TimePeriod.daily
public var numberOfSections: Int {
return 1 return 1
} }
...@@ -63,6 +66,7 @@ class ForecastCellFactory: CellFactoryProtocol { ...@@ -63,6 +66,7 @@ class ForecastCellFactory: CellFactoryProtocol {
registerCell(type: ForecastWindSpeedCell.self, tableView: tableView) registerCell(type: ForecastWindSpeedCell.self, tableView: tableView)
registerCell(type: SunPhaseCell.self, tableView: tableView) registerCell(type: SunPhaseCell.self, tableView: tableView)
registerCell(type: MoonPhaseCell.self, tableView: tableView) registerCell(type: MoonPhaseCell.self, tableView: tableView)
registerCell(type: AdCell.self, tableView: tableView)
} }
public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell { public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell {
...@@ -88,11 +92,45 @@ class ForecastCellFactory: CellFactoryProtocol { ...@@ -88,11 +92,45 @@ class ForecastCellFactory: CellFactoryProtocol {
sunCell.updateSunPosition() sunCell.updateSunPosition()
case let moonCell as MoonPhaseCell: case let moonCell as MoonPhaseCell:
moonCell.updateMoonPosition() moonCell.updateMoonPosition()
case let adCell as AdCell:
adCell.adView?.start()
default:
break
}
}
public func didHide(cell: UITableViewCell) {
switch cell {
case let adCell as AdCell:
adCell.adView?.stop()
default: default:
break break
} }
} }
private func adView(for timePeriod: TimePeriod, indexPath: IndexPath) -> AdView {
if let adView = adViewCache[self.timePeriod]?[indexPath] {
return adView
}
let adView = AdView()
var placementName: String!
var timePeriodString: String!
switch timePeriod {
case .daily:
placementName = placementNameForecastExtendedBanner
timePeriodString = "D"
case .hourly:
placementName = placementNameForecastHourlyBanner
timePeriodString = "H"
}
adView.set(placementName: placementName, adType: .banner)
adView.loggingAlias = "🔹 Forecast \(timePeriodString!) Banner"
var edited = adViewCache[timePeriod] ?? [IndexPath: AdView]()
edited[indexPath] = adView
adViewCache[timePeriod] = edited
return adView
}
//Private //Private
private func dailyCellFor(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell { private func dailyCellFor(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell {
guard let cellType = DailyForecastCellType(rawValue: indexPath.row) else { guard let cellType = DailyForecastCellType(rawValue: indexPath.row) else {
...@@ -137,6 +175,10 @@ class ForecastCellFactory: CellFactoryProtocol { ...@@ -137,6 +175,10 @@ class ForecastCellFactory: CellFactoryProtocol {
cell.configure(with: loc) cell.configure(with: loc)
} }
return cell return cell
case .ad:
let cell = dequeueReusableCell(type: AdCell.self, tableView: tableView, indexPath: indexPath)
cell.adView = adView(for: timePeriod, indexPath: indexPath)
return cell
} }
} }
...@@ -179,8 +221,14 @@ class ForecastCellFactory: CellFactoryProtocol { ...@@ -179,8 +221,14 @@ class ForecastCellFactory: CellFactoryProtocol {
} }
} }
return cell return cell
case .ad:
let cell = dequeueReusableCell(type: AdCell.self, tableView: tableView, indexPath: indexPath)
cell.adView = adView(for: timePeriod, indexPath: indexPath)
return cell
} }
} }
} }
//MARK:- ForecastTimePeriodCell Delegate //MARK:- ForecastTimePeriodCell Delegate
......
...@@ -90,7 +90,7 @@ class TodayCellFactory: CellFactoryProtocol { ...@@ -90,7 +90,7 @@ class TodayCellFactory: CellFactoryProtocol {
if let adView = adViewCache[indexPath] { if let adView = adViewCache[indexPath] {
return adView return adView
} }
let adView = adViewCache[indexPath] ?? AdView() let adView = AdView()
adView.loggingAlias = "📍 Today Banner" adView.loggingAlias = "📍 Today Banner"
adView.set(placementName: placementNameTodayBanner, adType: .banner) adView.set(placementName: placementNameTodayBanner, adType: .banner)
adViewCache[indexPath] = adView adViewCache[indexPath] = adView
......
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