Skip to content

Commit

Permalink
Merge pull request #2 from teufelaudio/UpdateFromUIExtensions
Browse files Browse the repository at this point in the history
Update from UI extensions
  • Loading branch information
melle authored Mar 23, 2021
2 parents 9261525 + 564ac5e commit f79c1a8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
47 changes: 44 additions & 3 deletions Sources/CombineRextensions/StoreSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,49 @@ extension View {
path: KeyPath<State, Context?>,
dismissAction: Action,
producer: ViewProducer<Context, ContentView>) -> some View {
return sheet(item: store.binding[path],
onDismiss: { store.dispatch(dismissAction) },
content: producer.view)
return sheet(
item: store.binding[path],
onDismiss: { store.dispatch(dismissAction) },
content: producer.view
)
}
}

// MARK: - Sheets from Store
extension View {
/// Presents a sheet when the given `Context` key path is non-nil. The view that will be shown
/// is created by the given `ViewProducer`.
///
/// - Parameters:
/// - store: The view model containing the state.
/// - path: The key path pointing to the element to show as sheet.
/// - dismissAction: Action that should be dispatched to the store when a modal is dismissed.
/// - producer: The ViewProducer that transforms the given `Context` into a View.
public func storeSheet<Action, State, Context: Identifiable, ContentView: View>(
_ store: ObservableViewModel<Action, State>,
path: KeyPath<State, Context?>,
dismissAction: Action,
producer: ViewProducer<Context, ContentView>,
file: String = #file,
function: String = #function,
line: UInt = #line,
info: String? = nil) -> some View {
return sheet(
item: store.binding[path],
onDismiss: {
withAnimation {
store.dispatch(
dismissAction,
from: ActionSource(
file: file,
function: function,
line: line,
info: (info.map { $0 + " -> " } ?? "") + "View+StoreSheet.swift onDismiss closure"
)
)
}
},
content: producer.view
)
}
}
14 changes: 14 additions & 0 deletions Sources/CombineRextensions/ViewProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ extension ViewProducer where ProducedView == EmptyView {
.init { _ in EmptyView() }
}
}

extension ViewProducer where ProducedView == AnyView {
public static func pure() -> ViewProducer {
.init { _ in AnyView(EmptyView()) }
}
}

#if DEBUG
extension ViewProducer {
public static var crash: ViewProducer {
. init { _ in fatalError("Debug ViewProducer crash") }
}
}
#endif

0 comments on commit f79c1a8

Please sign in to comment.