Skip to content

Commit

Permalink
Add dismissTopmostView method.
Browse files Browse the repository at this point in the history
update Readme
  • Loading branch information
fatbobman committed Mar 21, 2022
1 parent a1ba73d commit 1e403ba
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ The methods currently provided by the Container Manager are.
* dismissAllView(in containers: [String], onlyShowing: Bool, animated flag: Bool)

Dismiss all views in the specified containers

* dismissTopmostView(in containers: [String], animated flag: Bool)

Dismiss the top view in specified containers

### Blockable animation

Expand Down
4 changes: 4 additions & 0 deletions READMECN.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ struct ContentView1: View {

撤销指定容器内的所有视图

* dismissTopmostView(in containers: [String], animated flag: Bool)

撤销指定容器的顶视图

### 可屏蔽动画

无论是直接调用容器管理器还是使用 View modifier,当将 animated 设为 false 时,均可强制取消转场动画。
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftUIOverlayContainer/Container/QueueHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ extension ContainerQueueHandler {
}
}

func dismissTopmostView(animated flag: Bool) {
if let theTopView = mainQueue.first {
dismiss(id: theTopView.id, animated: flag)
}
}

/// Push view into specific queue
func pushViewIntoQueue(_ identifiableView: IdentifiableContainerView, queue: QueueType, animated flag: Bool = true) {
switch queue {
Expand Down Expand Up @@ -239,6 +245,8 @@ extension ContainerQueueHandler {
dismissAll(animated: animated)
case .dismissShowing(let animated):
dismissMainQueue(animated: animated)
case .dismissTopmostView(let animated):
dismissTopmostView(animated: animated)
}
}

Expand All @@ -256,6 +264,8 @@ extension ContainerQueueHandler {
dismissAll(animated: animated)
case .dismissShowing(let animated):
dismissMainQueue(animated: animated)
case .dismissTopmostView(let animated):
dismissTopmostView(animated: animated)
}
}

Expand All @@ -277,6 +287,8 @@ extension ContainerQueueHandler {
dismissAll(animated: animated)
case .dismissShowing(let animated):
dismissMainQueue(animated: animated)
case .dismissTopmostView(let animated):
dismissTopmostView(animated: animated)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ enum OverlayContainerAction {
///
/// Pass false to disable animation of transition
case dismissShowing(Bool)
/// Dismiss the top view in the container
///
/// Pass false to disable animation of transition
case dismissTopmostView(Bool)
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ extension ContainerManager: ContainerViewManagementForEnvironment {
}
}
}

/// Dismiss the top view in the containers
/// - Parameters:
/// - containers: container names
/// - flag: Pass false, disable animation when dismiss the view
public func dismissTopmostView(in containers: [String], animated flag: Bool) {
for container in containers {
if let publisher = getPublisher(for: container) {
publisher.upstream.send(.dismissTopmostView(flag))
}
}
}
}

public extension ContainerManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public protocol ContainerViewManagementForEnvironment {
/// - containers: container names
/// - flag: pass true to animate the transition
func dismissAllView(in containers: [String], onlyShowing: Bool, animated flag: Bool)

/// Dismiss the top view in containers
/// - Parameters:
/// - container: container names
/// - flag: pass true to animate the transition
func dismissTopmostView(in containers: [String], animated flag: Bool)
}

/// A type defines logging behavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ class QueueHandlerForMultipleUnitTests: XCTestCase {
XCTAssertEqual(handler.mainQueue.count, 0)
}

func testDismissTopmostView() throws {
// given
let view1 = IdentifiableContainerView(
id: UUID(), view: MessageView(), viewConfiguration: MessageView(), isPresented: nil
)
let view2 = IdentifiableContainerView(
id: UUID(), view: MessageView(), viewConfiguration: MessageView(), isPresented: nil
)

let perform = handler.getStrategyHandler(for: .multiple)

// when
perform(.show(view1, false))
perform(.show(view2, false))

// dismiss view
perform(.dismissTopmostView(false))

// then
XCTAssertEqual(handler.mainQueue.count, 1)
XCTAssertEqual(handler.mainQueue.first?.id, view2.id)
}

func testShowViewAfterConnect() async throws {
// given
let view1 = MessageView()
Expand Down Expand Up @@ -298,7 +321,6 @@ class QueueHandlerForMultipleUnitTests: XCTestCase {
XCTAssertEqual(handler.mainQueue.count, 3)
XCTAssertEqual(handler.tempQueue.count, 1)
}

}

struct ContainerConfiguration: ContainerConfigurationProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ class QueueHandlerForOneByOneTests: XCTestCase {
XCTAssertEqual(handler.mainQueue.count, 0)
}

func testDismissTopmostView() throws {
// given
let view = MessageView()
let identifiableView = IdentifiableContainerView(
id: UUID(), view: view, viewConfiguration: view, isPresented: nil
)
let perform = handler.getStrategyHandler(for: .oneByOne)

// when
perform(.show(identifiableView, true))
perform(.dismissTopmostView(false))

// then
XCTAssertEqual(handler.mainQueue.count, 0)
}

func testDismissAllView() throws {
// given
let view = MessageView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ class QueueHandlerForOneByeOneWaitFinishTests: XCTestCase {
XCTAssertEqual(handler.tempQueue.count, 0)
}

func testDismissTopmostView() async throws {
// given
let view = MessageView()
let identifiableView1 = IdentifiableContainerView(
id: UUID(), view: view, viewConfiguration: view, isPresented: nil
)
let identifiableView2 = IdentifiableContainerView(
id: UUID(), view: view, viewConfiguration: view, isPresented: nil
)

let perform = handler.getStrategyHandler(for: .oneByOneWaitFinish)

// when
perform(.show(identifiableView1, false))
perform(.show(identifiableView2, false))
perform(.dismissTopmostView(false))

// then
XCTAssertEqual(handler.mainQueue.count, 1)
XCTAssertEqual(handler.tempQueue.count, 0)
XCTAssertEqual(handler.mainQueue.first?.id, identifiableView2.id)
}

func testDismissShowingView() throws {
// given
let view = MessageView()
Expand Down

0 comments on commit 1e403ba

Please sign in to comment.