Files
2021-09-02 09:58:15 +00:00

59 lines
2.2 KiB
Swift

//
// NotificationsTableViewManager.swift
// PrivadoVPN
//
// Created by Zhandos Bolatbekov on 21.01.2021.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import UIKit
final class NotificationsTableViewManager: NSObject, UITableViewDataSource, UITableViewDelegate {
enum Constants {
static let cellIdentifier = "CellIdentifier"
static let cellIdentifierIPAD = "CellIdentifierIPAD"
}
private weak var tableView: UITableView?
private var notificationCells: [NotificationCellAdapter] = []
// MARK: - Init
init(tableView: UITableView) {
super.init()
self.tableView = tableView
tableView.delegate = self
tableView.dataSource = self
tableView.register(NotificationTableViewCell.self, forCellReuseIdentifier: Constants.cellIdentifier)
tableView.register(NotificationTableViewCellIPAD.self, forCellReuseIdentifier: Constants.cellIdentifierIPAD)
}
// MARK: - Interface
func display(notificationCells: [NotificationCellAdapter]) {
self.notificationCells = notificationCells
self.tableView?.reloadData()
}
// MARK: - UITableViewDelegate & UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.notificationCells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cellAdapter = notificationCells[safe: indexPath.row] else { return UITableViewCell() }
var cell: UITableViewCell?
if cellAdapter.type == .iphone {
cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellIdentifier, for: indexPath) as? NotificationTableViewCell
} else {
cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellIdentifierIPAD, for: indexPath) as? NotificationTableViewCellIPAD
}
guard let cellToDisplay = cell as? NotificationCellInput else { return UITableViewCell() }
guard let cell = cell else { return UITableViewCell() }
cellToDisplay.display(viewAdapter: cellAdapter)
return cell
}
}