Digital PCT265 story MVAPCT-272: Adjust code for sectionList template.

This commit is contained in:
Hedden, Kyle Matthew 2024-10-17 22:04:32 -04:00
parent 1a93309d9d
commit 8ec1a4495e
3 changed files with 39 additions and 48 deletions

View File

@ -99,6 +99,12 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
open override func createTableView() -> TableView {
let tableView = super.createTableView()
tableView.dataSource = dataSource
return tableView
}
open override func setupDataSource(for tableView:
UITableView) -> UITableViewDataSource {
dataSource = UITableViewDiffableDataSource(tableView: tableView) { [weak self] tableView, indexPath, moleculeInfo in
guard let self = self,
let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId),
@ -115,8 +121,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
cell.setNeedsLayout()
return cell
}
tableView.dataSource = dataSource
return tableView
return dataSource
}
open override func parsePageJSON(loadObject: MVMCoreLoadObject) throws -> PageModelProtocol {

View File

@ -9,36 +9,32 @@
open class SectionListTemplate: MoleculeListTemplate {
public var sectionMoleculesInfo: [(header: MoleculeInfo?, footer: MoleculeInfo?, rows: [MoleculeInfo])]?
public var sectionMoleculesInfo: [(header: SectionMoleculeInfo?, footer: SectionMoleculeInfo?)] = []
open override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableView.automaticDimension
}
override func setup() {
// Create quick reference
var sectionList: [(header: MoleculeInfo?, footer: MoleculeInfo?, rows: [MoleculeInfo])] = []
var sectionList: [(header: SectionMoleculeInfo?, footer: SectionMoleculeInfo?)] = []
if let sections = (templateModel as? SectionListTemplateModel)?.sections {
for section in sections {
let header = createMoleculeInfo(with: section.header)
let footer = createMoleculeInfo(with: section.footer)
var rows: [MoleculeInfo] = []
for row in section.rows {
if let rowInfo = createMoleculeInfo(with: row) {
rows.append(rowInfo)
}
}
sectionList.append((header: header, footer: footer, rows: rows))
}
guard let sections = (templateModel as? SectionListTemplateModel)?.sections else { return }
moleculeModelCache.removeAll() // Refresh the cache for full page reload.
var initialDataSnapshot = ListDataSnapshot()
initialDataSnapshot.appendSections(Array(0..<sections.count))
for (index, section) in sections.enumerated() {
let header = createSectionMoleculeInfo(with: section.header)
let footer = createSectionMoleculeInfo(with: section.footer)
sectionList.append((header, footer))
let listItems = register(listItemModels: section.rows)
initialDataSnapshot.appendItems(listItems, toSection: index)
}
sectionMoleculesInfo = sectionList.count > 0 ? sectionList : nil
}
func getMoleculeInfo(for indexPath: IndexPath) -> MoleculeInfo? {
sectionMoleculesInfo?[indexPath.section].rows[indexPath.row]
sectionMoleculesInfo = sectionList
dataSource.apply(initialDataSnapshot)
}
open override func createTableView() -> TableView {
@ -46,7 +42,7 @@ open class SectionListTemplate: MoleculeListTemplate {
tableView.backgroundColor = .clear
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
tableView.delegate = self
tableView.dataSource = self
tableView.dataSource = setupDataSource(for: tableView)
tableView.insetsContentViewsToSafeArea = false
return tableView
}
@ -58,22 +54,18 @@ open class SectionListTemplate: MoleculeListTemplate {
open override func registerWithTable() {
super.registerWithTable()
guard let sections = sectionMoleculesInfo else { return }
for section in sections {
for section in sectionMoleculesInfo {
if let header = section.header {
tableView.register(header.class, forHeaderFooterViewReuseIdentifier: header.identifier)
}
if let footer = section.footer {
tableView.register(footer.class, forHeaderFooterViewReuseIdentifier: footer.identifier)
}
for row in section.rows {
tableView.register(row.class, forCellReuseIdentifier: row.identifier)
}
}
}
public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
guard let moleculeInfo = sectionMoleculesInfo?[section].header else {
guard let moleculeInfo = sectionMoleculesInfo[safe: section]?.header else {
return CGFloat.leastNormalMagnitude
}
let estimatedHeight = (moleculeInfo.class as? MoleculeViewProtocol.Type)?.estimatedHeight(with: moleculeInfo.molecule, delegateObject() as? MVMCoreUIDelegateObject)
@ -81,14 +73,14 @@ open class SectionListTemplate: MoleculeListTemplate {
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard (sectionMoleculesInfo?[section].header) != nil else {
guard (sectionMoleculesInfo[safe:section]?.header) != nil else {
return CGFloat.leastNormalMagnitude
}
return UITableView.automaticDimension
}
public func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
guard let moleculeInfo = sectionMoleculesInfo?[section].footer else {
guard let moleculeInfo = sectionMoleculesInfo[safe:section]?.footer else {
return CGFloat.leastNormalMagnitude
}
let estimatedHeight = (moleculeInfo.class as? MoleculeViewProtocol.Type)?.estimatedHeight(with: moleculeInfo.molecule, delegateObject() as? MVMCoreUIDelegateObject)
@ -96,15 +88,14 @@ open class SectionListTemplate: MoleculeListTemplate {
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
guard (sectionMoleculesInfo?[section].footer) != nil else {
guard (sectionMoleculesInfo[safe:section]?.footer) != nil else {
return CGFloat.leastNormalMagnitude
}
return UITableView.automaticDimension
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let sectionInfo = sectionMoleculesInfo?[section],
let headerInfo = sectionInfo.header
guard let headerInfo = sectionMoleculesInfo[safe: section]?.header
else { return nil }
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerInfo.identifier)
@ -117,8 +108,7 @@ open class SectionListTemplate: MoleculeListTemplate {
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard let sectionInfo = sectionMoleculesInfo?[section],
let footerInfo = sectionInfo.footer
guard let footerInfo = sectionMoleculesInfo[safe: section]?.footer
else { return nil }
let footer = tableView.dequeueReusableHeaderFooterView(withIdentifier: footerInfo.identifier)
@ -130,15 +120,7 @@ open class SectionListTemplate: MoleculeListTemplate {
return footer
}
open override func getNumberOfSections() -> Int {
sectionMoleculesInfo?.count ?? 0
}
open override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
sectionMoleculesInfo?[section].rows.count ?? 0
}
func createMoleculeInfo(with listItem: MoleculeModelProtocol?) -> MoleculeInfo? {
func createSectionMoleculeInfo(with listItem: MoleculeModelProtocol?) -> SectionMoleculeInfo? {
guard let listItem = listItem,
let moleculeClass = ModelRegistry.getMoleculeClass(listItem)
@ -149,6 +131,6 @@ open class SectionListTemplate: MoleculeListTemplate {
return (moleculeName, moleculeClass, listItem)
}
public typealias MoleculeInfo = (identifier: String, class: AnyClass, molecule: MoleculeModelProtocol)
public typealias SectionMoleculeInfo = (identifier: String, class: AnyClass, molecule: MoleculeModelProtocol)
}

View File

@ -68,11 +68,15 @@ open class ProgrammaticTableViewController: ProgrammaticScrollViewController, UI
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.delegate = self
tableView.dataSource = self
tableView.dataSource = setupDataSource(for: tableView)
tableView.insetsContentViewsToSafeArea = false
return tableView
}
open func setupDataSource(for tableView: UITableView) -> UITableViewDataSource {
return self
}
/// Registers classes and nibs. Can subclass for different nibs. Can call super and then add new ones after as well.
open func registerWithTable() { }