-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PStack and updates name to FlowStacks
- Loading branch information
1 parent
6b5ab66
commit f0e2293
Showing
21 changed files
with
732 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Pod::Spec.new do |s| | ||
|
||
s.name = 'FlowStacks' | ||
s.version = '0.0.7' | ||
s.summary = 'Hoist navigation state into a coordinator in SwiftUI.' | ||
|
||
s.description = <<-DESC | ||
FlowStacks allows you to hoist SwiftUI navigation or presentation state into a | ||
higher-level coordinator view. The coordinator pattern allows you to write isolated views | ||
that have zero knowledge of their context within an app. | ||
DESC | ||
|
||
s.homepage = 'https://github.com/johnpatrickmorgan/FlowStacks' | ||
s.license = { :type => 'MIT', :file => 'LICENSE' } | ||
s.author = { 'johnpatrickmorgan' => 'johnpatrickmorganuk@gmail.com' } | ||
s.source = { :git => 'https://github.com/johnpatrickmorgan/FlowStacks.git', :tag => s.version.to_s } | ||
s.social_media_url = 'https://twitter.com/jpmmusic' | ||
|
||
|
||
s.ios.deployment_target = '13.0' | ||
s.osx.deployment_target = '11.0' | ||
s.watchos.deployment_target = '7.0' | ||
s.tvos.deployment_target = '13.0' | ||
|
||
s.swift_version = '5.4' | ||
|
||
s.source_files = 'Sources/**/*' | ||
|
||
s.frameworks = 'Foundation', 'SwiftUI' | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
// swift-tools-version:5.3 | ||
// swift-tools-version:5.5 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "NStack", | ||
name: "FlowStacks", | ||
platforms: [ | ||
.iOS(.v13), .watchOS(.v7), .macOS(.v11), .tvOS(.v13), | ||
], | ||
products: [ | ||
.library( | ||
name: "NStack", | ||
targets: ["NStack"]), | ||
name: "FlowStacks", | ||
targets: ["FlowStacks"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "NStack", | ||
name: "FlowStacks", | ||
dependencies: []), | ||
.testTarget( | ||
name: "NStackTests", | ||
dependencies: ["NStack"]), | ||
name: "FlowStacksTests", | ||
dependencies: ["FlowStacks"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
|
||
/// A thin wrapper around an array. PFlow provides some convenience methods for presenting and dismissing. | ||
public struct PFlow<Screen> { | ||
|
||
/// The underlying array of screens. | ||
public internal(set) var array: [(Screen, PresentationOptions)] | ||
|
||
/// Initializes the stack with an empty array of screens. | ||
public init() { | ||
self.array = [] | ||
} | ||
|
||
/// Initializes the stack with a single root screen. | ||
/// - Parameter root: The root screen. | ||
public init(root: Screen) { | ||
self.array = [(root, .init(style: .default))] | ||
} | ||
|
||
/// Pushes a new screen onto the stack. | ||
/// - Parameter screen: The screen to present. | ||
/// - Parameter style: How to present the screen. | ||
/// - Parameter onDismiss: Called when the presented view is later | ||
/// dismissed. | ||
public mutating func present(_ screen: Screen, style: PresentationStyle = .default, onDismiss: (() -> Void)? = nil) { | ||
let options = PresentationOptions(style: style, onDismiss: onDismiss) | ||
array.append((screen, options)) | ||
} | ||
|
||
/// Dismisses the top screen off the stack. | ||
public mutating func dismiss() { | ||
array = array.dropLast() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
/// PStack maintains a stack of presented views for use within a `PresentationView`. | ||
public struct PStack<Screen, ScreenView: View>: View { | ||
|
||
/// The array of screens that represents the presentation stack. | ||
@Binding var stack: [(Screen, PresentationOptions)] | ||
|
||
/// A closure that builds a `ScreenView` from a `Screen`. | ||
@ViewBuilder var buildView: (Screen) -> ScreenView | ||
|
||
/// Initializer for creating an PStack using a binding to an array of screens. | ||
/// - Parameters: | ||
/// - stack: A binding to an array of screens. | ||
/// - buildView: A closure that builds a `ScreenView` from a `Screen`. | ||
public init(_ stack: Binding<[(Screen, PresentationOptions)]>, @ViewBuilder buildView: @escaping (Screen) -> ScreenView) { | ||
self._stack = stack | ||
self.buildView = buildView | ||
} | ||
|
||
public var body: some View { | ||
stack | ||
.enumerated() | ||
.reversed() | ||
.reduce(PresentationNode<Screen, ScreenView>.end) { presentedNode, new in | ||
let (index, (screen, options)) = new | ||
return PresentationNode<Screen, ScreenView>.view( | ||
buildView(screen), | ||
presenting: presentedNode, | ||
stack: $stack, | ||
index: index, | ||
options: options | ||
) | ||
} | ||
} | ||
} | ||
|
||
public extension PStack { | ||
|
||
/// Convenience initializer for creating an PStack using a binding to a `Stack` | ||
/// of screens. | ||
/// - Parameters: | ||
/// - stack: A binding to a stack of screens. | ||
/// - buildView: A closure that builds a `ScreenView` from a `Screen`. | ||
init(_ stack: Binding<PFlow<Screen>>, @ViewBuilder buildView: @escaping (Screen) -> ScreenView) { | ||
self._stack = Binding( | ||
get: { stack.wrappedValue.array }, | ||
set: { stack.wrappedValue.array = $0 } | ||
) | ||
self.buildView = buildView | ||
} | ||
} |
Oops, something went wrong.