Skip to content

Commit

Permalink
Add router
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed May 17, 2024
1 parent 3a4956c commit 078ee88
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
branches: ["main"]
workflow_dispatch:

jobs:

build-swiftpm:
name: Build SwiftPM
uses: oversizedev/GithubWorkflows/.github/workflows/build-swiftpm.yml@main
with:
package: "OversizeRouter"
secrets: inherit

# tests:
# name: Test
# needs: build-swiftpm
# uses: oversizedev/GithubWorkflows/.github/workflows/test.yml@main
# secrets: inherit

bump:
name: Bump version
needs: build-swiftpm
uses: oversizedev/GithubWorkflows/.github/workflows/bump.yml@main
secrets: inherit
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Release

on:
push:
tags:
- "*.*.*"

jobs:
build:
name: Create release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Release
uses: softprops/action-gh-release@v1
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// swift-tools-version: 5.10
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OversizeRouter",
platforms: [
.iOS(.v16),
.macOS(.v13),
.tvOS(.v15),
.watchOS(.v9),
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
Expand Down
23 changes: 23 additions & 0 deletions Sources/OversizeRouter/AlertRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright © 2024 Alexander Romanov
// AlertRouter.swift, created on 11.04.2024
//

import Foundation

public class AlertRouter<RootAlert: Alertable>: ObservableObject {
// Alert
@Published public var alert: RootAlert? = nil

public init() {}
}

public extension AlertRouter {
func present(_ alert: RootAlert) {
self.alert = alert
}

func dismiss() {
alert = nil
}
}
30 changes: 30 additions & 0 deletions Sources/OversizeRouter/HUDRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright © 2024 Alexander Romanov
// HUDRouter.swift, created on 14.04.2024
//

import Foundation

public enum HUDMessageType {
case `default`
case success
case destructive
case deleted
case archived
}

public class HUDRouter: ObservableObject {
@Published public var isShowHud: Bool = false
@Published public var hudText: String = ""
@Published public var type: HUDMessageType = .default

public init() {}
}

public extension HUDRouter {
func present(_ text: String, type: HUDMessageType = .default) {
hudText = text
self.type = type
isShowHud = true
}
}
23 changes: 23 additions & 0 deletions Sources/OversizeRouter/MenuRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright © 2024 Alexander Romanov
// OversizeRouter.swift, created on 06.05.2024
//

import Foundation

public class MenuRouter<Menu: Menuble>: ObservableObject {
@Published public var menu: Menu?
@Published public var subMenu: Menu?

public init(menu: Menu) {
self.menu = menu
}

public func changeMenu(_ menu: Menu) {
self.menu = menu
}

public func changeSubMenu(_ menu: Menu) {
self.menu = menu
}
}
2 changes: 0 additions & 2 deletions Sources/OversizeRouter/OversizeRouter.swift

This file was deleted.

22 changes: 22 additions & 0 deletions Sources/OversizeRouter/Protocols/Alertable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright © 2024 Alexander Romanov
// Alertable.swift, created on 14.04.2024
//

import Foundation

public protocol Alertable: Equatable, Hashable, Identifiable {}

public extension Alertable {
static func == (lhs: Self, rhs: Self) -> Bool {
if lhs.id == rhs.id {
true
} else {
false
}
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
12 changes: 12 additions & 0 deletions Sources/OversizeRouter/Protocols/Menuble.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Copyright © 2024 Alexander Romanov
// File.swift, created on 06.05.2024
//

public protocol Menuble: CaseIterable, Equatable, Identifiable {}

public extension Menuble {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
22 changes: 22 additions & 0 deletions Sources/OversizeRouter/Protocols/Routable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright © 2024 Alexander Romanov
// Routable.swift, created on 14.04.2024
//

import SwiftUI

public protocol Routable: Equatable, Hashable, Identifiable {}

public extension Routable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

static func == (lhs: Self, rhs: Self) -> Bool {
if lhs.id == rhs.id {
true
} else {
false
}
}
}
14 changes: 14 additions & 0 deletions Sources/OversizeRouter/Protocols/Tabable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Copyright © 2024 Alexander Romanov
// Tabable.swift, created on 14.04.2024
//

import Foundation

public protocol Tabable: CaseIterable, Equatable, Identifiable {}

public extension Tabable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
118 changes: 118 additions & 0 deletions Sources/OversizeRouter/Router.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// Copyright © 2024 Alexander Romanov
// Router.swift, created on 13.04.2024
//

import SwiftUI

@available(iOS 16.0, *)
public final class Router<Destination: Routable>: ObservableObject {
// Path
@Published public var path = NavigationPath()
@Published public var sheetPath = NavigationPath()
@Published public var fullScreenCoverPath = NavigationPath()

// Sheets
@Published public var sheet: Destination?
@Published public var fullScreenCover: Destination?
@Published public var menu: Destination?
@Published public var sheetDetents: Set<PresentationDetent> = []
@Published public var dragIndicator: Visibility = .hidden
@Published public var dismissDisabled: Bool = false

public init() {}
}

@available(iOS 16.0, *)
public extension Router {
func changeMenu(_ screen: Destination) {
menu = screen
}
}

@available(iOS 16.0, *)
public extension Router {
func move(_ screen: Destination) {
path.append(screen)
}

func backToRoot() {
path.removeLast(path.count)
}

func back(_ count: Int = 1) {
let pathCount = path.count - count
if pathCount > -1 {
path.removeLast(count)
}
}
}

// MARK: - Sheets

@available(iOS 16.0, *)
public extension Router {
func present(_ sheet: Destination, fullScreen: Bool = false) {
if fullScreen {
if fullScreenCover != nil {
fullScreenCover = nil
}
fullScreenCover = sheet
} else {
restSheet()
self.sheet = sheet
}
}

func present(_ sheet: Destination, detents: Set<PresentationDetent> = [.large], indicator: Visibility = .hidden, dismissDisabled: Bool = false) {
restSheet()
sheetDetents = detents
dragIndicator = indicator
self.dismissDisabled = dismissDisabled
self.sheet = sheet
}

func backOrDismiss() {
if sheet != nil || fullScreenCover != nil {
sheet = nil
fullScreenCover = nil
} else {
back()
}
}

func dismiss() {
sheet = nil
fullScreenCover = nil
}

func dismissSheet() {
sheet = nil
}

func dismissFullScreenCover() {
fullScreenCover = nil
}

func dismissDisabled(_ isDismissDisabled: Bool = true) {
dismissDisabled = isDismissDisabled
}

private func restSheet() {
if sheet != nil {
sheet = nil
}
if fullScreenCover != nil {
fullScreenCover = nil
}
if dragIndicator != .hidden {
dragIndicator = .hidden
}
if dismissDisabled {
dismissDisabled = false
}
if sheetDetents.isEmpty == false {
sheetDetents = []
}
}
}
18 changes: 18 additions & 0 deletions Sources/OversizeRouter/TabRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright © 2024 Alexander Romanov
// TabRouter.swift, created on 13.04.2024
//

import Foundation

public class TabRouter<Tab: Tabable>: ObservableObject {
@Published public var tab: Tab

public init(tab: Tab) {
self.tab = tab
}

public func changeTab(_ tab: Tab) {
self.tab = tab
}
}

0 comments on commit 078ee88

Please sign in to comment.