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 }
  }
}

  • Id

    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 OrderedState with an ordered array of identifiable substates.

    Declaration

    Swift

    @inlinable
    public init(_ values: [Substate])

    Parameters

    values

    An array of substates. The position of each substate will be used as the initial order.

  • Create a new OrderedState with a variadic number of substates.

    Declaration

    Swift

    @inlinable
    public init(_ value: Substate...)

    Parameters

    value

    A 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

    id

    The 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

    value

    A 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

    value

    A 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

    id

    The 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

    index

    The 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

    indexSet

    Removes 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

    areInIncreasingOrder

    Orders the items by indicating whether not the second item is bigger than the first item.

  • Returns an OrderedState with the new sort order.

    Declaration

    Swift

    @inlinable
    public func sorted(by areInIncreasingOrder: (Substate, Substate) -> Bool) -> OrderedState<Substate>

    Parameters

    areInIncreasingOrder

    Orders the items by indicating whether not the second item is bigger than the first item.

    Return Value

    A new OrderedState with the provided sort operation.

  • Filters the substates using a predicate.

    Declaration

    Swift

    @inlinable
    public func filter(_ isIncluded: (Substate) -> Bool) -> [Substate]

    Parameters

    isIncluded

    Indicate 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

    position

    The 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

    position

    The 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) -> Int

    Parameters

    i

    The index of the substate directly before the target one.

    Return Value

    The next index

  • Get the id of a substate directly after a sibling one.

    Declaration

    Swift

    @inlinable
    public func index(after i: Id) -> Id

    Parameters

    i

    The id of the substate directly before the target one.

    Return Value

    The next id

Available where Substate: Equatable

  • Declaration

    Swift

    @inlinable
    public static func == (lhs: OrderedState<Substate>, rhs: OrderedState<Substate>) -> Bool

Available where Substate: Decodable

  • 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) throws

    Parameters

    decoder

    The decoder.

Available where Substate: Encodable

  • 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) throws

    Parameters

    encoder

    The encoder.