OrderedState

public struct OrderedState<Substate> : StateType where Substate : IdentifiableState

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

    public var values: [Substate] { get }
  • The number of substates

    Declaration

    Swift

    public var count: Int { get }
  • Create a new OrderedState with an ordered array of identifiable substates.

    Declaration

    Swift

    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

    public init(_ value: Substate...)

    Parameters

    value

    A variadic list of substates. The position of each substate will be used as the initial order.

  • Append a new substate to the end of the OrderedState.

    Declaration

    Swift

    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

    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

    public mutating func insert(_ value: Substate, at index: Int)
  • Inserts a collection of substates at the given index OrderedState.

    Declaration

    Swift

    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

    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

    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

    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

    public mutating func move(from indexSet: IndexSet, to index: Int)
  • Resorts the order of substates with the given sort operation.

    Declaration

    Swift

    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

    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

    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

    public var startIndex: Int { get }
  • The last index of the collection.

    Declaration

    Swift

    public var endIndex: Int { get }
  • Subscript based on the index of the substate.

    Declaration

    Swift

    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

    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

    public  func makeIterator() -> IndexingIterator<[Substate]>

    Return Value

    The ordered iterator.

  • Get the index of a substate after a sibling one.

    Declaration

    Swift

    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

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

    Parameters

    i

    The id of the substate directly before the target one.

    Return Value

    The next id

  • Declaration

    Swift

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