From aaa53935e698cd53a0923896647d3da658b0a10e Mon Sep 17 00:00:00 2001 From: Robin Lemaire Date: Tue, 17 Oct 2023 17:46:57 +0200 Subject: [PATCH 1/3] [Fix] Switch: Remove the space when there is no text --- .../Switch/View/SwiftUI/SwitchView.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift index 983479349..85ed46b7f 100644 --- a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift +++ b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift @@ -52,7 +52,7 @@ public struct SwitchView: View { } // MARK: - View - + public var body: some View { HStack(alignment: .top) { ForEach(self.subviewsTypes(), id: \.self) { @@ -100,12 +100,14 @@ public struct SwitchView: View { @ViewBuilder private func space() -> some View { - Spacer() - .frame( - width: self.viewModel.horizontalSpacing.scaledMetric( - with: self.contentStackViewSpacingMultiplier + if self.viewModel.horizontalSpacing ?? 0 > 0 { + Spacer() + .frame( + width: self.viewModel.horizontalSpacing.scaledMetric( + with: self.contentStackViewSpacingMultiplier + ) ) - ) + } } @ViewBuilder From 6689e1b6452fd7c0111f69f775165b05c07a3405 Mon Sep 17 00:00:00 2001 From: Robin Lemaire Date: Tue, 24 Oct 2023 14:17:17 +0200 Subject: [PATCH 2/3] [Fix] move rule on makeSubview --- .../Switch/View/SwiftUI/SwitchView.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift index 85ed46b7f..6a19f32ef 100644 --- a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift +++ b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift @@ -75,7 +75,9 @@ public struct SwitchView: View { private func makeSubview(from type: SwitchSubviewType) -> some View { switch type { case .space: - self.space() + if self.viewModel.horizontalSpacing ?? 0 > 0 { + self.space() + } case .text: self.text() case .toggle: @@ -100,14 +102,12 @@ public struct SwitchView: View { @ViewBuilder private func space() -> some View { - if self.viewModel.horizontalSpacing ?? 0 > 0 { - Spacer() - .frame( - width: self.viewModel.horizontalSpacing.scaledMetric( - with: self.contentStackViewSpacingMultiplier - ) + Spacer() + .frame( + width: self.viewModel.horizontalSpacing.scaledMetric( + with: self.contentStackViewSpacingMultiplier ) - } + ) } @ViewBuilder From 6839c548f6bc6d81ad3d9a7193205e8c20336556 Mon Sep 17 00:00:00 2001 From: Robin Lemaire Date: Tue, 24 Oct 2023 17:27:51 +0200 Subject: [PATCH 3/3] [Fix] move space rule on SwitchSubviewType --- .../SubviewType/SwitchSubviewType.swift | 22 +++++++--- .../SubviewType/SwitchSubviewTypeTests.swift | 42 +++++++++++++++++-- .../Switch/View/SwiftUI/SwitchView.swift | 9 ++-- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewType.swift b/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewType.swift index 0b9167bcf..340abb0c3 100644 --- a/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewType.swift +++ b/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewType.swift @@ -13,13 +13,23 @@ enum SwitchSubviewType { case text case toggle - // MARK: - Properties + // MARK: - Methods - static var leftAlignmentCases: [Self] { - return [.toggle, .space, .text] - } + static func allCases(isLeftAlignment: Bool, showSpace: Bool) -> [Self] { + var cases: [Self] + + // Left or right alignement ? + if isLeftAlignment { + cases = [.toggle, .text] + } else { + cases = [.text, .toggle] + } + + // Add spaces ? + if showSpace { + cases.insert(.space, at: 1) + } - static var rightAlignmentCases: [Self] { - return [.text, .space, .toggle] + return cases } } diff --git a/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewTypeTests.swift b/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewTypeTests.swift index 576559315..bc5c62775 100644 --- a/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewTypeTests.swift +++ b/core/Sources/Components/Switch/View/SwiftUI/SubviewType/SwitchSubviewTypeTests.swift @@ -13,9 +13,26 @@ final class SwitchSubviewTypeTests: XCTestCase { // MARK: - Tests - func test_leftAlignmentCases() { + func test_allCases_when_isLeftAlignment_is_true_and_showSpace_is_false() { // GIVEN / WHEN - let allCases = SwitchSubviewType.leftAlignmentCases + let allCases = SwitchSubviewType.allCases( + isLeftAlignment: true, + showSpace: false + ) + + // THEN + XCTAssertEqual( + allCases, + [.toggle, .text] + ) + } + + func test_allCases_when_isLeftAlignment_is_true_and_showSpace_is_true() { + // GIVEN / WHEN + let allCases = SwitchSubviewType.allCases( + isLeftAlignment: true, + showSpace: true + ) // THEN XCTAssertEqual( @@ -24,9 +41,26 @@ final class SwitchSubviewTypeTests: XCTestCase { ) } - func test_rightAlignmentCases() { + func test_allCases_when_isLeftAlignment_is_false_and_showSpace_is_false() { // GIVEN / WHEN - let allCases = SwitchSubviewType.rightAlignmentCases + let allCases = SwitchSubviewType.allCases( + isLeftAlignment: false, + showSpace: false + ) + + // THEN + XCTAssertEqual( + allCases, + [.text, .toggle] + ) + } + + func test_allCases_when_isLeftAlignment_is_false_and_showSpace_is_true() { + // GIVEN / WHEN + let allCases = SwitchSubviewType.allCases( + isLeftAlignment: false, + showSpace: true + ) // THEN XCTAssertEqual( diff --git a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift index 6a19f32ef..0298ad2cb 100644 --- a/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift +++ b/core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift @@ -68,16 +68,17 @@ public struct SwitchView: View { // MARK: - Subview Maker private func subviewsTypes() -> [SwitchSubviewType] { - return (self.viewModel.isToggleOnLeft == true) ? SwitchSubviewType.leftAlignmentCases : SwitchSubviewType.rightAlignmentCases + return SwitchSubviewType.allCases( + isLeftAlignment: self.viewModel.isToggleOnLeft == true, + showSpace: self.viewModel.horizontalSpacing ?? 0 > 0 + ) } @ViewBuilder private func makeSubview(from type: SwitchSubviewType) -> some View { switch type { case .space: - if self.viewModel.horizontalSpacing ?? 0 > 0 { - self.space() - } + self.space() case .text: self.text() case .toggle: