Skip to content

Commit

Permalink
feat: freeze first column - iOS (#87)
Browse files Browse the repository at this point in the history
* feat: freeze first column ios

* fix: add total resize

* fix: out of bounds

Co-authored-by: Vittorio Cellucci <[email protected]>
  • Loading branch information
2 people authored and enell committed Feb 9, 2023
1 parent ebcede7 commit bcce6f4
Show file tree
Hide file tree
Showing 27 changed files with 1,162 additions and 625 deletions.
3 changes: 3 additions & 0 deletions ios/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
disabled_rules:
- line_length
- identifier_name
- function_body_length
- file_length
- type_body_length
4 changes: 2 additions & 2 deletions ios/ColorParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ColorParser {
"teal": .systemTeal]

func fromCSS(cssString: String) -> UIColor {
if(cssString == "none") {
if cssString == "none" {
return UIColor.clear
}
let hex = cssString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
Expand All @@ -47,7 +47,7 @@ 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: "")
if(hex.hasPrefix("rgba")) {
if hex.hasPrefix("rgba") {
rgbString = hex.replacingOccurrences(of: "rgba(", with: "")
} else {
rgbString = hex.replacingOccurrences(of: "rgb(", with: "")
Expand Down
45 changes: 26 additions & 19 deletions ios/ColumnWidths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,68 @@ import Foundation
import Combine

class ColumnWidths {
var columnWidths = [Double]();
var columnWidths = [Double]()
var key: String?

func resetColumnWidths(widths: [Double]) {
columnWidths = widths
}


func count() -> Int {
return columnWidths.count
}

func loadDefaultWidths(_ frame: CGRect, columnCount: Int, dataRows: [DataRow]) {
if(!loadFromStorage(columnCount)) {
if !loadFromStorage(columnCount) {
let defaultWidth = frame.width / Double(columnCount)
let widths = [Double](repeating: defaultWidth, count: columnCount)
resetColumnWidths(widths: widths)
calculateDefaultColWidth(dataRows: dataRows, defaultWidth: defaultWidth, columnCount: columnCount, frame: frame)
}
}

fileprivate func loadFromStorage(_ columnCount: Int) -> Bool {
let storageKey = getStorageKey()
let defaults = UserDefaults.standard
if let data = defaults.array(forKey: storageKey) as? [Double] {
if(data.count != columnCount) {
if data.count != columnCount {
return false
}
resetColumnWidths(widths: data)
return true
}
return false
}

func saveToStorage() {
let storageKey = getStorageKey()
let defaults = UserDefaults.standard
defaults.set(columnWidths, forKey: storageKey)
}

fileprivate func getStorageKey() -> String {
guard let key = key else {return ""}
let prefix = UIDevice.current.orientation.isLandscape ? "landscape." : "portrait.";
let prefix = UIDevice.current.orientation.isLandscape ? "landscape." : "portrait."
let storageKey = prefix + key
return storageKey
}

func calculateDefaultColWidth(dataRows: [DataRow], defaultWidth: Double, columnCount: Int, frame: CGRect) {
// get max width
var widths = [Double](repeating: defaultWidth, count: columnCount)
var totalWidth = 0.0
columnWidths.enumerated().forEach{ (index, value) in
columnWidths.enumerated().forEach { (index, _) in
let maxWidth = getMaxWidthFrom(dataRows: dataRows, index: index)
widths[index] = maxWidth
totalWidth += maxWidth
}
if(totalWidth > frame.width){
if totalWidth > frame.width {
resetColumnWidths(widths: widths)
}

}
fileprivate func getMaxWidthFrom(dataRows : [DataRow], index: Int) -> Double {

fileprivate func getMaxWidthFrom(dataRows: [DataRow], index: Int) -> Double {
var maxWidth = 0.0
for row in dataRows {
let fontAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0)]
Expand All @@ -76,17 +80,20 @@ class ColumnWidths {
}
return maxWidth
}

func getTotalWidth() -> Double {
return columnWidths.reduce(0, { $0 + $1 })
}


func getTotalWidth(range: CountableRange<Int>) -> Double {
return columnWidths[range].reduce(0, { $0 + $1 })
}

func resize(index: Int, by: CGPoint) {
columnWidths[index] += by.x
if index + 1 < columnWidths.count {
columnWidths[index + 1] -= by.x
}
}



}
Loading

0 comments on commit bcce6f4

Please sign in to comment.