Skip to content

Commit

Permalink
code-refactor: updates DefaultRoute object (#26) (#27)
Browse files Browse the repository at this point in the history
* Renames `RouteBase` to `DefaultRoute`

* Updates example

* Updates readme
  • Loading branch information
felilo authored Jun 19, 2024
1 parent 761d34a commit 389af34
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ public enum MyTabbarPage: TabbarPage, CaseIterable {
// MARK: TabbarPage
// ---------------------------------------------------------

public var badge: String? {
switch self {
case .first: "new"
case .second: "10"
}
}

@ViewBuilder
public var icon: (any View) {
switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import Foundation
import SUICoordinator

class TabbarFlowCoordinator: Coordinator<RouteBase> {
class TabbarFlowCoordinator: Coordinator<DefaultRoute> {

// ---------------------------------------------------------------------
// MARK: Coordinator
Expand All @@ -34,7 +34,7 @@ class TabbarFlowCoordinator: Coordinator<RouteBase> {
override func start(animated: Bool = true) async {
let viewModel = TabbarActionListViewModel(coordinator: self)

let route = RouteBase(
let route = DefaultRoute(
presentationStyle: .push,
content: TabbarActionListView(viewModel: viewModel)
)
Expand All @@ -56,10 +56,6 @@ class TabbarFlowCoordinator: Coordinator<RouteBase> {
await navigate(to: coordinator, presentationStyle: .sheet)
}

func close() async {
await router.close()
}

func finish() async {
await finishFlow(animated: true)
}
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ _____
These are the most important features and actions that you can perform:
<br>


#### RouteType

To create any route in `SUICoordinator` you need to extend your object to the `RouteType` protocol; Additionally, you can add your custom functions if you need. As you can see, in our example we are using custom types (`enums`) to implement it.

Last but not least, you can also use `DefaultRoute` to create custom routes as demonstrated in the `TabBarFlowCoordinator` [example](https://github.com/felilo/SUICoordinator/blob/main/Examples/SUICoordinatorExample/SUICoordinatorExample/Coordinators/TabbarFlow/TabbarFlowCoordinator.swift)
<br>


#### Router

The router is encharge to manage the navigation stack and coordinate the transitions between different views. It abstracts away the navigation details from the views, allowing them to focus on their specific features such as:
Expand Down
72 changes: 72 additions & 0 deletions Sources/SUICoordinator/Router/DefaultRoute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// DefaultRoute.swift
//
// Copyright (c) Andres F. Lozano
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation
import SwiftUI

/// A struct representing a default route with a specified presentation style and content view.
public struct DefaultRoute: RouteType {

// ---------------------------------------------------------
// MARK: - Properties
// ---------------------------------------------------------

/// The presentation style for the route transition.
private let _presentationStyle: TransitionPresentationStyle

/// The content view for the route.
public var content: any View

// ---------------------------------------------------------
// MARK: - Constructor
// ---------------------------------------------------------

/// Initializes a new instance of `DefaultRoute`.
///
/// - Parameters:
/// - presentationStyle: The presentation style for the route transition.
/// - content: The content view for the route.
public init(
presentationStyle: TransitionPresentationStyle,
content: (any View)
) {
self.content = content
self._presentationStyle = presentationStyle
}

// ---------------------------------------------------------
// MARK: - RouteNavigation
// ---------------------------------------------------------

/// The presentation style for the route transition.
public var presentationStyle: TransitionPresentationStyle {
_presentationStyle
}

/// The view to be presented for the route.
@ViewBuilder
public var view: any View {
content
}
}
44 changes: 0 additions & 44 deletions Sources/SUICoordinator/Router/RouteBase.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/SUICoordinator/Tabbar/TabbarCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Combine
/// An open class representing a coordinator for managing a tabbar-based navigation.
///
/// Tabbar coordinators handle the navigation and coordination of pages within a tabbar.
open class TabbarCoordinator<Page>: Coordinator<RouteBase>, TabbarCoordinatorType where Page: TabbarPage {
open class TabbarCoordinator<Page>: Coordinator<DefaultRoute>, TabbarCoordinatorType where Page: TabbarPage {

// --------------------------------------------------------------------
// MARK: Wrapper properties
Expand Down Expand Up @@ -85,7 +85,7 @@ open class TabbarCoordinator<Page>: Coordinator<RouteBase>, TabbarCoordinatorTyp
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the start process.
open override func start(animated: Bool = true) async {
let route = RouteBase(
let route = DefaultRoute(
presentationStyle: presentationStyle,
content: customView ?? TabbarCoordinatorView(viewModel: self)
)
Expand Down

0 comments on commit 389af34

Please sign in to comment.