Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Component] Switch: Add SwiftUI version #455

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// CGFloat+ScaledMetricExtension.swift
// SparkCore
//
// Created by robin.lemaire on 14/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation

extension CGFloat {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever, I like this.


/// Because the @ScaledMetric cannot be updated,
/// we add this the scaled metric multiplier value.
/// This value must be multiplied with you want to make dynamic (width, height, padding, ...)
/// - note: - Please use this value only for @ScaledMetric on SwiftUI
///
/// **Example**
/// This example shows how to create view this multiplier on SwiftUI View
/// ```swift
/// @ScaledMetric private var spacingMultiplier: CGFloat = ScaledMetric.scaledMetricMultiplier
/// ```
static var scaledMetricMultiplier: CGFloat {
return 1
}

/// Because the @ScaledMetric cannot be updated,
/// we must multiply the value that you want to make dynamic
/// with the scaled value mutiliplier get from CGFloat.scaledMetricMultiplier
/// - Parameter multiplier: the scaled value mutiliplier get from CGFloat.scaledMetricMultiplier and stock on @ScaledMetric var
/// - note: - Please use this value only for @ScaledMetric on SwiftUI
///
/// **Example**
/// This example shows how to implement the scaled metric value for the width of a view
/// ```swift
/// @ScaledMetric private var spacingMultiplier: CGFloat = ScaledMetric.scaledMetricMultiplier
/// @ObservedObject private var viewModel: MyViewModel
///
/// var body: any View {
/// Spacer()
/// .frame(width: self.viewModel.spacing.scaledMetric(self.spacingMultiplier))
/// }
/// ```
func scaledMetric(with multiplier: CGFloat) -> CGFloat {
self * multiplier
}
}

public extension Optional where Wrapped == CGFloat {

/// Same as **scaledMetric(with:)** func with optional value
/// If the CGFloat is nil, the default value is 0
func scaledMetric(with multiplier: CGFloat) -> CGFloat {
return (self ?? 0).scaledMetric(with: multiplier)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ struct SwitchSutTests {
(images: images, text: "My Full Content Switch", attributedText: nil), // Images + text
(images: nil, text: "My Content Switch", attributedText: nil), // Only text
(images: images, text: nil, attributedText: attributedText), // Images + attributed text
(images: nil, text: nil, attributedText: attributedText) // Only attributed text
(images: nil, text: nil, attributedText: attributedText), // Only attributed text
(images: nil, text: nil, attributedText: nil) // Nothing
]

return items.map { item -> SwitchSutTests in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// SwitchSubviewType.swift
// SparkCore
//
// Created by robin.lemaire on 11/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation

enum SwitchSubviewType {
case space
case text
case toggle

// MARK: - Properties

static var leftAlignmentCases: [Self] {
return [.toggle, .space, .text]
}

static var rightAlignmentCases: [Self] {
return [.text, .space, .toggle]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// SwitchSubviewTypeTests.swift
// SparkCoreTests
//
// Created by robin.lemaire on 14/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import XCTest
@testable import SparkCore

final class SwitchSubviewTypeTests: XCTestCase {

// MARK: - Tests

func test_leftAlignmentCases() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.leftAlignmentCases

// THEN
XCTAssertEqual(
allCases,
[.toggle, .space, .text]
)
}

func test_rightAlignmentCases() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.rightAlignmentCases

// THEN
XCTAssertEqual(
allCases,
[.text, .space, .toggle]
)
}
}
Loading
Loading