Skip to content

Commit

Permalink
③ [Currency selection] Add table view header (#570)
Browse files Browse the repository at this point in the history
* Add SelectCurrencyViewController

* Fix tests

* Add tableview header

* Update snapshots

* Added ViewController tests

* Removed recordMode line from test

* Simplify layout code for header

* Remove SettingsCurrencyCell

* Fix snapshot tests

* Remove CaseIterable conformance on SettingsAccountCellType

* Use reuseIdentifier local constant

* Update pragma

* Update tableview instantiation

* Implicit returns in lazy vars

* Fix alignment

* ① [Currency selection] Remove currency picker (#568)

* Remove currency picker

* swiftlint

* Remove redundant enum case

* Remove reference

* Remove local var, improve function name

* Set label colour

* Update snapshots

* Fix project file

* Correctly size header/footer views

* Make the tests pass (#585)

* Run snapshot tests on all devices

* Remove duplicate extension, use keypath setter

* Fix autolayout warnings
  • Loading branch information
justinswart authored Feb 19, 2019
1 parent 4ca636e commit 2c0552b
Show file tree
Hide file tree
Showing 34 changed files with 207 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "icon--currency-header.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,3 @@ extension PaymentMethodsViewController: AddNewCardViewControllerDelegate {
}
}
}

private extension UIView {
func constrainEdges(to view: UIView) {
self.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
self.leadingAnchor.constraint(equalTo: view.leadingAnchor),
self.trailingAnchor.constraint(equalTo: view.trailingAnchor),
self.topAnchor.constraint(equalTo: view.topAnchor),
self.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}

private extension UITableView {
func ksr_sizeHeaderFooterViewsToFit() {
let keyPaths: [ReferenceWritableKeyPath<UITableView, UIView?>] = [
(\.tableHeaderView),
(\.tableFooterView)
]

keyPaths.forEach { keyPath in
if let view = self[keyPath: keyPath] {
let size = view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

if view.frame.height != size.height {
view.frame.size.height = size.height

self[keyPath: keyPath] = view
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,55 @@ final class SelectCurrencyViewController: UIViewController, MessageBannerViewCon
self.view.addSubview(self.tableView)
self.tableView.constrainEdges(to: self.view)

let headerContainerView = UIView(frame: .zero)
headerContainerView.addSubview(self.headerView)
self.headerView.constrainEdges(to: headerContainerView, priority: .defaultHigh)

self.tableView.tableHeaderView = headerContainerView
self.headerView.widthAnchor.constraint(equalTo: self.tableView.widthAnchor).isActive = true

self.viewModel.inputs.viewDidLoad()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

self.tableView.ksr_sizeHeaderFooterViewsToFit()
}

override func bindStyles() {
super.bindStyles()

_ = self.tableView
|> settingsTableViewStyle
|> \.separatorStyle .~ .singleLine

_ = self.headerView
|> \.text %~ { _ in
"""
\(Strings.Making_this_change())\n
\(Strings.A_successfully_funded_project_will_collect_your_pledge_in_its_native_currency())
"""
}
}

// MARK: - Subviews

private lazy var tableView: UITableView = {
return UITableView(frame: .zero, style: .plain)
UITableView(frame: .zero, style: .plain)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
|> \.tableFooterView .~ UIView(frame: .zero)
|> \.dataSource .~ self
|> \.delegate .~ self
}()

private lazy var headerView: SelectCurrencyTableViewHeader = {
SelectCurrencyTableViewHeader(frame: .zero)
}()
}

extension SelectCurrencyViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Currency.allCases.count
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Library
import Prelude
import Result
import XCTest
@testable import Kickstarter_Framework
@testable import KsApi

internal final class SelectCurrencyViewControllerTests: TestCase {
override func setUp() {
super.setUp()
UIView.setAnimationsEnabled(false)
}

override func tearDown() {
UIView.setAnimationsEnabled(true)
super.tearDown()
}

func testView() {
combos(Language.allLanguages, Device.allCases)
.forEach { language, device in
withEnvironment(language: language) {
let vc = SelectCurrencyViewController.instantiate()
vc.configure(with: .USD)
let (parent, _) = traitControllers(device: device, orientation: .portrait, child: vc)

FBSnapshotVerifyView(parent.view, identifier: "lang_\(language)_device_\(device)")
}
}
}
}
62 changes: 62 additions & 0 deletions Kickstarter-iOS/Views/SelectCurrencyTableViewHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Library
import Prelude
import Prelude_UIKit
import UIKit

final class SelectCurrencyTableViewHeader: UIView {

override init(frame: CGRect) {
super.init(frame: frame)

self.addSubview(self.headerStackView)
self.headerStackView.constrainEdges(to: self)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func bindStyles() {
super.bindStyles()

_ = self.headerImageView
|> \.contentMode .~ .scaleAspectFill

_ = self.headerStackView
|> \.axis .~ .vertical
|> \.alignment .~ .center
|> \.spacing .~ Styles.grid(2)
|> \.layoutMargins .~ .init(
top: Styles.grid(4), left: Styles.grid(2), bottom: Styles.grid(2), right: Styles.grid(2)
)
|> \.isLayoutMarginsRelativeArrangement .~ true

_ = self.headerLabel
|> settingsDescriptionLabelStyle
|> \.textColor .~ .ksr_text_dark_grey_500
|> \.backgroundColor .~ .ksr_grey_200
}

// MARK: Accessors

public var text: String? {
didSet {
_ = self.headerLabel |> \.text .~ text
}
}

// MARK: Subviews

private lazy var headerStackView: UIStackView = {
return UIStackView(arrangedSubviews: [
self.headerImageView,
self.headerLabel
])
}()

private lazy var headerImageView: UIImageView = {
UIImageView(image: image(named: "icon--currency-header", inBundle: Bundle.framework))
}()

private lazy var headerLabel: UILabel = { UILabel(frame: .zero) }()
}
Loading

0 comments on commit 2c0552b

Please sign in to comment.