Skip to content

Commit

Permalink
feat: RoutingController macro improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkrouk committed Dec 31, 2023
1 parent bbf8063 commit 97881a2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Sources/CombineNavigation/Destinations/StackDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public protocol GrouppedDestinationProtocol<DestinationID> {
}

/// Wrapper for creating and accessing managed navigation stack controllers
///
/// > ⚠️ Sublasses or typealiases must contain "StackDestination" in their name
/// > to be processed by `@RoutingController` macro
@propertyWrapper
open class StackDestination<
DestinationID: Hashable,
Expand Down
3 changes: 3 additions & 0 deletions Sources/CombineNavigation/Destinations/TreeDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public protocol SingleDestinationProtocol {
}

/// Wrapper for creating and accessing managed navigation destination controller
///
/// > ⚠️ Sublasses or typealiases must contain "TreeDestination" in their name
/// > to be processed by `@RoutingController` macro
@propertyWrapper
open class TreeDestination<Controller: CocoaViewController>:
Weakifiable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ extension RoutingControllerMacro: ExtensionMacro {

let isNavigationChild = variable.attributes.contains { attribute in
switch attribute {
case let .attribute(attribute) where attribute.name.name.hasSuffix("Destination"):
case let .attribute(attribute) where
attribute.name.name.contains("TreeDestination") ||
attribute.name.name.contains("StackDestination"):
return true
default:
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,23 @@ final class RoutingControllerMacroTests: XCTestCase {
}
}

func testAttachmentToClass_CustomDestinations() {
func testAttachmentToClass_CustomTreeDestinations() {
assertMacro {
"""
@RoutingController
final class CustomController {
@CustomDestination
@CustomTreeDestination
var firstDetailController: CocoaViewController?
@CustomDestination
@CustomTreeDestination
var secondDetailController: CocoaViewController?
}
"""
} expansion: {
"""
final class CustomController {
@CustomDestination
@CustomTreeDestination
var firstDetailController: CocoaViewController?
@CustomDestination
@CustomTreeDestination
var secondDetailController: CocoaViewController?
}
Expand All @@ -242,10 +242,10 @@ final class RoutingControllerMacroTests: XCTestCase {
/// Use in `navigationDestination`/`navigationStack` methods to map
/// routes to specific destinations using `destinations` method
public struct Destinations {
@CustomDestination
@CustomTreeDestination
var firstDetailController: CocoaViewController?
@CustomDestination
@CustomTreeDestination
var secondDetailController: CocoaViewController?
public subscript(_ id: some Hashable) -> UIViewController? {
Expand All @@ -263,4 +263,57 @@ final class RoutingControllerMacroTests: XCTestCase {
"""
}
}

func testAttachmentToClass_CustomStackDestinations() {
assertMacro {
"""
@RoutingController
final class CustomController {
@CustomStackDestination
var firstDetailController: [Int: CocoaViewController]
@CustomTreeDestinationOf<MyCoolIdentifableVC>
var secondDetailController
}
"""
} expansion: {
"""
final class CustomController {
@CustomStackDestination
var firstDetailController: [Int: CocoaViewController]
@CustomTreeDestinationOf<MyCoolIdentifableVC>
var secondDetailController
}
extension CustomController: CombineNavigation.RoutingController {
/// Container for captured destinations without referring to self
///
/// > Generated by `CombineNavigationMacros.RoutingController` macro
///
/// Use in `navigationDestination`/`navigationStack` methods to map
/// routes to specific destinations using `destinations` method
public struct Destinations {
@CustomStackDestination
var firstDetailController: [Int: CocoaViewController]
@CustomTreeDestinationOf<MyCoolIdentifableVC>
var secondDetailController
public subscript(_ id: Int) -> UIViewController? {
return firstDetailController[id]
}
}
public func _makeDestinations() -> Destinations {
return Destinations(
firstDetailController: $firstDetailController,
secondDetailController: $secondDetailController
)
}
}
"""
}
}
}

0 comments on commit 97881a2

Please sign in to comment.