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 ac5c65edd0
commit fd8b9956ab
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 { open override func createTableView() -> TableView {
let tableView = super.createTableView() 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 dataSource = UITableViewDiffableDataSource(tableView: tableView) { [weak self] tableView, indexPath, moleculeInfo in
guard let self = self, guard let self = self,
let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId), let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId),
@ -115,8 +121,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
cell.setNeedsLayout() cell.setNeedsLayout()
return cell return cell
} }
tableView.dataSource = dataSource return dataSource
return tableView
} }
open override func parsePageJSON(loadObject: MVMCoreLoadObject) throws -> PageModelProtocol { open override func parsePageJSON(loadObject: MVMCoreLoadObject) throws -> PageModelProtocol {

View File

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