|
| 1 | +// |
| 2 | +// Element+Item.swift |
| 3 | +// BlueprintUILists |
| 4 | +// |
| 5 | +// Created by Kyle Van Essen on 7/24/22. |
| 6 | +// |
| 7 | + |
| 8 | +import BlueprintUI |
| 9 | +import ListableUI |
| 10 | + |
| 11 | + |
| 12 | +// MARK: Item / ItemContent Extensions |
| 13 | + |
| 14 | +extension Element { |
| 15 | + |
| 16 | + /// Converts the given `Element` into a Listable `Item`. You many also optionally |
| 17 | + /// configure the item, setting its values such as the `onDisplay` callbacks, etc. |
| 18 | + /// |
| 19 | + /// ```swift |
| 20 | + /// MyElement(...) |
| 21 | + /// .item { item in |
| 22 | + /// item.insertAndRemoveAnimations = .scaleUp |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + /// |
| 26 | + /// ## ⚠️ Performance Considerations |
| 27 | + /// Unless your `Element` conforms to `Equatable` or `IsEquivalentContent`, |
| 28 | + /// it will return `false` for `isEquivalent` for each content update, which can dramatically |
| 29 | + /// hurt performance for longer lists (eg, more than 20 items): it will be re-measured for each content update. |
| 30 | + /// |
| 31 | + /// It is encouraged for these longer lists, you ensure your `Element` conforms to one of these protocols. |
| 32 | + public func item( |
| 33 | + configure : (inout Item<WrappedElementContent<Self, ObjectIdentifier>>) -> () = { _ in } |
| 34 | + ) -> Item<WrappedElementContent<Self, ObjectIdentifier>> { |
| 35 | + Item( |
| 36 | + WrappedElementContent( |
| 37 | + represented: self, |
| 38 | + identifierValue: ObjectIdentifier(Self.Type.self) |
| 39 | + ), |
| 40 | + configure: configure |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + /// Converts the given `Element` into a Listable `Item` with the provided ID. You can use this ID |
| 45 | + /// to scroll to or later access the item through the regular list access APIs. |
| 46 | + /// You many also optionally configure the item, setting its values such as the `onDisplay` callbacks, etc. |
| 47 | + /// |
| 48 | + /// ```swift |
| 49 | + /// MyElement(...) |
| 50 | + /// .item(id: "my-provided-id") { item in |
| 51 | + /// item.insertAndRemoveAnimations = .scaleUp |
| 52 | + /// } |
| 53 | + /// ``` |
| 54 | + /// |
| 55 | + /// ## ⚠️ Performance Considerations |
| 56 | + /// Unless your `Element` conforms to `Equatable` or `IsEquivalentContent`, |
| 57 | + /// it will return `false` for `isEquivalent` for each content update, which can dramatically |
| 58 | + /// hurt performance for longer lists (eg, more than 20 items): it will be re-measured for each content update. |
| 59 | + /// |
| 60 | + /// It is encouraged for these longer lists, you ensure your `Element` conforms to one of these protocols. |
| 61 | + public func item<ID:Hashable>( |
| 62 | + id : ID, |
| 63 | + configure : (inout Item<WrappedElementContent<Self, ID>>) -> () = { _ in } |
| 64 | + ) -> Item<WrappedElementContent<Self, ID>> { |
| 65 | + Item( |
| 66 | + WrappedElementContent( |
| 67 | + represented: self, |
| 68 | + identifierValue: id |
| 69 | + ), |
| 70 | + configure: configure |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /// Used by internal Listable methods to convert type-erased `Element` instances into `Item` instances. |
| 75 | + func toAnyItemConvertible() -> AnyItemConvertible { |
| 76 | + /// We use `type(of:)` to ensure we get the actual type, not just `Element`. |
| 77 | + WrappedElementContent( |
| 78 | + represented: self, |
| 79 | + identifierValue: ObjectIdentifier(type(of: self)) |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +public struct WrappedElementContent<ElementType:Element, IdentifierValue:Hashable> : BlueprintItemContent |
| 86 | +{ |
| 87 | + public let represented : ElementType |
| 88 | + |
| 89 | + public let identifierValue: IdentifierValue |
| 90 | + |
| 91 | + public func isEquivalent(to other: Self) -> Bool { |
| 92 | + false |
| 93 | + } |
| 94 | + |
| 95 | + public func element(with info: ApplyItemContentInfo) -> Element { |
| 96 | + represented |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +extension WrappedElementContent where ElementType : Equatable { |
| 102 | + |
| 103 | + public func isEquivalent(to other: Self) -> Bool { |
| 104 | + represented == other.represented |
| 105 | + } |
| 106 | + |
| 107 | + public var reappliesToVisibleView: ReappliesToVisibleView { |
| 108 | + .ifNotEquivalent |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +extension WrappedElementContent where ElementType : IsEquivalentContent { |
| 114 | + |
| 115 | + public func isEquivalent(to other: Self) -> Bool { |
| 116 | + represented.isEquivalent(to: other.represented) |
| 117 | + } |
| 118 | + |
| 119 | + public var reappliesToVisibleView: ReappliesToVisibleView { |
| 120 | + .ifNotEquivalent |
| 121 | + } |
| 122 | +} |
0 commit comments