Skip to content

Commit

Permalink
Merge pull request #24 from RakuyoKit/feature/epoxy-row-optimization
Browse files Browse the repository at this point in the history
feat: Added method to set Epoxy custom Row size
  • Loading branch information
rakuyoMo authored May 21, 2024
2 parents 7ce2493 + f9a3473 commit e438363
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
54 changes: 54 additions & 0 deletions Sources/Core/Utilities/Size.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Size.swift
// RakuyoKit
//
// Created by Rakuyo on 2024/5/21.
// Copyright © 2024 RakuyoKit. All rights reserved.
//

import Foundation

// MARK: - Size

public struct Size {
public var width: Float

public var height: Float

public init(width: Float, height: Float) {
self.width = width
self.height = height
}
}

// MARK: - Logic

extension Size {
public static var zero: Self {
.init(width: 0, height: 0)
}

public static var greatestFiniteMagnitude: Self {
.init(width: .greatestFiniteMagnitude, height: .greatestFiniteMagnitude)
}

public var cgSize: CGSize {
.init(width: CGFloat(width), height: CGFloat(height))
}
}

// MARK: Hashable

extension Size: Hashable { }

// MARK: Equatable

extension Size: Equatable { }

// MARK: CustomStringConvertible

extension Size: CustomStringConvertible {
public var description: String {
"{ height:\(height), width:\(width) }"
}
}
18 changes: 18 additions & 0 deletions Sources/Epoxy/Row/ButtonRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ import RAKCore
/// If you want to extend, consider building your own view with
/// the help of `ButtonRow.Style`, `ButtonRow.Content` and `ButtonRow.Behaviors`.
public final class ButtonRow: UIButton {
private lazy var size: Size? = .zero

/// Closure for touch down event.
private lazy var didTouchDown: ButtonClosure? = nil

/// Closure for tap event.
private lazy var didTap: ButtonClosure? = nil
}

// MARK: - Life cycle

extension ButtonRow {
override public var intrinsicContentSize: CGSize {
size?.cgSize ?? super.intrinsicContentSize
}
}

// MARK: Action

extension ButtonRow {
Expand All @@ -43,6 +53,11 @@ extension ButtonRow {

extension ButtonRow: StyledView {
public struct Style: Hashable {
/// button size
///
/// When a side value is `greatestFiniteMagnitude`, adaptive size will be used on that side
public let size: Size?

/// The tint color.
public let tintColor: UIColor?

Expand All @@ -56,10 +71,12 @@ extension ButtonRow: StyledView {
public let titleStyle: TextRow.Style?

public init(
size: Size? = nil,
tintColor: UIColor? = nil,
type: UIButton.ButtonType = .system,
titleStyle: TextRow.Style? = nil
) {
self.size = size
self.tintColor = tintColor
self.type = type
self.titleStyle = titleStyle
Expand All @@ -71,6 +88,7 @@ extension ButtonRow: StyledView {

translatesAutoresizingMaskIntoConstraints = false

size = style.size
tintColor = style.tintColor

if let titleStyle = style.titleStyle {
Expand Down
20 changes: 19 additions & 1 deletion Sources/Epoxy/Row/ImageRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,27 @@ import RAKCore
///
/// If you want to extend, consider building your own view with
/// the help of `ImageRow.Style`, `ImageRow.Content` and `ImageRow.Behaviors`.
public final class ImageRow: UIImageView { }
public final class ImageRow: UIImageView {
private lazy var size: Size? = .zero
}

// MARK: - Life cycle

extension ImageRow {
override public var intrinsicContentSize: CGSize {
size?.cgSize ?? super.intrinsicContentSize
}
}

// MARK: StyledView

extension ImageRow: StyledView {
public struct Style: Hashable {
/// Image row size
///
/// When a side value is `greatestFiniteMagnitude`, adaptive size will be used on that side
public let size: Size?

/// The tint color.
public let tintColor: UIColor?

Expand All @@ -35,10 +50,12 @@ extension ImageRow: StyledView {
public let blockAccessibilityDescription: Bool

public init(
size: Size? = nil,
tintColor: UIColor? = nil,
contentMode: ContentMode = .scaleToFill,
blockAccessibilityDescription: Bool = false
) {
self.size = size
self.tintColor = tintColor
self.contentMode = contentMode
self.blockAccessibilityDescription = blockAccessibilityDescription
Expand All @@ -50,6 +67,7 @@ extension ImageRow: StyledView {

translatesAutoresizingMaskIntoConstraints = false

size = style.size
tintColor = style.tintColor
contentMode = style.contentMode

Expand Down
4 changes: 4 additions & 0 deletions Sources/Epoxy/Row/SwitchRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ extension SwitchRow {

extension SwitchRow: StyledView {
public struct Style: Hashable {
public let scale: CGFloat
public let onTintColor: UIColor?
public let thumbTintColor: UIColor?
public let onImage: UIImage?
public let offImage: UIImage?

public init(
scale: CGFloat = 1,
onTintColor: ConvertibleToColor? = nil,
thumbTintColor: ConvertibleToColor? = nil,
onImage: UIImage? = nil,
offImage: UIImage? = nil
) {
self.scale = scale
self.onTintColor = onTintColor?.color
self.thumbTintColor = thumbTintColor?.color
self.onImage = onImage
Expand All @@ -60,6 +63,7 @@ extension SwitchRow: StyledView {

translatesAutoresizingMaskIntoConstraints = false

transform = { CGAffineTransform(scaleX: $0, y: $0) }(style.scale)
onTintColor = style.onTintColor
thumbTintColor = style.thumbTintColor
onImage = style.onImage
Expand Down
16 changes: 15 additions & 1 deletion Sources/Epoxy/Row/TextRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,38 @@ import RAKCore
///
/// If you want to extend, consider building your own view with
/// the help of `TextRow.Style` and `TextRow.Content`.
public final class TextRow: UILabel { }
public final class TextRow: UILabel {
private lazy var size: Size? = .zero
}

// MARK: - Life cycle

extension TextRow {
override public var intrinsicContentSize: CGSize {
size?.cgSize ?? super.intrinsicContentSize
}
}

// MARK: StyledView

extension TextRow: StyledView {
public struct Style: Hashable {
public let size: Size?
public let font: UIFont
public let color: UIColor
public let alignment: NSTextAlignment
public let numberOfLines: Int
public let lineBreakMode: NSLineBreakMode

public init(
size: Size? = nil,
font: UIFont = .systemFont(ofSize: UIFont.labelFontSize),
color: ConvertibleToColor = UIColor.label,
alignment: NSTextAlignment = .left,
numberOfLines: Int = 0,
lineBreakMode: NSLineBreakMode = .byTruncatingTail
) {
self.size = size
self.font = font
self.color = color.color
self.alignment = alignment
Expand All @@ -49,6 +62,7 @@ extension TextRow: StyledView {

translatesAutoresizingMaskIntoConstraints = false

size = style.size
font = style.font
textColor = style.color
textAlignment = style.alignment
Expand Down

0 comments on commit e438363

Please sign in to comment.