Skip to content

Instantly share code, notes, and snippets.

@jonathanduty
Created June 29, 2018 12:27
Show Gist options
  • Save jonathanduty/9645169175c6d7b4ff25b2b573a53966 to your computer and use it in GitHub Desktop.
Save jonathanduty/9645169175c6d7b4ff25b2b573a53966 to your computer and use it in GitHub Desktop.
Device+Extensions.swift
import UIKit
struct Device {
let ran: Bool
@discardableResult static func whenIphoneX(_ block: () -> Void) -> Device {
return runIfTrue(Device.isIphoneX, block: block)
}
@discardableResult static func whenNotIphoneX(_ block: () -> Void) -> Device {
return runIfFalse(Device.isIphoneX, block: block)
}
@discardableResult static func whenIphone8Plus(_ block: () -> Void) -> Device {
return runIfTrue(Device.isIphone8Plus, block: block)
}
static var isIphoneX: Bool
{
return mainScreenHeightGreaterThan(height:800)
}
static var isIphone8Plus: Bool
{
return mainScreenHeightGreaterThan(height:700)
}
static func mainScreenHeightGreaterThan(height: Int) -> Bool {
return UIScreen.main.bounds.size.height >= CGFloat(height)
}
static fileprivate func runIfTrue(_ run: Bool, block: () -> Void) -> Device {
if run {
block()
}
return Device(ran: run)
}
static fileprivate func runIfFalse(_ run: Bool, block: () -> Void) -> Device {
if run == false {
block()
}
return Device(ran: run)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment