Skip to content

Commit

Permalink
Add routing draft
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed May 16, 2024
1 parent 00bb1cc commit bad3524
Show file tree
Hide file tree
Showing 17 changed files with 1,133 additions and 1,016 deletions.
5 changes: 4 additions & 1 deletion Sources/OversizeKit/RouterKit/HUDRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
//

import Foundation
import OversizeModels

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) {
func present(_ text: String, type: HUDMessageType = .default) {
hudText = text
self.type = type
isShowHud = true
}
}
23 changes: 23 additions & 0 deletions Sources/OversizeKit/RouterKit/MenuRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright © 2024 Alexander Romanov
// File.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
}
}
42 changes: 42 additions & 0 deletions Sources/OversizeKit/RouterKit/PresentationRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Copyright © 2024 Alexander Romanov
// PresentationRouter.swift, created on 16.05.2024
//

import Foundation

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

public class PresentationRouter<RootAlert: Alertable>: ObservableObject {
// Alert
@Published public var alert: RootAlert? = nil
@Published public var isShowHud: Bool = false
@Published public var hudText: String = ""
@Published public var type: HUDMessageType = .default

public init() {}
}

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

func dismiss() {
alert = nil
}
}

public extension PresentationRouter {
func present(_ text: String, type: HUDMessageType = .default) {
hudText = text
self.type = type
isShowHud = true
}
}
10 changes: 4 additions & 6 deletions Sources/OversizeKit/RouterKit/Protocols/Alertable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ import Foundation
public protocol Alertable: Equatable, Hashable, Identifiable {}

public extension Alertable {
var id: Self { self }

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

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/OversizeKit/RouterKit/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)
}
}
10 changes: 8 additions & 2 deletions Sources/OversizeKit/RouterKit/Protocols/Routable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import SwiftUI
public protocol Routable: Equatable, Hashable, Identifiable {}

public extension Routable {
var id: Self { self }

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

static func == (lhs: Self, rhs: Self) -> Bool {
if lhs.id == rhs.id {
true
} else {
false
}
}
}
17 changes: 17 additions & 0 deletions Sources/OversizeKit/RouterKit/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ public final class Router<Destination: Routable>: ObservableObject {
// 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) {
Expand Down Expand Up @@ -64,6 +72,15 @@ public extension Router {
self.sheet = sheet
}

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

func dismiss() {
sheet = nil
fullScreenCover = nil
Expand Down
Loading

0 comments on commit bad3524

Please sign in to comment.