Skip to content

Commit

Permalink
Order ZIndex by global index
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleve committed Aug 11, 2020
1 parent ed6a8ba commit 69ccbe1
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- [Simplify `Sizing` now that enums support default associated values.](https://github.com/kyleve/Listable/pull/189). Now instead of separate `.thatFits` and `.thatFitsWith(Constraint)` enums, there is a single `.thatFits(Constraint = .noConstraint)` case (the same applies for `autolayout`).

- Changed [how `zIndexes` are assigned to header and items](https://github.com/kyleve/Listable/pull/193). Now by default, the `zIndex` of items is based on their global index path, with later items having a larger index path than earlier ones. Headers and footer still have the highest and lowest `zIndexes`, respectively.

### Misc

# Past Releases
Expand Down
7 changes: 4 additions & 3 deletions Listable/Sources/HeaderFooter/HeaderFooter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ extension HeaderFooter : SignpostLoggable
public struct HeaderFooterLayout : Equatable
{
public var width : CustomWidth

public init(width : CustomWidth = .default)
{

public init(
width : CustomWidth = .default
) {
self.width = width
}
}
3 changes: 1 addition & 2 deletions Listable/Sources/Item/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ public struct ItemLayout : Equatable
itemSpacing : CGFloat? = nil,
itemToSectionFooterSpacing : CGFloat? = nil,
width : CustomWidth = .default
)
{
) {
self.itemSpacing = itemSpacing
self.itemSpacing = itemSpacing
self.width = width
Expand Down
4 changes: 3 additions & 1 deletion Listable/Sources/Layout/CollectionViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ final class CollectionViewLayout : UICollectionViewLayout
direction: self.layout.direction,
showsScrollIndicators: self.appearance.showsScrollIndicators
)

self.performLayout()
}

Expand All @@ -347,6 +347,8 @@ final class CollectionViewLayout : UICollectionViewLayout

self.layout.updateLayout(in: view)

self.layout.setZIndexes()

self.layout.updateOverscrollFooterPosition(in: view)
self.layout.adjustPositionsForLayoutUnderflow(in: view)

Expand Down
37 changes: 37 additions & 0 deletions Listable/Sources/Layout/ListLayout/ListLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ public protocol AnyListLayout : AnyObject
delegate : CollectionViewLayoutDelegate,
in collectionView : UICollectionView
)

func setZIndexes()
}


public extension AnyListLayout
{
func setZIndexes()
{
// list header 2
//
// section header 1
// item 0
// item 0
// section footer -1
//
// section header 2
// item 1
// section footer 0
//
// list footer -2
// overscroll footer -3

self.content.header.zIndex = self.content.sections.count

self.content.footer.zIndex = -2
self.content.overscrollFooter.zIndex = -3

self.content.sections.forEachWithIndex { sectionIndex, _, section in
section.header.zIndex = sectionIndex + 1
section.footer.zIndex = sectionIndex - 1

section.items.forEach { item in
item.zIndex = sectionIndex
}
}
}
}


Expand Down
11 changes: 9 additions & 2 deletions Listable/Sources/Layout/ListLayout/ListLayoutContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ public final class ListLayoutContent
protocol ListLayoutContentItem : AnyObject
{
var size : CGSize { get set }

var x : CGFloat { get set }
var y : CGFloat { get set }

var zIndex : Int { get set }
}


Expand Down Expand Up @@ -320,6 +323,8 @@ public extension ListLayoutContent
var y : CGFloat = .zero
var pinnedY : CGFloat? = nil

var zIndex : Int = 0

var defaultFrame : CGRect {
CGRect(
origin: CGPoint(x: self.x, y: self.y),
Expand Down Expand Up @@ -354,7 +359,7 @@ public extension ListLayoutContent
let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: self.kind.rawValue, with: indexPath)

attributes.frame = self.visibleFrame
attributes.zIndex = self.kind.zIndex
attributes.zIndex = self.zIndex

return attributes
}
Expand All @@ -376,6 +381,8 @@ public extension ListLayoutContent
var x : CGFloat = .zero
var y : CGFloat = .zero

var zIndex : Int = 0

var frame : CGRect {
CGRect(
origin: CGPoint(x: self.x, y: self.y),
Expand Down Expand Up @@ -404,7 +411,7 @@ public extension ListLayoutContent
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)

attributes.frame = self.frame
attributes.zIndex = 0
attributes.zIndex = self.zIndex

return attributes
}
Expand Down
14 changes: 1 addition & 13 deletions Listable/Sources/Layout/SupplementaryKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation


enum SupplementaryKind : String, CaseIterable
public enum SupplementaryKind : String, CaseIterable
{
case listHeader = "Listable.ListHeader"
case listFooter = "Listable.ListFooter"
Expand All @@ -17,18 +17,6 @@ enum SupplementaryKind : String, CaseIterable
case sectionFooter = "Listable.SectionFooter"

case overscrollFooter = "Listable.OverscrollFooter"

var zIndex : Int {
switch self {
case .listHeader: return 1
case .listFooter: return 1

case .sectionHeader: return 2
case .sectionFooter: return 1

case .overscrollFooter: return 1
}
}

func indexPath(in section : Int) -> IndexPath
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
@testable import Listable


final class LayoutDescriptionTests : XCTest
final class LayoutDescriptionTests : XCTestCase
{
func test_createPopulatedLayout()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,4 @@ fileprivate func AssertListLayoutContentItemEqual(
XCTAssertTrue(lhs === rhs, "")
}
}

15 changes: 15 additions & 0 deletions Listable/Tests/Layout/ListLayout/ListLayoutTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ListLayoutTests.swift
// Listable-Unit-Tests
//
// Created by Kyle Van Essen on 8/10/20.
//

import XCTest
@testable import Listable


final class ListLayoutTests : XCTestCase
{

}

0 comments on commit 69ccbe1

Please sign in to comment.