Revise codes following MR comments

This commit is contained in:
Xi Zhang 2024-07-12 16:12:54 -04:00
parent de60fdfaf9
commit 53c4bd6c46
2 changed files with 52 additions and 57 deletions

View File

@ -23,17 +23,8 @@ import UIKit
private var tracklayer = CAShapeLayer() private var tracklayer = CAShapeLayer()
private var labelLayer = CATextLayer() private var labelLayer = CATextLayer()
var setProgressColor: UIColor = UIColor.red { var progressColor: UIColor = UIColor.red
didSet { var trackColor: UIColor = UIColor.lightGray
progressLayer.strokeColor = setProgressColor.cgColor
}
}
var setTrackColor: UIColor = UIColor.lightGray {
didSet {
tracklayer.strokeColor = setTrackColor.cgColor
}
}
// A path with which CAShapeLayer will be drawn on the screen // A path with which CAShapeLayer will be drawn on the screen
private var viewCGPath: CGPath? { private var viewCGPath: CGPath? {
@ -61,37 +52,29 @@ import UIKit
guard let model = model as? CircularProgressBarModel else { return } guard let model = model as? CircularProgressBarModel else { return }
// set background color // set background color
if let backgroundColor = model.backgroundColor { backgroundColor = model.backgroundColor?.uiColor ?? UIColor.clear
self.backgroundColor = backgroundColor.uiColor
} else {
self.backgroundColor = UIColor.clear
}
configureProgressViewToBeCircular() configureProgressViewToBeCircular()
// set progress color // set progress color
if let color = model.color { progressColor = model.color?.uiColor ?? .red
setProgressColor = color.uiColor progressLayer.strokeColor = progressColor.cgColor
} else {
setProgressColor = UIColor.red
}
// set track color // set track color
if let trackColor = model.trackColor { trackColor = model.trackColor?.uiColor ?? .lightGray
setTrackColor = trackColor.uiColor tracklayer.strokeColor = trackColor.cgColor
} else {
setProgressColor = UIColor.lightGray
}
// show circular progress view with animation. // show circular progress view with animation.
showProgressWithAnimation(duration: graphModel?.duration ?? 1.0, value: Float(graphModel?.percent ?? 0) / 100) showProgressWithAnimation(duration: graphModel?.duration ?? 1.0, value: Float(graphModel?.percent ?? 0) / 100)
// show progress percentage label. // show progress percentage label.
showProgressPercentage() if let drawText = model.drawText, drawText {
showProgressPercentage()
}
} }
private func configureProgressViewToBeCircular() { private func configureProgressViewToBeCircular() {
let lineWidth = graphModel?.lineWidth ?? 5.0 let lineWidth = graphModel?.lineWidth ?? 4.0
self.drawShape(using: tracklayer, lineWidth: lineWidth) self.drawShape(using: tracklayer, lineWidth: lineWidth)
self.drawShape(using: progressLayer, lineWidth: lineWidth) self.drawShape(using: progressLayer, lineWidth: lineWidth)

View File

@ -2,29 +2,31 @@
// CircularProgressBarModel.swift // CircularProgressBarModel.swift
// MVMCoreUI // MVMCoreUI
// //
// https://oneconfluence.verizon.com/display/MFD/Circular+Progress+Tracker
//
// Created by Xi Zhang on 7/5/24. // Created by Xi Zhang on 7/5/24.
// Copyright © 2024 Verizon Wireless. All rights reserved. // Copyright © 2024 Verizon Wireless. All rights reserved.
// //
import Foundation import Foundation
public class CircularProgressBarModel: MoleculeModelProtocol { public class CircularProgressBarModel: MoleculeModelProtocol {
public static var identifier: String = "circularProgress" public static var identifier: String = "circularProgress"
public var id: String = UUID().uuidString public var id: String = UUID().uuidString
public var size: GraphSize = .small { public var percent: Int = 0
public var size: GraphSize? = .small {
didSet { didSet {
updateSize() updateSize()
} }
} }
public var diameter: CGFloat = 84 public var diameter: CGFloat? = 84
public var lineWidth: CGFloat = 5 public var lineWidth: CGFloat? = 4
public var duration : Double = 1.0 public var duration : Double? = 1.0
public var color: Color? public var color: Color? = Color(uiColor: UIColor.mfGet(forHex: "#007AB8"))
public var trackColor: Color? public var trackColor: Color? = Color(uiColor: .mvmCoolGray3)
public var percent: Int? public var drawText: Bool? = true
public var backgroundColor: Color? = Color(uiColor: UIColor.clear) public var backgroundColor: Color? = Color(uiColor: UIColor.clear)
public init() { public init() {
@ -33,21 +35,24 @@ public class CircularProgressBarModel: MoleculeModelProtocol {
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case id case id
case moleculeName
case percent
case size case size
case diameter case diameter
case lineWidth case lineWidth
case duration case duration
case color case color
case trackColor case trackColor
case percent case drawText
case backgroundColor case backgroundColor
case moleculeName
} }
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
percent = try typeContainer.decode(Int.self, forKey: .percent)
if let size = try typeContainer.decodeIfPresent(GraphSize.self, forKey: .size) { if let size = try typeContainer.decodeIfPresent(GraphSize.self, forKey: .size) {
self.size = size self.size = size
} }
@ -65,46 +70,53 @@ public class CircularProgressBarModel: MoleculeModelProtocol {
self.duration = duration self.duration = duration
} }
color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) if let drawText = try typeContainer.decodeIfPresent(Bool.self, forKey: .drawText) {
trackColor = try typeContainer.decodeIfPresent(Color.self, forKey: .trackColor) self.drawText = drawText
percent = try typeContainer.decodeIfPresent(Int.self, forKey: .percent) }
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) {
self.color = color
}
if let trackColor = try typeContainer.decodeIfPresent(Color.self, forKey: .trackColor) {
self.trackColor = trackColor
}
if let backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) {
self.backgroundColor = backgroundColor
}
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id) try container.encode(id, forKey: .id)
try container.encode(moleculeName, forKey: .moleculeName) try container.encode(moleculeName, forKey: .moleculeName)
try container.encode(size, forKey: .size) try container.encode(percent, forKey: .percent)
try container.encode(diameter, forKey: .diameter) try container.encodeIfPresent(size, forKey: .size)
try container.encode(lineWidth, forKey: .lineWidth) try container.encodeIfPresent(diameter, forKey: .diameter)
try container.encode(duration, forKey: .duration) try container.encodeIfPresent(lineWidth, forKey: .lineWidth)
try container.encodeIfPresent(duration, forKey: .duration)
try container.encodeIfPresent(drawText, forKey: .drawText)
try container.encodeIfPresent(trackColor, forKey: .trackColor) try container.encodeIfPresent(trackColor, forKey: .trackColor)
try container.encodeIfPresent(color, forKey: .color) try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(percent, forKey: .percent)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
} }
func getCGColorsFromArray(_ colorArray: [String]) -> [Color] {
return colorArray.map { (colorString) -> Color in
return Color(uiColor: UIColor.mfGet(forHex: colorString))
}
}
func updateSize() { func updateSize() {
switch size { switch size {
case .small: case .small:
diameter = MFSizeObject(standardSize: 64)?.getValueBasedOnApplicationWidth() ?? 64 diameter = MFSizeObject(standardSize: 64)?.getValueBasedOnApplicationWidth() ?? 64
lineWidth = MFSizeObject(standardSize: 5)?.getValueBasedOnApplicationWidth() ?? 5 lineWidth = MFSizeObject(standardSize: 4)?.getValueBasedOnApplicationWidth() ?? 4
break break
case .medium: case .medium:
diameter = MFSizeObject(standardSize: 84)?.getValueBasedOnApplicationWidth() ?? 84 diameter = MFSizeObject(standardSize: 84)?.getValueBasedOnApplicationWidth() ?? 84
lineWidth = MFSizeObject(standardSize: 5)?.getValueBasedOnApplicationWidth() ?? 5 lineWidth = MFSizeObject(standardSize: 4)?.getValueBasedOnApplicationWidth() ?? 4
break break
case .large: case .large:
diameter = MFSizeObject(standardSize: 124)?.getValueBasedOnApplicationWidth() ?? 124 diameter = MFSizeObject(standardSize: 124)?.getValueBasedOnApplicationWidth() ?? 124
lineWidth = MFSizeObject(standardSize: 5)?.getValueBasedOnApplicationWidth() ?? 5 lineWidth = MFSizeObject(standardSize: 4)?.getValueBasedOnApplicationWidth() ?? 4
break
case .none:
break break
} }
} }