Skip to content

Commit

Permalink
[Refacto-557] Button: Add common Control management
Browse files Browse the repository at this point in the history
  • Loading branch information
robergro committed Oct 27, 2023
1 parent 1b5e557 commit 60b94c4
Show file tree
Hide file tree
Showing 9 changed files with 842 additions and 0 deletions.
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
}
}
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"
)
}
}
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
}
}
}
Loading

0 comments on commit 60b94c4

Please sign in to comment.