Skip to content

Instantly share code, notes, and snippets.

@felixvisee
Created July 9, 2015 14:03
Show Gist options
  • Save felixvisee/3b945e18f7373c1915b8 to your computer and use it in GitHub Desktop.
Save felixvisee/3b945e18f7373c1915b8 to your computer and use it in GitHub Desktop.
A `Box`-like struct returning a value based on the current traits of a view.
public class View: UIView {
public override var traitCollection: UITraitCollection {
return UITraitCollection(traitsFromCollections: [
UITraitCollection(horizontalSizeClass: .Compact),
UITraitCollection(verticalSizeClass: .Compact)
])
}
}
let view = View()
let variable = Variable(view: view, definition: [
(UITraitCollection(horizontalSizeClass: .Compact), 1),
(UITraitCollection(horizontalSizeClass: .Unspecified), 5)
])
variable.value
import UIKit
public struct Variable<T> {
public var view: UIView
public var definition: [(traitCollection: UITraitCollection, value: T)]
public var value: T? {
for (traitCollection, value) in definition {
if view.traitCollection.containsTraitsInCollection(traitCollection) {
return value
}
}
return nil
}
}
@hffmnn
Copy link

hffmnn commented Jul 10, 2015

To make my concerns more clear: A view is defined by a vertical and a horizontal size class.

public class View: UIView {
    public override var traitCollection: UITraitCollection {
        return UITraitCollection(traitsFromCollections: [
            UITraitCollection(horizontalSizeClass: .Compact),
            UITraitCollection(verticalSizeClass: .Compact)
        ])
    }
}

The definition on Variable only returns a value considering one part of the trait collection.

let view = View()
let variable = Variable(view: view, definition: [
    (UITraitCollection(horizontalSizeClass: .Compact), (width: 10, height: 10)),
    (UITraitCollection(verticalSizeClass: .Compact), (width: 20, height: 20)),
    (UITraitCollection(horizontalSizeClass: .Regular), (width: 30, height: 30)),
    (UITraitCollection(verticalSizeClass: .Regular), (width: 40, height: 40))
    ])

This returns (10, 10), which is not really correct.
What about something like:

import UIKit

public struct Variable<T> {
    public var view: UIView
    public var definition: [(traitCollection: UITraitCollection, traitCollection: UITraitCollection, value: T)]

    public var value: T? {
        for (traitCollection, traitCollection2, value) in definition {
            if view.traitCollection.containsTraitsInCollection(traitCollection) &&
            view.traitCollection.containsTraitsInCollection(traitCollection2) {
                return value
            }
        }

        return nil
    }
}

public class View: UIView {
    public override var traitCollection: UITraitCollection {
        return UITraitCollection(traitsFromCollections: [
            UITraitCollection(horizontalSizeClass: .Compact),
            UITraitCollection(verticalSizeClass: .Compact)
            ])
    }
}

let view = View()
let variable = Variable(view: view, definition: [
    (UITraitCollection(horizontalSizeClass: .Compact), UITraitCollection(verticalSizeClass: .Compact), (width: 10, height: 10)),
    (UITraitCollection(horizontalSizeClass: .Regular), UITraitCollection(verticalSizeClass: .Regular), (width: 30, height: 30))
    ])

print(variable.value)

Maybe even make the second traitcollection optional?

@felixvisee
Copy link
Author

Much easier:

let variable = Variable(view: view, definition: [
    (UITraitCollection(traitsFromCollections: [ UITraitCollection(horizontalSizeClass: .Compact), UITraitCollection(verticalSizeClass: .Compact) ]), (width: 10, height: 10)),
    (UITraitCollection(traitsFromCollections: [ UITraitCollection(horizontalSizeClass: .Regular), UITraitCollection(verticalSizeClass: .Regular) ]), (width: 30, height: 30))
])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment