Skip to content

Commit

Permalink
User can set a specific ID for view that needs to be displayed
Browse files Browse the repository at this point in the history
Give developers more flexibility to control views
  • Loading branch information
fatbobman committed Mar 20, 2022
1 parent 8c4c460 commit 29b1736
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ extension ContainerManager: ContainerViewManagementForViewModifier {
@discardableResult
func _show<Content>(
view: Content,
with ID: UUID? = nil,
in container: String,
using configuration: ContainerViewConfigurationProtocol,
isPresented: Binding<Bool>? = nil,
Expand All @@ -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,
Expand Down Expand Up @@ -149,6 +150,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment {
@discardableResult
public func show<Content>(
view: Content,
with ID: UUID? = nil,
in container: String,
using configuration: ContainerViewConfigurationProtocol,
animated: Bool = true
Expand All @@ -163,6 +165,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment {
@discardableResult
public func show<Content>(
containerView: Content,
with ID: UUID? = nil,
in container: String,
animated: Bool = true
) -> UUID? where Content: ContainerView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protocol ContainerViewManagementForViewModifier {
/// - Returns: container view ID
@discardableResult
func _show<Content: View>(view: Content,
with ID: UUID?,
in container: String,
using configuration: ContainerViewConfigurationProtocol,
isPresented: Binding<Bool>?,
Expand All @@ -57,6 +58,7 @@ public protocol ContainerViewManagementForEnvironment {
/// - Returns: container view ID
func show<Content>(
view: Content,
with ID: UUID?,
in container: String,
using configuration: ContainerViewConfigurationProtocol,
animated: Bool
Expand All @@ -68,6 +70,7 @@ public protocol ContainerViewManagementForEnvironment {
/// - Returns: container view ID
func show<Content>(
containerView: Content,
with ID: UUID?,
in container: String,
animated: Bool
) -> UUID? where Content: ContainerView
Expand Down
88 changes: 62 additions & 26 deletions Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable> = []
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<AnyCancellable> = []
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<AnyCancellable> = []
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 {
Expand Down

0 comments on commit 29b1736

Please sign in to comment.