Files
GitHawk/Classes/Issues/Comments/Tables/IssueCommentTableCell.swift
Ryan Nystrom 62e2763a85 Refactor markdown parsing to use text lib (#1674)
* milestones refactored with styled text

* reference refactored

* refactor commit references

* fix styled text bug

* request event refactored

* refactor titles

* warm caches for bg-gen models

* delete old text cell

* move shortlinks into MMMarkdown

* almost there

* build green

* running and trimming whitespaces

* styled text working

* fix tests

* tests passing
2018-03-31 10:12:51 -04:00

112 lines
3.5 KiB
Swift

//
// IssueCommentTableCell.swift
// Freetime
//
// Created by Ryan Nystrom on 7/6/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
import IGListKit
final class IssueCommentTableCell: IssueCommentBaseCell,
ListBindable,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout {
static let inset = UIEdgeInsets(
top: 0,
left: Styles.Sizes.rowSpacing / 2,
bottom: Styles.Sizes.rowSpacing,
right: Styles.Sizes.rowSpacing / 2
)
weak var delegate: MarkdownStyledTextViewDelegate?
private let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = .zero
return UICollectionView(frame: .zero, collectionViewLayout: layout)
}()
private let identifier = "cell"
private var model: IssueCommentTableModel?
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.register(IssueCommentTableCollectionCell.self, forCellWithReuseIdentifier: identifier)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = .white
collectionView.isPrefetchingEnabled = false
collectionView.contentInset = IssueCommentTableCell.inset
contentView.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = contentView.bounds
}
// MARK: ListBindable
func bindViewModel(_ viewModel: Any) {
guard let viewModel = viewModel as? IssueCommentTableModel else { return }
self.model = viewModel
collectionView.reloadData()
}
// MARK: UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return model?.columns.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model?.columns[section].rows.count ?? 0
}
func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
guard let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? IssueCommentTableCollectionCell,
let columns = model?.columns
else { fatalError("Cell is wrong type or missing model/item") }
let column = columns[indexPath.section]
let rows = column.rows
let row = rows[indexPath.item]
cell.configure(row.string)
cell.textView.tapDelegate = delegate
cell.contentView.backgroundColor = row.fill ? Styles.Colors.Gray.lighter.color : .white
cell.setRightBorder(visible: columns.last === column)
cell.setBottomBorder(visible: rows.last === row)
return cell
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
guard let model = model else { fatalError("Missing model") }
return CGSize(
width: model.columns[indexPath.section].width,
height: model.rowHeights[indexPath.item]
)
}
}