Skip to content

Commit

Permalink
feat: conditional coloring ioS
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Aug 4, 2022
1 parent 693692b commit 0892713
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 37 deletions.
8 changes: 6 additions & 2 deletions ios/ColorParser.swift
Expand Up @@ -47,8 +47,12 @@ class ColorParser {
return UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
} else if hex.hasPrefix("rgb") {
var rgbString = hex.replacingOccurrences(of: "rgb(", with: "")
rgbString = hex.replacingOccurrences(of: "rgba(", with: "")
rgbString = hex.replacingOccurrences(of: ")", with: "")
if(hex.hasPrefix("rgba")) {
rgbString = hex.replacingOccurrences(of: "rgba(", with: "")
} else {
rgbString = hex.replacingOccurrences(of: "rgb(", with: "")
}
rgbString = rgbString.replacingOccurrences(of: ")", with: "")
let split = rgbString.split(separator: ",")
var values: [Double] = [Double]()
for s in split {
Expand Down
69 changes: 51 additions & 18 deletions ios/DataCellView.swift
Expand Up @@ -7,28 +7,41 @@

import Foundation
import UIKit

func isDarkColor(color: UIColor) -> Bool {
if color == .clear {
return false
}
var r, g, b, a: CGFloat
(r, g, b, a) = (0, 0, 0, 0)
color.getRed(&r, green: &g, blue: &b, alpha: &a)
let lum = 0.2126 * r + 0.7152 * g + 0.0722 * b
return lum < 0.50
}

class DataCellView: UICollectionViewCell {
var border = UIBezierPath()
var dataRow: DataRow?
var borderColor = UIColor.black.withAlphaComponent(0.1)
var selectionsEngine: SelectionsEngine?
var cellColor: UIColor?
var numberOfLines = 1;

static let minWidth: CGFloat = 40

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

required init?(coder: NSCoder) {
super.init(coder: coder)
}

func setData(row: DataRow, withColumns cols: [DataColumn], theme: TableTheme, selectionsEngine: SelectionsEngine) {
func setData(row: DataRow, withColumns cols: [DataColumn], theme: TableTheme, selectionsEngine: SelectionsEngine, withStyle styleInfo: StylingInfo) {
dataRow = row
borderColor = ColorParser().fromCSS(cssString: theme.borderBackgroundColor ?? "#F0F0F0")
createCells(row: row, withColumns: cols)

var x = 0
let views = contentView.subviews
row.cells.enumerated().forEach {(index, element) in
Expand All @@ -52,7 +65,9 @@ class DataCellView: UICollectionViewCell {
label.column = index
label.cell = element
label.checkSelected(selectionsEngine)
label.textColor = cellColor!
let backgroundColor = getBackgroundColor(col: col, element: element, withStyle: styleInfo)
label.backgroundColor = backgroundColor
label.textColor = isDarkColor(color: backgroundColor) ? .white : getForgroundColor(col: col, element: element, withStyle: styleInfo)
label.numberOfLines = numberOfLines
}
}
Expand All @@ -61,14 +76,32 @@ class DataCellView: UICollectionViewCell {

}
}


fileprivate func getBackgroundColor(col: DataColumn, element: DataCell, withStyle styleInfo: StylingInfo) -> UIColor {
guard let attributes = element.qAttrExps else {return .clear}
guard let values = attributes.qValues else {return .clear}
let colorString = values[styleInfo.backgroundColorIdx].qText ?? "none"
let colorValue = ColorParser().fromCSS(cssString: colorString.lowercased())
return colorValue
}

fileprivate func getForgroundColor(col: DataColumn, element: DataCell, withStyle styleInfo: StylingInfo) -> UIColor {
guard let attributes = element.qAttrExps else {return cellColor!}
guard let values = attributes.qValues else {return cellColor!}
if let qText = values[styleInfo.foregroundColorIdx].qText {
let colorValue = ColorParser().fromCSS(cssString: qText.lowercased())
return colorValue
}
return cellColor!
}

fileprivate func createCells(row: DataRow, withColumns cols: [DataColumn]) {
let views = contentView.subviews
if views.count < row.cells.count {
var x = 0
clearAllCells()
for col in cols {

if let representation = col.representation {
if(representation.type == "miniChart") {
let miniChartView = MiniChartView(frame: .zero)
Expand All @@ -78,7 +111,7 @@ class DataCellView: UICollectionViewCell {
let label = PaddedLabel(frame: .zero)
if col.isDim == true {
if let selectionsEngine = selectionsEngine {
label.makeSelectable(selectionsEngine: selectionsEngine)
label.makeSelectable(selectionsEngine: selectionsEngine)
}
}
let sizedFont = UIFont.systemFont(ofSize: 14)
Expand All @@ -92,13 +125,13 @@ class DataCellView: UICollectionViewCell {
}
}
}

fileprivate func clearAllCells() {
for view in contentView.subviews {
view.removeFromSuperview()
}
}

func updateSize(_ translation: CGPoint, forColumn index: Int) -> Bool {
let view = contentView.subviews[index]
let newWidth = view.frame.width + translation.x
Expand All @@ -117,32 +150,32 @@ class DataCellView: UICollectionViewCell {
v.frame = new
v.setNeedsDisplay()
}

resizeContentView(view: view, width: newWidth)

return true
}

fileprivate func resizeContentView(view: UIView, width: CGFloat) {
if view.frame.width != width {
let oldFrame = view.frame
let newFrame = CGRect(x: oldFrame.origin.x, y: 0, width: width, height: oldFrame.height)
view.frame = newFrame
view.setNeedsDisplay()
}

}

override func draw(_ rect: CGRect) {
super.draw(rect)

border.move(to: CGPoint(x: 0, y: rect.height))
border.addLine(to: CGPoint(x: rect.width, y: rect.height))
border.close()

border.lineWidth = 1
borderColor.set()
border.stroke()

}
}
27 changes: 26 additions & 1 deletion ios/DataCollectionView.swift
Expand Up @@ -12,6 +12,7 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
case noCellForIdentifier
}

var stylingInfo = StylingInfo()
var dataColumns: [DataColumn]?
var dataRows: [DataRow]?
var dataSize: DataSize?
Expand Down Expand Up @@ -97,6 +98,7 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
fileprivate func setData(columns: [DataColumn], withRows rows: [DataRow]) {
dataColumns = columns
dataRows = rows
setupDataCols()
let flowLayout = UICollectionViewFlowLayout()
let uiCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
uiCollectionView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -118,6 +120,26 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
signalVisibleRows()

}

fileprivate func setupDataCols() {
guard let dataColumns = dataColumns else {
return
}

for col in dataColumns {
if let stylingInfo = col.stylingInfo {
var index = 0
for style in stylingInfo {
if( style == "cellBackgroundColor") {
self.stylingInfo.backgroundColorIdx = index;
} else if (style == "cellForegroundColor") {
self.stylingInfo.foregroundColorIdx = index
}
index += 1
}
}
}
}

func appendData(rows: [DataRow]) {
DispatchQueue.main.async {
Expand All @@ -137,7 +159,10 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
if let data = dataRows {
let dataRow = data[indexPath.row]
cell.selectionsEngine = self.selectionsEngine
cell.setData(row: dataRow, withColumns: dataColumns!, theme: tableTheme!, selectionsEngine: selectionsEngine!)
cell.setData(row: dataRow, withColumns: dataColumns!,
theme: tableTheme!,
selectionsEngine: selectionsEngine!,
withStyle: self.stylingInfo)
}
return cell
}
Expand Down
3 changes: 1 addition & 2 deletions ios/DataColumn.swift
Expand Up @@ -51,8 +51,7 @@ struct DataColumn: Codable {
let sortDirection: String?
let dataColIdx: Double?
let representation: Representation?
let max: Double?
let min: Double?
let stylingInfo: [String]?
}

struct TotalsCell: Decodable {
Expand Down
55 changes: 41 additions & 14 deletions ios/DataRow.swift
Expand Up @@ -6,8 +6,28 @@
//

import Foundation

struct StylingInfo {
var backgroundColorIdx = 0
var foregroundColorIdx = 0
}

struct MatrixCell: Decodable {
let qNum: Double?
let qText: String?
enum CodingKeys: String, CodingKey {
case qNum, qText
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.qText = try container.decodeIfPresent(String.self, forKey: .qText) ?? nil
if let temp = try? container.decode(Double.self, forKey: .qNum) {
self.qNum = temp
} else {
self.qNum = nil
}
}
}

struct Matrix: Decodable {
Expand All @@ -16,6 +36,10 @@ struct Matrix: Decodable {
let qMin: Double?
}

struct AttriExpr: Decodable {
let qValues: [MatrixCell]?
}

struct DataCell: Decodable {
var qText: String?
var qNum: Double?
Expand All @@ -27,20 +51,22 @@ struct DataCell: Decodable {
var rawRowIdx: Double?
var rawColIdx: Double?
var qMiniChart: Matrix?
var qAttrExps: AttriExpr?

enum CodingKeys: String, CodingKey {
case qText
case qNum
case qElemNumber
case qState
case rowIdx
case colIdx
case isDim
case rawRowIdx
case rawColIdx
case qMiniChart
case qText
case qNum
case qElemNumber
case qState
case rowIdx
case colIdx
case isDim
case rawRowIdx
case rawColIdx
case qMiniChart
case qAttrExps
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.qText = try container.decodeIfPresent(String.self, forKey: .qText) ?? ""
Expand All @@ -52,14 +78,15 @@ struct DataCell: Decodable {
self.rawColIdx = try container.decodeIfPresent(Double.self, forKey: .rawColIdx) ?? -1
self.isDim = try container.decodeIfPresent(Bool.self, forKey: .isDim) ?? false
self.qMiniChart = try container.decodeIfPresent(Matrix.self, forKey: .qMiniChart) ?? nil

self.qAttrExps = try container.decodeIfPresent(AttriExpr.self, forKey: .qAttrExps) ?? nil

if let temp = try? container.decode(Double.self, forKey: .qNum) {
self.qNum = temp
} else {
self.qNum = nil
}
}

}

struct DataRow: Decodable {
Expand All @@ -77,7 +104,7 @@ struct DataRow: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DynamicCodingKeys.self)
var tempArray = [DataCell]()

for key in container.allKeys where key.stringValue != "key" {
let decodedCell = try container.decode(DataCell.self, forKey: DynamicCodingKeys(stringValue: key.stringValue)!)
tempArray.append(decodedCell)
Expand Down

0 comments on commit 0892713

Please sign in to comment.