/
CollectionCellAdapter+Events.swift
106 lines (93 loc) · 3.41 KB
/
CollectionCellAdapter+Events.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Owl
// A declarative type-safe framework for building fast and flexible list with Tables & Collections
//
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: hello@danielemargutti.com
//
// Copyright © 2019 Daniele Margutti. Licensed under Apache 2.0 License.
//
import UIKit
public extension CollectionCellAdapter {
struct Event {
/// Index path of represented context's cell instance
public let indexPath: IndexPath?
/// Represented model instance
public let element: Model?
/// Managed source collection
public var collection: UICollectionView? {
guard let cell = (cell as? UICollectionViewCell),
let collection = cell.superview as? UICollectionView else {
return nil
}
return collection
}
/// Managed source collection's bounds size
public var collectionSize: CGSize? {
return collection?.bounds.size
}
/// Represented cell instance.
/// Depending from the source event where the context is generated it maybe nil.
/// When not `nil` it's stricly typed to its parent adapter cell's definition.
public var cell: Cell?
/// Initialize a new context from a source event.
/// Instances of the Context are generated automatically and received from events; you don't need to allocate on your own.
///
/// - Parameters:
/// - model: source generic model
/// - cell: source generic cell
/// - path: cell's path
/// - collection: parent cell's collection instance
internal init(element: Any?, cell: Any?, path: IndexPath?) {
self.element = element as? Model
self.cell = cell as? Cell
self.indexPath = path
}
}
struct EventsSubscriber {
public var dequeue: ((Event) -> Void)? = nil
public var shouldSelect: ((Event) -> Bool)? = nil
public var shouldDeselect: ((Event) -> Bool)? = nil
public var didSelect: ((Event) -> Void)? = nil
public var didDeselect: ((Event) -> Void)? = nil
public var didHighlight: ((Event) -> Void)? = nil
public var didUnhighlight: ((Event) -> Void)? = nil
public var shouldHighlight: ((Event) -> Bool)? = nil
public var willDisplay: ((Event) -> Void)? = nil
public var endDisplay: ((Event) -> Void)? = nil
public var shouldShowEditMenu: ((Event) -> Bool)? = nil
public var canPerformEditAction: ((Event) -> Bool)? = nil
public var performEditAction: ((_ ctx: Event, _ selector: Selector, _ sender: Any?) -> Void)? = nil
public var canFocus: ((Event) -> Bool)? = nil
public var itemSize: ((Event) -> CGSize)? = nil
//var generateDragPreview: ((EventContext) -> UIDragPreviewParameters?)? = nil
//var generateDropPreview: ((EventContext) -> UIDragPreviewParameters?)? = nil
public var prefetch: ((_ items: [Model], _ paths: [IndexPath]) -> Void)? = nil
public var cancelPrefetch: ((_ items: [Model], _ paths: [IndexPath]) -> Void)? = nil
public var shouldSpringLoad: ((Event) -> Bool)? = nil
}
}
public enum CollectionAdapterEventID: Int {
case dequeue
case shouldSelect
case shouldDeselect
case didSelect
case didDeselect
case didHighlight
case didUnhighlight
case shouldHighlight
case willDisplay
case endDisplay
case shouldShowEditMenu
case canPerformEditAction
case performEditAction
case canFocus
case itemSize
//case generateDragPreview
//case generateDropPreview
case prefetch
case cancelPrefetch
case shouldSpringLoad
}