Skip to content

Commit

Permalink
tvOS, macOS support (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed May 31, 2018
1 parent ea36f39 commit 8c7826a
Show file tree
Hide file tree
Showing 45 changed files with 1,782 additions and 449 deletions.
20 changes: 13 additions & 7 deletions Classes/Enums/Padding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,27 @@ public enum Padding {

extension Padding {

#if os(iOS) || os(tvOS)
typealias EdgeInsetsAlias = UIEdgeInsets
#elseif os(OSX)
typealias EdgeInsetsAlias = NSEdgeInsets
#endif

/// UIEdgeInsets representation
var value: UIEdgeInsets {
var value: EdgeInsetsAlias {
switch self {
case .top(let top):
return UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0)
return EdgeInsetsAlias(top: top, left: 0, bottom: 0, right: 0)
case .left(let left):
return UIEdgeInsets(top: 0, left: left, bottom: 0, right: 0)
return EdgeInsetsAlias(top: 0, left: left, bottom: 0, right: 0)
case .right(let right):
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: right)
return EdgeInsetsAlias(top: 0, left: 0, bottom: 0, right: right)
case .horizontal(let left, let right):
return UIEdgeInsets(top: 0, left: left, bottom: 0, right: right)
return EdgeInsetsAlias(top: 0, left: left, bottom: 0, right: right)
case .full(let top, let left, let right):
return UIEdgeInsets(top: top, left: left, bottom: 0, right: right)
return EdgeInsetsAlias(top: top, left: left, bottom: 0, right: right)
case .none:
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
return EdgeInsetsAlias(top: 0, left: 0, bottom: 0, right: 0)
}
}

Expand Down
14 changes: 9 additions & 5 deletions Classes/Enums/Position.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ public struct Position: ExpressibleByIntegerLiteral {
case reversed(Int)

/// Up to another element
case relation(UIView, margin: CGFloat)
case relation(ViewAlias, margin: CGFloat)

/// Match position of another view
case match(UIView, margin: CGFloat)
case match(ViewAlias, margin: CGFloat)

/// Dynamic position
case dynamic

#if os(iOS) || os(tvOS)
/// Custom position for a given size class (trait collection)
case custom(((_ traitCollection: UITraitCollection) -> Position))
case custom(((_ traitCollection: NSTraitCollection) -> Position))
#endif

}

Expand All @@ -56,16 +58,18 @@ public struct Position: ExpressibleByIntegerLiteral {
public static func reversed(_ value: Int) -> Position { return .init(.reversed(value)) }

/// Up to another element
public static func relation(_ view: UIView, margin: CGFloat = 0) -> Position { return .init(.relation(view, margin: margin)) }
public static func relation(_ view: ViewAlias, margin: CGFloat = 0) -> Position { return .init(.relation(view, margin: margin)) }

/// Match position of another view
public static func match(_ view: UIView, margin: CGFloat = 0) -> Position { return .init(.match(view, margin: margin)) }
public static func match(_ view: ViewAlias, margin: CGFloat = 0) -> Position { return .init(.match(view, margin: margin)) }

/// Dynamic position
public static var dynamic: Position { return .init(.dynamic) }

#if os(iOS) || os(tvOS)
/// Custom position for a given size class (trait collection)
public static func custom(_ closure: @escaping ((_ traitCollection: UITraitCollection) -> Position)) -> Position { return .init(.custom(closure)) }
#endif

/// First column
public static var first: Position { return .col(0) }
Expand Down
20 changes: 12 additions & 8 deletions Classes/Enums/Vertical.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ public struct Vertical {
case exactly(fromTop: CGFloat)

/// Match top of another view
case match(UIView, margin: CGFloat)
case match(ViewAlias, margin: CGFloat)

/// Below another view (view, margin)
case below(UIView, margin: CGFloat)
case below(ViewAlias, margin: CGFloat)

/// Above another view (view, margin)
case above(UIView, margin: CGFloat)
case above(ViewAlias, margin: CGFloat)

/// Maintains a position (row) under a set of elements
case row([UIView], margin: CGFloat)
case row([ViewAlias], margin: CGFloat)

#if os(iOS) || os(tvOS)
/// Custom vertical position for a given size class (trait collection)
case custom(((_ traitCollection: UITraitCollection) -> Vertical))
#endif

}

Expand All @@ -50,18 +52,20 @@ public struct Vertical {
public static func exactly(fromTop: CGFloat) -> Vertical { return .init(.exactly(fromTop: fromTop)) }

/// Match top of another view
public static func match(_ view: UIView, margin: CGFloat = 0) -> Vertical { return .init(.match(view, margin: margin)) }
public static func match(_ view: ViewAlias, margin: CGFloat = 0) -> Vertical { return .init(.match(view, margin: margin)) }

/// Maintains a position under a set of elements
public static func below(_ views: [UIView], margin: CGFloat = 0) -> Vertical { return .init(.row(views, margin: margin)) }
public static func below(_ views: [ViewAlias], margin: CGFloat = 0) -> Vertical { return .init(.row(views, margin: margin)) }

/// Below another view (view, margin)
public static func below(_ view: UIView, margin: CGFloat = 0) -> Vertical { return .init(.below(view, margin: margin)) }
public static func below(_ view: ViewAlias, margin: CGFloat = 0) -> Vertical { return .init(.below(view, margin: margin)) }

/// Above another view (view, margin)
public static func above(_ view: UIView, margin: CGFloat = 0) -> Vertical { return .init(.above(view, margin: margin)) }
public static func above(_ view: ViewAlias, margin: CGFloat = 0) -> Vertical { return .init(.above(view, margin: margin)) }

#if os(iOS) || os(tvOS)
/// Custom vertical position for a given size class (trait collection)
public static func custom(_ closure: @escaping ((_ traitCollection: UITraitCollection) -> Vertical)) -> Vertical { return .init(.custom(closure)) }
#endif

}
9 changes: 7 additions & 2 deletions Classes/Extensions/GridSrollView+Subviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

@_exported import Foundation
@_exported import SnapKit
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


extension GridScrollView {
Expand All @@ -23,7 +28,7 @@ extension GridScrollView {
- padding: Subview padding on the grid
- redraw: SnapKit make closure for that fine tunning we all need
*/
public func add(subview view: UIView, _ vertical: Vertical? = nil, from: Position = .first, space: Position = .last, padding: Padding = .none, redraw: ((_ make: ConstraintMaker) -> Void)? = nil) {
public func add(subview view: ViewAlias, _ vertical: Vertical? = nil, from: Position = .first, space: Position = .last, padding: Padding = .none, redraw: ((_ make: ConstraintMaker) -> Void)? = nil) {
let subview = Subview(
properties: Properties(
vertical: vertical,
Expand All @@ -38,7 +43,7 @@ extension GridScrollView {
}

@available(*, unavailable, message: "This method is unavailable, please use add(subview:_:from:space:padding:redraw:) instead")
open override func addSubview(_ view: UIView) {
open override func addSubview(_ view: ViewAlias) {
fatalError("Add subview is not supported on a GridView")
}

Expand Down
2 changes: 2 additions & 0 deletions Classes/Extensions/GridView+Drawing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@_exported import Foundation
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


Expand Down
31 changes: 31 additions & 0 deletions Classes/Extensions/GridView+Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,40 @@
//

@_exported import Foundation
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


extension GridView {

#if os(iOS) || os(tvOS)
/// Layout subviews override
@available(*, unavailable, message: "This method is unavailable")
open override func layoutSubviews() {
super.layoutSubviews()

executeLayout()
}
#elseif os(OSX)
/// Layout subviews override
@available(*, unavailable, message: "This method is unavailable")
open override func layout() {
macLayout()
}

/// Mac layout
func macLayout() {
super.layout()

executeLayout()
}
#endif

/// Execute layout
private func executeLayout() {
if self.superview != nil {
config.recalculate()
config.reDraw()
Expand Down Expand Up @@ -68,8 +93,10 @@ extension GridView {
for view in views {
make.top.greaterThanOrEqualTo(view.snp.bottom).offset(margin)
}
#if os(iOS) || os(tvOS)
case .custom(let closure):
set(vertical: closure(traitCollection))
#endif
}
}
set(vertical: vertical)
Expand Down Expand Up @@ -97,8 +124,10 @@ extension GridView {
make.left.equalTo(view).offset(margin + leftPadding)
case .relation(let view, margin: let margin):
make.left.equalTo(view.snp.right).offset(margin + leftPadding)
#if os(iOS) || os(tvOS)
case .custom(let closure):
setLeft(position: closure(traitCollection))
#endif
}
}
setLeft(position: subview.properties.from)
Expand Down Expand Up @@ -127,8 +156,10 @@ extension GridView {
make.right.equalTo(view).offset(margin - rightPadding)
case .relation(let view, margin: let margin):
make.right.equalTo(view.snp.left).offset(margin - rightPadding)
#if os(iOS) || os(tvOS)
case .custom(let closure):
setRight(position: closure(traitCollection))
#endif
}
}
setRight(position: subview.properties.space)
Expand Down
11 changes: 8 additions & 3 deletions Classes/Extensions/GridView+Subviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

@_exported import Foundation
@_exported import SnapKit
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


extension GridView {
Expand All @@ -23,7 +28,7 @@ extension GridView {
- padding: Subview padding on the grid
- redraw: SnapKit make closure for that fine tunning we all need
*/
public func add(subview view: UIView, _ vertical: Vertical? = nil, from: Position = .first, space: Position = .last, padding: Padding = .none, redraw: ((_ make: ConstraintMaker) -> Void)? = nil) {
public func add(subview view: ViewAlias, _ vertical: Vertical? = nil, from: Position = .first, space: Position = .last, padding: Padding = .none, redraw: ((_ make: ConstraintMaker) -> Void)? = nil) {
let subview = Subview(
properties: Properties(
vertical: vertical,
Expand All @@ -38,7 +43,7 @@ extension GridView {
}

@available(*, unavailable, message: "This method is unavailable, please use add(subview:_:from:space:padding:redraw:) instead")
open override func addSubview(_ view: UIView) {
open override func addSubview(_ view: ViewAlias) {
fatalError("Add subview is not supported on a GridView")
}

Expand All @@ -57,7 +62,7 @@ extension GridView {
extension GridView {

/// Will remove subview from grid view
open override func willRemoveSubview(_ subview: UIView) {
open override func willRemoveSubview(_ subview: ViewAlias) {
super.willRemoveSubview(subview)

cleanGridSubviews()
Expand Down
2 changes: 2 additions & 0 deletions Classes/Extensions/UIColor+Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@_exported import Foundation
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


Expand Down
23 changes: 23 additions & 0 deletions Classes/Libs/Compatibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Compatibility.swift
// Hagrid
//
// Created by Ondrej Rafaj on 31/05/2018.
// Copyright © 2018 LiveUI. All rights reserved.
//

import Foundation

#if os(iOS) || os(tvOS)

@_exported import UIKit

public typealias ViewAlias = UIView

#elseif os(OSX)

@_exported import Cocoa

public typealias ViewAlias = NSView

#endif
2 changes: 1 addition & 1 deletion Classes/Libs/Subview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct Subview {
public let properties: Properties

/// View
public let view: UIView
public let view: ViewAlias

}

Expand Down
2 changes: 1 addition & 1 deletion Classes/New Group/GridViewInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public protocol GridViewInterface {
- padding: Subview padding on the grid
- redraw: SnapKit make closure for that fine tunning we all need
*/
func add(subview view: UIView, _ vertical: Vertical?, from: Position, space: Position, padding: Padding, redraw: ((_ make: ConstraintMaker) -> Void)?)
func add(subview view: ViewAlias, _ vertical: Vertical?, from: Position, space: Position, padding: Padding, redraw: ((_ make: ConstraintMaker) -> Void)?)

}
6 changes: 4 additions & 2 deletions Classes/New Group/ScrollViewForwarder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@_exported import Foundation
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


Expand Down Expand Up @@ -213,11 +215,11 @@ extension GridScrollView {
set { scrollView.canCancelContentTouches = newValue }
}

open func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: UIView) -> Bool {
open func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: ViewAlias) -> Bool {
return touchesShouldBegin(touches, with: event, in: view)
}

open func touchesShouldCancel(in view: UIView) -> Bool {
open func touchesShouldCancel(in view: ViewAlias) -> Bool {
return touchesShouldCancel(in: view)
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/View controllers/GridScrollViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class GridScrollViewController: UIViewController {

/// Original view (removed)
@available(*, unavailable, message: "This property is unavailable", renamed: "gridView")
open override var view: UIView! {
open override var view: ViewAlias! {
get { return gridView }
set { }
}
Expand Down
4 changes: 3 additions & 1 deletion Classes/View controllers/GridViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@_exported import Foundation
#if os(iOS) || os(tvOS)
@_exported import UIKit
#elseif os(OSX)
@_exported import Cocoa
#endif


Expand All @@ -24,7 +26,7 @@ open class GridViewController: UIViewController {

/// Original view (removed)
@available(*, unavailable, message: "This property is unavailable", renamed: "gridView")
open override var view: UIView! {
open override var view: ViewAlias! {
get { return gridView }
set { }
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Views/GridScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


/// GridView enabled scroll view
open class GridScrollView: UIView, GridViewInterface, ScrollViewForwarder {
open class GridScrollView: ViewAlias, GridViewInterface, ScrollViewForwarder {

/// Scrolling direction
public enum Direction {
Expand Down
Loading

0 comments on commit 8c7826a

Please sign in to comment.