|
| 1 | +// |
| 2 | +// InfoListViewController.swift |
| 3 | +// Authenticator |
| 4 | +// |
| 5 | +// Copyright (c) 2017 Authenticator authors |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +import UIKit |
| 27 | + |
| 28 | +final class InfoListViewController: UITableViewController { |
| 29 | + private var viewModel: InfoList.ViewModel |
| 30 | + private let dispatchAction: (InfoList.Effect) -> Void |
| 31 | + |
| 32 | + // MARK: Initialization |
| 33 | + |
| 34 | + init(viewModel: InfoList.ViewModel, dispatchAction: @escaping (InfoList.Effect) -> Void) { |
| 35 | + self.viewModel = viewModel |
| 36 | + self.dispatchAction = dispatchAction |
| 37 | + super.init(nibName: nil, bundle: nil) |
| 38 | + } |
| 39 | + |
| 40 | + required init?(coder aDecoder: NSCoder) { |
| 41 | + fatalError("init(coder:) has not been implemented") |
| 42 | + } |
| 43 | + |
| 44 | + func updateWithViewModel(_ viewModel: InfoList.ViewModel) { |
| 45 | + self.viewModel = viewModel |
| 46 | + applyViewModel() |
| 47 | + } |
| 48 | + |
| 49 | + private func applyViewModel() { |
| 50 | + title = viewModel.title |
| 51 | + } |
| 52 | + |
| 53 | + // MARK: View Lifecycle |
| 54 | + |
| 55 | + override func viewDidLoad() { |
| 56 | + super.viewDidLoad() |
| 57 | + |
| 58 | + tableView.backgroundColor = UIColor.otpBackgroundColor |
| 59 | + tableView.separatorStyle = .none |
| 60 | + tableView.indicatorStyle = .white |
| 61 | + self.tableView.rowHeight = UITableViewAutomaticDimension |
| 62 | + self.tableView.estimatedRowHeight = 44.0 |
| 63 | + |
| 64 | + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, |
| 65 | + target: self, |
| 66 | + action: #selector(done)) |
| 67 | + |
| 68 | + applyViewModel() |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: Target Actions |
| 72 | + |
| 73 | + func done() { |
| 74 | + dispatchAction(.done) |
| 75 | + } |
| 76 | + |
| 77 | + // MARK: UITableViewDataSource |
| 78 | + |
| 79 | + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 80 | + return viewModel.rowModels.count |
| 81 | + } |
| 82 | + |
| 83 | + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 84 | + let cell = tableView.dequeueReusableCellWithClass(InfoListCell.self) |
| 85 | + updateCell(cell, forRowAtIndexPath: indexPath) |
| 86 | + return cell |
| 87 | + } |
| 88 | + |
| 89 | + fileprivate func updateCell(_ cell: InfoListCell, forRowAtIndexPath indexPath: IndexPath) { |
| 90 | + let rowModel = viewModel.rowModels[indexPath.row] |
| 91 | + cell.updateWithRowModel(rowModel) |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: UITableViewDelegate |
| 95 | + |
| 96 | + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 97 | + let rowModel = viewModel.rowModels[indexPath.row] |
| 98 | + dispatchAction(rowModel.action) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class InfoListCell: UITableViewCell { |
| 103 | + private static let titleFont = UIFont.systemFont(ofSize: 18, weight: UIFontWeightMedium) |
| 104 | + private static let descriptionFont = UIFont.systemFont(ofSize: 15, weight: UIFontWeightLight) |
| 105 | + private static let callToActionFont = UIFont.systemFont(ofSize: 15, weight: UIFontWeightSemibold) |
| 106 | + private static let paragraphStyle: NSParagraphStyle = { |
| 107 | + let paragraphStyle = NSMutableParagraphStyle() |
| 108 | + paragraphStyle.lineHeightMultiple = 1.3 |
| 109 | + return paragraphStyle |
| 110 | + }() |
| 111 | + |
| 112 | + let titleLabel = UILabel() |
| 113 | + let descriptionLabel = UILabel() |
| 114 | + var customConstraints: [NSLayoutConstraint]? |
| 115 | + |
| 116 | + // MARK: Initialization |
| 117 | + |
| 118 | + override init(style: UITableViewCellStyle, reuseIdentifier: String?) { |
| 119 | + super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) |
| 120 | + configureCell() |
| 121 | + } |
| 122 | + |
| 123 | + required init?(coder aDecoder: NSCoder) { |
| 124 | + super.init(coder: aDecoder) |
| 125 | + configureCell() |
| 126 | + } |
| 127 | + |
| 128 | + private func configureCell() { |
| 129 | + backgroundColor = .otpBackgroundColor |
| 130 | + |
| 131 | + titleLabel.textColor = .otpForegroundColor |
| 132 | + titleLabel.font = InfoListCell.titleFont |
| 133 | + titleLabel.translatesAutoresizingMaskIntoConstraints = false |
| 134 | + contentView.addSubview(titleLabel) |
| 135 | + |
| 136 | + descriptionLabel.textColor = .otpForegroundColor |
| 137 | + descriptionLabel.numberOfLines = 0 |
| 138 | + descriptionLabel.font = InfoListCell.descriptionFont |
| 139 | + descriptionLabel.translatesAutoresizingMaskIntoConstraints = false |
| 140 | + contentView.addSubview(descriptionLabel) |
| 141 | + |
| 142 | + selectedBackgroundView = UIView() |
| 143 | + selectedBackgroundView?.backgroundColor = UIColor(white: 0, alpha: 0.25) |
| 144 | + |
| 145 | + setNeedsUpdateConstraints() |
| 146 | + } |
| 147 | + |
| 148 | + override func updateConstraints() { |
| 149 | + if customConstraints == nil { |
| 150 | + let newConstraints = [ |
| 151 | + titleLabel.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), |
| 152 | + titleLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), |
| 153 | + titleLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), |
| 154 | + titleLabel.bottomAnchor.constraint(equalTo: descriptionLabel.topAnchor), |
| 155 | + descriptionLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), |
| 156 | + descriptionLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), |
| 157 | + descriptionLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), |
| 158 | + ] |
| 159 | + contentView.addConstraints(newConstraints) |
| 160 | + customConstraints = newConstraints |
| 161 | + } |
| 162 | + |
| 163 | + // "Important: Call [super updateConstraints] as the final step in your implementation." |
| 164 | + super.updateConstraints() |
| 165 | + } |
| 166 | + |
| 167 | + // MARK: Update |
| 168 | + |
| 169 | + func updateWithRowModel(_ rowModel: InfoList.RowModel) { |
| 170 | + titleLabel.text = rowModel.title |
| 171 | + |
| 172 | + let attributes = [NSParagraphStyleAttributeName: InfoListCell.paragraphStyle] |
| 173 | + let attributedDetails = NSMutableAttributedString(string: rowModel.description + " " + rowModel.callToAction, |
| 174 | + attributes: attributes) |
| 175 | + attributedDetails.addAttribute(NSFontAttributeName, value: InfoListCell.callToActionFont, |
| 176 | + range: (attributedDetails.string as NSString).range(of: rowModel.callToAction)) |
| 177 | + descriptionLabel.attributedText = attributedDetails |
| 178 | + } |
| 179 | +} |
0 commit comments