Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale to fill content view #2

Merged
merged 7 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
## [0.2](https://github.com/pavankataria/SwiftDataTables/tree/0.2) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.1.1...0.2)

**Merged pull requests:**
**Implemented enhancements:**

- Image bundle [\#1](https://github.com/pavankataria/SwiftDataTables/pull/1) ([pavankataria](https://github.com/pavankataria))

Expand Down
34 changes: 34 additions & 0 deletions Example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Change Log

## [0.2.6](https://github.com/pavankataria/SwiftDataTables/tree/0.2.6) (2017-03-11)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2.5...0.2.6)

## [0.2.5](https://github.com/pavankataria/SwiftDataTables/tree/0.2.5) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2.4...0.2.5)

## [0.2.4](https://github.com/pavankataria/SwiftDataTables/tree/0.2.4) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2.3...0.2.4)

## [0.2.3](https://github.com/pavankataria/SwiftDataTables/tree/0.2.3) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2.2...0.2.3)

## [0.2.2](https://github.com/pavankataria/SwiftDataTables/tree/0.2.2) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2.1...0.2.2)

## [0.2.1](https://github.com/pavankataria/SwiftDataTables/tree/0.2.1) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.2...0.2.1)

## [0.2](https://github.com/pavankataria/SwiftDataTables/tree/0.2) (2017-03-10)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.1.1...0.2)

**Implemented enhancements:**

- Image bundle [\#1](https://github.com/pavankataria/SwiftDataTables/pull/1) ([pavankataria](https://github.com/pavankataria))

## [0.1.1](https://github.com/pavankataria/SwiftDataTables/tree/0.1.1) (2017-03-09)
[Full Changelog](https://github.com/pavankataria/SwiftDataTables/compare/0.1.0...0.1.1)

## [0.1.0](https://github.com/pavankataria/SwiftDataTables/tree/0.1.0) (2017-03-09)


\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
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.4)
- SwiftDataTables (0.2.5)

DEPENDENCIES:
- FBSnapshotTestCase
Expand All @@ -30,7 +30,7 @@ SPEC CHECKSUMS:
Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29
Nimble-Snapshots: e743439f26c2fa99d8f7e0d7c01c99bcb40aa6f2
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff
SwiftDataTables: 3c87142d2539a9cd554e9567eb91689cfd9d6c39
SwiftDataTables: 05cf3879c95cb0dceb421bd7233e8f56602b1a18

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.

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

4 changes: 2 additions & 2 deletions Example/SwiftDataTables.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
Expand All @@ -527,7 +527,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
Expand Down
9 changes: 9 additions & 0 deletions Example/SwiftDataTables/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
9 changes: 3 additions & 6 deletions Example/SwiftDataTables/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ class ViewController: UIViewController {
self.dataTable.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)

//25/255, green: 33/255, blue: 39/255, alpha: 1)

self.dataTable.autoresizingMask = [.flexibleWidth, .flexibleHeight]

self.dataTable.frame = self.view.bounds

self.view.addSubview(self.dataTable);
}
}
Expand All @@ -51,9 +48,9 @@ extension ViewController {
"Id",
"Name",
"Email",
"Number",
"City",
"Balance"
"Number"
// "City",
// "Balance"
]
}

Expand Down
5 changes: 4 additions & 1 deletion SwiftDataTables/Classes/DataStructureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public struct DataStructureModel {
self.headerTitles = headerTitles
let unfilteredData = data
let sanitisedData = unfilteredData.filter({ currentRowData in
return currentRowData.count == self.columnCount
//Trim column count for current row to the number of headers present
let rowWithPreferredColumnCount = Array(currentRowData.prefix(upTo: self.columnCount))
return rowWithPreferredColumnCount.count == self.columnCount
})

self.data = sanitisedData//sanitisedData
self.shouldFitTitles = shouldMakeTitlesFitInColumn
self.columnAverageContentLength = self.processColumnDataAverages(data: self.data)
Expand Down
95 changes: 85 additions & 10 deletions SwiftDataTables/Classes/SwiftDataTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,19 @@ public class SwiftDataTable: UIView {
fileprivate var rowViewModels = [[DataCellViewModel]]()
fileprivate var paginationViewModel: PaginationHeaderViewModel!
fileprivate var menuLengthViewModel: MenuLengthHeaderViewModel!

fileprivate var columnWidths = [CGFloat]()

//MARK: - Lifecycle
public init(data: [[String]],
headerTitles: [String],
frame: CGRect = .zero)
{
self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
// self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
super.init(frame: frame)
self.createDataCellViewModels(with: self.dataStructure)
self.layout = SwiftDataTableFlowLayout(dataTable: self)

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

self.registerObservers()
self.collectionView.reloadData()
}

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

// public override var frame: CGRect {
// get {
// return super.frame
// }
// set {
// super.frame = frame
//// if frame != .zero {
//// self.calculateColumnWidths()
//// }
// }
// }
func set(data: [[String]], headerTitles: [String]){
let dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
self.createDataModels(with:dataStructure)
self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles)
self.createDataCellViewModels(with: self.dataStructure)
self.layout = SwiftDataTableFlowLayout(dataTable: self)
self.calculateColumnWidths()
}

func calculateColumnWidths(){
//calculate the automatic widths for each column
self.columnWidths.removeAll()
for columnIndex in Array(0..<self.numberOfHeaderColumns()) {
self.columnWidths.append(self.automaticWidthForColumn(index: columnIndex))
}

if self.shouldContentWidthScaleToFillFrame(){
self.scaleToFillColumnWidths()
}
}

func scaleToFillColumnWidths(){
//if content width is smaller than ipad width
let totalColumnWidth = self.columnWidths.reduce(0, +)
let totalWidth = self.frame.width
let gap: CGFloat = totalWidth - totalColumnWidth
guard totalColumnWidth < totalWidth else {
return
}
//calculate the percentage width presence of each column in relation to the frame width of the collection view
for columnIndex in Array(0..<self.columnWidths.count) {
let columnWidth = self.columnWidths[columnIndex]
let columnWidthPercentagePresence = columnWidth / totalColumnWidth
//add result of gap size divided by percentage column width to each column automatic width.
let gapPortionToDistributeToCurrentColumn = gap * columnWidthPercentagePresence
//apply final result of each column width to the column width array.
self.columnWidths[columnIndex] = columnWidth + gapPortionToDistributeToCurrentColumn
}
}
//MARK: - Events
// public func tapped(headerView: DataHeaderFooterViewModel){
//// self.headerViewModels.forEach { if $0.data != headerView.data { $0.toggleToDefault() } }
Expand Down Expand Up @@ -268,7 +312,7 @@ extension SwiftDataTable: UIScrollViewDelegate {
if self.disableScrollViewRightBounce(){
let maxX = self.collectionView.contentSize.width-self.collectionView.frame.width
if (self.collectionView.contentOffset.x >= maxX){
self.collectionView.contentOffset.x = maxX-1
self.collectionView.contentOffset.x = max(maxX-1, 0)
}
}
if self.disableScrollViewBottomBounce(){
Expand Down Expand Up @@ -353,6 +397,10 @@ extension SwiftDataTable {
return self.dataStructure.footerTitles.count
}


func shouldContentWidthScaleToFillFrame() -> Bool{
return true
}
func shouldSectionHeadersFloat() -> Bool {
return true
}
Expand Down Expand Up @@ -381,13 +429,39 @@ extension SwiftDataTable {
return 0
}

func widthForColumn(index: Int) -> CGFloat {
let dataStructure = self.dataStructure

/// Automatically calcualtes the width the column should be based on the content
/// in the rows under the column.
///
/// - Parameter index: The column index
/// - Returns: The automatic width of the column irrespective of the Data Grid frame width
func automaticWidthForColumn(index: Int) -> CGFloat {
print(self.frame.width)
let columnAverage: CGFloat = CGFloat(dataStructure.averageDataLengthForColumn(index: index))
let sortingArrowVisualElementWidth: CGFloat = 20 // This is ugly
let averageDataColumnWidth: CGFloat = columnAverage * self.pixelsPerCharacter() + sortingArrowVisualElementWidth
return max(averageDataColumnWidth, max(self.minimumColumnWidth(), self.minimumHeaderColumnWidth(index: index)))
}
// func automaticWidthForAllColumns(){
// let automaticCalculatedWidth: CGFloat = Array(0..<self.numberOfHeaderColumns())
// .reduce(0.0){
// return $0 + self.automaticWidthForColumn(index: $1)
// }
//
// if automaticCalculatedWidth < self.collectionView.frame.width {
// let emptyGap = self.collectionView.frame.width - automaticCalculatedWidth
//
// }
//// return automaticCalculatedWidth
// }

func widthForColumn(index: Int) -> CGFloat {
return self.columnWidths[index]
}

func calculateContentWidth() -> CGFloat {
return Array(0..<self.numberOfColumns()).reduce(self.widthForRowHeader()) { $0 + self.widthForColumn(index: $1)}
}

func heightForRow(index: Int) -> CGFloat {
return 44
Expand All @@ -400,6 +474,7 @@ extension SwiftDataTable {
func minimumColumnWidth() -> CGFloat {
return 70
}

func minimumHeaderColumnWidth(index: Int) -> CGFloat {
return CGFloat(self.pixelsPerCharacter() * CGFloat(self.dataStructure.headerTitles[index].characters.count))
}
Expand Down
4 changes: 2 additions & 2 deletions SwiftDataTables/Classes/SwiftDataTableFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SwiftDataTableFlowLayout: UICollectionViewFlowLayout {
guard self.cache.isEmpty else {
return
}

self.dataTable.calculateColumnWidths()
let methodStart = Date()

var xOffsets = [CGFloat]()
Expand Down Expand Up @@ -91,7 +91,7 @@ class SwiftDataTableFlowLayout: UICollectionViewFlowLayout {
}

override var collectionViewContentSize: CGSize {
let width = Array(0..<self.dataTable.numberOfColumns()).reduce(self.dataTable.widthForRowHeader()) { $0 + self.dataTable.widthForColumn(index: $1)}
let width = self.dataTable.calculateContentWidth()
let height = Array(0..<self.dataTable.numberOfRows()).reduce(self.dataTable.heightForSectionHeader() + self.dataTable.heightForSectionFooter() + self.dataTable.heightForPaginationView() + self.dataTable.heightForMenuLengthView()) {
$0 + self.dataTable.heightForRow(index: $1) + self.dataTable.heightOfInterRowSpacing()
}
Expand Down