OrderedState
public struct OrderedState<Substate> where Substate : Identifiable
extension OrderedState: MutableCollection
extension OrderedState: RandomAccessCollection
extension OrderedState: Equatable where Substate: Equatable
extension OrderedState: Decodable where Substate: Decodable
extension OrderedState: Encodable where Substate: Encodable
A container state that holds an ordered collection of substates.
It’s a common requirement to store a collection of substates. For example, a list of entities retrieved from a service.
For an optimal solution, you typically require a lookup table of entity states by their ids. However, you also need an ordered array
to display those entities in a list to the user. You end up managing both a dictionary of entities and an ordered array of their ids.
This struct manages that responsibility for you. It also provides conveniences for direct use by SwiftUI List views.
var todos: OrderedState<TodoState> = ...
// When a user adds a new todo:
todos.append(todo)
// When a user deletes multiple todos
todos.delete(at: indexSet)
// Using the OrderedState with a list view.
var body: some View {
List {
ForEach(todos) {
}
.onDelete { todos.delete(at: $0 }
.onMove { todos.move(from: $0, to: $1 }
}
}
-
Undocumented
Declaration
Swift
public typealias Id = Substate.ID -
Declaration
Swift
public typealias Index = Int -
The substates as an ordered array
Declaration
Swift
@inlinable public var values: [Substate] { get } -
The number of substates
Declaration
Swift
@inlinable public var count: Int { get } -
Create a new
OrderedStatewith an ordered array of identifiable substates.Declaration
Swift
@inlinable public init(_ values: [Substate])Parameters
valuesAn array of substates. The position of each substate will be used as the initial order.
-
Create a new
OrderedStatewith a variadic number of substates.Declaration
Swift
@inlinable public init(_ value: Substate...)Parameters
valueA variadic list of substates. The position of each substate will be used as the initial order.
-
Retrieves a value by its id.
The subscript API should be used in most cases. This method is provided in case the Substate’s id type is also an int.
Declaration
Swift
@inlinable public func value(forId id: Id) -> Substate?Parameters
idThe id of the substate.
Return Value
The substate if it exists.
-
Append a new substate to the end of the
OrderedState.Declaration
Swift
@inlinable public mutating func append(_ value: Substate)Parameters
valueA new substate to append to the end of the list.
-
Prepend a new substate to the beginning of the
OrderedState.Declaration
Swift
@inlinable public mutating func prepend(_ value: Substate)Parameters
valueA new substate to append to the beginning of the list.
-
Inserts a new substate at the given index
OrderedState.Declaration
Swift
@inlinable public mutating func insert(_ value: Substate, at index: Int) -
Inserts a collection of substates at the given index
OrderedState.Declaration
Swift
@inlinable public mutating func insert<C>(contentsOf values: C, at index: Int) where Substate == C.Element, C : Collection -
Removes a substate for the given id.
Declaration
Swift
@inlinable public mutating func remove(forId id: Id)Parameters
idThe id of the substate to remove. This will adjust the order of items.
-
Removes a substate at a given index.
Declaration
Swift
@inlinable public mutating func remove(at index: Int)Parameters
indexThe index of the substate to remove. This will adjust the order of items.
-
Removes substates at the provided indexes.
Declaration
Swift
@inlinable public mutating func remove(at indexSet: IndexSet)Parameters
indexSetRemoves all items in the provided indexSet. This will adjust the order of items.
-
Moves a set of substates at the specified indexes to a new index position.
Declaration
Swift
@inlinable public mutating func move(from indexSet: IndexSet, to index: Int) -
Resorts the order of substates with the given sort operation.
Declaration
Swift
@inlinable public mutating func sort(by areInIncreasingOrder: (Substate, Substate) -> Bool)Parameters
areInIncreasingOrderOrders the items by indicating whether not the second item is bigger than the first item.
-
Returns an
OrderedStatewith the new sort order.Declaration
Swift
@inlinable public func sorted(by areInIncreasingOrder: (Substate, Substate) -> Bool) -> OrderedState<Substate>Parameters
areInIncreasingOrderOrders the items by indicating whether not the second item is bigger than the first item.
Return Value
A new
OrderedStatewith the provided sort operation. -
Filters the substates using a predicate.
Declaration
Swift
@inlinable public func filter(_ isIncluded: (Substate) -> Bool) -> [Substate]Parameters
isIncludedIndicate the state should be included in the returned array.
Return Value
an array of substates filtered by the provided operation.
-
The starting index of the collection.
Declaration
Swift
@inlinable public var startIndex: Int { get } -
The last index of the collection.
Declaration
Swift
@inlinable public var endIndex: Int { get } -
Subscript based on the index of the substate.
Declaration
Swift
@inlinable public subscript(position: Int) -> Substate { get set }Parameters
positionThe index of the substate.
Return Value
The substate
-
Subscript based on the id of the substate.
Declaration
Swift
@inlinable public subscript(position: Id) -> Substate? { get set }Parameters
positionThe id of the substate.
Return Value
The substate
-
Create an ordered iterator of the substates.
Declaration
Swift
@inlinable public func makeIterator() -> IndexingIterator<[Substate]>Return Value
The ordered iterator.
-
Get the index of a substate after a sibling one.
Declaration
Swift
@inlinable public func index(after i: Int) -> IntParameters
iThe index of the substate directly before the target one.
Return Value
The next index
-
Declaration
Swift
@inlinable public static func == (lhs: OrderedState<Substate>, rhs: OrderedState<Substate>) -> Bool
-
Decodes the
OrderState<_>from an unkeyed container.This allows the
OrderedState<_>to be decoded from a simple array.Throws
This function throws an error if the OrderedState could not be decoded.Declaration
Swift
public init(from decoder: Decoder) throwsParameters
decoderThe decoder.
-
Encodes the
OrderState<_>as an unkeyed container of values.This allows the
OrderedState<_>to be encoded as simple array.Throws
This function throws an error if the OrderedState could not be encoded.Declaration
Swift
@inlinable public func encode(to encoder: Encoder) throwsParameters
encoderThe encoder.
View on GitHub
OrderedState Structure Reference