Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Tests/SUICoordinatorTests/SheetCoordinatorTests.swift
  • Loading branch information
felilo committed Jun 17, 2024
2 parents e4c8fe7 + cdcb4e5 commit 761d34a
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ class HomeCoordinator: Coordinator<HomeRoute> {
}

func finsh() async {
await finishFlow(animated: true)
await finishFlow()
}
}
7 changes: 6 additions & 1 deletion Sources/SUICoordinator/Coordinator/CoordinatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public struct CoordinatorView<Route: RouteType>: CoordinatorViewType, View {
// --------------------------------------------------------------------

@StateObject var viewModel: Coordinator<Route>
@State private var isLoaded = false

// --------------------------------------------------------------------
// MARK: Properties
Expand Down Expand Up @@ -61,6 +62,7 @@ public struct CoordinatorView<Route: RouteType>: CoordinatorViewType, View {
public var body: some View {
RouterView(viewModel: viewModel.router)
.onViewDidLoad { [weak viewModel] in
isLoaded = true
Task { await viewModel?.start() }
}
}
Expand All @@ -69,5 +71,8 @@ public struct CoordinatorView<Route: RouteType>: CoordinatorViewType, View {
// MARK: CoordinatorViewType
// --------------------------------------------------------------------

func clean() { Task { await onClean?() } }
func clean() {
guard isLoaded else { return }
Task { await onClean?() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ extension CoordinatorType {
/// - animated: A boolean value indicating whether to animate the cleanup process.
func emptyCoordinator(animated: Bool) async {
guard let parent = parent else {
await removeChildren()
return await router.restart(animated: animated)
await router.restart(animated: animated)
await cleanView(animated: false)
return await removeChildren()
}

await parent.removeChild(coordinator: self)
Expand Down Expand Up @@ -154,7 +155,7 @@ extension CoordinatorType {
)
}

if (parent is (any TabbarCoordinatable)) {
if let parent, (parent is (any TabbarCoordinatable)) {
await router.close(animated: animated, finishFlow: true)
await handleFinish(parent)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public extension CoordinatorType {
///
/// - Parameters:
/// - animated: A boolean value indicating whether to animate the finish flow process.
@MainActor func finishFlow(animated: Bool) async -> Void {
@MainActor func finishFlow(animated: Bool = true) async -> Void {
await finish(animated: animated, withDissmis: true)
}

Expand Down
3 changes: 0 additions & 3 deletions Sources/SUICoordinator/Router/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class Router<Route: RouteType>: ObservableObject, RouterType {
// --------------------------------------------------------------------

/// The first view in the navigation flow.
// @Published public var mainView: Route?
public var mainView: Route?
/// The array of routes managed by the navigation router.
@Published public var items: [Route] = []
Expand Down Expand Up @@ -187,8 +186,6 @@ public class Router<Route: RouteType>: ObservableObject, RouterType {
@MainActor public func clean(animated: Bool, withMainView: Bool = true) async -> Void {
await runActionWithAnimation(animated) { [weak self] in
return {
self?.sheetCoordinator.clean(animated: animated)
self?.items = []
self?.coordinator = nil
if withMainView { self?.mainView = nil }
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/SUICoordinator/Router/RouterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ struct RouterView<Router: RouterType>: View {
private func addSheetTo(view: some View ) -> some View {
view
.onChange(of: viewModel.mainView, perform: onChangeFirstView)
.sheetCoordinating(
.sheetCoordinator(
coordinator: viewModel.sheetCoordinator,
onDissmis: { [weak viewModel] index in
viewModel?.sheetCoordinator.remove(at: index)
},
onDissmis: { viewModel.sheetCoordinator.remove(at: $0) },
onDidLoad: nil
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import SwiftUI

public extension View {

func sheetCoordinating(
func sheetCoordinator(
coordinator: SheetCoordinator<(any View)>,
index: Int = 0,
isLast: Bool = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final public class SheetCoordinator<T>: ObservableObject {
/// - animated: A boolean value indicating whether to animate the removal.
func removeLastSheet(animated: Bool = true) -> Void {
guard !items.isEmpty, !isCleaning else { return }
removeNilItems(at: totalItems)
makeNilItem(at: totalItems)
}

/// Removes the item at the specified index.
Expand All @@ -115,7 +115,7 @@ final public class SheetCoordinator<T>: ObservableObject {
guard !items.isEmpty, !isCleaning else { return }

isCleaning = true
removeNilItems(at: 0)
makeNilItem(at: 0)
resetValues()
}

Expand All @@ -128,11 +128,11 @@ final public class SheetCoordinator<T>: ObservableObject {
items.removeAll(where: { $0 == nil || $0?.view == nil })
}

/// Removes `nil` items at the specified index.
/// Makes item `nil` at the specified index.
///
/// - Parameters:
/// - index: The index at which to remove `nil` items.
private func removeNilItems(at index: Int) {
private func makeNilItem(at index: Int) {
items[index] = nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct SheetCoordinatorView: ViewModifier {
items: $coordinator.items,
content: { (index, item) in
let view = AnyView(item.view)
.sheetCoordinating(
.sheetCoordinator(
coordinator: coordinator,
index: (index + 1),
isLast: index == coordinator.totalItems,
Expand Down
5 changes: 0 additions & 5 deletions Tests/SUICoordinatorTests/CoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ final class CoordinatorTests: XCTestCase {
await sut.start(animated: false)
await sut.router.navigate(to: .pushStep2, animated: false )
await navigateToCoordinator(coordinator, in: sut)
await sut.router.navigate(to: .sheetStep, animated: false )

await finishFlow(sut: sut)
XCTAssertEqual(sut.router.items.count, 0)
XCTAssertNotNil(sut.router.mainView)
XCTAssertTrue(sut.children.isEmpty)
XCTAssertEqual(sut.router.sheetCoordinator.items.count, 0)
}
Expand Down Expand Up @@ -141,10 +139,7 @@ final class CoordinatorTests: XCTestCase {
// --------------------------------------------------------------------

private func makeSUT(file: StaticString = #filePath, line: UInt = #line) -> AnyCoordinator {
let parent = OtherCoordinator()
let coordinator = AnyCoordinator()
coordinator.parent = parent
parent.children.append(coordinator)
trackForMemoryLeaks(coordinator, file: file, line: line)
return coordinator
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SUICoordinatorTests/Helpers/Tests+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension XCTestCase {
}

func finishFlow(sut: (any CoordinatorType)) async {
await sut.finishFlow(animated: false)
async let _ = await sut.finishFlow(animated: false)
}

func getNameOf<T>(object: T) -> String {
Expand Down
1 change: 1 addition & 0 deletions Tests/SUICoordinatorTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final class RouterTests: XCTestCase {
await sut.navigate(to: .sheetStep, animated: false)
await sut.navigate(to: .fullScreenStep, animated: false)
await sut.clean(animated: false)
await sut.restart(animated: false)

XCTAssertEqual(sut.items.count, 0)
XCTAssertEqual(sut.sheetCoordinator.items.count, 0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/SUICoordinatorTests/SheetCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class SheetCoordinatorTests: XCTestCase {
let finalRoute = makeSheetItem("Final Item")

await presentSheet(makeSheetItem("First Item"), with: sut)
await sut.presentSheet(finalRoute)
await presentSheet(finalRoute, with: sut)

XCTAssertEqual(sut.items.count, 2)
XCTAssertEqual(sut.items.last??.id, finalRoute.id)
Expand Down
5 changes: 0 additions & 5 deletions Tests/SUICoordinatorTests/TabbarCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ final class TabbarCoordinatorTests: XCTestCase {
file: StaticString = #filePath,
line: UInt = #line
) -> AnyTabbarCoordinator {
let parent = OtherCoordinator()
let coordinator = AnyTabbarCoordinator(currentPage: currentPage)

coordinator.parent = parent
parent.children.append(coordinator)

trackForMemoryLeaks(coordinator, file: file, line: line)
return coordinator
}
Expand Down

0 comments on commit 761d34a

Please sign in to comment.