kean/Align

Improvement AnchorCollectionSize

bagusandinata opened this issue ยท 2 comments

Hi, @kean thanks for great library ๐Ÿ˜€

I don't know if this ever entered the framework or not, if I have case set size with same width and height like

view.ancors.size.equal(CGSize(width: 100 height: 100))

Can I just write simple code like this ?


view.ancors.size.equal(100)

I'm just giving a suggestion what if AnchorCollectionSize improve like

public protocol ConstraintSize {}
extension CGSize: ConstraintSize {}
extension Int: ConstraintSize {}
extension Double: ConstraintSize {}
extension CGFloat: ConstraintSize {}

public struct AnchorCollectionSize {
    /// Set the size of item.
    @discardableResult public func equal(_ size: ConstraintSize) -> [NSLayoutConstraint] {
        switch size {
        case is CGFloat, is Int, is Double:
            let _size: CGFloat = convertSizeToEqual(from: size)
            return [width.equal(_size), height.equal(_size)]
        case let size as CGSize:
            return [width.equal(size.width), height.equal(size.height)]
        default:
            return []
        }
    }

    /// Set the size of item.
    @discardableResult public func greaterThanOrEqul(_ size: ConstraintSize) -> [NSLayoutConstraint] {
        switch size {
        case is CGFloat, is Int, is Double:
            let _size: CGFloat = convertSizeToEqual(from: size)
            return [width.greaterThanOrEqual(_size), height.greaterThanOrEqual(_size)]
        case let size as CGSize:
            return [width.greaterThanOrEqual(size.width), height.greaterThanOrEqual(size.height)]
        default:
            return []
        }
    }

    /// Set the size of item.
    @discardableResult public func lessThanOrEqual(_ size: ConstraintSize) -> [NSLayoutConstraint] {
        switch size {
        case is CGFloat, is Int, is Double:
            let _size: CGFloat = convertSizeToEqual(from: size)
            return [width.lessThanOrEqual(_size), height.lessThanOrEqual(_size)]
        case let size as CGSize:
            return [width.lessThanOrEqual(size.width), height.lessThanOrEqual(size.height)]
        default:
            return []
        }
    }

    /// Makes convert ConstraintSize to CGFloat
    private func convertSizeToEqual(from size: ConstraintSize) -> CGFloat {
        var _size: CGFloat = 0
        
        if let size = size as? Int {
            _size = CGFloat(size)
        } else if let size = size as? Double {
            _size = CGFloat(size)
        } else if let size = size as? CGFloat {
            _size = size
        }
        
        return _size
    }
}
kean commented

Hey, thanks @bagusandinata, glad you like it.

My goal with Align was to make it as small as possible. This is why it's missing some affordances. What I usually do when working with sizes is the following:

extension CGSize {
    init(side: CGFloat) { ... }
}

view.ancors.size.equal(CGSize.side(100))
view.ancors.size.equal(.side(100))

thanks @kean, I got it ๐Ÿ˜€