Skip to content

Commit

Permalink
Improved clear row functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
AramSemerjyan committed Jun 3, 2020
1 parent 786fbd5 commit 77111ff
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
17 changes: 14 additions & 3 deletions Example/ugrid/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ViewController: UIViewController {
// MARK: - private vars
private var _dataSource: [Int] = Array(0...1000)
private var _layout = UGridFlowLayout()
private var _layoutType: LayoutType = .less
private var _layoutType: LayoutType = .more
private var _dirrection: UICollectionView.ScrollDirection = .vertical

private var _dirrectionTitle: String {
Expand Down Expand Up @@ -83,6 +83,17 @@ extension ViewController: UICollectionViewDataSource {

cell.backgroundColor = .init(red: (135 / 255), green: (206 / 255), blue: (250 / 255), alpha: 1)

if let label = cell.contentView.viewWithTag(32) as? UILabel {
label.text = "\(_dataSource[indexPath.row])"
} else {
let label = UILabel()
label.tag = 32
label.text = "\(_dataSource[indexPath.row])"
label.sizeToFit()

cell.contentView.addSubview(label)
}

return cell
}
}
Expand Down Expand Up @@ -111,9 +122,9 @@ class CustomSizeCountInrow: IGridItemsInRow {
case .more:
switch size {
case .small:
return 6
return 8
case .middle:
return 3
return 4
case .big:
return 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class ClearCompleteRow {
final class ClearVerticalCompleteRow: ClearCompleteRow { }

extension ClearVerticalCompleteRow: IGridCompleteRowFinder {
func findCompleteRows(inAttributes attributes: [UICollectionViewLayoutAttributes], beforeFrame frame: CGRect, completion: @escaping (CGFloat) -> Void) {
if (frame.maxX + gridSize.smallGrid.width) >= (layout.layoutWidth - layout.inset.left) {
let height = attributes.sorted { $0.frame.maxY > $1.frame.maxY }.first?.frame.maxY ?? 0

// Check that heigh for all items in the line is same
// if it's not, that means that there can be an empy space to place new item
if height == frame.maxY {
completion(height)
func findCompleteRows(inAttributes attributes: [UICollectionViewLayoutAttributes], completion: @escaping (CGFloat) -> Void) {
let itemsCount = gridSize.countInRow(forItems: .small)
let suffix = attributes.suffix(itemsCount)

if suffix.count == itemsCount {
let maxY = suffix.sorted { $0.frame.maxY > $1.frame.maxY }.first?.frame.maxY ?? 0
let minY = suffix.sorted { $0.frame.maxY < $1.frame.maxY }.first?.frame.maxY ?? 0

if minY == maxY {
completion(maxY)
}
}
}
Expand All @@ -36,14 +38,16 @@ extension ClearVerticalCompleteRow: IGridCompleteRowFinder {
final class ClearHorizontalCompleteRow: ClearCompleteRow { }

extension ClearHorizontalCompleteRow: IGridCompleteRowFinder {
func findCompleteRows(inAttributes attributes: [UICollectionViewLayoutAttributes], beforeFrame frame: CGRect, completion: @escaping (CGFloat) -> Void) {
if (frame.maxY + gridSize.smallGrid.height) >= (layout.layoutHeight - layout.inset.left) {
let width = attributes.sorted { $0.frame.maxX > $1.frame.maxX }.first?.frame.maxX ?? 0

// Check that heigh for all items in the line is same
// if it's not, that means that there can be an empy space to place new item
if width == frame.maxX {
completion(width)
func findCompleteRows(inAttributes attributes: [UICollectionViewLayoutAttributes], completion: @escaping (CGFloat) -> Void) {
let itemsCount = gridSize.countInRow(forItems: .small)
let suffix = attributes.suffix(itemsCount)

if suffix.count == itemsCount {
let maxX = suffix.sorted { $0.frame.maxX > $1.frame.maxX }.first?.frame.maxX ?? 0
let minX = suffix.sorted { $0.frame.maxX < $1.frame.maxX }.first?.frame.maxX ?? 0

if minX == maxX {
completion(maxX)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import UIKit
protocol IGridCompleteRowFinder {

func findCompleteRows(inAttributes attributes: [UICollectionViewLayoutAttributes],
beforeFrame frame: CGRect,
completion: @escaping (CGFloat) -> Void
)
}
6 changes: 3 additions & 3 deletions ugrid/Classes/Calculation/GridCalculationLogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ final class GridCalculationLogic: IGridCalculation {

a.frame = spaceRect

attributesInRow.append(a)

// This will decrease complexity
// If there is fully complete line, like:
// if grid type is 'less', and there is [small, small, small, small] in one line
// this will remove all four attributes, because there is no point for having them
// while looking for empty space
_findCompleteRows[_layout.scrollingDirection.rawValue]?.findCompleteRows(inAttributes: attributesInRow, beforeFrame: a.frame, completion: { (v) in
_findCompleteRows[_layout.scrollingDirection.rawValue]?.findCompleteRows(inAttributes: attributesInRow, completion: { (v) in
attributesInRow.removeAll()

startCoord = v
})

attributesInRow.append(a)
}

if _layout.scrollingDirection == .vertical {
Expand Down
13 changes: 11 additions & 2 deletions ugrid/Classes/GridSize/GridSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ final class GridSize {
}

extension GridSize: IGridSize {
func countInRow(forItems itemType: SizeType) -> Int {
return Int(_gridInRow.itemsInRow(forSizeType: itemType, andLayoutType: _layoutType))
}

func setType(_ layoutType: LayoutType) {
_layoutType = layoutType
}
Expand Down Expand Up @@ -67,13 +71,18 @@ extension GridSize: IGridSize {
}

var bigGrid: CGSize {
let itemWidth = getItemWidth(forGridSize: .big)
var itemWidth = getItemWidth(forGridSize: .big)

switch _layoutType {
case .less:
return .init(width: itemWidth, height: middleGrid.height)
case .more:
return .init(width: itemWidth * 6 / 9.1, height: middleGrid.height)

if _gridInRow.itemsInRow(forSizeType: .big, andLayoutType: _layoutType) == 1 {
itemWidth = itemWidth - getItemWidth(forGridSize: .middle) - 10
}

return .init(width: itemWidth, height: middleGrid.height)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions ugrid/Classes/GridSize/IGridSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ protocol IGridSize: IGridSizeConfigurable {

func setType(_ layoutType: LayoutType)
func getSize(forGridSizeType sizeType: SizeType) -> CGSize

//TODO: maybe that is not such a good idea to provide this function
// through IGridSize interface
func countInRow(forItems itemType: SizeType) -> Int
}

0 comments on commit 77111ff

Please sign in to comment.