Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created October 11, 2022 14:12
Show Gist options
  • Save stevencurtis/20d1b0d083c37ce83372439d6822d31a to your computer and use it in GitHub Desktop.
Save stevencurtis/20d1b0d083c37ce83372439d6822d31a to your computer and use it in GitHub Desktop.
import UIKit
import DataProvider
final class CurrentPlanDataSource: NSObject {
// MARK: - Cell -
enum Cell {
case daysLeft(daysLeft: Int, endDate: Date, newPlan: String?, currentPlanTitle: String)
case cancelDowngrade(previousPlanTitle: String)
case module(CurrentPlan.Module)
case paymentHistory
}
// MARK: - Properties -
var planDetails: CurrentPlan? {
didSet {
updateCells()
}
}
private(set) var cells: [Cell] = []
// MARK: - Functions -
// MARK: Private
private func updateCells() {
guard let planDetails = planDetails else {
cells = []
return
}
cells.removeAll()
if let daysEnd = planDetails.daysLeft, let endDate = planDetails.endsAt {
cells.append(.daysLeft(daysLeft: daysEnd, endDate: endDate, newPlan: planDetails.downgradingTo, currentPlanTitle: planDetails.title))
}
if let _ = planDetails.downgradingTo {
cells.append(.cancelDowngrade(previousPlanTitle: planDetails.title))
}
planDetails.modules.forEach { moduleModel in
cells.append(.module(moduleModel))
}
cells.append(.paymentHistory)
}
}
extension CurrentPlanDataSource: UITableViewDataSource {
// MARK: - UITableViewDataSource -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
cells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch cells[indexPath.row] {
case let .daysLeft(daysLeft, endDate, newPlan, currentPlan):
let cell: DaysLeftTableViewCell = tableView.dequeueCell(for: indexPath)
let viewModel = DaysLeftCellViewModel(daysLeft: daysLeft, endDate: endDate, planToTransition: newPlan, currentPlan: currentPlan)
cell.configure(with: viewModel)
return cell
case .cancelDowngrade(let previousPlanTitle):
let cell: CancelDowngradeTableViewCell = tableView.dequeueCell(for: indexPath)
cell.configure(with: previousPlanTitle)
return cell
case .module(let model):
let cell: CurrentPlanModuleTableViewCell = tableView.dequeueCell(for: indexPath)
let viewModel = CurrentPlanModuleCellViewModel(module: model)
cell.configure(with: viewModel)
return cell
case .paymentHistory:
let cell: PlanPaymentHistoryTableViewCell = tableView.dequeueCell(for: indexPath)
return cell
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment