-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #675 from adevinta/204-component-slider-develop
[Slider#204] Implementation of SliderUIControl
- Loading branch information
Showing
25 changed files
with
1,827 additions
and
257 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
core/Sources/Components/Slider/AccessibilityIdentifiier/SliderAccessibilityIdentifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// SliderAccessibilityIdentifier.swift | ||
// SparkCore | ||
// | ||
// Created by louis.borlee on 12.12.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The accessibility identifiers for the slider. | ||
public enum SliderAccessibilityIdentifier { | ||
|
||
// MARK: - Properties | ||
|
||
/// The text label accessibility identifier. | ||
public static let slider = "spark-slider" | ||
} |
105 changes: 105 additions & 0 deletions
105
core/Sources/Components/Slider/Handle/View/SliderHandleUIControl.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// SliderHandleUIControl.swift | ||
// SparkCore | ||
// | ||
// Created by louis.borlee on 23/11/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import Combine | ||
|
||
final class SliderHandleUIControl: UIControl { | ||
|
||
private let handleView = UIView() | ||
private let activeIndicatorView = UIView() | ||
|
||
private var cancellables = Set<AnyCancellable>() | ||
|
||
let viewModel: SliderHandleViewModel | ||
|
||
override var isHighlighted: Bool { | ||
didSet { | ||
self.activeIndicatorView.isHidden = !self.isHighlighted | ||
} | ||
} | ||
|
||
init(viewModel: SliderHandleViewModel) { | ||
self.viewModel = viewModel | ||
super.init(frame: .init( | ||
origin: .zero, | ||
size: .init( | ||
width: SliderConstants.handleSize.width, | ||
height: SliderConstants.handleSize.height | ||
) | ||
)) | ||
self.setupHandleView() | ||
self.setupActiveHandleView() | ||
self.subscribeToViewModelChanges() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setupHandleView() { | ||
self.handleView.frame = self.bounds | ||
self.handleView.isUserInteractionEnabled = false | ||
self.handleView.layer.cornerRadius = SliderConstants.handleSize.height / 2 | ||
self.addSubview(self.handleView) | ||
} | ||
|
||
private func setupActiveHandleView() { | ||
self.activeIndicatorView.setBorderWidth(1.0) | ||
self.activeIndicatorView.isUserInteractionEnabled = false | ||
self.activeIndicatorView.isHidden = true | ||
self.activeIndicatorView.layer.cornerRadius = SliderConstants.activeIndicatorSize.height / 2 | ||
|
||
self.activeIndicatorView.frame.size = .init(width: SliderConstants.activeIndicatorSize.width, height: SliderConstants.activeIndicatorSize.height) | ||
self.activeIndicatorView.center = self.handleView.center | ||
|
||
self.insertSubview(self.activeIndicatorView, belowSubview: self.handleView) | ||
} | ||
|
||
private func subscribeToViewModelChanges() { | ||
self.viewModel.$color.subscribe(in: &self.cancellables) { [weak self] newColor in | ||
guard let self else { return } | ||
self.handleView.backgroundColor = newColor.uiColor | ||
self.activeIndicatorView.setBorderColor(from: newColor) | ||
} | ||
self.viewModel.$activeIndicatorColor.subscribe(in: &self.cancellables) { [weak self] newActiveIndicatorColor in | ||
guard let self else { return } | ||
self.activeIndicatorView.backgroundColor = newActiveIndicatorColor.uiColor | ||
} | ||
} | ||
|
||
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { | ||
let beginTracking = super.beginTracking(touch, with: event) | ||
if let supercontrol = superview as? UIControl { | ||
return supercontrol.beginTracking(touch, with: event) | ||
} | ||
return beginTracking | ||
} | ||
|
||
override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { | ||
let continueTracking = super.continueTracking(touch, with: event) | ||
if let supercontrol = superview as? UIControl { | ||
return supercontrol.continueTracking(touch, with: event) | ||
} | ||
return continueTracking | ||
} | ||
|
||
override func cancelTracking(with event: UIEvent?) { | ||
super.cancelTracking(with: event) | ||
if let supercontrol = superview as? UIControl { | ||
supercontrol.cancelTracking(with: event) | ||
} | ||
} | ||
|
||
override func endTracking(_ touch: UITouch?, with event: UIEvent?) { | ||
super.endTracking(touch, with: event) | ||
if let supercontrol = superview as? UIControl { | ||
supercontrol.endTracking(touch, with: event) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/Sources/Components/Slider/Handle/ViewModel/SliderHandleViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// SliderHandleViewModel.swift | ||
// SparkCore | ||
// | ||
// Created by louis.borlee on 23/11/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class SliderHandleViewModel { | ||
|
||
@Published var color: any ColorToken | ||
@Published var activeIndicatorColor: any ColorToken | ||
|
||
init(color: some ColorToken, | ||
activeIndicatorColor: some ColorToken) { | ||
self.color = color | ||
self.activeIndicatorColor = activeIndicatorColor | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/Sources/Components/Slider/Properties/Private/SliderColors+ExtensionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// SliderColors+ExtensionTests.swift | ||
// SparkCore | ||
// | ||
// Created by louis.borlee on 08/12/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import SparkCore | ||
|
||
extension SliderColors { | ||
static func mocked(colors: Colors) -> SliderColors { | ||
return .init( | ||
track: colors.feedback.alert, | ||
indicator: colors.accent.accentVariant, | ||
handle: colors.states.neutralPressed, | ||
handleActiveIndicator: colors.basic.onBasicContainer | ||
) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/Sources/Components/Slider/Properties/Private/SliderRadii+ExtensionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SliderRadii+ExtensionTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by louis.borlee on 08/12/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import SparkCore | ||
|
||
extension SliderRadii { | ||
static func mocked() -> SliderRadii { | ||
return .init( | ||
trackRadius: 0.123, | ||
indicatorRadius: 49.3 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ents/Slider/UseCase/GetColors/SliderGetColorsUseCasableGeneratedMock+ExtensionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// SliderGetColorsUseCasableGeneratedMock+ExtensionTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by louis.borlee on 06/12/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import SparkCore | ||
|
||
extension SliderGetColorsUseCasableGeneratedMock { | ||
static func mocked(returnedColors colors: SliderColors) -> SliderGetColorsUseCasableGeneratedMock { | ||
let mock = SliderGetColorsUseCasableGeneratedMock() | ||
mock._executeWithThemeAndIntent = { _, _ in | ||
return colors | ||
} | ||
return mock | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.