-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update APIs used to add elements to lists
- Loading branch information
Showing
14 changed files
with
640 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// Element+HeaderFooter.swift | ||
// BlueprintUILists | ||
// | ||
// Created by Kyle Van Essen on 7/24/22. | ||
// | ||
|
||
import BlueprintUI | ||
import ListableUI | ||
|
||
|
||
// MARK: HeaderFooter / HeaderFooterContent Extensions | ||
|
||
|
||
extension Element { | ||
|
||
/// Converts the given `Element` into a Listable `HeaderFooter`. You many also optionally | ||
/// configure the header / footer, setting its values such as the `onTap` callbacks, etc. | ||
/// | ||
/// ```swift | ||
/// MyElement(...) | ||
/// .headerFooter { header in | ||
/// header.onTap = { ... } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ## ⚠️ Performance Considerations | ||
/// Unless your `Element` conforms to `Equatable` or `IsEquivalentContent`, | ||
/// it will return `false` for `isEquivalent` for each content update, which can dramatically | ||
/// hurt performance for longer lists (eg, more than 20 items): it will be re-measured for each content update. | ||
/// | ||
/// It is encouraged for these longer lists, you ensure your `Element` conforms to one of these protocols. | ||
public func headerFooter( | ||
configure : (inout HeaderFooter<WrappedHeaderFooterContent<Self>>) -> () = { _ in } | ||
) -> HeaderFooter<WrappedHeaderFooterContent<Self>> { | ||
HeaderFooter( | ||
WrappedHeaderFooterContent( | ||
represented: self | ||
), | ||
configure: configure | ||
) | ||
} | ||
|
||
/// Used by internal Listable methods to convert type-erased `Element` instances into `Item` instances. | ||
func toHeaderFooterConvertible() -> AnyHeaderFooterConvertible { | ||
/// We use `type(of:)` to ensure we get the actual type, not just `Element`. | ||
WrappedHeaderFooterContent( | ||
represented: self | ||
) | ||
} | ||
} | ||
|
||
|
||
public struct WrappedHeaderFooterContent<ElementType:Element> : BlueprintHeaderFooterContent | ||
{ | ||
public let represented : ElementType | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
false | ||
} | ||
|
||
public var elementRepresentation: Element { | ||
represented | ||
} | ||
} | ||
|
||
|
||
extension WrappedHeaderFooterContent where ElementType : Equatable { | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
represented == other.represented | ||
} | ||
|
||
public var reappliesToVisibleView: ReappliesToVisibleView { | ||
.ifNotEquivalent | ||
} | ||
} | ||
|
||
|
||
extension WrappedHeaderFooterContent where ElementType : IsEquivalentContent { | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
represented.isEquivalent(to: other.represented) | ||
} | ||
|
||
public var reappliesToVisibleView: ReappliesToVisibleView { | ||
.ifNotEquivalent | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// Element+Item.swift | ||
// BlueprintUILists | ||
// | ||
// Created by Kyle Van Essen on 7/24/22. | ||
// | ||
|
||
import BlueprintUI | ||
import ListableUI | ||
|
||
|
||
// MARK: Item / ItemContent Extensions | ||
|
||
extension Element { | ||
|
||
/// Converts the given `Element` into a Listable `Item`. You many also optionally | ||
/// configure the item, setting its values such as the `onDisplay` callbacks, etc. | ||
/// | ||
/// ```swift | ||
/// MyElement(...) | ||
/// .item { item in | ||
/// item.insertAndRemoveAnimations = .scaleUp | ||
/// } | ||
/// ``` | ||
/// | ||
/// ## ⚠️ Performance Considerations | ||
/// Unless your `Element` conforms to `Equatable` or `IsEquivalentContent`, | ||
/// it will return `false` for `isEquivalent` for each content update, which can dramatically | ||
/// hurt performance for longer lists (eg, more than 20 items): it will be re-measured for each content update. | ||
/// | ||
/// It is encouraged for these longer lists, you ensure your `Element` conforms to one of these protocols. | ||
public func item( | ||
configure : (inout Item<WrappedElementContent<Self, ObjectIdentifier>>) -> () = { _ in } | ||
) -> Item<WrappedElementContent<Self, ObjectIdentifier>> { | ||
Item( | ||
WrappedElementContent( | ||
represented: self, | ||
identifierValue: ObjectIdentifier(Self.Type.self) | ||
), | ||
configure: configure | ||
) | ||
} | ||
|
||
/// Converts the given `Element` into a Listable `Item` with the provided ID. You can use this ID | ||
/// to scroll to or later access the item through the regular list access APIs. | ||
/// You many also optionally configure the item, setting its values such as the `onDisplay` callbacks, etc. | ||
/// | ||
/// ```swift | ||
/// MyElement(...) | ||
/// .item(id: "my-provided-id") { item in | ||
/// item.insertAndRemoveAnimations = .scaleUp | ||
/// } | ||
/// ``` | ||
/// | ||
/// ## ⚠️ Performance Considerations | ||
/// Unless your `Element` conforms to `Equatable` or `IsEquivalentContent`, | ||
/// it will return `false` for `isEquivalent` for each content update, which can dramatically | ||
/// hurt performance for longer lists (eg, more than 20 items): it will be re-measured for each content update. | ||
/// | ||
/// It is encouraged for these longer lists, you ensure your `Element` conforms to one of these protocols. | ||
public func item<ID:Hashable>( | ||
id : ID, | ||
configure : (inout Item<WrappedElementContent<Self, ID>>) -> () = { _ in } | ||
) -> Item<WrappedElementContent<Self, ID>> { | ||
Item( | ||
WrappedElementContent( | ||
represented: self, | ||
identifierValue: id | ||
), | ||
configure: configure | ||
) | ||
} | ||
|
||
/// Used by internal Listable methods to convert type-erased `Element` instances into `Item` instances. | ||
func toAnyItemConvertible() -> AnyItemConvertible { | ||
/// We use `type(of:)` to ensure we get the actual type, not just `Element`. | ||
WrappedElementContent( | ||
represented: self, | ||
identifierValue: ObjectIdentifier(type(of: self)) | ||
) | ||
} | ||
} | ||
|
||
|
||
public struct WrappedElementContent<ElementType:Element, IdentifierValue:Hashable> : BlueprintItemContent | ||
{ | ||
public let represented : ElementType | ||
|
||
public let identifierValue: IdentifierValue | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
false | ||
} | ||
|
||
public func element(with info: ApplyItemContentInfo) -> Element { | ||
represented | ||
} | ||
} | ||
|
||
|
||
extension WrappedElementContent where ElementType : Equatable { | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
represented == other.represented | ||
} | ||
|
||
public var reappliesToVisibleView: ReappliesToVisibleView { | ||
.ifNotEquivalent | ||
} | ||
} | ||
|
||
|
||
extension WrappedElementContent where ElementType : IsEquivalentContent { | ||
|
||
public func isEquivalent(to other: Self) -> Bool { | ||
represented.isEquivalent(to: other.represented) | ||
} | ||
|
||
public var reappliesToVisibleView: ReappliesToVisibleView { | ||
.ifNotEquivalent | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.