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 2d801ac
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
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
59 changes: 44 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,42 @@ public struct Item<Content:ItemContent> : AnyItem
}


public extension Item
{
struct OnSelect
{
public typealias Callback = (OnSelect) -> ()

public var item : Item
}

struct OnDeselect
{
public typealias Callback = (OnDeselect) -> ()

public var item : Item
}

struct OnDisplay
{
public typealias Callback = (OnDisplay) -> ()

public var item : Item

public var isFirstDisplay : Bool
}

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 2d801ac

Please sign in to comment.