diff --git a/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManager.swift b/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManager.swift index 9ec9fdc..f0eb0d2 100644 --- a/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManager.swift +++ b/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManager.swift @@ -111,6 +111,7 @@ extension ContainerManager: ContainerViewManagementForViewModifier { @discardableResult func _show( view: Content, + with ID: UUID? = nil, in container: String, using configuration: ContainerViewConfigurationProtocol, isPresented: Binding? = nil, @@ -119,7 +120,7 @@ extension ContainerManager: ContainerViewManagementForViewModifier { guard let publisher = getPublisher(for: container) else { return nil } - let viewID = UUID() + let viewID = ID ?? UUID() // If no specific ID is given, generate a new ID let identifiableContainerView = IdentifiableContainerView( id: viewID, view: view, @@ -149,6 +150,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment { @discardableResult public func show( view: Content, + with ID: UUID? = nil, in container: String, using configuration: ContainerViewConfigurationProtocol, animated: Bool = true @@ -163,6 +165,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment { @discardableResult public func show( containerView: Content, + with ID: UUID? = nil, in container: String, animated: Bool = true ) -> UUID? where Content: ContainerView { diff --git a/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManagerProtocols.swift b/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManagerProtocols.swift index 823a066..719d02f 100644 --- a/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManagerProtocols.swift +++ b/Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManagerProtocols.swift @@ -36,6 +36,7 @@ protocol ContainerViewManagementForViewModifier { /// - Returns: container view ID @discardableResult func _show(view: Content, + with ID: UUID?, in container: String, using configuration: ContainerViewConfigurationProtocol, isPresented: Binding?, @@ -57,6 +58,7 @@ public protocol ContainerViewManagementForEnvironment { /// - Returns: container view ID func show( view: Content, + with ID: UUID?, in container: String, using configuration: ContainerViewConfigurationProtocol, animated: Bool @@ -68,6 +70,7 @@ public protocol ContainerViewManagementForEnvironment { /// - Returns: container view ID func show( containerView: Content, + with ID: UUID?, in container: String, animated: Bool ) -> UUID? where Content: ContainerView diff --git a/Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift b/Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift index 64f7f1f..b0a4d6d 100644 --- a/Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift +++ b/Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift @@ -85,33 +85,69 @@ class ContainerManagerTests: XCTestCase { } func testSendView() throws { - // given - let containerName = "message" - let messageView = MessageView() - let publisher = manager.registerContainer(for: containerName) - let expectation = XCTestExpectation(description: "get view from container 1") - var cancellable: Set = [] - manager.debugLevel = 2 - var resultID: UUID? - - // when - publisher - .sink(receiveValue: { action in - switch action { - case .show(let identifiableView, _): - resultID = identifiableView.id - expectation.fulfill() - default: - break - } - }) - .store(in: &cancellable) - - let viewID = manager.show(view: messageView, in: containerName, using: messageView) + func testSendView() throws { + // given + let containerName = "message" + let messageView = MessageView() + let publisher = manager.registerContainer(for: containerName) + let expectation = XCTestExpectation(description: "get view from container 1") + var cancellable: Set = [] + manager.debugLevel = 2 + var resultID: UUID? + + // when + publisher + .sink(receiveValue: { action in + switch action { + case .show(let identifiableView, _): + resultID = identifiableView.id + expectation.fulfill() + default: + break + } + }) + .store(in: &cancellable) + + let viewID = manager.show(view: messageView, in: containerName, using: messageView) + + // then + wait(for: [expectation], timeout: 2) + XCTAssertEqual(resultID, viewID) + } + } - // then - wait(for: [expectation], timeout: 2) - XCTAssertEqual(resultID, viewID) + func testSendViewID() throws { + func testSendView() throws { + // given + let containerName = "message" + let messageView = MessageView() + let publisher = manager.registerContainer(for: containerName) + let expectation = XCTestExpectation(description: "get view from container 1") + var cancellable: Set = [] + manager.debugLevel = 2 + var resultIDFromSink: UUID? + let viewID = UUID() + + // when + publisher + .sink(receiveValue: { action in + switch action { + case .show(let identifiableView, _): + resultIDFromSink = identifiableView.id + expectation.fulfill() + default: + break + } + }) + .store(in: &cancellable) + + let viewIDFromShow = manager.show(view: messageView, in: containerName, using: messageView) + + // then + wait(for: [expectation], timeout: 2) + XCTAssertEqual(viewID, viewIDFromShow) + XCTAssertEqual(resultIDFromSink, viewID) + } } func testSendViewWithGetPublisher() throws {