-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from Shopify/ftd-cells
Simple Cell Examples
- Loading branch information
Showing
21 changed files
with
905 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
FunctionalTableData.xcodeproj/xcshareddata/xcschemes/FunctionalTableData.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// ButtonState.swift | ||
// Shopify | ||
// | ||
// Created by Raul Riera on 2017-05-02. | ||
// Copyright © 2017 Shopify. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import FunctionalTableData | ||
|
||
typealias ButtonCell = HostCell<UIButton, ButtonState, LayoutMarginsTableItemLayout> | ||
|
||
public struct ButtonState: Equatable { | ||
public let title: String | ||
public let isEnabled: Bool | ||
public let alignment: UIControl.ContentHorizontalAlignment | ||
public let action: (UIButton) -> Void | ||
|
||
public init(title: String, isEnabled: Bool = true, alignment: UIControl.ContentHorizontalAlignment = .center, action: @escaping (UIButton) -> Void) { | ||
self.title = title | ||
self.isEnabled = isEnabled | ||
self.alignment = alignment | ||
self.action = action | ||
} | ||
|
||
public static func updateView(_ view: UIButton, state: ButtonState?) { | ||
guard let state = state else { | ||
view.setTitle(nil, for: .normal) | ||
view.isEnabled = true | ||
view.contentHorizontalAlignment = .center | ||
view.setActions([]) | ||
return | ||
} | ||
|
||
view.setTitle(state.title, for: .normal) | ||
view.isEnabled = state.isEnabled | ||
view.contentHorizontalAlignment = state.alignment | ||
view.setAction(for: .touchUpInside, action: state.action) | ||
} | ||
|
||
public static func ==(lhs: ButtonState, rhs: ButtonState) -> Bool { | ||
return lhs.title == rhs.title && lhs.isEnabled == rhs.isEnabled && lhs.alignment == rhs.alignment | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
FunctionalTableDataDemo/Cells/Combined/CombinedState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// CombinedState.swift | ||
// Shopify | ||
// | ||
// Created by Geoffrey Foster on 2017-01-18. | ||
// Copyright © 2017 Shopify. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import FunctionalTableData | ||
|
||
public typealias CombinedCell<View1: UIView, State1: Equatable, View2: UIView, State2: Equatable, Layout: TableItemLayout> = HostCell<CombinedView<View1, View2>, CombinedState<State1, State2>, Layout> | ||
|
||
public struct CombinedState<S1: Equatable, S2: Equatable>: Equatable { | ||
public let state1: S1 | ||
public let state2: S2 | ||
public init(state1: S1, state2: S2) { | ||
self.state1 = state1 | ||
self.state2 = state2 | ||
} | ||
|
||
public static func ==(lhs: CombinedState, rhs: CombinedState) -> Bool { | ||
return lhs.state1 == rhs.state1 && lhs.state2 == rhs.state2 | ||
} | ||
} | ||
|
||
extension CombinedState: Encodable where S1: Encodable, S2: Encodable { | ||
enum CodingKeys: CodingKey { | ||
case state1 | ||
case state2 | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(state1, forKey: .state1) | ||
try container.encode(state2, forKey: .state2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// CombinedView.swift | ||
// Shopify | ||
// | ||
// Created by Geoffrey Foster on 2017-01-18. | ||
// Copyright © 2017 Shopify. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import FunctionalTableData | ||
|
||
public class CombinedView<View1: UIView, View2: UIView>: UIView { | ||
public let view1 = View1() | ||
public let view2 = View2() | ||
public let stackView: UIStackView | ||
|
||
public override init(frame: CGRect) { | ||
stackView = UIStackView(frame: frame) | ||
super.init(frame: frame) | ||
stackView.addArrangedSubview(view1) | ||
stackView.addArrangedSubview(view2) | ||
|
||
stackView.translatesAutoresizingMaskIntoConstraints = false | ||
addSubview(stackView) | ||
|
||
NSLayoutConstraint.activate([ | ||
stackView.trailingAnchor.constraint(equalTo: trailingAnchor), | ||
stackView.leadingAnchor.constraint(equalTo: leadingAnchor), | ||
stackView.topAnchor.constraint(equalTo: topAnchor), | ||
stackView.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor) | ||
]) | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
} |
Oops, something went wrong.