-
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 #495 from adevinta/feature/component/chip_469
[Chip#469] Update designs to latest version
- Loading branch information
Showing
28 changed files
with
1,729 additions
and
1,231 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
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
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
96 changes: 96 additions & 0 deletions
96
core/Sources/Components/Chip/UseCase/ChipGetColorsUseCase.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,96 @@ | ||
// | ||
// ChipGetColorsUseCase.swift | ||
// SparkDemo | ||
// | ||
// Created by michael.zimmermann on 03.05.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import SwiftUI | ||
import Foundation | ||
|
||
/// A use case to calculate the colors of a chip depending on the theme, variant and intent | ||
// sourcery: AutoMockable | ||
protocol ChipGetColorsUseCasable { | ||
/// Function `execute` calculates the chip colors | ||
/// | ||
/// Parameters: | ||
/// - theme: The spark theme. | ||
/// - variant: The variant of the chip, if it is outlined, filled, etc. | ||
/// - intent: The intent color, e.g. main, support. | ||
/// - state: The current state of the chip | ||
/// Returns: | ||
/// ChipColors: all the colors used for the chip | ||
func execute(theme: Theme, | ||
variant: ChipVariant, | ||
intent: ChipIntent, | ||
state: ChipState | ||
) -> ChipStateColors | ||
} | ||
|
||
/// ChipGetColorsUseCase: A use case to calculate the colors of a chip depending on the theme, variand and intent | ||
struct ChipGetColorsUseCase: ChipGetColorsUseCasable { | ||
// MARK: - Properties | ||
private let outlinedIntentColorsUseCase: ChipGetIntentColorsUseCasable | ||
private let tintedIntentColorsUseCase: ChipGetIntentColorsUseCasable | ||
|
||
// MARK: - Initializer | ||
/// Initializer | ||
/// | ||
/// Parameters: | ||
/// - outlinedIntentColorsUseCase: A use case to calculate the intent colors of outlined chips. | ||
/// - tintedIntentColorsUseCase: A use case to calculate the intent colors of tinted chips. | ||
init(outlinedIntentColorsUseCase: ChipGetIntentColorsUseCasable = ChipGetOutlinedIntentColorsUseCase(), | ||
tintedIntentColorsUseCase: ChipGetIntentColorsUseCasable = ChipGetTintedIntentColorsUseCase() | ||
) { | ||
self.outlinedIntentColorsUseCase = outlinedIntentColorsUseCase | ||
self.tintedIntentColorsUseCase = tintedIntentColorsUseCase | ||
|
||
} | ||
|
||
// MARK: - Functions | ||
|
||
/// The funcion execute calculates the chip colors based on the parameters. | ||
/// | ||
/// Parameters: | ||
/// - theme: The current theme to be used | ||
/// - variant: The variant of the chip, whether it's filled, outlined, etc. | ||
/// - intent: The intent color of the chip, e.g. main, support | ||
/// - state: The current state of the chip, e.g. selected, enabled, pressed | ||
func execute(theme: Theme, | ||
variant: ChipVariant, | ||
intent: ChipIntent, | ||
state: ChipState) -> ChipStateColors { | ||
|
||
let intentUseCase: ChipGetIntentColorsUseCasable = variant == .tinted ? self.tintedIntentColorsUseCase : self.outlinedIntentColorsUseCase | ||
|
||
let colors = intentUseCase.execute(theme: theme, intent: intent) | ||
|
||
if state.isPressed { | ||
return .init( | ||
background: colors.pressedBackground, | ||
border: colors.border, | ||
foreground: colors.text) | ||
} | ||
|
||
var stateColors = ChipStateColors( | ||
background: colors.background, | ||
border: colors.border, | ||
foreground: colors.text) | ||
|
||
if state.isSelected { | ||
stateColors.background = colors.selectedBackground | ||
stateColors.foreground = colors.selectedText | ||
} | ||
|
||
if state.isDisabled { | ||
if let backgroundColor = colors.disabledBackground { | ||
stateColors.background = backgroundColor | ||
} | ||
stateColors.opacity = theme.dims.dim3 | ||
} | ||
|
||
return stateColors | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
core/Sources/Components/Chip/UseCase/ChipGetColorsUseCaseTests.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,166 @@ | ||
// | ||
// ChipGetColorsUseCaseTests.swift | ||
// SparkCoreTests | ||
// | ||
// Created by michael.zimmermann on 08.05.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import XCTest | ||
|
||
@testable import SparkCore | ||
|
||
final class ChipGetColorsUseCaseTests: XCTestCase { | ||
|
||
// MARK: - Properties | ||
private var sut: ChipGetColorsUseCase! | ||
private var outlinedIntentColorsUseCase: ChipGetIntentColorsUseCasableGeneratedMock! | ||
private var tintedIntentColorsUseCase: ChipGetIntentColorsUseCasableGeneratedMock! | ||
private var theme: ThemeGeneratedMock! | ||
|
||
// MARK: - Setup | ||
override func setUp() { | ||
super.setUp() | ||
|
||
self.outlinedIntentColorsUseCase = .init() | ||
self.tintedIntentColorsUseCase = .init() | ||
self.sut = .init( | ||
outlinedIntentColorsUseCase: self.outlinedIntentColorsUseCase, | ||
tintedIntentColorsUseCase: self.tintedIntentColorsUseCase) | ||
self.theme = .init() | ||
|
||
let dims = DimsGeneratedMock() | ||
dims.dim3 = 0.33 | ||
self.theme.dims = dims | ||
} | ||
|
||
// MARK: - Tests | ||
|
||
func test_outlined_variant_uses_correct_use_case() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.outlinedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.background, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.text, | ||
opacity: 1.0) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .outlined, intent: .basic, state: .default) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
|
||
func test_tinted_variant_uses_correct_use_case() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.tintedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.background, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.text, | ||
opacity: 1.0) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .tinted, intent: .basic, state: .default) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
|
||
func test_pressed_has_correct_background() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.tintedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.pressedBackground, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.text, | ||
opacity: 1.0) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .tinted, intent: .basic, state: .pressed) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
|
||
func test_selected_has_correct_backgorund_and_text() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.tintedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.selectedBackground, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.selectedText, | ||
opacity: 1.0) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .tinted, intent: .basic, state: .selected) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
|
||
func test_disabled_has_correct_opacity() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.tintedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.background, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.text, | ||
opacity: self.theme.dims.dim3) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .tinted, intent: .basic, state: .disabled) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
|
||
func test_selected_and_disabled_has_correct_background() { | ||
// Given | ||
let chipIntentColors = ChipIntentColors.mocked() | ||
|
||
self.tintedIntentColorsUseCase.executeWithThemeAndIntentReturnValue = chipIntentColors | ||
|
||
let expectedColors = ChipStateColors( | ||
background: chipIntentColors.selectedBackground, | ||
border: chipIntentColors.border, | ||
foreground: chipIntentColors.selectedText, | ||
opacity: self.theme.dims.dim3) | ||
|
||
// When | ||
let colors = self.sut.execute(theme: self.theme, variant: .tinted, intent: .basic, state: .selectedDisabled) | ||
|
||
XCTAssertEqual(colors, expectedColors) | ||
} | ||
} | ||
|
||
private extension ChipIntentColors { | ||
static func mocked() -> ChipIntentColors { | ||
return .init( | ||
border: ColorTokenGeneratedMock.random(), | ||
text: ColorTokenGeneratedMock.random(), | ||
selectedText: ColorTokenGeneratedMock.random(), | ||
background: ColorTokenGeneratedMock.random(), | ||
pressedBackground: ColorTokenGeneratedMock.random(), | ||
selectedBackground: ColorTokenGeneratedMock.random()) | ||
} | ||
} | ||
|
||
private extension ChipState { | ||
static let pressed = ChipState(isEnabled: true, isPressed: true, isSelected: false) | ||
static let disabled = ChipState(isEnabled: false, isPressed: false, isSelected: false) | ||
static let selected = ChipState(isEnabled: true, isPressed: false, isSelected: true) | ||
static let selectedDisabled = ChipState(isEnabled: false, isPressed: false, isSelected: true) | ||
} |
Oops, something went wrong.