Skip to content

Commit

Permalink
Support providing additional information to Item callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleve committed Jun 4, 2020
1 parent f6016b5 commit 37c2c82
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Changed

- Update `Item` callbacks to [allow for providing more info to the callback parameters](https://github.com/kyleve/Listable/pull/160).

### Misc

# Past Releases
Expand Down
3 changes: 2 additions & 1 deletion Demo/Sources/Demos/DemosRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public final class DemosRootViewController : UIViewController
selectionStyle: .tappable,
onSelect : { _ in
self.push(CollectionViewBasicDemoViewController())
})
}
)

section += Item(
DemoItem(text: "Blueprint Integration"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ extension PresentationState

private(set) var isDisplayed : Bool = false

private var hasDisplayed : Bool = false
private var hasEndedDisplay : Bool = false

func setAndPerform(isDisplayed: Bool) {
guard self.isDisplayed != isDisplayed else {
return
Expand All @@ -161,9 +164,21 @@ extension PresentationState
self.isDisplayed = isDisplayed

if self.isDisplayed {
self.model.onDisplay?(self.model.content)
self.model.onDisplay?(.init(
item: self.model,
isFirstDisplay: self.hasDisplayed == false
)
)

self.hasDisplayed = true
} else {
self.model.onEndDisplay?(self.model.content)
self.model.onEndDisplay?(.init(
item: self.model,
isFirstEndDisplay: self.hasEndedDisplay == false
)
)

self.hasEndedDisplay = true
}
}

Expand Down Expand Up @@ -288,13 +303,13 @@ extension PresentationState
if isSelected {
if let onSelect = self.model.onSelect {
SignpostLogger.log(log: .listInteraction, name: "Item onSelect", for: self.model) {
onSelect(self.model.content)
onSelect(.init(item: self.model))
}
}
} else {
if let onDeselect = self.model.onDeselect {
SignpostLogger.log(log: .listInteraction, name: "Item onDeselect", for: self.model) {
onDeselect(self.model.content)
onDeselect(.init(item: self.model))
}
}
}
Expand Down
63 changes: 48 additions & 15 deletions Listable/Sources/Item/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@ public struct Item<Content:ItemContent> : AnyItem

public var reordering : Reordering?

public typealias OnSelect = (Content) -> ()
public var onSelect : OnSelect?

public typealias OnDeselect = (Content) -> ()
public var onDeselect : OnDeselect?

public typealias OnDisplay = (Content) -> ()
public var onDisplay : OnDisplay?

public typealias OnEndDisplay = (Content) -> ()
public var onEndDisplay : OnEndDisplay?
public var onSelect : OnSelect.Callback?
public var onDeselect : OnDeselect.Callback?
public var onDisplay : OnDisplay.Callback?
public var onEndDisplay : OnEndDisplay.Callback?

internal let reuseIdentifier : ReuseIdentifier<Content>

Expand Down Expand Up @@ -92,10 +85,10 @@ public struct Item<Content:ItemContent> : AnyItem
selectionStyle : ItemSelectionStyle? = nil,
swipeActions : SwipeActionsConfiguration? = nil,
reordering : Reordering? = nil,
onDisplay : OnDisplay? = nil,
onEndDisplay : OnEndDisplay? = nil,
onSelect : OnSelect? = nil,
onDeselect : OnDeselect? = nil
onDisplay : OnDisplay.Callback? = nil,
onEndDisplay : OnEndDisplay.Callback? = nil,
onSelect : OnSelect.Callback? = nil,
onDeselect : OnDeselect.Callback? = nil
)
{
self.content = content
Expand Down Expand Up @@ -172,6 +165,46 @@ public struct Item<Content:ItemContent> : AnyItem
}


public extension Item
{
/// Value passed to the `onSelect` callback for `Item`.
struct OnSelect
{
public typealias Callback = (OnSelect) -> ()

public var item : Item
}

/// Value passed to the `onDeselect` callback for `Item`.
struct OnDeselect
{
public typealias Callback = (OnDeselect) -> ()

public var item : Item
}

/// Value passed to the `onDisplay` callback for `Item`.
struct OnDisplay
{
public typealias Callback = (OnDisplay) -> ()

public var item : Item

public var isFirstDisplay : Bool
}

/// Value passed to the `onEndDisplay` callback for `Item`.
struct OnEndDisplay
{
public typealias Callback = (OnEndDisplay) -> ()

public var item : Item

public var isFirstEndDisplay : Bool
}
}


/// Allows specifying default properties to apply to an item when it is initialized,
/// if those values are not provided to the initializer.
/// Only non-nil values are used – if you do not want to provide a default value,
Expand Down

0 comments on commit 37c2c82

Please sign in to comment.