-
Notifications
You must be signed in to change notification settings - Fork 0
LoadableListView
m-housh edited this page Aug 20, 2021
·
1 revision
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)
)
}
}
}
}
}
View
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
)
- 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.
public let store:
Store<LoadableListViewStateFor<Element, Failure>, LoadableListViewActionFor<Element, 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