-
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.
[Refacto-557] Button: Add common Control management
- Loading branch information
Showing
9 changed files
with
842 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
core/Sources/Common/Control/PropertyState/ControlPropertyState.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,23 @@ | ||
// | ||
// ControlPropertyState.swift | ||
// SparkCore | ||
// | ||
// Created by robin.lemaire on 25/10/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
/// Contains the dynamic property for a ControlState. | ||
final class ControlPropertyState<T> { | ||
|
||
// MARK: - Properties | ||
|
||
var value: T? | ||
private let state: ControlState | ||
|
||
// MARK: - Initialization | ||
|
||
/// Init the object with a state. The value is nil by default. | ||
init(for state: ControlState) { | ||
self.state = state | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
core/Sources/Common/Control/PropertyState/ControlPropertyStateTests.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,27 @@ | ||
// | ||
// ControlPropertyStateTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by robin.lemaire on 25/10/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftUI | ||
@testable import SparkCore | ||
|
||
final class ControlPropertyStateTests: XCTestCase { | ||
|
||
// MARK: - Tests | ||
|
||
func test_default_value() { | ||
// GIVEN / WHEN | ||
let state = ControlPropertyState<String>(for: .normal) | ||
|
||
// THEN | ||
XCTAssertNil( | ||
state.value, | ||
"Wrong value. Should be nil" | ||
) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
core/Sources/Common/Control/PropertyStates/ControlPropertyStates.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,81 @@ | ||
// | ||
// ControlPropertyStates.swift | ||
// SparkCore | ||
// | ||
// Created by robin.lemaire on 25/10/2023. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Manage all the states for a dynamic property. | ||
final class ControlPropertyStates<PropertyType> { | ||
|
||
// MARK: - Type Alias | ||
|
||
private typealias PropertyState = ControlPropertyState<PropertyType> | ||
|
||
// MARK: - Properties | ||
|
||
private var normalState = PropertyState(for: .normal) | ||
private var highlightedState = PropertyState(for: .highlighted) | ||
private var disabledState = PropertyState(for: .disabled) | ||
private var selectedState = PropertyState(for: .selected) | ||
|
||
// MARK: - Setter | ||
|
||
/// Set the new value for a state. | ||
/// - Parameters: | ||
/// - value: the new value | ||
/// - state: the state for the new value | ||
func setValue(_ value: PropertyType?, for state: ControlState) { | ||
let propertyState: PropertyState | ||
|
||
switch state { | ||
case .normal: | ||
propertyState = self.normalState | ||
case .highlighted: | ||
propertyState = self.highlightedState | ||
case .disabled: | ||
propertyState = self.disabledState | ||
case .selected: | ||
propertyState = self.selectedState | ||
} | ||
|
||
propertyState.value = value | ||
} | ||
|
||
// MARK: - Getter | ||
|
||
/// Get the value for a state. | ||
/// - Parameters: | ||
/// - state: the state of the value | ||
func value(forState state: ControlState) -> PropertyType? { | ||
switch state { | ||
case .normal: return self.normalState.value | ||
case .highlighted: return self.highlightedState.value | ||
case .disabled: return self.disabledState.value | ||
case .selected: return self.selectedState.value | ||
} | ||
} | ||
|
||
/// Get the value for the status of the control. | ||
/// - Parameters: | ||
/// - state: the status of the control | ||
func value(forStatus status: ControlStatus) -> PropertyType? { | ||
// isHighlighted has the highest priority, | ||
// then isDisabled, | ||
// then isSelected, | ||
// and if there is no matching case, we always return the normal value. | ||
|
||
if status.isHighlighted, let value = self.highlightedState.value { | ||
return value | ||
} else if status.isDisabled, let value = self.disabledState.value { | ||
return value | ||
} else if status.isSelected, let value = self.selectedState.value { | ||
return value | ||
} else { | ||
return self.normalState.value | ||
} | ||
} | ||
} |
Oops, something went wrong.