OpenBytesNavigation

OpenBytesNavigation 是一个 SwiftUI 库,为您的 SwiftUI 应用程序提供灵活且易于使用的导航系统。 它允许您在视图之间导航、显示模态视图、显示警报和操作表以及显示 Toast 通知。

用法

要使用 OpenBytesNavigation,您需要创建一个 OpenBytesNavigationPath 的实例,该实例跟踪导航状态。 然后,您可以使用 OpenBytesNavigationView 视图来显示您的内容并提供导航功能。

import OpenBytesNavigation
import SwiftUI

struct ContentView: View {
    @ObservedObject private var path = OpenBytesNavigationPath(id: "path_id")

    var body: some View {
        OpenBytesNavigationView(path: path) {
            // your content here
        }
    }
}

导航示例

SwiftUI 预览

OpenBytesNavigationView 上调用 preview 函数以创建导航堆栈的预览。这对于 SwiftUI 预览非常有用,因为路径将无法保存。

struct ExampleView_Previews: PreviewProvider {
    static var previews: some View {
        OpenBytesNavigationView.preview {
            // your content here
        }
    }
}

操作表

path.actionSheet(
    title: "Delete Item?",
    actions: {
        Button("Delete", role: .destructive) {
            // Delete item
        }
    },
    message: {
        Text("Are you sure you want to delete this item?")
    }
)

警报

path.alert(
    title: Text("Error"),
    message: Text("An error occurred. Please try again."),
    primaryButton: .default(Text("OK")),
    secondaryButton: .cancel()
)

模态

path.modal(
    body: {
        Text("Modal body")
    }
)

Toast

path.toast(
    title: "Success",
    message: "Item saved successfully",
    style: .success
)

推入 (Push)

.navigationDestination(for: Date.self, destination: { DateView(date: $0) })
...
path.push(Date())

弹出 (Pop)

path.pop()

保存和加载

let id = "test"
let path = OpenBytesNavigationPath(id: id, isPreview: false)

XCTAssertEqual(path.navigation.count, 0)

path.push("Value")

XCTAssertEqual(path.navigation.count, 1)

path.save()

let loadedPath = OpenBytesNavigationPath.load(id: id)

XCTAssertEqual(loadedPath.navigation.count, path.navigation.count)