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

[CM 1206] Add additional shapes #4

Merged
merged 6 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 41 additions & 3 deletions Sources/YStepper/SwiftUI/Views/Stepper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import YMatterType
public struct Stepper {
@Environment(\.sizeCategory) var sizeCategory
let buttonSize: CGSize = CGSize(width: 44, height: 44)

@ObservedObject private var appearanceObserver = Stepper.AppearanceObserver()
@ObservedObject private var valueObserver = Stepper.ValueObserver()

Expand Down Expand Up @@ -96,9 +97,8 @@ extension Stepper: View {
}
.frame(width: (2 * buttonSize.width) + getStringSize(sizeCategory).width)
.background(
Capsule()
.strokeBorder(Color(appearance.borderColor), lineWidth: appearance.borderWidth)
.background(Capsule().foregroundColor(Color(appearance.backgroundColor)))
getShape()
.background(getShapeWithoutStroke().foregroundColor(Color(appearance.backgroundColor)))
)
}

Expand Down Expand Up @@ -133,6 +133,44 @@ extension Stepper: View {
.frame(width: getStringSize(sizeCategory).width)
.accessibilityLabel(getAccessibilityLabelText())
}

@ViewBuilder
func getShape() -> some View {
switch appearance.shape {
case .none:
EmptyView()
case .rectangle:
Rectangle().strokeBorder(Color(appearance.borderColor), lineWidth: appearance.borderWidth)
case .roundRect(cornerRadius: let cornerRadius):
RoundedRectangle(
cornerSize: CGSize(
width: cornerRadius,
height: cornerRadius
)
).strokeBorder(Color(appearance.borderColor), lineWidth: appearance.borderWidth)
case .capsule:
Capsule().strokeBorder(Color(appearance.borderColor), lineWidth: appearance.borderWidth)
}
}

@ViewBuilder
func getShapeWithoutStroke() -> some View {
switch appearance.shape {
case .none:
EmptyView()
case .rectangle:
Rectangle()
case .roundRect(cornerRadius: let cornerRadius):
RoundedRectangle(
cornerSize: CGSize(
width: cornerRadius,
height: cornerRadius
)
)
case .capsule:
Capsule()
}
}
}

extension Stepper {
Expand Down
8 changes: 7 additions & 1 deletion Sources/YStepper/UIKit/StepperControl+Appearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extension StepperControl {
public var incrementImage: UIImage
/// Decrement button image
public var decrementImage: UIImage
/// Stepper's shape
public var shape: Shape
/// Whether to show delete button or not.
var hasDeleteButton: Bool { deleteImage != nil }

Expand All @@ -40,6 +42,8 @@ extension StepperControl {
/// - deleteImage: Delete button image. Default is `Appearance.defaultDeleteImage`
/// - incrementImage: Increment button image. Default is `Appearance.defaultIncrementImage`
/// - decrementImage: Decrement button image. Default is `Appearance.defaultDecrementImage`
/// - shape: Stepper's shape. Default is `.capsule`

public init(
textStyle: (textColor: UIColor, typography: Typography) = (.label, .systemLabel),
foregroundColor: UIColor = .label,
Expand All @@ -48,7 +52,8 @@ extension StepperControl {
borderWidth: CGFloat = 1.0,
deleteImage: UIImage? = Appearance.defaultDeleteImage,
incrementImage: UIImage = Appearance.defaultIncrementImage,
decrementImage: UIImage = Appearance.defaultDecrementImage
decrementImage: UIImage = Appearance.defaultDecrementImage,
shape: Shape = .capsule
) {
self.textStyle = textStyle
self.backgroundColor = backgroundColor
Expand All @@ -57,6 +62,7 @@ extension StepperControl {
self.deleteImage = deleteImage
self.incrementImage = incrementImage
self.decrementImage = decrementImage
self.shape = shape
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions Sources/YStepper/UIKit/YStepper+Shapes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// YStepper+Shapes.swift
// YStepper
//
// Created by Sahil Saini on 15/03/23.
// Copyright © 2023 Y Media Labs. All rights reserved.
//

import UIKit

extension StepperControl.Appearance {
/// Stepper's shape.
public enum Shape: Equatable {
/// None
case none
/// Rectangle.
case rectangle
/// Rounded rectangle.
case roundRect(cornerRadius: CGFloat)
mpospese marked this conversation as resolved.
Show resolved Hide resolved
/// Capsule.
case capsule
}
}
60 changes: 60 additions & 0 deletions Tests/YStepperTests/Views/StepperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,66 @@ final class StepperTests: XCTestCase {
XCTAssertNotNil(button)
}

func testShapesNotNill() {
mpospese marked this conversation as resolved.
Show resolved Hide resolved
var expectedAppearance = StepperControl.Appearance(shape: .rectangle)
var sut = makeSUT(appearance: expectedAppearance)
let rectShape = sut.getShape()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(rectShape)

expectedAppearance = StepperControl.Appearance(shape: .roundRect(cornerRadius: 10))
sut.appearance = expectedAppearance
let roundedRectShape = sut.getShape()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(roundedRectShape)

expectedAppearance = StepperControl.Appearance(shape: .capsule)
sut.appearance = expectedAppearance
let capsuleShape = sut.getShape()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(capsuleShape)

expectedAppearance = StepperControl.Appearance(shape: .none)
sut.appearance = expectedAppearance
let emptyView = sut.getShape()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(emptyView)
}

func testShapesWithoutStrokeNotNill() {
mpospese marked this conversation as resolved.
Show resolved Hide resolved
var expectedAppearance = StepperControl.Appearance(shape: .rectangle)
var sut = makeSUT(appearance: expectedAppearance)
let rectShape = sut.getShapeWithoutStroke()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(rectShape)

expectedAppearance = StepperControl.Appearance(shape: .roundRect(cornerRadius: 10))
sut.appearance = expectedAppearance
let roundedRectShape = sut.getShapeWithoutStroke()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(roundedRectShape)

expectedAppearance = StepperControl.Appearance(shape: .capsule)
sut.appearance = expectedAppearance
let capsuleShape = sut.getShapeWithoutStroke()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(capsuleShape)

expectedAppearance = StepperControl.Appearance(shape: .none)
sut.appearance = expectedAppearance
let emptyView = sut.getShapeWithoutStroke()

XCTAssertEqual(sut.appearance.shape, expectedAppearance.shape)
XCTAssertNotNil(emptyView)
}

func testPreviewNotNil() {
XCTAssertNotNil(Stepper_Previews.previews)
}
Expand Down