From be094b3cf91879c7d654d3b25c97ce585e3cb2e0 Mon Sep 17 00:00:00 2001 From: "Murugan, Vimal" Date: Mon, 13 Jan 2020 22:49:51 +0530 Subject: [PATCH] review comments updated --- .../Models/Molecules/DoughnutChartModel.swift | 61 +------------------ MVMCoreUI/Molecules/DoughnutChart.swift | 21 ++++--- MVMCoreUI/Molecules/DoughnutChartView.swift | 5 -- 3 files changed, 14 insertions(+), 73 deletions(-) diff --git a/MVMCoreUI/Models/Molecules/DoughnutChartModel.swift b/MVMCoreUI/Models/Molecules/DoughnutChartModel.swift index f6a1efb8..15ecab85 100644 --- a/MVMCoreUI/Models/Molecules/DoughnutChartModel.swift +++ b/MVMCoreUI/Models/Molecules/DoughnutChartModel.swift @@ -9,50 +9,13 @@ import Foundation @objcMembers public class DoughnutChartModel: MoleculeProtocol { - public var backgroundColor: String? public static var identifier: String = "doughnutChart" public var title: LabelModel? public var subtitle: LabelModel? public var sections: [ChartItemModel] - public var lineWidth: CGFloat = 30.0 - public var lineGap: CGFloat = 0.05 //For 3px gap - public var spaceRequired = true - - enum CodingKeys: String, CodingKey { - case list - case title - case subtitle - case sections - } - - required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.title = try typeContainer.decodeMoleculeIfPresent(codingKey: .title) as? LabelModel - self.subtitle = try typeContainer.decodeMoleculeIfPresent(codingKey: .subtitle) as? LabelModel - //self.sections = try typeContainer.decodeMolecules(codingKey: .sections) as! [ChartItemModel] - - //TODO: Hasve to check with scott - self.sections = [] - guard var container = try? typeContainer.nestedUnkeyedContainer(forKey: .sections) else { - return - } - var sections = [ChartItemModel]() - while !container.isAtEnd { - if let element = try container - .decodeIfPresent(ChartItemModel.self) { - sections.append(element) - } - } - self.sections = sections - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encodeModelIfPresent(title, forKey: .title) - try container.encodeModelIfPresent(subtitle, forKey: .subtitle) - try container.encodeModels(sections as [Model], forKey: .sections) - } + public var lineWidth: CGFloat? + public var spaceRequired: Bool? } @@ -62,24 +25,4 @@ import Foundation public var percent: CGFloat public var color: String public static var identifier: String = "chartItem" - - enum CodingKeys: String, CodingKey { - case label - case percent - case color - } - - required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.label = try typeContainer.decodeMolecule(codingKey: .label) as! LabelModel - self.percent = try typeContainer.decode(CGFloat.self, forKey: .percent) - self.color = try typeContainer.decode(String.self, forKey: .color) - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encodeModel(label, forKey: .label) - try container.encode(percent, forKey: .percent) - try container.encode(color, forKey: .color) - } } diff --git a/MVMCoreUI/Molecules/DoughnutChart.swift b/MVMCoreUI/Molecules/DoughnutChart.swift index 08bdefb1..ac19af7d 100644 --- a/MVMCoreUI/Molecules/DoughnutChart.swift +++ b/MVMCoreUI/Molecules/DoughnutChart.swift @@ -21,7 +21,6 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol { var heightConstraint: NSLayoutConstraint? private var sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 150)! - //private var lineSizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 30)! var height: CGFloat = 150 { didSet { @@ -105,7 +104,6 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol { subTitleLabel.setWithModel(doughnutChartModel.subtitle, delegateObject, additionalData) titleLabel.textAlignment = .center subTitleLabel.textAlignment = .center - //lineSizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: doughnutChartModel.lineWidth)! updateLabelContainer() drawGraph() } @@ -118,20 +116,17 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol { height: widthHeight) if let doughnutChart = doughnutChartModel { var prevPercent: CGFloat = 0.0 - //If Single item in array. We don't need gap - doughnutChartModel?.lineGap = (doughnutChart.sections.count == 1) ? 0.0 : doughnutChart.lineGap for model in doughnutChart.sections { - prevPercent += drawBar(model, prevPercent, doughnutChart.spaceRequired, doughnutChart.lineGap) + prevPercent += drawBar(model, prevPercent) } } } - func drawBar(_ chartModel: ChartItemModel, _ prevPercent: CGFloat, _ spaceRequired: Bool, _ lineGap: CGFloat) -> CGFloat { + func drawBar(_ chartModel: ChartItemModel, _ prevPercent: CGFloat) -> CGFloat { let shapeLayer = CAShapeLayer() shapeLayer.frame = containerLayer.frame shapeLayer.lineWidth = lineWidth() - //lineSizeObject.getValueBasedOnApplicationWidth() shapeLayer.fillColor = nil shapeLayer.strokeColor = UIColor.mfGet(forHex: chartModel.color).cgColor @@ -141,7 +136,7 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol { let clockwise = true let value: CGFloat = chartModel.percent - let gap: CGFloat = spaceRequired ? lineGap : 0.0 + let gap: CGFloat = spaceReuired() ? lineGap() : 0.0 let startAngle = ((prevPercent / 100.0) * 2 * .pi) - (0.5 * .pi) let endAngle = ((value / 100.0) * 2 * .pi) + startAngle - gap let circlePath = UIBezierPath(arcCenter: arcCenter, @@ -167,7 +162,15 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol { func lineWidth() -> CGFloat { return doughnutChartModel?.lineWidth ?? 30 - //lineSizeObject.getValueBasedOnApplicationWidth() + 5 + } + + func lineGap() -> CGFloat { + //If array is having the single item then no space required + return doughnutChartModel?.sections.count == 1 ? 0.0 : 0.05 + } + + func spaceReuired() -> Bool { + return doughnutChartModel?.spaceRequired ?? true } func updateLabelContainer() { diff --git a/MVMCoreUI/Molecules/DoughnutChartView.swift b/MVMCoreUI/Molecules/DoughnutChartView.swift index ebe77200..95e23440 100644 --- a/MVMCoreUI/Molecules/DoughnutChartView.swift +++ b/MVMCoreUI/Molecules/DoughnutChartView.swift @@ -171,9 +171,4 @@ class ColorViewWithLabel: View, MVMCoreUIViewConstrainingProtocol { colorView.backgroundColor = UIColor.mfGet(forHex: chartItemModel.color) } - override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) { - Label.setUILabel(label, withJSON: json?.optionalDictionaryForKey("label"), delegate: delegateObject, additionalData: additionalData) - colorView.backgroundColor = UIColor.mfGet(forHex: json?.optionalStringForKey("color") ?? "#000000") - } - }