-
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 #290 from adevinta/fix/badge/sizing
[Badge#1189] Corrected autoscaling and badge height depending on size.
- Loading branch information
Showing
8 changed files
with
331 additions
and
113 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
24 changes: 24 additions & 0 deletions
24
core/Sources/Components/Badge/Properties/Private/BadgeSizeDependentAttributes.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,24 @@ | ||
// | ||
// BadgeSizeDependentAttributes.swift | ||
// SparkCore | ||
// | ||
// Created by michael.zimmermann on 03.08.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct BadgeSizeDependentAttributes: Equatable { | ||
|
||
let offset: EdgeInsets | ||
let height: CGFloat | ||
let font: TypographyFontToken | ||
|
||
static func == (lhs: BadgeSizeDependentAttributes, rhs: BadgeSizeDependentAttributes) -> Bool { | ||
return lhs.offset == rhs.offset && | ||
lhs.height == rhs.height && | ||
lhs.font.font == rhs.font.font && | ||
lhs.font.uiFont == rhs.font.uiFont | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
core/Sources/Components/Badge/UseCase/BadgeGetSizeAttributesUseCase.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,57 @@ | ||
// | ||
// BadgeGetSizeUseCase.swift | ||
// SparkCore | ||
// | ||
// Created by michael.zimmermann on 03.08.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
// sourcery: AutoMockable | ||
protocol BadgeGetSizeAttributesUseCaseable { | ||
func execute(theme: Theme, size: BadgeSize) -> BadgeSizeDependentAttributes | ||
} | ||
|
||
/// A use case that returns size specific attributes according to the theme | ||
struct BadgeGetSizeAttributesUseCase: BadgeGetSizeAttributesUseCaseable { | ||
|
||
// MARK: - Functions | ||
func execute(theme: Theme, size: BadgeSize) -> BadgeSizeDependentAttributes { | ||
return .init(offset: size.offset(spacing: theme.layout.spacing), | ||
height: size.badgeHeight(), | ||
font: size.font(typography: theme.typography)) | ||
} | ||
} | ||
|
||
// MARK: - Private helper extension | ||
private extension BadgeSize { | ||
func offset(spacing: LayoutSpacing) -> EdgeInsets { | ||
switch self { | ||
case .normal: return .init(vertical: spacing.small, | ||
horizontal: spacing.medium) | ||
case .small: return .init(vertical: 0, | ||
horizontal: spacing.small) | ||
} | ||
} | ||
|
||
func badgeHeight() -> CGFloat { | ||
switch self { | ||
case .normal: | ||
return BadgeConstants.height.normal | ||
case .small: | ||
return BadgeConstants.height.small | ||
} | ||
} | ||
|
||
func font(typography: Typography) -> TypographyFontToken { | ||
switch self { | ||
case .normal: | ||
return typography.captionHighlight | ||
case .small: | ||
return typography.smallHighlight | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
core/Sources/Components/Badge/UseCase/BadgeGetSizeAttributesUseCaseTests.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,47 @@ | ||
// | ||
// BadgeGetSizeAttributesUseCase.swift | ||
// SparkCoreTests | ||
// | ||
// Created by michael.zimmermann on 03.08.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
@testable import SparkCore | ||
import XCTest | ||
|
||
final class BadgeGetSizeAttributesUseCaseTests: TestCase { | ||
|
||
// MARK: - Properties | ||
var sut: BadgeGetSizeAttributesUseCase! | ||
var theme: ThemeGeneratedMock! | ||
|
||
// MARK: - Setup | ||
override func setUp() { | ||
super.setUp() | ||
self.theme = .mocked() | ||
self.sut = BadgeGetSizeAttributesUseCase() | ||
} | ||
|
||
// MARK: - Tests | ||
func test_size_small() throws { | ||
let attributes = sut.execute(theme: self.theme, size: .small) | ||
|
||
let expectedAttributes = BadgeSizeDependentAttributes( | ||
offset: .init(vertical: 0, horizontal: 3), | ||
height: 16, | ||
font: self.theme.typography.smallHighlight) | ||
|
||
XCTAssertEqual(attributes, expectedAttributes) | ||
} | ||
|
||
func test_size_normal() throws { | ||
let attributes = sut.execute(theme: self.theme, size: .normal) | ||
|
||
let expectedAttributes = BadgeSizeDependentAttributes( | ||
offset: .init(vertical: 3, horizontal: 5), | ||
height: 24, | ||
font: self.theme.typography.captionHighlight) | ||
|
||
XCTAssertEqual(attributes, expectedAttributes) | ||
} | ||
} |
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.