Skip to content

Commit

Permalink
Move to layout-type-based storage of configs on Item, Section, and He…
Browse files Browse the repository at this point in the history
…aderFooter
  • Loading branch information
kyleve committed Jan 22, 2021
1 parent e4d56d1 commit 4e927d8
Show file tree
Hide file tree
Showing 30 changed files with 664 additions and 248 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- [Introduce support for layout customization for `Item`, `HeaderFooter`, and `Section`](https://github.com/kyleve/Listable/pull/257) for all `ListLayout` types, not just `.list`.

### Removed

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class CollectionViewBasicDemoViewController : UIViewController
list += self.rows.map { sectionRows in
Section("Demo Section") { section in

section.columns = .init(count: 2, spacing: 10.0)
section.layouts.list.columns = .init(count: 2, spacing: 10.0)

if self.showsSectionHeaders {
section.header = HeaderFooter(DemoHeader(title: "Section Header"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ final public class CollectionViewDictionaryDemoViewController : UIViewController
state.value.filter = string
}

rows += Item(search, layout: .init(width: .fill))
rows += Item(
search,
layouts: .init {
$0.list.width = .fill
}
)
}

var hasContent = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class HorizontalLayoutViewController : UIViewController

horizontal += Section("cards") { section in

section.columns = .init(count: 2, spacing: 20.0)
section.layouts.list.columns = .init(count: 2, spacing: 20.0)

section += Item(
CardElement(title: "This is the first card", detail: "Isn't it neat?", color: .white(0.90)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ final class InvoicesPaymentScheduleDemoViewController : UIViewController

}
},

sizing: .thatFits(),
layout: ItemLayout(itemSpacing: 20.0)

layouts: .init {
$0.list.itemSpacing = 20.0
}
)

section += Item(
Expand Down Expand Up @@ -140,7 +144,9 @@ final class InvoicesPaymentScheduleDemoViewController : UIViewController
}
},
sizing: .thatFits(),
layout: ItemLayout(itemSpacing: 20.0)
layouts: .init {
$0.list.itemSpacing = 20
}
)

section += Item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class ItemizationEditorViewController : UIViewController

list += Section(SectionIdentifier.variations) { section in

section.columns = .init(count: 2, spacing: 20.0)
section.layouts.list.columns = .init(count: 2, spacing: 20.0)
section.header = HeaderFooter(Header(title: variationsTitle), sizing: .thatFits())
section.footer = HeaderFooter(Footer(text: footerText), sizing: .thatFits())

Expand All @@ -97,7 +97,7 @@ final class ItemizationEditorViewController : UIViewController
list += itemization.modifiers.map { set in
Section(SectionIdentifier.modifier(set.name)) { section in

section.columns = .init(count: 2, spacing: 20.0)
section.layouts.list.columns = .init(count: 2, spacing: 20.0)

section.header = HeaderFooter(Header(title: set.name), sizing: .thatFits())
section.footer = HeaderFooter(Footer(text: "Choose modifiers"), sizing: .thatFits())
Expand All @@ -118,7 +118,7 @@ final class ItemizationEditorViewController : UIViewController

list += Section(SectionIdentifier.discounts) { section in

section.columns = .init(count: 2, spacing: 20.0)
section.layouts.list.columns = .init(count: 2, spacing: 20.0)
section.header = HeaderFooter(Header(title: "Discounts"), sizing: .thatFits())

section += self.availableOptions.allDiscounts.map { discount in
Expand All @@ -134,7 +134,7 @@ final class ItemizationEditorViewController : UIViewController

list += Section(SectionIdentifier.taxes) { section in

section.columns = .init(count: 2, spacing: 20.0)
section.layouts.list.columns = .init(count: 2, spacing: 20.0)
section.header = HeaderFooter(Header(title: "Taxes"), sizing: .thatFits())

section += self.availableOptions.allTaxes.map { tax in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class SpacingCustomizationViewController : UIViewController

list += Section("custom-50") { section in

section.layout = Section.Layout(customInterSectionSpacing: 50)
section.layouts.list = .init(customInterSectionSpacing: 50)

section += Item(
CardElement(title: "Default Row In 50 Spacing Section", color: .white(0.95)),
Expand All @@ -52,7 +52,7 @@ final class SpacingCustomizationViewController : UIViewController

list += Section("custom-100") { section in

section.layout = Section.Layout(customInterSectionSpacing: 100)
section.layouts.list = .init(customInterSectionSpacing: 100)

section += Item(
CardElement(title: "Default Row In 100 Spacing Section", color: .white(0.95)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class WidthCustomizationViewController : UIViewController

list += Section("default") { section in

section.layout = Section.Layout(width: .default)
section.layouts.list.width = .default

section += Item(
CardElement(title: "Default Row In Default Section", color: .white(0.95)),
Expand All @@ -45,7 +45,7 @@ final class WidthCustomizationViewController : UIViewController

list += Section("fill") { section in

section.layout = Section.Layout(width: .fill)
section.layouts.list.width = .fill

section += Item(
CardElement(title: "Default Row In Fill Section", color: .white(0.95)),
Expand All @@ -55,51 +55,58 @@ final class WidthCustomizationViewController : UIViewController

list += Section("custom-1") { section in

section.layout = Section.Layout(width: .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .left
)
section.layouts.list.width = .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .left
)
)

section += Item(
CardElement(title: "Default Row In Left Section", color: .white(0.95)),
sizing: .thatFits(),
layout: .init(width: .default)
layouts: .init {
$0.list.width = .default
}
)

section += Item(
CardElement(title: "Left Aligned In Left Section", color: .white(0.95)),
sizing: .thatFits(),
layout: .init(width: .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .left
))
)
layouts: .init {
$0.list.width = .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .left
)
)
}
)

section += Item(
CardElement(title: "Center Aligned In Left Section", color: .white(0.95)),
sizing: .thatFits(),
layout: .init(width: .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .center
))
)
layouts: .init {
$0.list.width = .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .center
)
)
}
)

section += Item(
CardElement(title: "Right Aligned In Left Section", color: .white(0.95)),
sizing: .thatFits(),
layout: .init(width: .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .right
))
)
layouts: .init {
$0.list.width = .custom(.init(
padding: HorizontalPadding(uniform: 10.0),
width: .atMost(200.0),
alignment: .right
)
)
}
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Demo/Sources/Demos/DemosRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ public final class DemosRootViewController : ListViewController

section += Item(
DemoItem(text: "Tappable Row"),
selectionStyle: .selectable()
selectionStyle: .tappable
)

section += Item(
DemoItem(text: "Tappable Row (Slow Is Selected)"),
selectionStyle: .selectable(),
selectionStyle: .tappable,
onSelect: { _ in
Thread.sleep(forTimeInterval: 0.5)
}
Expand Down
4 changes: 2 additions & 2 deletions ListableUI/Sources/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ public struct Content
/// }
/// }
///
public mutating func callAsFunction<Identifier:Hashable>(_ identifier : Identifier, build : Section.Build)
public mutating func callAsFunction<Identifier:Hashable>(_ identifier : Identifier, configure : Section.Configure)
{
self += Section(identifier, build: build)
self += Section(identifier, configure: configure)
}

/// Removes the `Item` at the given `IndexPath`.
Expand Down
40 changes: 40 additions & 0 deletions ListableUI/Sources/ContentLayoutsStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ContentLayoutsStorage.swift
// ListableUI
//
// Created by Kyle Van Essen on 1/22/21.
//

import Foundation



///
/// Underlying storage used in `ItemLayouts`, `HeaderFooterLayouts`, and `SectionLayouts`.
/// See those types for more information.
///
struct ContentLayoutsStorage {

private var storage : [ObjectIdentifier:Any] = [:]

func get<ValueType>(
_ valueType : ValueType.Type,
default defaultValue : @autoclosure () -> ValueType
) -> ValueType
{
let ID = ObjectIdentifier(valueType)

if let anyValue = self.storage[ID] {
return anyValue as! ValueType
} else {
return defaultValue()
}
}

mutating func set<ValueType>(
_ valueType : ValueType.Type,
new newValue : ValueType
) {
self.storage[ObjectIdentifier(valueType)] = newValue
}
}
2 changes: 1 addition & 1 deletion ListableUI/Sources/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public extension Section {
@available(*, unavailable, renamed: "Section.init(_:build:)")
init<Identifier:Hashable>(
identifier : Identifier,
build : Build = { _ in }
configure : Configure = { _ in }
) {
fatalError()
}
Expand Down
6 changes: 5 additions & 1 deletion ListableUI/Sources/EmbeddedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public extension Item where Content == EmbeddedList
{
Item(
EmbeddedList(identifier: identifier, build: build),

sizing: sizing.toStandardSizing,
layout: ItemLayout(width: .fill)

layouts: .init {
$0.list = .init(width: .fill)
}
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions ListableUI/Sources/HeaderFooter/AnyHeaderFooter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import Foundation
public protocol AnyHeaderFooter : AnyHeaderFooter_Internal
{
var sizing : Sizing { get set }
var layout : HeaderFooterLayout { get set }
var layouts : HeaderFooterLayouts { get set }
}


public protocol AnyHeaderFooter_Internal
{
var layout : HeaderFooterLayout { get }
var layouts : HeaderFooterLayouts { get }

func apply(
to headerFooterView : UIView,
Expand Down
6 changes: 3 additions & 3 deletions ListableUI/Sources/HeaderFooter/HeaderFooter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct HeaderFooter<Content:HeaderFooterContent> : AnyHeaderFooter
public var content : Content

public var sizing : Sizing
public var layout : HeaderFooterLayout
public var layouts : HeaderFooterLayouts

public typealias OnTap = (Content) -> ()
public var onTap : OnTap?
Expand All @@ -42,15 +42,15 @@ public struct HeaderFooter<Content:HeaderFooterContent> : AnyHeaderFooter
public init(
_ content : Content,
sizing : Sizing = .thatFits(.init(.atLeast(.default))),
layout : HeaderFooterLayout = HeaderFooterLayout(),
layouts : HeaderFooterLayouts = .init(),
onTap : OnTap? = nil
) {
assertIsValueType(Content.self)

self.content = content

self.sizing = sizing
self.layout = layout
self.layouts = layouts

self.onTap = onTap

Expand Down
20 changes: 0 additions & 20 deletions ListableUI/Sources/HeaderFooter/HeaderFooterLayout.swift

This file was deleted.

Loading

0 comments on commit 4e927d8

Please sign in to comment.