Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #126

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
/// Interface for progressing the view
protocol ViewProgressable {

/// Invalidates the progress shown on the chart
/// Sets values for the chart
///
/// - Parameter animated: Indicating if invalidation should be animated
func invalidateChart(animated: Bool)
/// - Parameters:
/// - animated: Indicating if the change should be animated
/// - toZero: Indicating if charts should be cleared
func setChart(animated: Bool, toZero: Bool)
}
2 changes: 0 additions & 2 deletions CarLens/Source Files/Common/Services/ToastDisplayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ enum ToastDisplayer {
toastLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9),
toastLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20)
])
toastLabel.heightAnchor.constraint(equalToConstant: Constants.attachPinErrorLabelHeight)
toastLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9)

UIView.animate(withDuration: Constants.animationDuration,
delay: 1.0,
Expand Down
59 changes: 25 additions & 34 deletions CarLens/Source Files/Common/Views/CarListCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ import UIKit

final class CarListCardView: View, ViewSetupable {

private lazy var topSpeedProgressView = PartOvalProgressView(state: .topSpeed(0),
invalidateChartInstantly: false)
private lazy var topSpeedProgressView = PartOvalProgressView(state: .topSpeed(0))

private lazy var accelerationProgressView = PartOvalProgressView(state: .accelerate(0),
invalidateChartInstantly: false)
private lazy var accelerationProgressView = PartOvalProgressView(state: .accelerate(0))

private lazy var engineProgressView = HorizontalProgressChartView(state: .engine(0),
invalidateChartInstantly: false)
private lazy var engineProgressView = HorizontalProgressChartView(state: .engine(0))

private lazy var powerProgressView = HorizontalProgressChartView(state: .power(0),
invalidateChartInstantly: false)
private lazy var powerProgressView = HorizontalProgressChartView(state: .power(0))

private lazy var starsProgressView: HorizontalStarsView = {
let view = HorizontalStarsView(starCount: 3, invalidateChartInstantly: false)
let view = HorizontalStarsView(starCount: 3)
view.backgroundColor = .white
return view.layoutable()
}()

private lazy var progressableViews: [ViewProgressable] = [
topSpeedProgressView,
accelerationProgressView,
engineProgressView,
powerProgressView,
starsProgressView
]

private lazy var descriptionLabel: UILabel = {
let view = UILabel()
view.textColor = .crFontLightGray
Expand Down Expand Up @@ -83,43 +87,30 @@ final class CarListCardView: View, ViewSetupable {
func setup(with car: Car) {
makeImageView.image = car.isDiscovered ? car.image.logoUnlocked : car.image.logoLocked
topSpeedProgressView.setup(state: .topSpeed(car.speed),
invalidateChartInstantly: false,
setChartWithoutAnimation: false,
isLocked: !car.isDiscovered)
accelerationProgressView.setup(state: .accelerate(car.acceleration),
invalidateChartInstantly: false,
setChartWithoutAnimation: false,
isLocked: !car.isDiscovered)
engineProgressView.setup(state: .engine(car.engine),
invalidateChartInstantly: false,
setChartWithoutAnimation: false,
isLocked: !car.isDiscovered)
powerProgressView.setup(state: .power(car.power), invalidateChartInstantly: false, isLocked: !car.isDiscovered)
starsProgressView.setup(starCount: car.stars, invalidateChartInstantly: false, isLocked: !car.isDiscovered)
descriptionLabel.textColor = car.isDiscovered ? .crFontLightGray : .crBackgroundLightGray
powerProgressView.setup(state: .power(car.power), setChartWithoutAnimation: false, isLocked: !car.isDiscovered)
starsProgressView.setup(starCount: car.stars, setChartWithoutAnimation: false, isLocked: !car.isDiscovered)
descriptionLabel.textColor = car.isDiscovered ? .crFontLightGray : .crBackgroundLightGray
let descriptionFont = car.isDiscovered ? UIFont.systemFont(ofSize: 12) : .blokkNeueFont(ofSize: 12)
descriptionLabel.attributedText = NSAttributedStringFactory.trackingApplied(car.description,
font: descriptionFont,
tracking: .condensed)
}

/// Invalidates the charts visible on the view
///
/// - Parameter animated: Indicating if invalidation should be animated
func invalidateCharts(animated: Bool) {
topSpeedProgressView.invalidateChart(animated: animated)
accelerationProgressView.invalidateChart(animated: animated)
engineProgressView.invalidateChart(animated: animated)
powerProgressView.invalidateChart(animated: animated)
starsProgressView.invalidateChart(animated: animated)
}

/// Clear the progress shown on charts
/// Sets all charts of the view
///
/// - Parameter animated: Indicating if progress change should be animated
func clearCharts(animated: Bool) {
topSpeedProgressView.clearChart(animated: animated)
accelerationProgressView.clearChart(animated: animated)
engineProgressView.clearChart(animated: animated)
powerProgressView.clearChart(animated: animated)
starsProgressView.clearChart(animated: animated)
/// - Parameters:
/// - animated: Indicating if the change should be animated
/// - toZero: Indicating if charts should be cleared
func setCharts(animated: Bool, toZero: Bool) {
progressableViews.forEach { $0.setChart(animated: animated, toZero: toZero) }
}

/// - SeeAlso: ViewSetupable
Expand Down
3 changes: 1 addition & 2 deletions CarLens/Source Files/Common/Views/CarListNavigationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ final class CarListNavigationBar: View, ViewSetupable {

/// Progress view displayed as full oval figure
lazy var progressView = FullOvalProgressView(currentNumber: currentNumber,
maximumNumber: maximumNumber,
invalidateChartInstantly: false).layoutable()
maximumNumber: maximumNumber).layoutable()

/// Initializes CarListNavigationBar
///
Expand Down
31 changes: 12 additions & 19 deletions CarLens/Source Files/Common/Views/FullOvalProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ final class FullOvalProgressView: View, ViewSetupable {
/// - Parameters:
/// - currentNumber: Currently achieved number
/// - maximumNumber: Maximum available number
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
init(currentNumber: Int, maximumNumber: Int, invalidateChartInstantly: Bool) {
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
init(currentNumber: Int, maximumNumber: Int, setChartWithoutAnimation: Bool = false) {
self.currentNumber = currentNumber
self.maximumNumber = maximumNumber
super.init()
setup(currentNumber: currentNumber,
maximumNumber: maximumNumber,
invalidateChartInstantly: invalidateChartInstantly)
setChartWithoutAnimation: setChartWithoutAnimation)
accessibilityIdentifier = "carsList/view/ovalProgress"
}

Expand All @@ -58,9 +58,9 @@ final class FullOvalProgressView: View, ViewSetupable {
/// - Parameters:
/// - currentNumber: Currently achieved number
/// - maximumNumber: Maximum available number
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
func setup(currentNumber: Int, maximumNumber: Int, invalidateChartInstantly: Bool) {
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
func setup(currentNumber: Int, maximumNumber: Int, setChartWithoutAnimation: Bool) {
fullOvalLayerView.set(progress: 0, animated: false)
self.currentNumber = currentNumber
self.maximumNumber = maximumNumber
Expand All @@ -69,18 +69,11 @@ final class FullOvalProgressView: View, ViewSetupable {
valueLabel.attributedText = NSAttributedStringFactory.trackingApplied(valueText,
font: valueLabel.font,
tracking: .condensed)
if invalidateChartInstantly {
invalidateChart(animated: false)
if setChartWithoutAnimation {
setChart(animated: false, toZero: false)
}
}

/// Clear the progress shown on the chart
///
/// - Parameter animated: Indicating if progress change should be animated
func clearChart(animated: Bool) {
fullOvalLayerView.set(progress: 0, animated: animated)
}

/// - SeeAlso: ViewSetupable
func setupViewHierarchy() {
[fullOvalLayerView, valueLabel].forEach(addSubview)
Expand All @@ -100,8 +93,8 @@ final class FullOvalProgressView: View, ViewSetupable {
extension FullOvalProgressView: ViewProgressable {

/// - SeeAlso: ViewProgressable
func invalidateChart(animated: Bool) {
let progress = Double(currentNumber) / Double(maximumNumber)
fullOvalLayerView.set(progress: progress, animated: animated)
func setChart(animated: Bool, toZero: Bool) {
let progress = toZero ? 0 : Double(currentNumber) / Double(maximumNumber)
fullOvalLayerView.set(progress: progress, animated: animated)
}
}
31 changes: 12 additions & 19 deletions CarLens/Source Files/Common/Views/HorizontalProgressChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ final class HorizontalProgressChartView: View, ViewSetupable {
///
/// - Parameters:
/// - state: State to be shown by the view
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
/// - isLocked: Indicating if the info should be locked
init(state: State, invalidateChartInstantly: Bool, isLocked: Bool = false) {
init(state: State, setChartWithoutAnimation: Bool = false, isLocked: Bool = false) {
self.state = state
self.isLocked = isLocked
super.init()
setup(state: state, invalidateChartInstantly: invalidateChartInstantly, isLocked: isLocked)
setup(state: state, setChartWithoutAnimation: setChartWithoutAnimation, isLocked: isLocked)
}

/// Setups the view with given state. Use only inside reusable views.
///
/// - Parameters:
/// - state: State to be shown by the view
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
/// - isLocked: Indicating if the info should be locked
func setup(state: State, invalidateChartInstantly: Bool, isLocked: Bool = false) {
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
/// - isLocked: Indicating if the info should be locked
func setup(state: State, setChartWithoutAnimation: Bool, isLocked: Bool = false) {
animationView.set(progress: 0, animated: false)
self.state = state
self.isLocked = isLocked
Expand All @@ -88,22 +88,15 @@ final class HorizontalProgressChartView: View, ViewSetupable {
font: titleLabel.font,
tracking: .condensed)
}
if invalidateChartInstantly {
invalidateChart(animated: false)
if setChartWithoutAnimation {
setChart(animated: false, toZero: false)
}
valueLabel.textColor = isLocked ? .crFontGrayLocked : .crFontDark
if isLocked {
valueLabel.text = "?"
}
}

/// Clear the progress shown on the chart
///
/// - Parameter animated: Indicating if progress change should be animated
func clearChart(animated: Bool) {
animationView.set(progress: 0, animated: animated)
}

/// - SeeAlso: ViewSetupable
func setupViewHierarchy() {
[animationView, valueLabel, titleLabel].forEach(addSubview)
Expand All @@ -124,15 +117,15 @@ final class HorizontalProgressChartView: View, ViewSetupable {
extension HorizontalProgressChartView: ViewProgressable {

/// - SeeAlso: ViewProgressable
func invalidateChart(animated: Bool) {
func setChart(animated: Bool, toZero: Bool) {
var progress: Double
switch state {
case .power(let power):
progress = Double(power) / Double(chartConfig.referenceHorsePower)
case .engine(let engine):
progress = Double(engine) / Double(chartConfig.referenceEngineVolume)
}
if isLocked {
if isLocked || toZero {
progress = 0
}
animationView.set(progress: CGFloat(progress), animated: animated)
Expand Down
50 changes: 22 additions & 28 deletions CarLens/Source Files/Common/Views/HorizontalStarsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,33 @@ final class HorizontalStarsView: View, ViewSetupable {
///
/// - Parameters:
/// - starCount: Count of achieved stars
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
/// - isLocked: Indicating if the info should be locked
init(starCount: Int, invalidateChartInstantly: Bool, isLocked: Bool = false) {
init(starCount: Int, setChartWithoutAnimation: Bool = false, isLocked: Bool = false) {
self.starCount = starCount
self.isLocked = isLocked
super.init()
setup(starCount: starCount, invalidateChartInstantly: invalidateChartInstantly)
setup(starCount: starCount, setChartWithoutAnimation: setChartWithoutAnimation)
}

/// Setups the view with given parameters. Use only inside reusable views.
///
/// - Parameters:
/// - starCount: Count of achieved stars
/// - invalidateChartInstantly: Chart will be updated instantly without animation if this value indicates false.
/// When passing false, remember to use method `invalidatChart(animated:)` also
func setup(starCount: Int, invalidateChartInstantly: Bool, isLocked: Bool = false) {
/// - setChartWithoutAnimation: Chart will be updated instantly without animation if this value indicates true.
/// When passing true, remember to use method `setChart(animated:toZero:)` also
func setup(starCount: Int, setChartWithoutAnimation: Bool, isLocked: Bool = false) {
guard starCount >= 0, starCount <= 5 else {
fatalError("Wrong input provided for stars chart")
}
animationView.set(progress: 0, animated: false)
self.starCount = starCount
self.isLocked = isLocked
if invalidateChartInstantly {
invalidateChart(animated: false)
if setChartWithoutAnimation {
setChart(animated: false, toZero: false)
}
}

/// Invalidates the progress shown on the chart
///
/// - Parameter animated: Indicating if invalidation should be animated
func invalidateChart(animated: Bool) {
var progress = Double(starCount) / Double(numberOfStars + 1)
if isLocked {
progress = 0
}
animationView.set(progress: CGFloat(progress), animated: animated)
}

/// Clear the progress shown on the chart
///
/// - Parameter animated: Indicating if progress change should be animated
func clearChart(animated: Bool) {
animationView.set(progress: 0, animated: animated)
}
}

/// - SeeAlso: ViewSetupable
func setupViewHierarchy() {
Expand All @@ -77,3 +59,15 @@ final class HorizontalStarsView: View, ViewSetupable {
animationView.constraintToSuperviewEdges()
}
}

extension HorizontalStarsView: ViewProgressable {

/// - SeeAlso: ViewProgressable
func setChart(animated: Bool, toZero: Bool) {
var progress = Double(starCount) / Double(numberOfStars + 1)
if isLocked || toZero {
progress = 0
}
animationView.set(progress: CGFloat(progress), animated: animated)
}
}
Loading