Skip to content

Commit

Permalink
Refactors swift data table includiung a typealias for the content arr…
Browse files Browse the repository at this point in the history
…ay, adds string representation method for swift data table value type enum
  • Loading branch information
pavankataria committed Mar 13, 2017
1 parent 8176ba6 commit b454c08
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 221 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PODS:
- Nimble
- Quick
- Quick (1.0.0)
- SwiftDataTables (0.2.6)
- SwiftDataTables (0.2.7)

DEPENDENCIES:
- FBSnapshotTestCase
Expand All @@ -30,7 +30,7 @@ SPEC CHECKSUMS:
Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29
Nimble-Snapshots: e743439f26c2fa99d8f7e0d7c01c99bcb40aa6f2
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff
SwiftDataTables: 83d1044c3a7f147dfd3a2e5e1370bc199f4f6441
SwiftDataTables: 16d78aa44825faf00c65ec9678ec51f82d1620a3

PODFILE CHECKSUM: 9948d9c085c4f798f028aaa1e7597151e452280e

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SwiftDataTables.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

414 changes: 214 additions & 200 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions SwiftDataTables/Classes/DataStructureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct DataStructureModel {
return headerTitles.count// ?? 0
}
//MARK: - Public Properties
var data = [[String]]()
var data = DataTableContent()
var headerTitles = [String]()
var footerTitles = [String]()
var shouldFootersShowSortingElement: Bool = false
Expand All @@ -29,11 +29,11 @@ public struct DataStructureModel {

//MARK: - Lifecycle
init() {
self.init(data: [[String]](), headerTitles: [String]())
self.init(data: DataTableContent(), headerTitles: [String]())
}

init(
data: [[String]], headerTitles: [String],
data: DataTableContent, headerTitles: [String],
shouldMakeTitlesFitInColumn: Bool = true,
shouldDisplayFooterHeaders: Bool = true
//sortableColumns: [Int] // This will map onto which column can be sortable
Expand Down Expand Up @@ -72,12 +72,12 @@ public struct DataStructureModel {

//extension DataStructureModel {
//Finds the average content length in each column
private func processColumnDataAverages(data: [[String]]) -> [Float] {
private func processColumnDataAverages(data: DataTableContent) -> [Float] {
var columnContentAverages = [Float]()
for column in Array(0..<self.headerTitles.count) {
let averageForCurrentColumn = Array(0..<data.count).reduce(0){
let text = data[$1][column]
return $0 + text.characters.count
let dataType: DataTableValueType = data[$1][column]
return $0 + dataType.stringRepresentation.characters.count
}
columnContentAverages.append(Float(averageForCurrentColumn)/Float(data.count))
}
Expand Down
15 changes: 15 additions & 0 deletions SwiftDataTables/Classes/DataTableValueType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ public enum DataTableValueType {
case int(Int)
case float(Float)
case double(Double)

var stringRepresentation: String {
get {
switch self {
case .string(let value):
return String(value)
case .int(let value):
return String(value)
case .float(let value):
return String(value)
case .double(let value):
return String(value)
}
}
}
}
29 changes: 21 additions & 8 deletions SwiftDataTables/Classes/SwiftDataTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import UIKit

public typealias DataTableContent = [[DataTableValueType]]

public class SwiftDataTable: UIView {

public enum SupplementaryViewType: String {
/// Single header positioned at the top above the column section
case paginationHeader = "SwiftDataTablePaginationHeader"
Expand Down Expand Up @@ -82,17 +82,30 @@ public class SwiftDataTable: UIView {
fileprivate var columnWidths = [CGFloat]()

//MARK: - Lifecycle
public init(data: [[String]],
headerTitles: [String],
options: DataTableConfiguration = DataTableConfiguration(),
frame: CGRect = .zero)

public init(data: [[DataTableValueType]],
headerTitles: [String],
options: DataTableConfiguration = DataTableConfiguration(),
frame: CGRect = .zero)
{
// self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
super.init(frame: frame)

self.set(data: data, headerTitles: headerTitles, options: options)

self.registerObservers()


}
public convenience init(data: [[String]],
headerTitles: [String],
options: DataTableConfiguration = DataTableConfiguration(),
frame: CGRect = .zero)
{
self.init(
data: data.map { $0.map { .string($0) }},
headerTitles: headerTitles,
frame: frame
)

}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -129,7 +142,7 @@ public class SwiftDataTable: UIView {
collectionView.register(UINib(nibName: menuLengthIdentifier, bundle: podBundle), forSupplementaryViewOfKind: SupplementaryViewType.menuLengthHeader.rawValue, withReuseIdentifier: menuLengthIdentifier)
}

func set(data: [[String]], headerTitles: [String], options: DataTableConfiguration? = nil){
func set(data: [[DataTableValueType]], headerTitles: [String], options: DataTableConfiguration? = nil){
self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
self.createDataCellViewModels(with: self.dataStructure)
self.layout = SwiftDataTableFlowLayout(dataTable: self)
Expand Down

0 comments on commit b454c08

Please sign in to comment.