ernest0-production/screennavigatorkit
Framework that provide convenient environment for manage navigation in SwiftUI.
Pros:
- 🤢 No boolean flag such as
@State var isActive - 🤮 No
enumflag such as@State var route: RouteAction?with bigswitch-casestatement - 🤡 No implicit
UIKithacks withUIViewController - 💩 No singleton/shared/global presenter of application
🔩 Requirements
- iOS 14.0+
- Xcode 12.0+
- Swift 5.3+
🧐 How does it work?!
Framework has only two state object, each of which isolates "toggle-work" of @State var isActive: Bool and @State var isPresent: Bool flags.
2. `ModalStackController`
Like NavigationStackController, the ModalStackController control modal stack hierarchy and provide stack transformation using present and dismiss methods:
let modalStackController = ModalStackController()
// Standard usage
modalStackController.present(.sheet, Text("My View"))
modalStackController.present(.fullScreenCover, Text("Another View"))
modalStackController.dismiss()
modalStackController.dismissAll()
// Advanced usage
enum Screen: Hashable {
case detail
...
}
modalStackController.present(.sheet, tag: Screen.detail, DetailView())
modalStackController.dismiss(to: Screen.detail)🚧 NOTE: SwiftUI does not allow to dismiss multiple views at once! Therefore, methods such as dismissAll() or dismiss(to:)/dismiss(from:) will close all views sequentially.
Its companion is ModalStackView that bind ModalStackController with it:
struct ExampleApp: App {
@StateObject var modalStackController = ModalStackController()
var body: some Scene {
WindowGroup {
ModalStackView(modalStackController) {
RootView()
}
}
}
}Any presented view has access to ModalStackController through EnvironmentObject too:
struct RootView: View {
@EnvironmentObject var modalStackController: ModalStackController
var body: some View {
VStack {
Text("Home screen")
Button("FAQ") {
modalStackController.present(.sheet, FAQView())
}
Button("Authorize") {
modalStackController.present(.fullScreenCover, LoginView())
}
}
}
}💫 Just like in
NavigationStackControlleryou can tag presented views when present withModalStackController
API
NavigationStackController
- push
- push(tag:)
- pop
- pop(tag:)
- popLast(_ k:)
- popToRoot
ModalStackController
- present(_ presentationStyle:)
- present(_ presentationStyle:tag:)
- dismiss
- dismiss(tag:)
- dismissLast(_ k:)
- dismissAll
PresentationStyle
- sheet - fullScreenCover
FAQ
Can i mix this framework with existing navigation approach in my project?
Yes, you can. The framework does not affect navigation built in other ways, such as through the standard @State var isActive: Bool flags or through UIKit hacks.\ NavigationStackController and ModalStackController create local state and manage only their own state.
What about Alert?
Unfortunately, the framework does not support such a mechanism for working with Alert, BUT you can implement it yourself by analogy with ModalStackController.\ Your project can have many different custom presentations (popup, snackbar, toast, notifications) and each of them require specific logic for handle hierarchy, depending on their implementation.\ So adding new presentation methods to the framework is not planned.
📦 Installation
Swift Package Manager
Create a Package.swift file.
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/Ernest0-Production/ScreenNavigatorKit.git", from: "0.0.3")
],
targets: [
.target(name: "YOUR_TARGET_NAME", dependencies: ["ScreenNavigatorKit"])
]
)Credits
License
ScreenNavigatorKit is released under the MIT license. See LICENSE for details.
Package Metadata
Repository: ernest0-production/screennavigatorkit
Default branch: main
README: README.md