Git Product home page Git Product logo

customnavigationstack's Introduction

CustomNavigationStack

A Swift package providing a custom navigation stack for SwiftUI applications. This package helps in managing the navigation flow in a SwiftUI application using a custom stack-based approach.

Requirements

  • iOS 13.0+
  • Swift 5.3+

Installation with Swift Package Manager (Xcode 11+)

Swift Package Manager (SwiftPM)

Step-by-Step Guide:

  1. Open your project in Xcode.
  2. Go to File -> Swift Packages -> Add Package Dependency.
  3. Enter the repository URL: https://github.com/laukhin-alex/CustomNavigationStack.git or type CustomNavigationStack to search.
  4. Select the package and choose the dependency type (tagged version, branch, or commit). Then Xcode will set up everything for you.

Manual Package.swift Configuration

If you want to add CustomNavigationStack to your own framework or use CustomNavigationStack as a dependency, update your Package.swift file:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "YourProjectName",
    dependencies: [
        .package(url: "https://github.com/laukhin-alex/CustomNavigationStack.git", from: "1.0.2")
    ],
    targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["CustomNavigationStack"]),
        // Other targets...
    ]
)

## Usage

### Importing the Package

```swift
import CustomNavigationStack

Setting Up the Navigation

To set up the navigation stack, follow these steps:

1. Define your views conforming to CustomNavigation.

import SwiftUI
import CustomNavigationStack

struct FirstView: View, CustomNavigation {
    var navigationManager: CustomNavigationContainer?

    var body: some View {
        VStack {
            Text("First View")
            Button(action: {
                navigationManager?.push(view: SecondView(navigationManager: navigationManager))
            }) {
                Text("Go to Second View")
            }
        }
    }
}

struct SecondView: View, CustomNavigation {
    var navigationManager: CustomNavigationContainer?
    
    var body: some View {
        VStack {
            Text("Second View")
            Button(action: {
                navigationManager?.push(view: ThirdView(navigationManager: navigationManager))
            }) {
                Text("Go to third View")
            }
            Button(action: {
                navigationManager?.popToRoot()
            }) {
                Text("Back to Root View")
            }
        }
    }
}

struct ThirdView: View, CustomNavigation {
    var navigationManager: CustomNavigationContainer?

    var body: some View {
        VStack {
            Text("Third View")
            Button(action: {
                navigationManager?.pop()
            }) {
                Text("Back to second View")
            }
            Button(action: {
                navigationManager?.popToRoot()
            }) {
                Text("Back to Root View")
            }
        }
    }
}

**2. Implement your RootView.

*in case you have animated or special launch screen

import SwiftUI
import CustomNavigationStack

struct RootView: View, CustomNavigation {
    var navigationManager: CustomNavigationContainer?
    
    var body: some View {
        VStack {
            LaunchScreen() //mark: you can put here your loadingscreen
        }
        .task {
            await delayAndNavigate()
        }
    }
    private func delayAndNavigate() async {
        do {
            try await Task.sleep(nanoseconds: 3_000_000_000) // 3 sec, you can add more
            await MainActor.run {
                //MARK: for sure of updating UI in Main thread
                navigationManager?.push(view: 
                    FirstView(navigationManager: navigationManager)
                    )
            }
        } catch {
            // error handler
            print("Task was cancelled or failed")
        }
    }
}

*in case you don't

import SwiftUI
import CustomNavigationStack

struct RootView: View, CustomNavigation {
    var navigationManager: CustomNavigationContainer?
    
    var body: some View {
        VStack {
           FirstView(navigationManager: navigationManager)
        }
}

**3. Set up the main app entry point.

import SwiftUI

@main
struct YourApp: App {
    @StateObject var navigationViewModel = NavigationViewModel()
    
    var body: some Scene {
        WindowGroup {
            NavigationContainer(viewModel: navigationViewModel,
                                customStartScreen: true) { //mark: false if you don't have customStartScreen
                RootView()
            }
        }
    }
}

License

CustomNavigationStack is released for free.

Author

Created by Aleksandr Laukhin.

customnavigationstack's People

Contributors

laukhin-alex avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.