-
Notifications
You must be signed in to change notification settings - Fork 0
LoadablePicker
m-housh edited this page Aug 23, 2021
·
2 revisions
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)
}
}
}
}
}
View
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
)
- 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.
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
)
- 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.
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
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
- 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.
The store for the view.
public let store:
Store<LoadablePickerState<Element, Id, Failure>, LoadablePickerAction<Element, Id, Failure>>
public var body: some View
Generated at 2021-08-23T22:43:49+0000 using swift-doc 1.0.0-rc.1.
Types
- EditButton
- EditMode
- EditModeAction
- EmptyLoadRequest
- ListAction
- LoadError
- Loadable
- LoadableAction
- LoadableEnvironment
- LoadableForEachAction
- LoadableForEachEnvironment
- LoadableForEachState
- LoadableForEachStore
- LoadableList
- LoadableListAction
- LoadableListEnvironment
- LoadableListState
- LoadablePicker
- LoadablePickerAction
- LoadablePickerEnvironment
- LoadablePickerState
- LoadableView
- User
- UserAction