Skip to content

Commit 29b1736

Browse files
committed
User can set a specific ID for view that needs to be displayed
Give developers more flexibility to control views
1 parent 8c4c460 commit 29b1736

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ extension ContainerManager: ContainerViewManagementForViewModifier {
111111
@discardableResult
112112
func _show<Content>(
113113
view: Content,
114+
with ID: UUID? = nil,
114115
in container: String,
115116
using configuration: ContainerViewConfigurationProtocol,
116117
isPresented: Binding<Bool>? = nil,
@@ -119,7 +120,7 @@ extension ContainerManager: ContainerViewManagementForViewModifier {
119120
guard let publisher = getPublisher(for: container) else {
120121
return nil
121122
}
122-
let viewID = UUID()
123+
let viewID = ID ?? UUID() // If no specific ID is given, generate a new ID
123124
let identifiableContainerView = IdentifiableContainerView(
124125
id: viewID,
125126
view: view,
@@ -149,6 +150,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment {
149150
@discardableResult
150151
public func show<Content>(
151152
view: Content,
153+
with ID: UUID? = nil,
152154
in container: String,
153155
using configuration: ContainerViewConfigurationProtocol,
154156
animated: Bool = true
@@ -163,6 +165,7 @@ extension ContainerManager: ContainerViewManagementForEnvironment {
163165
@discardableResult
164166
public func show<Content>(
165167
containerView: Content,
168+
with ID: UUID? = nil,
166169
in container: String,
167170
animated: Bool = true
168171
) -> UUID? where Content: ContainerView {

Sources/SwiftUIOverlayContainer/ContainerManager/ContainerManagerProtocols.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protocol ContainerViewManagementForViewModifier {
3636
/// - Returns: container view ID
3737
@discardableResult
3838
func _show<Content: View>(view: Content,
39+
with ID: UUID?,
3940
in container: String,
4041
using configuration: ContainerViewConfigurationProtocol,
4142
isPresented: Binding<Bool>?,
@@ -57,6 +58,7 @@ public protocol ContainerViewManagementForEnvironment {
5758
/// - Returns: container view ID
5859
func show<Content>(
5960
view: Content,
61+
with ID: UUID?,
6062
in container: String,
6163
using configuration: ContainerViewConfigurationProtocol,
6264
animated: Bool
@@ -68,6 +70,7 @@ public protocol ContainerViewManagementForEnvironment {
6870
/// - Returns: container view ID
6971
func show<Content>(
7072
containerView: Content,
73+
with ID: UUID?,
7174
in container: String,
7275
animated: Bool
7376
) -> UUID? where Content: ContainerView

Tests/SwiftUIOverlayContainerTests/ContainerManagerTests.swift

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,69 @@ class ContainerManagerTests: XCTestCase {
8585
}
8686

8787
func testSendView() throws {
88-
// given
89-
let containerName = "message"
90-
let messageView = MessageView()
91-
let publisher = manager.registerContainer(for: containerName)
92-
let expectation = XCTestExpectation(description: "get view from container 1")
93-
var cancellable: Set<AnyCancellable> = []
94-
manager.debugLevel = 2
95-
var resultID: UUID?
96-
97-
// when
98-
publisher
99-
.sink(receiveValue: { action in
100-
switch action {
101-
case .show(let identifiableView, _):
102-
resultID = identifiableView.id
103-
expectation.fulfill()
104-
default:
105-
break
106-
}
107-
})
108-
.store(in: &cancellable)
109-
110-
let viewID = manager.show(view: messageView, in: containerName, using: messageView)
88+
func testSendView() throws {
89+
// given
90+
let containerName = "message"
91+
let messageView = MessageView()
92+
let publisher = manager.registerContainer(for: containerName)
93+
let expectation = XCTestExpectation(description: "get view from container 1")
94+
var cancellable: Set<AnyCancellable> = []
95+
manager.debugLevel = 2
96+
var resultID: UUID?
97+
98+
// when
99+
publisher
100+
.sink(receiveValue: { action in
101+
switch action {
102+
case .show(let identifiableView, _):
103+
resultID = identifiableView.id
104+
expectation.fulfill()
105+
default:
106+
break
107+
}
108+
})
109+
.store(in: &cancellable)
110+
111+
let viewID = manager.show(view: messageView, in: containerName, using: messageView)
112+
113+
// then
114+
wait(for: [expectation], timeout: 2)
115+
XCTAssertEqual(resultID, viewID)
116+
}
117+
}
111118

112-
// then
113-
wait(for: [expectation], timeout: 2)
114-
XCTAssertEqual(resultID, viewID)
119+
func testSendViewID() throws {
120+
func testSendView() throws {
121+
// given
122+
let containerName = "message"
123+
let messageView = MessageView()
124+
let publisher = manager.registerContainer(for: containerName)
125+
let expectation = XCTestExpectation(description: "get view from container 1")
126+
var cancellable: Set<AnyCancellable> = []
127+
manager.debugLevel = 2
128+
var resultIDFromSink: UUID?
129+
let viewID = UUID()
130+
131+
// when
132+
publisher
133+
.sink(receiveValue: { action in
134+
switch action {
135+
case .show(let identifiableView, _):
136+
resultIDFromSink = identifiableView.id
137+
expectation.fulfill()
138+
default:
139+
break
140+
}
141+
})
142+
.store(in: &cancellable)
143+
144+
let viewIDFromShow = manager.show(view: messageView, in: containerName, using: messageView)
145+
146+
// then
147+
wait(for: [expectation], timeout: 2)
148+
XCTAssertEqual(viewID, viewIDFromShow)
149+
XCTAssertEqual(resultIDFromSink, viewID)
150+
}
115151
}
116152

117153
func testSendViewWithGetPublisher() throws {

0 commit comments

Comments
 (0)