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

View File

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