Code changes based on review comments.

This commit is contained in:
Lekshmi S 2020-10-12 19:46:35 +05:30
parent 37f8f2ac25
commit 93943b2518
2 changed files with 32 additions and 30 deletions

View File

@ -19,6 +19,12 @@ import Foundation
return model as? StarModel
}
public var progressBar = UIProgressView(progressViewStyle: .bar)
public var size: CGFloat = 30 {
didSet {
widthConstraint?.constant = size
setNeedsDisplay()
}
}
//--------------------------------------------------
// MARK: - Constraints
@ -100,11 +106,6 @@ import Foundation
progressBar.progress = Float((model.percent) / 100.0)
progressBar.progressTintColor = model.fillColor.uiColor
progressBar.trackTintColor = .mvmWhite
setFrame(with: model.size)
}
func setFrame(with size: CGFloat) {
widthConstraint?.constant = size
setNeedsDisplay()
size = model.size
}
}

View File

@ -13,6 +13,7 @@ open class Stars: View {
// MARK: - Properties
//--------------------------------------------------
private var stack = UIStackView()
public var starElements = [Star]()
public var starsModel: StarsModel? {
return model as? StarsModel
}
@ -43,6 +44,7 @@ open class Stars: View {
super.set(with: model, delegateObject, additionalData)
self.delegateObject = delegateObject
createStar()
updateStar()
}
//------------------------------------------------------
@ -50,38 +52,37 @@ open class Stars: View {
//------------------------------------------------------
func createStar() {
if let starsModel = starsModel {
let stars = starsModel.stars
let percentRequiredToFillStarFully = CGFloat(100/(starsModel.stars.count))
let numberOfFilledStars = Int(starsModel.percent/percentRequiredToFillStarFully)
for (index, starModel) in stars.enumerated() {
for starModel in starsModel.stars {
let star = Star()
//Fill the stars based on percentage. Ex: if there were 4 stars, 75 percent is 3 full stars
if index < numberOfFilledStars {
starModel.percent = 100
} else if index == numberOfFilledStars {
let remainingProgress = (starsModel.percent).truncatingRemainder(dividingBy: percentRequiredToFillStarFully)
let fillPercent = (remainingProgress/percentRequiredToFillStarFully) * 100
starModel.percent = fillPercent
} else {
starModel.percent = 0
}
starModel.backgroundColor = starsModel.starBackgroundColor
if let borderColor = starsModel.borderColor {
starModel.borderColor = borderColor
}
if let fillColor = starsModel.fillColor {
starModel.fillColor = fillColor
}
starModel.size = starsModel.size
star.set(with: starModel, delegateObject, nil)
stack.addArrangedSubview(star)
starElements.append(star)
}
heightConstraint?.constant = starsModel.size
}
}
//Update star progress and size with starsModel values
func updateStar() {
if let starsModel = starsModel {
let percentRequiredToFillStarFully = CGFloat(100/(starElements.count))
let numberOfFilledStars = Int(starsModel.percent/percentRequiredToFillStarFully)
for (index, star) in starElements.enumerated() {
//Fill the stars based on percentage. Ex: if there were 4 stars, 75 percent is 3 full stars
if index < numberOfFilledStars {
star.progressBar.progress = 1
} else if index == numberOfFilledStars {
let remainingProgress = (starsModel.percent).truncatingRemainder(dividingBy: percentRequiredToFillStarFully)
let fillPercent = remainingProgress/percentRequiredToFillStarFully
star.progressBar.progress = Float(fillPercent)
} else {
star.progressBar.progress = 0
}
star.size = starsModel.size
}
}
}
public override func reset() {
super.reset()
stack.subviews.forEach({$0.removeFromSuperview()})