From 2bd55296b8719159ed079c15651de2c59a687d6f Mon Sep 17 00:00:00 2001 From: Panth Patel Date: Thu, 9 May 2019 17:31:18 -0400 Subject: [PATCH] ProgressBar molecule with a Label --- MVMCoreUI/Molecules/ProgressBar.swift | 65 +++++++++-------------- MVMCoreUI/Molecules/ProgressBarView.swift | 29 ++++------ 2 files changed, 33 insertions(+), 61 deletions(-) diff --git a/MVMCoreUI/Molecules/ProgressBar.swift b/MVMCoreUI/Molecules/ProgressBar.swift index 1ae7336a..15e0fedc 100644 --- a/MVMCoreUI/Molecules/ProgressBar.swift +++ b/MVMCoreUI/Molecules/ProgressBar.swift @@ -10,53 +10,36 @@ import Foundation public class ProgressBar: UIProgressView { - public func styleprogessbar(json: Dictionary) { - let percentage = Float(json["percent"] as! String) - let barstyle = json["barStyle"] as! Bool - let progresscolor = json["progressColor"] as! String - let backgroundcolor = json["backgroundColor"] as! String - let thickness = Float(json["thickness"] as! String) + public func styleprogessbar(json: [AnyHashable: Any]?) { + + var barstyle = Bool() + var progresscolor = String() + var backgroundcolor = String() + + let thickness = json?.floatFromStringForKey("thickness") + let percentage = json?.floatFromStringForKey("percent") + + if let backgroundcolorUnwrapped = json?.optionalStringForKey("backgroundColor") { + backgroundcolor = backgroundcolorUnwrapped + } + if let progresscolorUnwrapped = json?.optionalStringForKey("progressColor") { + progresscolor = progresscolorUnwrapped + } + if let barStyleUnwrapped = json?["barStyle"] as? Bool { + barstyle = barStyleUnwrapped + } + progressTintColor = UIColor.mfGet(forHex: progresscolor) + trackTintColor = UIColor.mfGet(forHex: backgroundcolor) + progress = (percentage ?? Float(PaddingThree))/100 - self.progressTintColor = UIColor.init(hex: progresscolor) - self.trackTintColor = UIColor.init(hex: backgroundcolor) - self.progress = percentage!/100 - self.frame.size.width = 200 switch barstyle { case true: - self.progressViewStyle = .bar + progressViewStyle = .bar default: - self.layer.cornerRadius = CGFloat(thickness!/2) - self.clipsToBounds = true + layer.cornerRadius = CGFloat((thickness ?? Float(PaddingTwo))/2) + clipsToBounds = true } } } - -extension UIColor { - public convenience init?(hex: String) { - let r, g, b, a: CGFloat - - if hex.hasPrefix("#") { - let start = hex.index(hex.startIndex, offsetBy: 1) - let hexColor = String(hex[start...]) - - if hexColor.count == 6 { - let scanner = Scanner(string: hexColor) - var hexNumber: UInt32 = 0 - - if scanner.scanHexInt32(&hexNumber) { - r = CGFloat((hexNumber & 0xff0000) >> 16) / 255 - g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255 - b = CGFloat(hexNumber & 0x0000ff) / 255 - - self.init(red: r, green: g, blue: b, alpha: 1.0) - return - } - } - - } - - return nil - } -} diff --git a/MVMCoreUI/Molecules/ProgressBarView.swift b/MVMCoreUI/Molecules/ProgressBarView.swift index 61cdde7b..4abb7e52 100644 --- a/MVMCoreUI/Molecules/ProgressBarView.swift +++ b/MVMCoreUI/Molecules/ProgressBarView.swift @@ -9,7 +9,7 @@ import UIKit @objcMembers open class ProgressBarView: ViewConstrainingView { - + var progress = ProgressBar() var descriptionLabel = Label() var thickness: Float? @@ -37,42 +37,31 @@ import UIKit open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) + progress.styleprogessbar(json: json) + let thickness = json?.floatFromStringForKey("thickness") + let textlabeljson = json?.optionalDictionaryForKey("label") - if let jsonUnwrapped = json as? Dictionary { - progress.styleprogessbar(json: jsonUnwrapped) - } - - let textlabeljson = json!["label"] as! Dictionary //UILabel.init(frame: CGRect(x:0, y:0, width: 300, height: 300)) - self.descriptionLabel.setWithJSON(textlabeljson, delegateObject: delegateObject, additionalData: additionalData) - thickness = Float(json!["thickness"] as! String) - // thickness = 20.0 + descriptionLabel.setWithJSON(textlabeljson, delegateObject: delegateObject, additionalData: additionalData) + progress.heightAnchor.constraint(equalToConstant: CGFloat(thickness ?? Float(PaddingTwo))).isActive = true } override open func setupView() { super.setupView() - translatesAutoresizingMaskIntoConstraints = false addSubview(descriptionLabel) addSubview(progress) + let vericalSpacing = MFStyler.defaultVerticalPaddingForApplicationWidth() progress.translatesAutoresizingMaskIntoConstraints = false descriptionLabel.translatesAutoresizingMaskIntoConstraints = false - let vericalSpacing = MFStyler.defaultVerticalPaddingForApplicationWidth() - descriptionLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true descriptionLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true descriptionLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true - descriptionLabel.numberOfLines = 0 - descriptionLabel.sizeToFit() - progress.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true progress.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true - progress.heightAnchor.constraint(equalToConstant: 20).isActive = true progress.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: vericalSpacing).isActive = true progress.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true - - } - - + + }