Merge branch 'bugfix/same_json_equatable' into 'develop'

Bugfix/same json equatable

See merge request BPHV_MIPS/mvm_core_ui!752
This commit is contained in:
Pfeil, Scott Robert 2021-08-12 17:57:36 +00:00
commit ee574adc8c
5 changed files with 20 additions and 12 deletions

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
public struct ImageHeadlineBodyModel: MoleculeModelProtocol { public class ImageHeadlineBodyModel: MoleculeModelProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
public struct ActionDetailWithImageModel: MoleculeModelProtocol { public class ActionDetailWithImageModel: MoleculeModelProtocol {
public static var identifier: String = "actionDetailWithImage" public static var identifier: String = "actionDetailWithImage"
public var moleculeName: String = ActionDetailWithImageModel.identifier public var moleculeName: String = ActionDetailWithImageModel.identifier
public var backgroundColor: Color? public var backgroundColor: Color?

View File

@ -7,7 +7,7 @@
// //
import Foundation import Foundation
public struct HeadlineBodyLinkToggleModel: MoleculeModelProtocol { public class HeadlineBodyLinkToggleModel: MoleculeModelProtocol {
public static var identifier: String = "headlineBodyLinkToggle" public static var identifier: String = "headlineBodyLinkToggle"
public var moleculeName: String = HeadlineBodyLinkToggleModel.identifier public var moleculeName: String = HeadlineBodyLinkToggleModel.identifier
public var backgroundColor: Color? public var backgroundColor: Color?

View File

@ -9,7 +9,7 @@
import Foundation import Foundation
public struct HeadlineBodyLinkModel: MoleculeModelProtocol { public class HeadlineBodyLinkModel: MoleculeModelProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------

View File

@ -188,10 +188,8 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
open func newData(for molecule: MoleculeModelProtocol) { open func newData(for molecule: MoleculeModelProtocol) {
//TODO: expand for header, navigation, etc //TODO: expand for header, navigation, etc
let json = molecule.toJSON()
guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in
//TODO: check for molecule protocol eqaulity if equal(moleculeA: molecule, moleculeB: moleculeInfo.molecule) {
if json == moleculeInfo.molecule.toJSON() {
return true return true
} else if let parent = moleculeInfo.molecule as? ParentMoleculeModelProtocol { } else if let parent = moleculeInfo.molecule as? ParentMoleculeModelProtocol {
// Get all molecules of the same type for faster check. // Get all molecules of the same type for faster check.
@ -201,8 +199,8 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
} }
return accumulator return accumulator
} }
for molecule in molecules { for moleculeB in molecules {
if json == molecule.toJSON() { if equal(moleculeA: molecule, moleculeB: moleculeB) {
return true return true
} }
} }
@ -283,6 +281,18 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
return modules return modules
} }
/// Checks if the two molecules are equal
private func equal(moleculeA: MoleculeModelProtocol, moleculeB: MoleculeModelProtocol) -> Bool {
// TODO: move this to a better approach, maybe a UUID for each model.
// Do instance check
if let classMoleculeA = moleculeA as? NSObjectProtocol,
let classMoleculeB = moleculeB as? NSObjectProtocol {
return classMoleculeA === classMoleculeB
}
// Do json check
return moleculeA.toJSON() == moleculeB.toJSON()
}
} }
extension MoleculeListTemplate: MoleculeListProtocol { extension MoleculeListTemplate: MoleculeListProtocol {
@ -320,9 +330,7 @@ extension MoleculeListTemplate: MoleculeListProtocol {
open func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { open func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? {
guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in
//TODO: check for molecule protocol eqaulity return equal(moleculeA: molecule, moleculeB: moleculeInfo.molecule)
let json = moleculeInfo.molecule.toJSON()
return json == molecule.toJSON()
}) else { return nil } }) else { return nil }
return IndexPath(row: index, section: 0) return IndexPath(row: index, section: 0)