-
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 #660 from adevinta/feature/component/rating-input-…
…1676 [RatingInput#1676] The rating input component
- Loading branch information
Showing
24 changed files
with
916 additions
and
33 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
core/Sources/Common/Foundation/Extension/CGPoint-Distance.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,17 @@ | ||
// | ||
// CGPoint-Distance.swift | ||
// SparkCore | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension CGPoint { | ||
|
||
/// Returns the distance between two points | ||
func distance(to other: CGPoint) -> CGFloat { | ||
CGFloat(hypotf(Float(self.x - other.x), Float(self.y - other.y))) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/Sources/Common/Foundation/Extension/CGPointDistanceTests.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,25 @@ | ||
// | ||
// CGPointDistanceTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import SparkCore | ||
|
||
final class CGPointDistanceTests: XCTestCase { | ||
|
||
func test_distance_same() throws { | ||
let point1 = CGPoint(x: 10, y: 10) | ||
let point2 = CGPoint(x: 10, y: -100) | ||
|
||
let distance1 = point1.distance(to: point2) | ||
let distance2 = point2.distance(to: point1) | ||
|
||
XCTAssertEqual(distance1, 110.0, "Expected distance does not match") | ||
XCTAssertEqual(distance1, distance2, "Expected both distances to be the same") | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/Sources/Common/Foundation/Extension/CGRect-Center.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,26 @@ | ||
// | ||
// CGRect.swift | ||
// SparkCore | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension CGRect { | ||
/// Returns the center of the x-coordinate of the rect | ||
var centerX: CGFloat { | ||
return (self.minX + self.maxX)/2 | ||
} | ||
|
||
/// Returns the center of the y-coordinate of the rect | ||
var centerY: CGFloat { | ||
return (self.minY + self.maxY)/2 | ||
} | ||
|
||
/// The center point of the rect | ||
var center: CGPoint { | ||
return CGPoint(x: self.centerX, y: self.centerY) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
core/Sources/Common/Foundation/Extension/CGRectCenterTests.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 @@ | ||
// | ||
// CGRectCenterTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import SparkCore | ||
|
||
final class CGRectCenterTests: XCTestCase { | ||
|
||
func testExample() throws { | ||
let rect = CGRect(x: 10, y: 10, width: 110, height: 30) | ||
|
||
XCTAssertEqual(rect.centerX, 65, "CenterX doesn't match expected value") | ||
XCTAssertEqual(rect.centerY, 25, "CenterY doesn't match expected value") | ||
|
||
XCTAssertEqual(rect.center, CGPoint(x: 65, y: 25), "Center point is not correct") | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
core/Sources/Common/Foundation/Extension/UIView-Closest.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 @@ | ||
// | ||
// UIView-Closest.swift | ||
// SparkCore | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
extension Array where Element: UIView { | ||
|
||
/// Returns the index of the array of views which is closest to the point. | ||
func index(closestTo location: CGPoint) -> Int? { | ||
let distances = self.map{ view in | ||
view.frame.center.distance(to: location) | ||
} | ||
let nearest = distances.enumerated().min { (left, right) in | ||
return left.element < right.element | ||
} | ||
return nearest?.offset | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
core/Sources/Common/Foundation/Extension/UIViewClosestTests.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 @@ | ||
// | ||
// UIViewClosestTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SparkCore | ||
|
||
final class UIViewClosestTests: XCTestCase { | ||
|
||
func test_closest() throws { | ||
let positions = [0, 100, 200, 300] | ||
let views = positions.map{ CGRect(x: $0, y: 10, width: 50, height: 50) }.map(UIView.init(frame:)) | ||
|
||
for (index, position) in positions.enumerated() { | ||
let closestIndex = views.index(closestTo: CGPoint(x: position+50, y: 100)) | ||
XCTAssertEqual(closestIndex, index, "Expected \(String(describing: closestIndex)) to be equal to \(index)") | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...ources/Components/Rating/AccessibilityIdentifier/RatingInputAccessibilityIdentifier.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,18 @@ | ||
// | ||
// RatingInputAccessibilityIdentifier.swift | ||
// SparkCore | ||
// | ||
// Created by Michael Zimmermann on 27.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The accessibility identifiers of the rating input. | ||
public enum RatingInputAccessibilityIdentifier { | ||
|
||
// MARK: - Properties | ||
|
||
/// The accessibility identifier. | ||
public static let identifier = "spark-rating-input" | ||
} |
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
43 changes: 43 additions & 0 deletions
43
core/Sources/Components/Rating/TestHelpers/RatingInputConfigurationSnapshotTests.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,43 @@ | ||
// | ||
// RatingInputConfigurationSnapshotTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
@testable import SparkCore | ||
|
||
struct RatingInputConfigurationSnapshotTests { | ||
|
||
// MARK: - Properties | ||
|
||
let scenario: RatingInputScenarioSnapshotTests | ||
|
||
let rating: CGFloat | ||
let intent = RatingIntent.main | ||
|
||
let modes: [ComponentSnapshotTestMode] | ||
let sizes: [UIContentSizeCategory] | ||
let state: RatingInputState | ||
|
||
// MARK: - Getter | ||
|
||
func testName() -> String { | ||
return [ | ||
"\(self.scenario.rawValue)", | ||
"\(self.intent)", | ||
"\(self.rating)", | ||
"\(self.state)" | ||
].joined(separator: "-") | ||
} | ||
} | ||
|
||
enum RatingInputState: CaseIterable { | ||
case enabled | ||
case disabled | ||
case pressed | ||
} |
105 changes: 105 additions & 0 deletions
105
core/Sources/Components/Rating/TestHelpers/RatingInputScenarioSnapshotTests.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,105 @@ | ||
// | ||
// RatingInputScenarioSnapshotTests.swift | ||
// SparkCoreUnitTests | ||
// | ||
// Created by Michael Zimmermann on 30.11.23. | ||
// Copyright © 2023 Adevinta. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import SwiftUI | ||
|
||
@testable import SparkCore | ||
|
||
enum RatingInputScenarioSnapshotTests: String, CaseIterable { | ||
|
||
case test1 | ||
case test2 | ||
case test3 | ||
|
||
// MARK: - Type Alias | ||
|
||
typealias Constants = ComponentSnapshotTestConstants | ||
|
||
|
||
// MARK: - Configurations | ||
func configuration(isSwiftUIComponent: Bool) -> [RatingInputConfigurationSnapshotTests] { | ||
switch self { | ||
case .test1: | ||
return self.test1(isSwiftUIComponent: isSwiftUIComponent) | ||
case .test2: | ||
return self.test2(isSwiftUIComponent: isSwiftUIComponent) | ||
case .test3: | ||
return self.test3(isSwiftUIComponent: isSwiftUIComponent) | ||
} | ||
} | ||
|
||
// MARK: - Scenarios | ||
|
||
/// Test 1 | ||
/// | ||
/// Description: To various rating values | ||
/// | ||
/// Content: | ||
/// - ratings: 2.0 | ||
/// - states: enabled | ||
/// - modes: all | ||
/// - accessibility sizes: default | ||
private func test1(isSwiftUIComponent: Bool) -> [RatingInputConfigurationSnapshotTests] { | ||
let ratings: [CGFloat] = [1.0, 5.0] | ||
|
||
return ratings.map { rating in | ||
return .init( | ||
scenario: self, | ||
rating: rating, | ||
modes: Constants.Modes.all, | ||
sizes: Constants.Sizes.default, | ||
state: .enabled | ||
) | ||
} | ||
} | ||
|
||
/// Test 2 | ||
/// | ||
/// | ||
/// Description: To various accessibility sizes | ||
/// | ||
/// Content: | ||
/// - ratings: [1.0] | ||
/// - modes: default | ||
/// - accessibility sizes: all | ||
/// - states: all | ||
private func test2(isSwiftUIComponent: Bool) -> [RatingInputConfigurationSnapshotTests] { | ||
return [.init( | ||
scenario: self, | ||
rating: 1.0, | ||
modes: Constants.Modes.default, | ||
sizes: Constants.Sizes.all, | ||
state: .enabled | ||
)] | ||
} | ||
|
||
/// Test 3 | ||
/// | ||
/// Description: To various rating values | ||
/// | ||
/// Content: | ||
/// - ratings: [1.0, 5.0] | ||
/// - states: disabled, pressed | ||
/// - modes: all | ||
/// - accessibility sizes: default | ||
private func test3(isSwiftUIComponent: Bool) -> [RatingInputConfigurationSnapshotTests] { | ||
|
||
return [RatingInputState.disabled, .pressed].map { state in | ||
return .init( | ||
scenario: self, | ||
rating: 2.0, | ||
modes: Constants.Modes.all, | ||
sizes: Constants.Sizes.default, | ||
state: state | ||
) | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.