From 78f035ce08a4769d7946ff226676f8e39e37df15 Mon Sep 17 00:00:00 2001 From: Panth Patel Date: Wed, 15 May 2019 12:19:54 -0400 Subject: [PATCH] Molecule ProgressBar --- MVMCoreUI/Molecules/ProgressBar.swift | 67 ++++++---------- MVMCoreUI/Molecules/ProgressBarView.swift | 93 ++++++++++++++++------- 2 files changed, 89 insertions(+), 71 deletions(-) diff --git a/MVMCoreUI/Molecules/ProgressBar.swift b/MVMCoreUI/Molecules/ProgressBar.swift index 1ae7336a..eadf20eb 100644 --- a/MVMCoreUI/Molecules/ProgressBar.swift +++ b/MVMCoreUI/Molecules/ProgressBar.swift @@ -10,53 +10,34 @@ 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..25fd0c23 100644 --- a/MVMCoreUI/Molecules/ProgressBarView.swift +++ b/MVMCoreUI/Molecules/ProgressBarView.swift @@ -11,7 +11,10 @@ import UIKit @objcMembers open class ProgressBarView: ViewConstrainingView { var progress = ProgressBar() - var descriptionLabel = Label() + var topleftlabel = Label() + var toprightlabel = Label() + var bottomleftlabel = Label() + var bottomrightlabel = Label() var thickness: Float? public init() { @@ -35,44 +38,78 @@ 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 topleftlabeljson = json?.optionalDictionaryForKey("label") + let toprightlabeljson = json?.optionalDictionaryForKey("toprightlabel") + let bottomleftlabeljson = json?.optionalDictionaryForKey("bottomleftlabel") + let bottomrightlabeljson = json?.optionalDictionaryForKey("bottomrightlabel") - 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 + topleftlabel.setWithJSON(topleftlabeljson, delegateObject: delegateObject, additionalData: additionalData) + toprightlabel.setWithJSON(toprightlabeljson, delegateObject: delegateObject, additionalData: additionalData) + bottomleftlabel.setWithJSON(bottomleftlabeljson, delegateObject: delegateObject, additionalData: additionalData) + bottomrightlabel.setWithJSON(bottomrightlabeljson, 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(topleftlabel) + addSubview(toprightlabel) + addSubview(bottomleftlabel) + addSubview(bottomrightlabel) addSubview(progress) 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() - + + topleftlabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true + topleftlabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true + let topleftconstarint = NSLayoutConstraint(item: topleftlabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0) + topleftconstarint.priority = UILayoutPriority(rawValue: 100) + topleftconstarint.isActive = true + topleftlabel.setContentHuggingPriority(UILayoutPriority(801), for: .horizontal) + topleftlabel.setContentHuggingPriority(UILayoutPriority(801), for: .vertical) + NSLayoutConstraint(item: toprightlabel, attribute: .leading, relatedBy: .equal, toItem: topleftlabel, attribute: .trailing, multiplier: 1.0, constant: PaddingTwo).isActive = true + toprightlabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true + toprightlabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true + toprightlabel.textAlignment = .right + let toprightconstraint = NSLayoutConstraint(item: toprightlabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0) + toprightconstraint.priority = UILayoutPriority(rawValue: 100) + toprightconstraint.isActive = true + toprightlabel.setContentHuggingPriority(UILayoutPriority(1000), for: .horizontal) + toprightlabel.setContentHuggingPriority(UILayoutPriority(802), for: .vertical) + + let progressconstraintone = NSLayoutConstraint(item: progress, attribute: .top, relatedBy: .equal, toItem: topleftlabel, attribute: .bottom, multiplier: 1.0, constant: PaddingTwo) + progressconstraintone.priority = UILayoutPriority(rawValue: 700) + progressconstraintone.isActive = true + let progressconstrainttwo = NSLayoutConstraint(item: progress, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: toprightlabel, attribute: .bottom, multiplier: 1.0, constant: PaddingTwo) + progressconstrainttwo.priority = UILayoutPriority(rawValue: 1000) + progressconstrainttwo.isActive = true 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 - + bottomleftlabel.topAnchor.constraint(equalTo: progress.bottomAnchor, constant: PaddingTwo).isActive = true + bottomleftlabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true + let bottomleftconstraint = NSLayoutConstraint(item: bottomleftlabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0) + bottomleftconstraint.priority = UILayoutPriority(100) + bottomleftconstraint.isActive = true + bottomleftlabel.setContentHuggingPriority(UILayoutPriority(900), for: .horizontal) + bottomleftlabel.setContentHuggingPriority(UILayoutPriority(850), for: .vertical) + NSLayoutConstraint(item: bottomrightlabel, attribute: .leading, relatedBy: .equal, toItem: bottomleftlabel, attribute: .trailing, multiplier: 1.0, constant: PaddingTwo).isActive = true + bottomrightlabel.topAnchor.constraint(equalTo: progress.bottomAnchor, constant: PaddingTwo).isActive = true + bottomrightlabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true + let bottomrightconstraint = NSLayoutConstraint(item: bottomrightlabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0) + bottomrightconstraint.priority = UILayoutPriority(100) + bottomrightconstraint.isActive = true + bottomrightlabel.textAlignment = .right + bottomrightlabel.setContentHuggingPriority(UILayoutPriority(1000), for: .horizontal) + bottomrightlabel.setContentHuggingPriority(UILayoutPriority(851), for: .vertical) } - - } + + + + + +