using existing model encoding func

This commit is contained in:
Kevin G Christiano 2020-01-24 13:20:02 -05:00
parent 7b8b2a059f
commit c250769620
2 changed files with 7 additions and 14 deletions

View File

@ -14,8 +14,10 @@ import Foundation
public var backgroundColor: Color?
public var peakingUI: Bool?
public var peakingArrowColor: Color?
public var moleculeName: String?
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
case peakingUI
case peakingArrowColor
@ -24,6 +26,7 @@ import Foundation
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
moleculeName = try typeContainer.decodeIfPresent(String.self, forKey: .moleculeName)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
peakingUI = try typeContainer.decodeIfPresent(Bool.self, forKey: .peakingUI)
peakingArrowColor = try typeContainer.decodeIfPresent(Color.self, forKey: .peakingArrowColor)
@ -33,6 +36,7 @@ import Foundation
public override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(peakingUI, forKey: .peakingUI)
try container.encodeIfPresent(peakingArrowColor, forKey: .peakingArrowColor)

View File

@ -32,21 +32,10 @@ import UIKit
guard newValue != oldValue,
let self = self,
let index = self.dropDown.pickerData.firstIndex(of: newValue),
let molecules = self.dropDownListItemModel?.molecules
let dropListItemJSON = self.dropDownListItemModel.toJSON(),
let json2d = dropListItemJSON.optionalArrayForKey("molecules") as? [[[AnyHashable: Any]]]
else { return }
var json2d = [[[AnyHashable: Any]]]()
for moleculeList in molecules {
var json1d = [[AnyHashable: Any]]()
for molecule in moleculeList {
if let moleculeDictionary = (molecule as? ListItemModel)?.toJSON() {
json1d.append(moleculeDictionary)
}
}
json2d.append(json1d)
}
if self.previousIndex != NSNotFound {
self.delegateObject?.moleculeDelegate?.removeMolecules(json2d[self.previousIndex], sender: self, animation: .fade)
}