diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..8116b40 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +buy_me_a_coffee: iletai +custom: ["https://www.paypal.me/iletai"] diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..7386051 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: Question or idea + url: https://github.com/iletai/BaseNavigationStack/discussions + about: Ask questions and discuss ideas in GitHub Discussions. + - name: Security vulnerability + url: https://github.com/iletai/BaseNavigationStack/security/advisories/new + about: Report security issues privately. diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md deleted file mode 100644 index 48d5f81..0000000 --- a/.github/ISSUE_TEMPLATE/custom.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -name: Custom issue template -about: Describe this issue template's purpose here. -title: '' -labels: '' -assignees: '' - ---- - - diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..2a58ed8 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,18 @@ +## Description + + +## Motivation & Context + + +## Types of changes +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] Documentation / CI update + +## Checklist: +- [ ] My code follows the code style of this project. +- [ ] My change requires a change to the documentation. +- [ ] I have updated the documentation accordingly. +- [ ] I have added tests to cover my changes. +- [ ] All new and existing tests passed (`swift test`). diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5ace460 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..4839e07 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,114 @@ +name: CI + +on: + push: + branches: [main, develop, 'release/**', 'hotfix/**'] + pull_request: + branches: [main, develop, 'release/**', 'hotfix/**'] + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +env: + SWIFTLINT_VERSION: 0.65.1 + +jobs: + lint: + name: SwiftLint + runs-on: macos-26 + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + + - name: Install SwiftLint ${{ env.SWIFTLINT_VERSION }} + run: | + curl -sSL -o swiftlint.zip "https://github.com/realm/SwiftLint/releases/download/${SWIFTLINT_VERSION}/portable_swiftlint.zip" + unzip -q swiftlint.zip -d "$RUNNER_TEMP/swiftlint" + echo "$RUNNER_TEMP/swiftlint" >> "$GITHUB_PATH" + + - name: Run SwiftLint + run: swiftlint --strict --quiet + + test-macos: + name: macOS Tests + runs-on: macos-26 + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + + - name: Select Xcode + uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + + - name: Run Tests + run: swift test --enable-code-coverage + + test-ios: + name: iOS Simulator Tests + runs-on: macos-26 + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + + - name: Select Xcode + uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + + - name: Run iOS Simulator Tests + run: | + SIM_ID=$(xcrun simctl list devices available | grep -E "iPhone" | head -1 | grep -o -E "[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}") + if [ -z "$SIM_ID" ]; then + echo "::error::No iPhone simulator available" + exit 1 + fi + echo "Selected simulator ID: $SIM_ID" + xcodebuild test \ + -scheme BaseNavigationStack \ + -destination "id=$SIM_ID" \ + CODE_SIGNING_ALLOWED=NO + + build-platforms: + name: Build (${{ matrix.platform }}) + runs-on: macos-26 + strategy: + fail-fast: false + matrix: + platform: [tvOS, watchOS, visionOS] + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + + - name: Select Xcode + uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + + - name: Build Package + run: | + xcodebuild build \ + -scheme BaseNavigationStack \ + -destination "generic/platform=${{ matrix.platform }}" \ + CODE_SIGNING_ALLOWED=NO + + build-example: + name: Build Example App + runs-on: macos-26 + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + + - name: Select Xcode + uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + + - name: Build Example + run: | + xcodebuild build \ + -project NavigationStackExample/NavigationStackExample.xcodeproj \ + -scheme NavigationStackExample \ + -destination 'generic/platform=iOS Simulator' \ + CODE_SIGNING_ALLOWED=NO diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..e0a7e97 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,62 @@ +name: Release + +on: + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+*' + - '[0-9]+.[0-9]+.[0-9]+*' + +permissions: + contents: write + +jobs: + release: + name: GitHub Release + runs-on: macos-26 + steps: + - name: Checkout Repository + uses: actions/checkout@v7 + with: + fetch-depth: 0 + + - name: Verify Tag Is On main + run: | + git fetch origin main + if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then + echo "::error::Tag ${GITHUB_REF_NAME} is not on main. Release from main only (see CONTRIBUTING.md)." + exit 1 + fi + + - name: Extract Release Notes From CHANGELOG + run: | + VERSION="${GITHUB_REF_NAME#v}" + awk -v ver="$VERSION" ' + index($0, "## [" ver "]") == 1 { found = 1; next } + found && /^## \[/ { exit } + found && /^\[[^]]+\]: / { exit } + found { print } + ' CHANGELOG.md > "$RUNNER_TEMP/notes.md" + if [ ! -s "$RUNNER_TEMP/notes.md" ]; then + echo "::error::No '## [$VERSION]' section in CHANGELOG.md" + exit 1 + fi + cat "$RUNNER_TEMP/notes.md" + + - name: Select Xcode + uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: latest-stable + + - name: Run Tests + run: swift test + + - name: Build Package for Release + run: swift build -c release + + - name: Create Release + uses: softprops/action-gh-release@v3 + with: + body_path: ${{ runner.temp }}/notes.md + prerelease: ${{ contains(github.ref_name, '-') }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.spi.yml b/.spi.yml new file mode 100644 index 0000000..85e03b8 --- /dev/null +++ b/.spi.yml @@ -0,0 +1,4 @@ +version: 1 +builder: + configs: + - documentation_targets: [BaseNavigationStack] diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..3febe17 --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,17 @@ +disabled_rules: + - trailing_whitespace + - line_length + - function_body_length + - type_body_length + +opt_in_rules: + - empty_count + - empty_string + +included: + - Sources + - Tests + +excluded: + - .build + - NavigationStackExample diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..06cd04f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,37 @@ +# Changelog + +All notable changes to this project are documented here. Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow [SemVer](https://semver.org). + +## [Unreleased] + +## [2.0.0] - 2026-11-02 + +### Added +- `NavigationRouter`: one `@MainActor @Observable` router for stack, sheet, and cover. +- `BaseNavigationContainer` and `View.navigationRouter(...)`. +- `maxDepth` stack cap (default 30) and duplicate-top push guard. +- `push`, `replacePath`, `present(sheet:)`, `present(cover:)` return `Bool` (`@discardableResult`) and log rejections via `os.Logger`. +- `send(_:)` returns its `Task` so callers can await the main-actor mutation. +- tvOS 17, watchOS 10, and visionOS 1 declared in `Package.swift`. + +### Changed +- **Breaking:** `swift-tools-version` 6.0 (Xcode 16+), Swift 6 language mode; minimums iOS 17 / macOS 14. +- Because `push`/`present`/`replacePath` now return `Bool`, generic single-expression closures such as `MainActor.run { router.push(x) }` infer `Bool` and warn about an unused result. Call `await router.push(x)` directly, or write `_ = router.push(x)`. +- macOS presents covers as sheets instead of ignoring them. +- A sheet and a cover can no longer be presented at the same time through the router API. +- `maxDepth` is clamped to `>= 1` on every assignment (assigning a negative value previously crashed `replacePath`); `initialPath` is truncated to `maxDepth`. + +### Removed +- **Breaking:** public `Collection.subscript(safe:)` and `Array.removeLastSafe()`. +- Redundant `@unchecked Sendable` on `@MainActor` classes. + +### Fixed +- v1 `BaseNavigationStack.dismiss()` pops a single-screen stack; `replaceNavigationStack(path:)` clears active modals. + +## [1.0.3] - 2023-12-24 + +Last 1.x release (`BaseNavigationStack` + `BaseViewProtocol`). + +[Unreleased]: https://github.com/iletai/BaseNavigationStack/compare/2.0.0...develop +[2.0.0]: https://github.com/iletai/BaseNavigationStack/compare/1.0.3...2.0.0 +[1.0.3]: https://github.com/iletai/BaseNavigationStack/releases/tag/1.0.3 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..3e1a68b --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,85 @@ + +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement by contacting the maintainer [@iletai](https://github.com/iletai) through a [private GitHub security advisory](https://github.com/iletai/BaseNavigationStack/security/advisories/new) or direct message. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c4eed53 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,47 @@ +# Contributing + +Thanks for helping improve BaseNavigationStack. Please read the [Code of Conduct](CODE_OF_CONDUCT.md) first. + +## Branches + +The project uses Git Flow: + +| Branch | Purpose | +| --- | --- | +| `develop` | Integration branch and default branch. All pull requests target `develop`. | +| `release/x.y.z` | Short-lived branch that freezes a version: CHANGELOG date and docs only, no new features. | +| `main` | Stable branch. Only receives merges from `release/*` (or `hotfix/*`). Every commit on `main` is a released version. | +| `feature/*`, `fix/*`, `docs/*`, `chore/*` | Work branches, created from `develop`. | +| `hotfix/x.y.z` | Urgent fix created from `main`, merged into `main` and back into `develop`. | + +## Pull requests + +1. Branch from `develop`. +2. Use [Conventional Commits](https://www.conventionalcommits.org) (`feat:`, `fix:`, `docs:`, `chore:`, `ci:`, `refactor:`, `test:`). +3. Add or update tests, and add an entry under `## [Unreleased]` in [CHANGELOG.md](CHANGELOG.md) for user-visible changes. +4. Before pushing: + ```bash + swift test + swiftlint --strict + ``` +5. CI must pass (macOS, iOS Simulator, tvOS, watchOS, visionOS, example app, SwiftLint). + +## Versioning + +[Semantic Versioning](https://semver.org). Tags have no `v` prefix (`2.0.0`, `2.0.1`). Removing or changing public API requires a major version. + +## Release process (maintainers) + +1. From an up-to-date `develop`, create `release/x.y.z`. +2. In `CHANGELOG.md`, rename `## [Unreleased]` to `## [x.y.z] - YYYY-MM-DD` and add a fresh empty `## [Unreleased]` above it. +3. Open a pull request `release/x.y.z` → `main`. After CI passes, merge with a **merge commit**. +4. Tag the merge commit on `main` and push the tag: + ```bash + git checkout main && git pull + git tag -a x.y.z -m "x.y.z" + git push origin x.y.z + ``` + The `Release` workflow checks that the tag is on `main` and listed in `CHANGELOG.md`, runs the tests, and publishes a GitHub Release with that CHANGELOG section as notes. +5. Merge `main` back into `develop` (pull request `main` → `develop`) so the CHANGELOG date reaches `develop`. + +Never move or delete a published tag: Swift Package Manager caches resolved versions. diff --git a/NavigationStackExample/NavigationStackExample.xcodeproj/project.pbxproj b/NavigationStackExample/NavigationStackExample.xcodeproj/project.pbxproj index b8ee5c7..a528775 100644 --- a/NavigationStackExample/NavigationStackExample.xcodeproj/project.pbxproj +++ b/NavigationStackExample/NavigationStackExample.xcodeproj/project.pbxproj @@ -12,11 +12,6 @@ 3E7A81C72B2B06C70003D5A3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3E7A81C62B2B06C70003D5A3 /* Assets.xcassets */; }; 3E7A81CA2B2B06C70003D5A3 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3E7A81C92B2B06C70003D5A3 /* Preview Assets.xcassets */; }; 3E7A81D52B2B07C50003D5A3 /* BaseNavigationStack in Frameworks */ = {isa = PBXBuildFile; productRef = 3E7A81D42B2B07C50003D5A3 /* BaseNavigationStack */; }; - 3E7A81D72B2B0F1A0003D5A3 /* ViewNavigationTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E7A81D62B2B0F1A0003D5A3 /* ViewNavigationTarget.swift */; }; - 3E7A81DC2B2B0FA90003D5A3 /* View+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E7A81DB2B2B0FA90003D5A3 /* View+Extension.swift */; }; - 3E7A81E02B2B0FE50003D5A3 /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E7A81DF2B2B0FE50003D5A3 /* ListView.swift */; }; - 3E7A81E22B2B10040003D5A3 /* BaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E7A81E12B2B10040003D5A3 /* BaseView.swift */; }; - 3E7A81E42B2C50A80003D5A3 /* PolicyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E7A81E32B2C50A80003D5A3 /* PolicyView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -25,11 +20,6 @@ 3E7A81C42B2B06C60003D5A3 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 3E7A81C62B2B06C70003D5A3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 3E7A81C92B2B06C70003D5A3 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; - 3E7A81D62B2B0F1A0003D5A3 /* ViewNavigationTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewNavigationTarget.swift; sourceTree = ""; }; - 3E7A81DB2B2B0FA90003D5A3 /* View+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Extension.swift"; sourceTree = ""; }; - 3E7A81DF2B2B0FE50003D5A3 /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = ""; }; - 3E7A81E12B2B10040003D5A3 /* BaseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseView.swift; sourceTree = ""; }; - 3E7A81E32B2C50A80003D5A3 /* PolicyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PolicyView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -63,9 +53,6 @@ 3E7A81C12B2B06C60003D5A3 /* NavigationStackExample */ = { isa = PBXGroup; children = ( - 3E7A81DE2B2B0FD90003D5A3 /* View */, - 3E7A81DD2B2B0FBE0003D5A3 /* BaseDefineTarget */, - 3E7A81DA2B2B0F4D0003D5A3 /* Utils */, 3E7A81C22B2B06C60003D5A3 /* NavigationStackExampleApp.swift */, 3E7A81C42B2B06C60003D5A3 /* ContentView.swift */, 3E7A81C62B2B06C70003D5A3 /* Assets.xcassets */, @@ -82,32 +69,6 @@ path = "Preview Content"; sourceTree = ""; }; - 3E7A81DA2B2B0F4D0003D5A3 /* Utils */ = { - isa = PBXGroup; - children = ( - 3E7A81DB2B2B0FA90003D5A3 /* View+Extension.swift */, - ); - path = Utils; - sourceTree = ""; - }; - 3E7A81DD2B2B0FBE0003D5A3 /* BaseDefineTarget */ = { - isa = PBXGroup; - children = ( - 3E7A81D62B2B0F1A0003D5A3 /* ViewNavigationTarget.swift */, - ); - path = BaseDefineTarget; - sourceTree = ""; - }; - 3E7A81DE2B2B0FD90003D5A3 /* View */ = { - isa = PBXGroup; - children = ( - 3E7A81DF2B2B0FE50003D5A3 /* ListView.swift */, - 3E7A81E12B2B10040003D5A3 /* BaseView.swift */, - 3E7A81E32B2C50A80003D5A3 /* PolicyView.swift */, - ); - path = View; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -185,12 +146,7 @@ buildActionMask = 2147483647; files = ( 3E7A81C52B2B06C60003D5A3 /* ContentView.swift in Sources */, - 3E7A81D72B2B0F1A0003D5A3 /* ViewNavigationTarget.swift in Sources */, - 3E7A81E02B2B0FE50003D5A3 /* ListView.swift in Sources */, - 3E7A81DC2B2B0FA90003D5A3 /* View+Extension.swift in Sources */, - 3E7A81E22B2B10040003D5A3 /* BaseView.swift in Sources */, 3E7A81C32B2B06C60003D5A3 /* NavigationStackExampleApp.swift in Sources */, - 3E7A81E42B2C50A80003D5A3 /* PolicyView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -340,7 +296,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.baseNavigationStack.NavigationStackExample; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; + SWIFT_VERSION = 6.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -369,7 +325,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.baseNavigationStack.NavigationStackExample; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; + SWIFT_VERSION = 6.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/NavigationStackExample/NavigationStackExample/BaseDefineTarget/ViewNavigationTarget.swift b/NavigationStackExample/NavigationStackExample/BaseDefineTarget/ViewNavigationTarget.swift deleted file mode 100644 index a394cf7..0000000 --- a/NavigationStackExample/NavigationStackExample/BaseDefineTarget/ViewNavigationTarget.swift +++ /dev/null @@ -1,111 +0,0 @@ -// -// ViewNavigationTarget.swift -// NavigationStackExample -// -// Created by iletai on 14/12/2023. -// - -import Foundation -import BaseNavigationStack -import SwiftUI - -public enum ViewNavigationTarget: BaseViewProtocol { - public func withNavigationDestination() -> some View { - switch self { - case .policy(_): - PolicyView() - case .list: - ListView() - case .splash: - SplashView() - default: - EmptyView() - } - } - - public static func == (lhs: ViewNavigationTarget, rhs: ViewNavigationTarget) -> Bool { - lhs.id == rhs.id - } - - public var id: String { - switch self { - case .policy(_): - return "policy" - case .updateProfile(_): - return "updateProfile" - case .splash: - return "splash" - case .main: - return "main" - case .list: - return "list" - } - } - - case splash - case main - case list - case policy(URL) - case updateProfile(() -> Void?) - - public func hash(into hasher: inout Hasher) { - switch self { - case .splash: - hasher.combine("splash") - case .main: - hasher.combine("main") - case .list: - hasher.combine("list") - case .policy(let url): - hasher.combine("policy") - hasher.combine(url) - case .updateProfile(let closure): - hasher.combine("updateProfile") - hasher.combine(ObjectIdentifier(closure as AnyObject)) - } - } - - public func withSheetDestination() -> some View { - switch self { - case .policy(_): - PolicyView() - .presentationDetents(self.detent) - case .list: - ListView() - .presentationDetents(self.detent) - case .splash: - SplashView() - .presentationDetents(self.detent) - default: - EmptyView() - } - } - - public func withFullScreenDestination() -> some View { - switch self { - case .policy(_): - PolicyView() - case .list: - ListView() - case .splash: - SplashView() - default: - EmptyView() - } - } - - public var detent: Set { - switch self { - case .splash: - return [.large] - case .main: - return [.large] - case .list: - return [.large] - case .policy(_): - return [.large] - case .updateProfile(_): - return [.large] - } - } -} diff --git a/NavigationStackExample/NavigationStackExample/ContentView.swift b/NavigationStackExample/NavigationStackExample/ContentView.swift index f8247d0..6285d2b 100644 --- a/NavigationStackExample/NavigationStackExample/ContentView.swift +++ b/NavigationStackExample/NavigationStackExample/ContentView.swift @@ -7,19 +7,293 @@ import SwiftUI import BaseNavigationStack -import Foundation -typealias BaseNavigation = BaseNavigationStack +// MARK: - Routes Definition + +enum DemoRoute: Hashable, Sendable { + case step(Int) + case detail(title: String) +} + +struct DemoSheet: Identifiable, Hashable, Sendable { + let id: String + let title: String +} + +struct DemoCover: Identifiable, Hashable, Sendable { + let id: String +} + +typealias DemoRouter = NavigationRouter + +// MARK: - Root Interactive Showcase View + struct ContentView: View { - @State var baseNavigation = BaseNavigation(isPresented: .constant(nil)) + @State private var router = DemoRouter() + @State private var selectedTab = 0 + + var body: some View { + TabView(selection: $selectedTab) { + // Tab 1: Stack Explorer + BaseNavigationContainer( + router: router, + content: { + StackExplorerView() + }, + destination: { route in + switch route { + case .step(let count): + StepDetailView(stepIndex: count) + case .detail(let title): + VStack(spacing: 16) { + Text("Detail View: \(title)") + .font(.title2) + Button("Dismiss Screen") { + router.dismiss() + } + .buttonStyle(.borderedProminent) + } + } + }, + sheet: { sheet in + NestedSheetContainerView(sheet: sheet) + }, + cover: { cover in + VStack(spacing: 20) { + Text("Full Screen Cover: \(cover.id)") + .font(.largeTitle) + Button("Dismiss Cover") { + router.dismiss() + } + .buttonStyle(.borderedProminent) + } + .padding() + } + ) + .tabItem { + Label("Stack", systemImage: "square.stack.3d.up") + } + .tag(0) + + // Tab 2: Async & Concurrency Lab + AsyncLabView() + .tabItem { + Label("Async Lab", systemImage: "bolt.horizontal") + } + .tag(1) + } + } +} + +// MARK: - Stack Explorer + +struct StackExplorerView: View { + @Environment(DemoRouter.self) private var router + + var body: some View { + VStack(spacing: 20) { + Text("BaseNavigationStack 2.0") + .font(.headline) + Text("Current Stack Depth: \(router.path.count) / \(router.maxDepth)") + .font(.subheadline) + .foregroundStyle(.secondary) + + // Breadcrumb Trail + ScrollView(.horizontal, showsIndicators: false) { + HStack { + Text("Root") + .padding(.horizontal, 10) + .padding(.vertical, 6) + .background(Color.blue.opacity(0.15)) + .clipShape(Capsule()) + + ForEach(router.path, id: \.self) { route in + Image(systemName: "chevron.right") + .font(.caption2) + .foregroundStyle(.tertiary) + Text("\(String(describing: route))") + .padding(.horizontal, 10) + .padding(.vertical, 6) + .background(Color.green.opacity(0.15)) + .clipShape(Capsule()) + } + } + .padding(.horizontal) + } + + Divider() + + VStack(spacing: 12) { + Button("Push Next Step") { + router.push(.step(router.path.count + 1)) + } + .buttonStyle(.borderedProminent) + + Button("Present Modal Sheet") { + router.present(sheet: DemoSheet(id: "modal-1", title: "Nested Flow Sheet")) + } + .buttonStyle(.bordered) + + Button("Pop Back") { + router.pop() + } + .buttonStyle(.bordered) + .disabled(router.path.isEmpty) + + Button("Pop to Root") { + router.popToRoot() + } + .buttonStyle(.bordered) + .foregroundStyle(.red) + .disabled(router.path.isEmpty) + } + + Spacer() + } + .padding() + .navigationTitle("Explorer") + } +} + +struct StepDetailView: View { + let stepIndex: Int + @Environment(DemoRouter.self) private var router + var body: some View { - NavigationStack(path: baseNavigation.navigationPath) { - SplashView() - .withNavigationRouter() - .withSheetRouter(sheetDestination: baseNavigation.presentingSheet) - .withFullScreenRouter(baseNavigation.presentingFullScreen) + VStack(spacing: 20) { + Text("Step Screen #\(stepIndex)") + .font(.title) + + Button("Push Step #\(stepIndex + 1)") { + router.push(.step(stepIndex + 1)) + } + .buttonStyle(.borderedProminent) + + Button("Pop Back") { + router.pop() + } + .buttonStyle(.bordered) + + Button("Pop to Root") { + router.popToRoot() + } + .buttonStyle(.bordered) + .foregroundStyle(.red) + } + .padding() + .navigationTitle("Step \(stepIndex)") + } +} + +// MARK: - Nested Modal Sub-Router View + +enum SubModalRoute: Hashable, Sendable { + case subDetail +} + +typealias SubRouter = NavigationRouter + +struct NestedSheetContainerView: View { + let sheet: DemoSheet + @State private var subRouter = SubRouter() + @Environment(DemoRouter.self) private var parentRouter + + var body: some View { + BaseNavigationContainer( + router: subRouter, + content: { + VStack(spacing: 20) { + Text(sheet.title) + .font(.title2) + Text("This sheet owns an isolated child router") + .font(.caption) + .foregroundStyle(.secondary) + + Button("Push Child Stack Screen") { + subRouter.push(.subDetail) + } + .buttonStyle(.borderedProminent) + + Button("Dismiss Entire Sheet") { + parentRouter.dismiss() + } + .buttonStyle(.bordered) + } + .padding() + .navigationTitle("Sheet Root") + }, + destination: { route in + switch route { + case .subDetail: + VStack(spacing: 16) { + Text("Child Stack Detail") + Button("Pop Child Screen") { + subRouter.pop() + } + .buttonStyle(.borderedProminent) + } + .navigationTitle("Child Detail") + } + } + ) + } +} + +// MARK: - Async & Stress Lab + +struct AsyncLabView: View { + @State private var labRouter = DemoRouter() + @State private var statusLog: [String] = [] + @State private var isLoading = false + + var body: some View { + NavigationStack(path: $labRouter.path) { + VStack(spacing: 16) { + Text("Async & Concurrency Guard Lab") + .font(.headline) + + if isLoading { + ProgressView("Simulating Background Network Call...") + } + + Button("Simulate Background Async Push") { + isLoading = true + let router = labRouter + Task.detached { + try? await Task.sleep(nanoseconds: 1_000_000_000) + router.send { r in + isLoading = false + r.push(.detail(title: "Loaded from Network Actor")) + } + } + } + .buttonStyle(.borderedProminent) + .disabled(isLoading) + + Button("Rapid Tap Stress Test (10x in 100ms)") { + for _ in 1...10 { + labRouter.push(.step(999)) + } + } + .buttonStyle(.bordered) + + Text("Stack items: \(labRouter.path.count)") + .font(.caption) + .foregroundStyle(.secondary) + + Spacer() + } + .padding() + .navigationTitle("Async Lab") + .navigationDestination(for: DemoRoute.self) { route in + switch route { + case .detail(let title): + Text(title) + case .step(let id): + Text("Stress step \(id)") + } + } } - .environment(baseNavigation) } } diff --git a/NavigationStackExample/NavigationStackExample/Utils/View+Extension.swift b/NavigationStackExample/NavigationStackExample/Utils/View+Extension.swift deleted file mode 100644 index b93fe2b..0000000 --- a/NavigationStackExample/NavigationStackExample/Utils/View+Extension.swift +++ /dev/null @@ -1,55 +0,0 @@ -// -// View+Extension.swift -// NavigationStackExample -// -// Created by iletai on 14/12/2023. -// - -import Foundation -import SwiftUI - -@MainActor -extension View { - func withNavigationRouter() -> some View { - navigationDestination(for: ViewNavigationTarget.self) { pushTo in - switch pushTo { - case .list: - ListView() - case .splash: - SplashView() - default: - EmptyView() - } - - } - } - - func withSheetRouter(sheetDestination: Binding) -> some View { - sheet(item: sheetDestination) { toSheet in - switch toSheet { - case .policy(_): - PolicyView() - case .list: - ListView() - case .splash: - SplashView() - default: - EmptyView() - } - } - } - - @MainActor - func withFullScreenRouter(_ presentFullView: Binding) -> some View { - fullScreenCover(item: presentFullView) { fullView in - switch fullView { - case .list: - ListView() - case .splash: - SplashView() - default: - EmptyView() - } - } - } -} diff --git a/NavigationStackExample/NavigationStackExample/View/BaseView.swift b/NavigationStackExample/NavigationStackExample/View/BaseView.swift deleted file mode 100644 index 2a11391..0000000 --- a/NavigationStackExample/NavigationStackExample/View/BaseView.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// BaseView.swift -// NavigationStackExample -// -// Created by iletai on 14/12/2023. -// - -import Foundation -import SwiftUI -import BaseNavigationStack - -struct BaseView: View { - @Environment(BaseNavigation.self) - var navigationRouter - - var body: some View { - VStack { - Text("Base View") - .font(.title) - .fontWeight(.bold) - HStack { - Button { - navigationRouter.pushToView(.list) - } label: { - Text("Push View") - } - Button { - navigationRouter.presentSheet(.policy(URL(string: "https://google.com")!)) - } label: { - Text("Present View") - } - Button { - navigationRouter.presentFullScreen(.list) - } label: { - Text("Present Full View") - } - } - .buttonStyle(.bordered) - Spacer() - } - .navigationTitle("BaseView") - .navigationBarTitleDisplayMode(.inline) - } -} - diff --git a/NavigationStackExample/NavigationStackExample/View/ListView.swift b/NavigationStackExample/NavigationStackExample/View/ListView.swift deleted file mode 100644 index 9f3481d..0000000 --- a/NavigationStackExample/NavigationStackExample/View/ListView.swift +++ /dev/null @@ -1,114 +0,0 @@ -// -// ListView.swift -// NavigationStackExample -// -// Created by iletai on 14/12/2023. -// - -import Foundation -import SwiftUI -import BaseNavigationStack - -struct ListView: View { - @Environment(BaseNavigation.self) var navigationRouter - @Environment(\.dismiss) var dimiss - var body: some View { - VStack { - Text("List View") - .font(.title) - .fontWeight(.bold) - HStack { - Button { - dimiss() - navigationRouter.dismiss() - } label: { - Text("Dis") - } - } - HStack { - Button { - navigationRouter.presentSheet(.policy(URL(string: "https://google.com")!)) - } label: { - Text("Present View") - } - Button { - navigationRouter.pushToView(.splash) - } label: { - Text("Splash") - } - Button { - navigationRouter.popBack() - } label: { - Text("PopBack") - } - Button { - navigationRouter.navigateToRoot() - } label: { - Text("PopRoot") - } - Button { - navigationRouter.presentFullScreen(.list) - } label: { - Text("FullView") - } - Button { - navigationRouter.presentFullScreen(.splash) - } label: { - Text("SplashView") - } - } - .buttonStyle(.bordered) - Spacer() - } - .navigationTitle("ListView") - .navigationBarTitleDisplayMode(.inline) - } -} - -struct SplashView: View { - @Environment(BaseNavigation.self) - var navigationRouter - - @Environment(\.dismiss) var dimiss - - var body: some View { - VStack { - Text("SplashView") - .font(.title) - .fontWeight(.bold) - HStack { - Button { - dimiss() - navigationRouter.dismiss() - } label: { - Text("Dis") - } - Button { - navigationRouter.pushToView(.list) - } label: { - Text("List") - } - Button { - navigationRouter.popBack() - } label: { - Text("PopBack") - } - Button { - navigationRouter.navigateToRoot() - } label: { - Text("PopRoot") - } - Button { - navigationRouter.presentFullScreen(.list) - } label: { - Text("FullView") - } - } - .buttonStyle(.bordered) - Spacer() - } - .navigationTitle("Splash") - .navigationBarTitleDisplayMode(.inline) - } -} - diff --git a/NavigationStackExample/NavigationStackExample/View/PolicyView.swift b/NavigationStackExample/NavigationStackExample/View/PolicyView.swift deleted file mode 100644 index 8c84198..0000000 --- a/NavigationStackExample/NavigationStackExample/View/PolicyView.swift +++ /dev/null @@ -1,65 +0,0 @@ -// -// PolicyView.swift -// NavigationStackExample -// -// Created by iletai on 15/12/2023. -// - -import Foundation -import SwiftUI -import BaseNavigationStack - -struct PolicyView: View { - @Environment(BaseNavigation.self) var navigationRouter - @Environment(\.dismiss) var dimiss - var body: some View { - VStack { - Text("Policy") - .font(.title) - .fontWeight(.bold) - HStack { - Button { - dimiss() - navigationRouter.dismiss() - } label: { - Text("Dis") - } - Button { - navigationRouter.presentSheet(.splash) - } label: { - Text("PresentSplas") - } - } - HStack { - Button { - navigationRouter.pushToView(.list) - } label: { - Text("List") - } - Button { - navigationRouter.presentSheet(.list) - } label: { - Text("Present List") - } - Button { - navigationRouter.popBack() - } label: { - Text("Pop Back") - } - Button { - navigationRouter.navigateToRoot() - } label: { - Text("Pop To Root") - } - Button { - navigationRouter.presentFullScreen(.list) - } label: { - Text("Present Full View") - } - } - .buttonStyle(.bordered) - Spacer() - } - .navigationTitle("Policy") - } -} diff --git a/Package.swift b/Package.swift index 92da97d..c295287 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -6,21 +6,21 @@ import PackageDescription let package = Package( name: "BaseNavigationStack", platforms: [ - .macOS(.v13), .iOS(.v17) + .macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v10), .visionOS(.v1) ], products: [ - // Products define the executables and libraries a package produces, making them visible to other packages. .library( name: "BaseNavigationStack", - targets: ["BaseNavigationStack"]), + targets: ["BaseNavigationStack"] + ), ], targets: [ - // Targets are the basic building blocks of a package, defining a module or a test suite. - // Targets can depend on other targets in this package and products from dependencies. .target( - name: "BaseNavigationStack"), + name: "BaseNavigationStack" + ), .testTarget( name: "BaseNavigationStackTests", - dependencies: ["BaseNavigationStack"]), + dependencies: ["BaseNavigationStack"] + ), ] ) diff --git a/README.md b/README.md index ae50068..7747637 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,376 @@ +
+ # BaseNavigationStack -Build a navigation controller with Navigation Stack SwiftUI -[![Swift Version](https://img.shields.io/badge/Swift-5.9-orange.svg)](https://swift.org) -[![Platform](https://img.shields.io/cocoapods/p/LFAlertController.svg?style=flat)](https://developer.apple.com/ios/) +**Type-safe, data-driven navigation for SwiftUI. One source of truth. Zero external dependencies.** + +[![Swift Version](https://img.shields.io/badge/Swift-6.0-F05138.svg?style=for-the-badge&logo=swift&logoColor=white)](https://swift.org) +[![Platforms](https://img.shields.io/badge/Platforms-iOS%2017%2B%20%7C%20macOS%2014%2B%20%7C%20tvOS%2017%2B%20%7C%20watchOS%2010%2B%20%7C%20visionOS%201%2B-007AFF.svg?style=for-the-badge&logo=apple&logoColor=white)](https://developer.apple.com) +[![Strict Concurrency](https://img.shields.io/badge/Swift%206-Strict%20Concurrency%20Complete-34C759.svg?style=for-the-badge)](https://www.swift.org/migration/documentation/swift-6-concurrency-migration-guide/) +[![Zero Dependencies](https://img.shields.io/badge/Dependencies-Zero-8E8E93.svg?style=for-the-badge)](#) +[![CI](https://img.shields.io/github/actions/workflow/status/iletai/BaseNavigationStack/ci.yml?branch=develop&style=for-the-badge&label=CI)](https://github.com/iletai/BaseNavigationStack/actions/workflows/ci.yml) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](LICENSE) + +[Features](#features) • [Quickstart](#quickstart) • [Architecture](#architecture) • [API Cheat Sheet](#api-cheat-sheet) • [Advanced Recipes](#advanced-recipes) • [Example App](#example-app) • [Docs & Wiki](#docs--wiki) • [Contributing](#contributing) • [License](#license) + +Minimums declared in `Package.swift`: iOS 17+, macOS 14+, tvOS 17+, watchOS 10+, visionOS 1+. macOS has no full screen cover, so covers are presented as sheets there. -BaseNavigationStack is a Swift package that provides a flexible and reusable base class for managing navigation stacks in SwiftUI applications. +
-## Overview +## Why BaseNavigationStack? -`BaseNavigationStack` is designed to simplify the management of navigation stacks in SwiftUI applications. It introduces a generic approach to handle different types for views and presentation targets. This allows you to build navigation flows with ease, supporting various types of views and presentation styles. +SwiftUI's built-in navigation is powerful, but state tends to sprawl across views. `BaseNavigationStack` unifies **push**, **sheet**, and **full screen cover** transitions under a single, observable router — keeping navigation predictable, testable, and safe under concurrency. -## Example: +## Comparison -[