Skip to content

Commit

Permalink
Adds Supports to Swift 6.0 (#43)
Browse files Browse the repository at this point in the history
* Code refactor - removes unnecessary properties from CoordinatorView

* The `CoordinatorView` works with generic data-source

* Renames  `PAGE` associatedtype to `Page`

* Code refactor - removes warnings

* Adds support for swift 6 (#42)

* Removes memory leak

* Updates example

* Updates Readme
  • Loading branch information
felilo authored Dec 18, 2024
1 parent e8ed7fa commit 3ee3588
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -577,7 +577,7 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct CustomTabbarView<DataSource: TabbarCoordinatorType>: View where DataSourc

@StateObject private var viewModel: DataSource
@State private var currentPage: Page
@State private var pages: [Page]
@State private var pages: [Page] = []
@State var badges = [BadgeItem]()

let widthIcon: CGFloat = 22
Expand All @@ -57,8 +57,6 @@ struct CustomTabbarView<DataSource: TabbarCoordinatorType>: View where DataSourc
init(viewModel: DataSource) {
self._viewModel = .init(wrappedValue: viewModel)
currentPage = viewModel.currentPage
pages = viewModel.pages
badges = viewModel.pages.map { (nil, $0) }
}


Expand Down Expand Up @@ -94,8 +92,10 @@ struct CustomTabbarView<DataSource: TabbarCoordinatorType>: View where DataSourc
guard let index = getBadgeIndex(page: page) else { return }
badges[index].value = value
}
.onAppear {
badges = pages.map { (nil, $0) }
.task {
currentPage = viewModel.currentPage
pages = viewModel.pages
badges = viewModel.pages.map { (nil, $0) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ class ActionListViewModel: ObservableObject {
self.coordinator = coordinator
}

func navigateToFirstView() async {
@MainActor func navigateToFirstView() async {
await coordinator.navigateToPushView()
}

func presentSheet() async {
@MainActor func presentSheet() async {
await coordinator.presentSheet()
}

func presentFullscreen() async {
@MainActor func presentFullscreen() async {
await coordinator.presentFullscreen()
}

func presentDetents() async {
@MainActor func presentDetents() async {
await coordinator.presentDetents()
}

func presentTabbarCoordinator() async {
@MainActor func presentTabbarCoordinator() async {
await coordinator.presentTabbarCoordinator()
}

func finish() async {
@MainActor func finish() async {
await coordinator.finish()
}

func showFinishButton() -> Bool {
@MainActor func showFinishButton() -> Bool {
!(coordinator.parent is HomeCoordinator)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class DetentsViewModel: ObservableObject {
self.coordinator = coordinator
}

func navigateToNextView() async {
@MainActor func navigateToNextView() async {
await coordinator.presentTabbarCoordinator()
}

func close() async {
@MainActor func close() async {
await coordinator.close()
}

func finishFlow() async {
@MainActor func finishFlow() async {
await coordinator.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class FullscreenViewModel: ObservableObject {
self.coordinator = coordinator
}

func navigateToNextView() async {
@MainActor func navigateToNextView() async {
await coordinator.presentDetents()
}

func close() async {
@MainActor func close() async {
await coordinator.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class PushViewModel: ObservableObject {
self.coordinator = coordinator
}

func navigateToNextView() async {
@MainActor func navigateToNextView() async {
await coordinator.presentSheet()
}

func close() async {
@MainActor func close() async {
await coordinator.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ struct SheetView: View {

VStack {
Button("Presents FullscreenView") {
Task { await viewModel.navigateToNextView() }
Task { await viewModel.navigateToNextView() }
}.buttonStyle(.borderedProminent)

Button("Close view") {
Task { await viewModel.close() }
Task { await viewModel.close() }
}.buttonStyle(.borderedProminent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class SheetViewModel: ObservableObject {
self.coordinator = coordinator
}

func navigateToNextView() async {
@MainActor func navigateToNextView() async {
await coordinator.presentFullscreen()
}

func close() async {
@MainActor func close() async {
await coordinator.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class TabbarActionListViewModel: ObservableObject {
self.coordinator = coordinator
}

func presentDefaultTabbarCoordinator() async {
@MainActor func presentDefaultTabbarCoordinator() async {
await coordinator.presentDefaultTabbarCoordinator()
}

func presentCustomTabbarCoordinator() async {
@MainActor func presentCustomTabbarCoordinator() async {
await coordinator.presentCustomTabbarCoordinator()
}

func finsh() async {
@MainActor func finsh() async {
await coordinator.finishFlow()
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension CoordinatorType {
guard let first = children.first else { return }

if let parent = first.parent as? (any TabbarCoordinatable) {
await parent.setCurrentPage(with: first)
parent.setCurrentPage(with: first)
}

await first.emptyCoordinator(animated: animated)
Expand Down Expand Up @@ -150,26 +150,25 @@ extension CoordinatorType {
/// - withDismiss: A boolean value indicating whether to dismiss the coordinator.
/// - Returns: An asynchronous void task representing the finish process.
func finish(animated: Bool = true, withDismiss: Bool = true) async -> Void {
let handleFinish = { (coordinator: TCoordinatorType) async -> Void in
await coordinator.emptyCoordinator(animated: animated)
}

guard let parent, withDismiss else {
return await handleFinish(self)
return await emptyCoordinator(animated: animated)
}

if parent is (any TabbarCoordinatable) {
await parent.parent?.closeLastSheet(animated: animated)
return await handleFinish(parent)
return await parent.emptyCoordinator(animated: animated)
}

await parent.closeLastSheet(animated: animated)
await handleFinish(self)
await emptyCoordinator(animated: animated)
}

/// Cleans up the coordinator.
func swipedAway() async {
func swipedAway() {
guard !isEmptyCoordinator else { return }
await finish(animated: false, withDismiss: false)

Task(priority: .low) { [weak self] in
await self?.finish(animated: false, withDismiss: false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ public extension CoordinatorType {
animated: animated,
presentationStyle: (presentationStyle != .push) ? presentationStyle : .sheet,
view: { [weak coordinator] in coordinator?.getView() },
onFinish: { Task(priority: .low) {
@MainActor [weak coordinator] in await coordinator?.swipedAway()
}}
onFinish: {[weak coordinator] in coordinator?.swipedAway()}
)

await router.presentSheet(item: item)
router.presentSheet(item: item)
}

/// Finishes the flow of the coordinator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Foundation
/// of a specific module or feature in an application.
///
/// - Important: Adopt this protocol in your custom coordinator implementations.
@MainActor
public protocol CoordinatorType: SCHashable, ObservableObject {

// ---------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Sources/SUICoordinator/Router/RouteType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ public protocol RouteType: SCHashable {
var presentationStyle: TransitionPresentationStyle { get }

/// The body of the route, conforming to the View protocol.
@ViewBuilder var view: Body { get }
@ViewBuilder @MainActor var view: Body { get }
}
1 change: 1 addition & 0 deletions Sources/SUICoordinator/Router/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public class Router<Route: RouteType>: ObservableObject, RouterType {
@MainActor public func clean(animated: Bool, withMainView: Bool = true) async -> Void {
await popToRoot(animated: false)
items.removeAll()
await sheetCoordinator.clean()
sheetCoordinator = .init()

if withMainView {
Expand Down
14 changes: 7 additions & 7 deletions Sources/SUICoordinator/Router/RouterType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,41 @@ public protocol RouterType: ObservableObject {
/// - route: The route to navigate to.
/// - presentationStyle: The transition presentation style for the navigation.
/// - animated: A boolean value indicating whether to animate the navigation.
func navigate(to route: Route, presentationStyle: TransitionPresentationStyle?, animated: Bool) async
@MainActor func navigate(to route: Route, presentationStyle: TransitionPresentationStyle?, animated: Bool) async

/// Presents a view or coordinator with optional presentation style and animation.
///
/// - Parameters:
/// - view: The view or coordinator to present.
/// - presentationStyle: The transition presentation style for the presentation.
/// - animated: A boolean value indicating whether to animate the presentation.
func present(_ view: Route, presentationStyle: TransitionPresentationStyle?, animated: Bool) async
@MainActor func present(_ view: Route, presentationStyle: TransitionPresentationStyle?, animated: Bool) async

/// Pops the top view or coordinator from the navigation stack.
///
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the pop action.
func pop(animated: Bool) async
@MainActor func pop(animated: Bool) async

/// Pops to the root of the navigation stack.
///
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the pop action.
func popToRoot(animated: Bool) async
@MainActor func popToRoot(animated: Bool) async

/// Pops to a specific view or coordinator in the navigation stack.
///
/// - Parameters:
/// - view: The target view or coordinator to pop to.
/// - animated: A boolean value indicating whether to animate the pop action.
/// - Returns: A boolean value indicating whether the pop action was successful.
func popToView<T>(_ view: T, animated: Bool) async -> Bool
@MainActor func popToView<T>(_ view: T, animated: Bool) async -> Bool

/// Dismisses the currently presented view or coordinator.
///
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the dismissal.
func dismiss(animated: Bool) async
@MainActor func dismiss(animated: Bool) async

/// Cleans up the current view or coordinator, optionally preserving the main view.
///
Expand All @@ -113,7 +113,7 @@ public protocol RouterType: ObservableObject {
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the closing action.
/// - finishFlow: A boolean value indicating whether to finish the associated flow.
func close(animated: Bool, finishFlow: Bool) async -> Void
@MainActor func close(animated: Bool, finishFlow: Bool) async -> Void

/// Restarts the current view or coordinator, optionally animating the restart.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import SwiftUI

TransitionPresentationStyle enumerates the different styles used for transitioning between views or presenting views within an application.
*/
public enum TransitionPresentationStyle: SCEquatable {
public enum TransitionPresentationStyle: SCEquatable, Sendable {

/// A push transition style, commonly used in navigation controllers.
case push
Expand Down
4 changes: 2 additions & 2 deletions Sources/SUICoordinator/Shared/Protocols/SheetItemType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Foundation

protocol SheetItemType: SCIdentifiable {
/// A boolean value indicating whether to animate the presentation.
var animated: Bool { get set }
func isAnimated() -> Bool

/// The transition presentation style for presenting the sheet item.
var presentationStyle: TransitionPresentationStyle { get set }
func getPresentationStyle() -> TransitionPresentationStyle
}
Loading

0 comments on commit 3ee3588

Please sign in to comment.