Skip to content

Commit

Permalink
Merge pull request #174 from adevinta/feature/component/badge
Browse files Browse the repository at this point in the history
Feature/component/badge
  • Loading branch information
LouisBorleeAdevinta authored Jun 14, 2023
2 parents 8a48b1e + b3e3e03 commit d0a8117
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 168 deletions.
28 changes: 14 additions & 14 deletions core/Demo/Classes/BadgeUIView_Previews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,62 +71,62 @@ struct BadgeUIView_Previews: PreviewProvider {
private var views = [
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .alert,
intent: .alert,
value: 6
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .primary,
badgeSize: .normal,
intent: .primary,
size: .normal,
value: 22,
format: .overflowCounter(maxValue: 20)
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .danger,
intent: .danger,
value: 10,
format: .custom(
formatter: BadgePreviewFormatter()
)
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .info,
intent: .info,
value: 20
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .primary
intent: .primary
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .neutral,
isBadgeOutlined: false
intent: .neutral,
isBorderVisible: false
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .secondary,
intent: .secondary,
value: 23
),
BadgeUIView(
theme: SparkTheme.shared,
badgeType: .success
intent: .success
)
]

var body: some View {
List {
Button("Change UIKit Badge 0 Type") {
views[0].setBadgeType(BadgeIntentType.allCases.randomElement() ?? .alert)
views[0].setIntent(BadgeIntentType.allCases.randomElement() ?? .alert)
}
Button("Change UIKit Badge 1 Value") {
views[1].setBadgeValue(2)
views[1].setValue(2)
}
Button("Change UIKit Badge 2 Outline") {
views[2].setBadgeOutlineEnabled(false)
views[2].setBorderVisible(false)
}
Button("Change UIKit Badge 3 Size") {
views[3].setBadgeSize(.small)
views[3].setSize(.small)
}
UIBadgeView(views: views)
.frame(height: 400)
Expand Down
20 changes: 10 additions & 10 deletions core/Demo/Classes/BadgeView_Previews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ struct BadgeView_Previews: PreviewProvider {
Text("Default Badge")
BadgeView(
theme: theme,
badgeType: .primary,
intent: .primary,
value: 3
)
.format(badgeFormat)
.outlined(standartBadgeIsOutlined)
.borderVisible(standartBadgeIsOutlined)
.value(standartBadgeValue)
.offset(x: 100, y: -15)
}
ZStack(alignment: .leading) {
Text("Small Custom")
BadgeView(
theme: SparkTheme.shared,
badgeType: smallCustomBadgeType,
intent: smallCustomBadgeType,
value: 22
)
.outlined(smallCustomBadgeIsOutlined)
.borderVisible(smallCustomBadgeIsOutlined)
.size(smallCustomBadgeSize)
.offset(x: 100, y: -15)
}
Expand All @@ -87,7 +87,7 @@ struct BadgeView_Previews: PreviewProvider {
Text("Danger Badge")
BadgeView(
theme: SparkTheme.shared,
badgeType: standartDangerBadgeType,
intent: standartDangerBadgeType,
value: 10
)
.format(.custom(
Expand All @@ -99,15 +99,15 @@ struct BadgeView_Previews: PreviewProvider {
Text("Text")
BadgeView(
theme: SparkTheme.shared,
badgeType: .info
intent: .info
)
.offset(x: 25, y: -15)
}
ZStack(alignment: .leading) {
Text("Text")
BadgeView(
theme: SparkTheme.shared,
badgeType: .neutral
intent: .neutral
)
.offset(x: 25, y: -15)
}
Expand All @@ -118,21 +118,21 @@ struct BadgeView_Previews: PreviewProvider {
Text("Text")
BadgeView(
theme: SparkTheme.shared,
badgeType: .primary
intent: .primary
)
}
HStack {
Text("Text")
BadgeView(
theme: SparkTheme.shared,
badgeType: .secondary
intent: .secondary
)
}
HStack {
Text("Text")
BadgeView(
theme: SparkTheme.shared,
badgeType: .success
intent: .success
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum BadgeFormat {

/// This function will return text value for your badge
/// wiht conformation to the selected **BadgeFormat** type
func badgeText(_ value: Int?) -> String {
func text(_ value: Int?) -> String {
switch self {
case .overflowCounter(let maxValue):
guard let value else {
Expand Down
34 changes: 17 additions & 17 deletions core/Sources/Components/Badge/View/SwiftUI/BadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SwiftUI
/// var body: any View {
/// HStack {
/// Text("Some text")
/// BadgeView(theme: YourTheme.shared, badgeType: .alert, value: value)
/// BadgeView(theme: YourTheme.shared, intent: .alert, value: value)
/// }
/// }
/// ```
Expand All @@ -36,9 +36,9 @@ public struct BadgeView: View {
.foregroundColor(self.viewModel.backgroundColor.color)
.frame(width: self.emptySize, height: self.emptySize)
.border(
width: self.viewModel.isBadgeOutlined ? borderWidth : 0,
radius: self.viewModel.badgeBorder.radius,
colorToken: self.viewModel.badgeBorder.color
width: self.viewModel.isBorderVisible ? borderWidth : 0,
radius: self.viewModel.border.radius,
colorToken: self.viewModel.border.color
)
.fixedSize()
} else {
Expand All @@ -48,20 +48,20 @@ public struct BadgeView: View {
.padding(.init(vertical: self.smallOffset, horizontal: self.mediumOffset))
.background(self.viewModel.backgroundColor.color)
.border(
width: self.viewModel.isBadgeOutlined ? borderWidth : 0,
radius: self.viewModel.badgeBorder.radius,
colorToken: self.viewModel.badgeBorder.color
width: self.viewModel.isBorderVisible ? borderWidth : 0,
radius: self.viewModel.border.radius,
colorToken: self.viewModel.border.color
)
.fixedSize()
.accessibilityIdentifier(BadgeAccessibilityIdentifier.text)
}
}

/// - Parameter theme: ``Theme``
/// - Parameter badgeType: ``BadgeIntentType``
/// - Parameter intent: ``BadgeIntentType``
/// - Parameter value: **Int?** You can set value to nil, to make ``BadgeView`` without text
public init(theme: Theme, badgeType: BadgeIntentType, value: Int? = nil) {
let viewModel = BadgeViewModel(theme: theme, badgeType: badgeType, value: value)
public init(theme: Theme, intent: BadgeIntentType, value: Int? = nil) {
let viewModel = BadgeViewModel(theme: theme, intent: intent, value: value)
self.viewModel = viewModel

self._smallOffset =
Expand All @@ -73,7 +73,7 @@ public struct BadgeView: View {
viewModel.horizontalOffset
)
self._emptySize = .init(wrappedValue: BadgeConstants.emptySize.width)
self._borderWidth = .init(wrappedValue: viewModel.badgeBorder.width)
self._borderWidth = .init(wrappedValue: viewModel.border.width)
}

// MARK: - Badge Modification Functions
Expand All @@ -82,25 +82,25 @@ public struct BadgeView: View {
/// By default Badge has an outline based on current ``Theme``.
///
/// Use @State variable to control outline based on this variable.
public func outlined(_ isOutlined: Bool) -> Self {
self.viewModel.isBadgeOutlined = isOutlined
public func borderVisible(_ isBorderVisible: Bool) -> Self {
self.viewModel.isBorderVisible = isBorderVisible
return self
}

/// Controlls text size of the Badge. By ``BadgeSize`` is *.normal*.
///
/// Text font size is based on ``BadgeSize`` value and current ``Theme``.
/// Use @State variable to control ``BadgeSize`` based on this variable.
public func size(_ badgeSize: BadgeSize) -> Self {
self.viewModel.badgeSize = badgeSize
public func size(_ size: BadgeSize) -> Self {
self.viewModel.size = size
return self
}

/// Controlls text format of the Badge. See more details in ``BadgeFormat``.
///
/// Use @State variable to control ``BadgeFormat`` based on this variable.
public func format(_ badgeFormat: BadgeFormat) -> Self {
self.viewModel.badgeFormat = badgeFormat
public func format(_ format: BadgeFormat) -> Self {
self.viewModel.format = format
return self
}

Expand Down
36 changes: 18 additions & 18 deletions core/Sources/Components/Badge/View/SwiftUI/BadgeViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,82 +27,82 @@ final class BadgeViewTests: SwiftUIComponentTestCase {
private let theme: Theme = SparkTheme()

func test_badge_all_cases_no_text() throws {
for badgeIntentType in BadgeIntentType.allCases {
let view = BadgeView(theme: theme, badgeType: badgeIntentType)
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(theme: theme, intent: badgeIntent)
.fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_\(badgeIntentType)")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_\(badgeIntent)")
}
}

func test_badge_all_cases_text() throws {
for badgeIntentType in BadgeIntentType.allCases {
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(
theme: theme,
badgeType: badgeIntentType,
intent: badgeIntent,
value: 23
).fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_with_text_\(badgeIntentType)")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_with_text_\(badgeIntent)")
}
}

func test_badge_all_cases_text_smal_size() throws {
for badgeIntentType in BadgeIntentType.allCases {
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(
theme: theme,
badgeType: badgeIntentType,
intent: badgeIntent,
value: 23
)
.size(.small)
.fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_with_text_\(badgeIntentType)_small_size")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_with_text_\(badgeIntent)_small_size")
}
}

func test_badge_all_cases_text_overflow_format() throws {
for badgeIntentType in BadgeIntentType.allCases {
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(
theme: theme,
badgeType: badgeIntentType,
intent: badgeIntent,
value: 23
)
.format(.overflowCounter(maxValue: 20))
.fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_overflow_format_text_\(badgeIntentType)")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_overflow_format_text_\(badgeIntent)")
}
}

func test_badge_all_cases_text_custom_format() throws {
for badgeIntentType in BadgeIntentType.allCases {
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(
theme: theme,
badgeType: badgeIntentType,
intent: badgeIntent,
value: 23
)
.format(.custom(
formatter: TestBadgeFormatting()
))
.fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_custom_format_text_\(badgeIntentType)")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_custom_format_text_\(badgeIntent)")
}
}

func test_badge_all_cases_no_text_custom_format() throws {
for badgeIntentType in BadgeIntentType.allCases {
for badgeIntent in BadgeIntentType.allCases {
let view = BadgeView(
theme: theme,
badgeType: badgeIntentType
intent: badgeIntent
)
.format(.custom(
formatter: TestBadgeFormatting()
))
.fixedSize()

assertSnapshotInDarkAndLight(matching: view, named: "test_badge_custom_format_no_text_\(badgeIntentType)")
assertSnapshotInDarkAndLight(matching: view, named: "test_badge_custom_format_no_text_\(badgeIntent)")
}
}
}
Loading

0 comments on commit d0a8117

Please sign in to comment.