Added indexColor property for NumberedList & UnOrderedList

This commit is contained in:
Krishna Kishore Bandaru 2020-12-08 12:56:07 +05:30
parent 695ac8b841
commit 0f6f0e8492
4 changed files with 25 additions and 7 deletions

View File

@ -17,16 +17,18 @@ import Foundation
case moleculeName case moleculeName
case backgroundColor case backgroundColor
case list case list
case indexColor
} }
// Numbered list model comes in the from of list = [MoleculeModelProtocol] // Numbered list model comes in the from of list = [MoleculeModelProtocol]
public required init(from decoder: Decoder) throws { public required init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
let indexColor = try typeContainer.decodeIfPresent(Color.self, forKey: .indexColor)
let list: [MoleculeModelProtocol] = try typeContainer.decodeModels(codingKey: .list) let list: [MoleculeModelProtocol] = try typeContainer.decodeModels(codingKey: .list)
var models: [MoleculeStackItemModel] = [] var models: [MoleculeStackItemModel] = []
for (index, molecule) in list.enumerated() { for (index, molecule) in list.enumerated() {
models.append(MoleculeStackItemModel(with: StringAndMoleculeModel(string: "\(index+1).", molecule: molecule))) models.append(MoleculeStackItemModel(with: StringAndMoleculeModel(string: "\(index+1).", molecule: molecule, indexColor: indexColor)))
} }
super.init(molecules: models, spacing: 0) super.init(molecules: models, spacing: 0)
} }
@ -36,11 +38,15 @@ import Foundation
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encode(moleculeName, forKey: .moleculeName) try container.encode(moleculeName, forKey: .moleculeName)
var indexColor: Color?
var models: [MoleculeModelProtocol] = [] var models: [MoleculeModelProtocol] = []
for molecule in molecules { for molecule in molecules {
models.append(((molecule as! MoleculeStackItemModel).molecule as! StringAndMoleculeModel).molecule) let stringAndMoleculeModel: StringAndMoleculeModel = ((molecule as! MoleculeStackItemModel).molecule as! StringAndMoleculeModel)
indexColor = stringAndMoleculeModel.indexColor
models.append(stringAndMoleculeModel.molecule)
} }
try container.encodeModels(models, forKey: .list) try container.encodeModels(models, forKey: .list)
try container.encodeIfPresent(indexColor, forKey: .indexColor)
} }
} }

View File

@ -13,10 +13,12 @@ public class StringAndMoleculeModel: MoleculeModelProtocol {
public var backgroundColor: Color? public var backgroundColor: Color?
public var string: String public var string: String
public var molecule: MoleculeModelProtocol public var molecule: MoleculeModelProtocol
public var indexColor: Color?
public init(string: String, molecule: MoleculeModelProtocol) { public init(string: String, molecule: MoleculeModelProtocol, indexColor: Color?) {
self.string = string self.string = string
self.molecule = molecule self.molecule = molecule
self.indexColor = indexColor
} }
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
@ -24,6 +26,7 @@ public class StringAndMoleculeModel: MoleculeModelProtocol {
case backgroundColor case backgroundColor
case string case string
case molecule case molecule
case indexColor
} }
public required init(from decoder: Decoder) throws { public required init(from decoder: Decoder) throws {
@ -31,6 +34,7 @@ public class StringAndMoleculeModel: MoleculeModelProtocol {
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
string = try typeContainer.decode(String.self, forKey: .string) string = try typeContainer.decode(String.self, forKey: .string)
molecule = try typeContainer.decodeModel(codingKey: .molecule) molecule = try typeContainer.decodeModel(codingKey: .molecule)
indexColor = try typeContainer.decodeIfPresent(Color.self, forKey: .indexColor)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
@ -39,5 +43,6 @@ public class StringAndMoleculeModel: MoleculeModelProtocol {
try container.encode(string, forKey: .string) try container.encode(string, forKey: .string)
try container.encodeModel(molecule, forKey: .molecule) try container.encodeModel(molecule, forKey: .molecule)
try container.encode(moleculeName, forKey: .moleculeName) try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(indexColor, forKey: .indexColor)
} }
} }

View File

@ -84,6 +84,7 @@ open class StringAndMoleculeView: View {
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
guard let model = model as? StringAndMoleculeModel else { return } guard let model = model as? StringAndMoleculeModel else { return }
label.text = model.string label.text = model.string
label.textColor = model.indexColor?.uiColor ?? .black
molecule.set(with: model.molecule, delegateObject, additionalData) molecule.set(with: model.molecule, delegateObject, additionalData)
} }

View File

@ -19,6 +19,7 @@ import Foundation
case backgroundColor case backgroundColor
case list case list
case bulletChar case bulletChar
case indexColor
} }
// Numbered list model comes in the from of list = [MoleculeModelProtocol] // Numbered list model comes in the from of list = [MoleculeModelProtocol]
@ -28,10 +29,11 @@ import Foundation
self.bulletChar = bulletChar self.bulletChar = bulletChar
} }
let indexColor = try typeContainer.decodeIfPresent(Color.self, forKey: .indexColor)
let list: [MoleculeModelProtocol] = try typeContainer.decodeModels(codingKey: .list) let list: [MoleculeModelProtocol] = try typeContainer.decodeModels(codingKey: .list)
var models: [MoleculeStackItemModel] = [] var models: [MoleculeStackItemModel] = []
for molecule in list { for molecule in list {
models.append(MoleculeStackItemModel(with: StringAndMoleculeModel(string: bulletChar, molecule: molecule))) models.append(MoleculeStackItemModel(with: StringAndMoleculeModel(string: bulletChar, molecule: molecule, indexColor: indexColor)))
} }
super.init(molecules: models, spacing: 0) super.init(molecules: models, spacing: 0)
} }
@ -42,10 +44,14 @@ import Foundation
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encode(moleculeName, forKey: .moleculeName) try container.encode(moleculeName, forKey: .moleculeName)
var indexColor: Color?
var models: [MoleculeModelProtocol] = [] var models: [MoleculeModelProtocol] = []
for molecule in molecules { for molecule in molecules {
models.append(((molecule as! MoleculeStackItemModel).molecule as! StringAndMoleculeModel).molecule) let stringAndMoleculeModel: StringAndMoleculeModel = ((molecule as! MoleculeStackItemModel).molecule as! StringAndMoleculeModel)
indexColor = stringAndMoleculeModel.indexColor
models.append(stringAndMoleculeModel.molecule)
} }
try container.encodeModels(models, forKey: .list) try container.encodeModels(models, forKey: .list)
try container.encodeIfPresent(indexColor, forKey: .indexColor)
} }
} }