Skip to content

Commit

Permalink
fix: image cells (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: Vittorio Cellucci <[email protected]>
  • Loading branch information
vcellu and vcellu authored Nov 9, 2022
1 parent 620cc25 commit c7acc3e
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 99 deletions.
15 changes: 15 additions & 0 deletions ios/ColumnResizerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ColumnResizerView: UIView {

fileprivate func createButton() {
let button = ResizerButtonView()
button.isUserInteractionEnabled = true
button.translatesAutoresizingMaskIntoConstraints = false
addSubview(button)
button.heightConstraint = button.heightAnchor.constraint(equalToConstant: TableTheme.CellContentHeight)
Expand Down Expand Up @@ -73,6 +74,17 @@ class ColumnResizerView: UIView {
}
}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
if view == button {
return view
}
if view == self {
return nil
}
return view
}

@objc func handleGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
Expand Down Expand Up @@ -113,6 +125,9 @@ class ColumnResizerView: UIView {
adjacentTable.layoutIfNeeded()
}
data.childCollectionView?.collectionViewLayout.invalidateLayout()

containerView?.multiColumnTable?.dataCollectionView?.childCollectionView?.showsVerticalScrollIndicator = false
containerView?.updateVScrollPos()
updateHeader(translation)
updateTotals(translation)
containerView?.testTruncation()
Expand Down
16 changes: 16 additions & 0 deletions ios/ContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,21 @@ class ContainerView: UIView {
layoutIfNeeded()
}
}

override func layoutSubviews() {
super.layoutSubviews()
updateVScrollPos()
}

func updateVScrollPos() {
let totalWidth = columnWidths.getTotalWidth()
let rawX = firstColumnTable?.horizontalScrolLView?.contentOffset.x ?? 0.0
// let scroll
let right = max(abs(self.frame.width - totalWidth) - rawX, 0)
multiColumnTable?.dataCollectionView?.childCollectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: right)
multiColumnTable?.dataCollectionView?.childCollectionView?.showsVerticalScrollIndicator = true
}



}
6 changes: 4 additions & 2 deletions ios/DataCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ class DataCellView: UICollectionViewCell, ExpandedCellProtocol {
}
} else if representation.type == "image" && !isDataView {
if let imageView = views[index] as? ImageCell {
let index = col.stylingInfo?.firstIndex(of: "imageUrl")
if let cellBackground = element.cellBackgroundColor {
imageView.backgroundColor = ColorParser.fromCSS(cssString: cellBackground)
} else {
imageView.backgroundColor = .clear
}
imageView.setData(data: element, representedAs: representation)
imageView.setData(data: element, representedAs: representation, index: index)
imageView.setNeedsDisplay()
}
} else {
Expand All @@ -146,7 +147,8 @@ class DataCellView: UICollectionViewCell, ExpandedCellProtocol {
if representation.type == "indicator", let indicator = element.indicator, let uniChar = DataCellView.iconMap[indicator.icon ?? "m"] {
label.setAttributedText(element.qText ?? "", withIcon: uniChar, element: element)
} else if representation.type == "url" {
label.setupUrl(col, cell: element)
let index = col.stylingInfo?.firstIndex(of: "url")
label.setupUrl(col, cell: element, index: index)
} else {
label.text = element.qText
label.textColor = getForgroundColor(col: col, element: element, withStyle: styleInfo[index])
Expand Down
2 changes: 1 addition & 1 deletion ios/DataCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
}
childCollectionView.translatesAutoresizingMaskIntoConstraints = false
childCollectionView.fitToView(self)
childCollectionView.showsVerticalScrollIndicator = false
childCollectionView.showsHorizontalScrollIndicator = false
childCollectionView.showsVerticalScrollIndicator = false
}

fileprivate func createSelectionBands() {
Expand Down
13 changes: 7 additions & 6 deletions ios/HeaderCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class HeaderCell: UIView {
button.topAnchor.constraint(equalTo: self.topAnchor),
button.bottomAnchor.constraint(equalTo: self.bottomAnchor),
button.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -TableTheme.DefaultResizerWidth / 2.0),
button.widthAnchor.constraint(equalToConstant: 30)
button.widthAnchor.constraint(equalToConstant: 20)
]
NSLayoutConstraint.activate(constraints)
addConstraints(constraints)
Expand Down Expand Up @@ -101,6 +101,7 @@ class HeaderCell: UIView {
paddedLabel.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: sizedFont)
paddedLabel.adjustsFontForContentSizeCategory = true
}


@objc func onPressedHeader(_ sender: UITapGestureRecognizer) {
setNeedsDisplay()
Expand Down Expand Up @@ -143,14 +144,14 @@ class HeaderCell: UIView {
currentBorder.removeFromSuperview()
}

func getLineCount() -> Int {
func getLineCount(_ width: Double) -> Int {
guard let paddedLabel = self.paddedLabel else { return 1 }
guard let dataColumn = dataColumn else { return 1 }
if !dataColumn.isDim {
return 1
var columnWidth = width
if searchButton != nil {
columnWidth -= 30
}

return paddedLabel.getLineCount(true)
return paddedLabel.getLineCount(true, columnWidth: columnWidth)
}

}
7 changes: 4 additions & 3 deletions ios/HeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class HeaderView: HeaderStyleView {
}

func setupConstraints(_ label: HeaderCell, width: Double, prev: HeaderCell?, index: Int) {
let isLast = index == dataRange.count - 1
label.translatesAutoresizingMaskIntoConstraints = false
label.dynamicWidth = label.widthAnchor.constraint(equalToConstant: width)
var constraints = [NSLayoutConstraint]()
Expand Down Expand Up @@ -140,10 +139,12 @@ class HeaderView: HeaderStyleView {
}

func getMaxLineCount() -> Int {
guard let columnWidths = columnWidths else { return 0 }
var lineCount = 1
for view in subviews {
subviews.enumerated().forEach{(index, view) in
if let headerCell = view as? HeaderCell {
lineCount = max(headerCell.getLineCount(), lineCount)
let width = columnWidths.columnWidths[index + dataRange.lowerBound]
lineCount = max(headerCell.getLineCount(width), lineCount)
}
}
return lineCount
Expand Down
44 changes: 31 additions & 13 deletions ios/HorizontalScrolViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,51 @@
import Foundation
class HorizontalScrollViewDelegate: NSObject, UIScrollViewDelegate {
weak var tableView: TableView?
weak var multiColumnTable: TableView?
weak var totalsView: UIView?
weak var headersView: UIView?
weak var columnWidths: ColumnWidths?
weak var grabber: ColumnResizerView?
var translateion = CGPoint.zero
var width = 0.0
var freezeFirstCol = false

func captureFirstColumnWidth() {
guard let columnWidths = self.columnWidths else { return }
width = columnWidths.columnWidths[0]
}


func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let tableView = self.tableView else { return }
guard let columnWidths = self.columnWidths else { return }
guard let tableView = self.tableView else { return }
guard let grabber = self.grabber else { return }

let totalWidth = columnWidths.getTotalWidth()
let shadowOffsetX = clampScrollPos(Float(scrollView.contentOffset.x))
let rawX = scrollView.contentOffset.x

if rawX <= 0 {
let x = width - rawX
let translation = CGPoint(x: x, y: 0)
columnWidths.columnWidths[0] = x
tableView.setWidth(x)
_ = tableView.dataCollectionView?.updateSize(translation, withColumn: 0)
tableView.dataCollectionView?.childCollectionView?.collectionViewLayout.invalidateLayout()
grabber.setPosition(x)
let right = max(abs(scrollView.frame.width - totalWidth) - rawX, 0)



if(rawX >= 0) {
multiColumnTable?.dataCollectionView?.childCollectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: right)
}
if (freezeFirstCol) {
if rawX <= 0 {
multiColumnTable?.dataCollectionView?.childCollectionView?.showsVerticalScrollIndicator = false
let x = width - rawX
let translation = CGPoint(x: x, y: 0)
columnWidths.columnWidths[0] = x
tableView.setWidth(x)
_ = tableView.dataCollectionView?.updateSize(translation, withColumn: 0)
tableView.dataCollectionView?.childCollectionView?.collectionViewLayout.invalidateLayout()
grabber.setPosition(x)
}

let offset = shadowOffsetX/100.0
updateShadow(offset: offset)
}

let offset = shadowOffsetX/100.0
updateShadow(offset: offset)

}

Expand All @@ -50,4 +64,8 @@ class HorizontalScrollViewDelegate: NSObject, UIScrollViewDelegate {
tableView?.setShadow(offset: min(offset, 0.4))
tableView?.setNeedsDisplay()
}

func updateVScroll() {

}
}
67 changes: 42 additions & 25 deletions ios/ImageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class ImageCell: UIView, ConstraintCellProtocol {
return 1
}

func setData(data: DataCell, representedAs rep: Representation) {
func setData(data: DataCell, representedAs rep: Representation, index: Int?) {
self.representation = rep
guard let qAttrExps = data.qAttrExps else {return}
guard let qValues = qAttrExps.qValues else {return}
guard let attrIndex = index else { return }
if qValues.count > 0 {
guard let urlString = qValues[0].qText else {return}
guard let urlString = qValues[attrIndex].qText else {return}
guard let url = URL(string: urlString) else {return}
DispatchQueue.global(qos: .background).async {
do {
Expand Down Expand Up @@ -80,37 +81,49 @@ class ImageCell: UIView, ConstraintCellProtocol {
let aspectRatio = image.size.width/image.size.height
let height = self.frame.height
let width = height * aspectRatio
var leadingAnchor = rep.imagePosition == "topCenter" ?
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor)

if(rep.imagePosition == "centerCenter") {
leadingAnchor = imageView.centerXAnchor.constraint(equalTo: self.centerXAnchor)
if rep.imagePosition == "centerCenter" {
imageView.fitToView(self)
} else {
var leadingAnchor = rep.imagePosition == "topCenter" ?
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor)


let constraints = [
leadingAnchor,
imageView.heightAnchor.constraint(equalToConstant: height),
imageView.widthAnchor.constraint(equalToConstant: width)
]
NSLayoutConstraint.activate(constraints)
addConstraints(constraints)
}
let constraints = [
leadingAnchor,
imageView.heightAnchor.constraint(equalToConstant: height),
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: width)
]
NSLayoutConstraint.activate(constraints)
addConstraints(constraints)
}
} else if rep.imageSize == "alwaysFit" {
if rep.imagePosition == "centerCenter" {
imageView.fitToView(self)
} else if let image = imageView.image {
let aspectRatio = image.size.width/image.size.height
let height = self.frame.height
let width = height * aspectRatio
var leadingAnchor = rep.imagePosition == "topCenter" ?
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
let aspectRatio = (image.size.height/image.size.width)
let maxWidth = self.frame.height / aspectRatio
let width = imageView.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth)
let leading = rep.imagePosition == "topCenter" ? imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.leadingAnchor.constraint(greaterThanOrEqualTo: self.trailingAnchor, constant: -maxWidth)


let constraints = [
leadingAnchor,
imageView.heightAnchor.constraint(equalToConstant: height),
imageView.widthAnchor.constraint(equalToConstant: width)
var constraints = [
leading,
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
imageView.topAnchor.constraint(equalTo: self.topAnchor),
imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
width,
]
// need to bind to the leading of imageView and keep trailing lower priority
if rep.imagePosition == "bottomCenter" {
let hardLeading = imageView.leadingAnchor.constraint(greaterThanOrEqualTo: self.leadingAnchor)
leading.priority = UILayoutPriority(999)
constraints.append(hardLeading)
}

NSLayoutConstraint.activate(constraints)
addConstraints(constraints)

Expand All @@ -136,10 +149,14 @@ class ImageCell: UIView, ConstraintCellProtocol {
}
imageView.clipsToBounds = true
}
updateConstraintsIfNeeded()
self.setNeedsLayout()
imageView.setNeedsLayout()
DispatchQueue.main.async {
self.updateContentScaleFactorIfNeeded()
}
}


func updateContentScaleFactorIfNeeded() {
guard let imageView = self.imageView else { return }
guard let image = imageView.image else { return }
Expand Down
1 change: 1 addition & 0 deletions ios/MulitColumnResizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MultiColumnResizer: ColumnResizerView {
tableView.layoutIfNeeded()
data.childCollectionView?.collectionViewLayout.invalidateLayout()

containerView?.updateVScrollPos()
updateHeader(translation)
updateTotals(translation)
updateAdjacent(by: translation.x)
Expand Down
Loading

0 comments on commit c7acc3e

Please sign in to comment.