Skip to content

LoadablePicker

m-housh edited this page Aug 23, 2021 · 2 revisions

LoadablePicker

A picker whose elements can be loaded.

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public struct LoadablePicker<
  Element,
  Id: Hashable,
  Failure: Error,
  Row: View
>: View where Failure: Equatable, Element: 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 usersPickerReducer = Reducer<
   LoadablePickerState<User, LoadError>,
   LoadablePickerAction<User, LoadError>,
   LoadablePickerEnvironmentFor<User, LoadError>
>.empty
  .loadablePicker(
     state: \.self,
     action: /LoadablePickerAction.self,
     environment: { $0 }
  )

struct LoadableUserPicker: View {
  let store: Store<LoadablePickerState<User, LoadError>, LoadablePickerAction<User, LoadError>>

  var body: some View {
    NavigationView {
      Form {
        LoadablePicker(
         "User",
         store: store,
         allowNilSelection: true
        ) { user in
          Text(user.name)
       }
     }
    }
  }
}

Inheritance

View

Initializers

init(id:store:allowNilSelection:autoLoad:title:nilSelectionTitle:row:)

Create a new loadable picker view.

public init(
    id: KeyPath<Element, Id>,
    store: Store<
      LoadablePickerState<Element, Id, Failure>, LoadablePickerAction<Element, Id, Failure>
    >,
    allowNilSelection: Bool = false,
    autoLoad: Bool = true,
    title: @escaping (LoadablePickerState<Element, Id, Failure>) -> String = { _ in "" },
    nilSelectionTitle: String? = "None",
    @ViewBuilder row: @escaping (Element) -> Row
  ) 

Parameters

  • store: The store for the view.
  • allowNilSelection: Flag for if we allow a nil selection.
  • autoLoad: Flag for if we automatically load items when the view appears.
  • title: The picker title based on the current state.
  • nilSelectionTitle: The title used for a row used to set the selection to nil. Will default to "None" if not supplied.
  • row: Creates a view for an element.

init(_:id:store:allowNilSelection:autoLoad:nilSelectionTitle:row:)

Create a new loadable picker view.

public init(
    _ title: String,
    id: KeyPath<Element, Id>,
    store: Store<
      LoadablePickerState<Element, Id, Failure>, LoadablePickerAction<Element, Id, Failure>
    >,
    allowNilSelection: Bool = false,
    autoLoad: Bool = true,
    nilSelectionTitle: String? = nil,
    @ViewBuilder row: @escaping (Element) -> Row
  ) 

Parameters

  • title: The picker title
  • store: The store for the view.
  • allowNilSelection: Flag for if we allow a nil selection.
  • autoLoad: Flag for if we automatically load items when the view appears.
  • nilSelectionTitle: The title used for a row used to set the selection to nil. Will default to "None" if not supplied.
  • row: Creates a view for an element.

init(store:allowNilSelection:autoLoad:title:nilSelectionTitle:row:)

public init(
    store: Store<
      LoadablePickerStateFor<Element, Failure>, LoadablePickerActionFor<Element, Failure>
    >,
    allowNilSelection: Bool = false,
    autoLoad: Bool = true,
    title: @escaping (LoadablePickerStateFor<Element, Failure>) -> String = { _ in "" },
    nilSelectionTitle: String? = "None",
    @ViewBuilder row: @escaping (Element) -> Row
  ) where Element: Identifiable, Id == Element.ID 

init(_:store:allowNilSelection:autoLoad:nilSelectionTitle:row:)

Create a new loadable picker view.

public init(
    _ title: String,
    store: Store<
      LoadablePickerStateFor<Element, Failure>, LoadablePickerActionFor<Element, Failure>
    >,
    allowNilSelection: Bool = false,
    autoLoad: Bool = true,
    nilSelectionTitle: String? = nil,
    @ViewBuilder row: @escaping (Element) -> Row
  ) where Element: Identifiable, Id == Element.ID 

Parameters

  • title: The picker title
  • store: The store for the view.
  • allowNilSelection: Flag for if we allow a nil selection.
  • autoLoad: Flag for if we automatically load items when the view appears.
  • nilSelectionTitle: The title used for a row used to set the selection to nil. Will default to "None" if not supplied.
  • row: Creates a view for an element.

Properties

store

The store for the view.

public let store:
    Store<LoadablePickerState<Element, Id, Failure>, LoadablePickerAction<Element, Id, Failure>>

body

public var body: some View 
Clone this wiki locally