Skip to content

LoadableListView

m-housh edited this page Aug 20, 2021 · 1 revision

LoadableListView

A loadable view that shows a list of items once they've been loaded.

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public struct LoadableListView<
  Element: Equatable,
  Id: Hashable,
  Failure: Error,
  Row: View
>: View where Failure: Equatable 

Example:

struct User: Equatable, Identifiable {
  let id: UUID = UUID()
  var name: String

  static let blob = User.init(name: "blob")
  static let blobJr = User.init(name: "blob-jr")
  static let blobSr = User.init(name: "blob-sr")
}

enum LoadError: Error, Equatable {
  case loadingFailed
}

extension LoadableListViewEnvironment where Element == User, LoadRequest == EmptyLoadRequest, Failure == LoadError {
  static let users = Self.init(
    load: { _ in
       Just([User.blob, .blobJr, .blobSr])
         .delay(for: .seconds(1), scheduler: DispatchQueue.main) // simulate a database call
         .setFailureType(to: LoadError.self)
         .eraseToEffect()
    },
    mainQueue: .main
  )
}

let usersReducer = Reducer<
   LoadableListViewStateFor<User, LoadError>,
  LoadableListViewActionFor<User, LoadError>,
   LoadableListViewEnvironmentFor<User, EmptyLoadRequest, LoadError>
>.empty
  .loadableList(
     state: \.self,
     action: /LoadableListViewActionFor<User, LoadError>.self,
     environment: { $0 }
  )

struct LoadableListViewPreviewWithEditModeButton: View {
  let store: Store<LoadableListViewStateFor<User, LoadError>, LoadableListViewActionFor<User, LoadError>>

  var body: some View {
    NavigationView {
       LoadableListView(store: store, autoLoad: true) { user in
         Text(user.name)
       }
       .toolbar {
          ToolbarItemGroup(placement: .confirmationAction) {
             EditButton(
              store: store.scope(state: \.editMode, action: LoadableListViewAction.editMode)
             )
          }
      }
     }
  }
}

Inheritance

View

Initializers

init(store:autoLoad:id:row:)

Create a new loadable list view.

public init(
    store: Store<
      LoadableListViewStateFor<Element, Failure>, LoadableListViewActionFor<Element, Failure>
    >,
    autoLoad: Bool = true,
    id: KeyPath<Element, Id>,
    @ViewBuilder row: @escaping (Element) -> Row
  ) 

Parameters

  • store: The store for the view state.
  • autoLoad: Whether we automatically load items when the view first appears.
  • id: The id used to identify the row.
  • row: The view builder for an individual row in the list.

Properties

store

public let store:
    Store<LoadableListViewStateFor<Element, Failure>, LoadableListViewActionFor<Element, Failure>>

body

public var body: some View 
Clone this wiki locally