8000 [PM-20171] Fix ViewItemView retain cycle by andrebispo5 · Pull Request #1581 · bitwarden/ios · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[PM-20171] Fix ViewItemView retain cycle #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ enum ViewItemAction: Equatable, Sendable {
/// The visibility button was pressed for the specified custom field.
case customFieldVisibilityPressed(CustomFieldState)

/// The view item disappeared from the screen.
case disappeared

/// The dismiss button was pressed.
case dismissPressed

Expand Down Expand Up @@ -57,7 +60,8 @@ enum ViewItemAction: Equatable, Sendable {
true
case let .copyPressed(_, field):
field.requiresMasterPasswordReprompt
case .dismissPressed,
case .disappeared,
.dismissPressed,
.passwordHistoryPressed,
.toastShown:
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ViewItemActionTests: BitwardenTestCase {
.requiresMasterPasswordReprompt
)

XCTAssertFalse(ViewItemAction.disappeared.requiresMasterPasswordReprompt)
XCTAssertFalse(ViewItemAction.dismissPressed.requiresMasterPasswordReprompt)

XCTAssertTrue(ViewItemAction.downloadAttachment(.fixture()).requiresMasterPasswordReprompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ final class ViewItemProcessor: StateProcessor<ViewItemState, ViewItemAction, Vie
/// The services used by this processor.
private let services: Services

/// The task that streams cipher details.
private(set) var streamCipherDetailsTask: Task<Void, Never>?

// MARK: Initialization

/// Creates a new `ViewItemProcessor`.
Expand Down Expand Up @@ -95,7 +98,10 @@ final class ViewItemProcessor: StateProcessor<ViewItemState, ViewItemAction, Vie
override func perform(_ effect: ViewItemEffect) async {
switch effect {
case .appeared:
await streamCipherDetails()
streamCipherDetailsTask?.cancel()
streamCipherDetailsTask = Task {
await streamCipherDetails()
}
case .checkPasswordPressed:
do {
guard let password = state.loadingState.data?.cipher.login?.password else { return }
Expand Down Expand Up @@ -155,6 +161,9 @@ final class ViewItemProcessor: StateProcessor<ViewItemState, ViewItemAction, Vie
)
}
}
case .disappeared:
streamCipherDetailsTask?.cancel()
streamCipherDetailsTask = nil
case .dismissPressed:
coordinator.navigate(to: .dismiss())
case let .downloadAttachment(at 8000 tachment):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class ViewItemProcessorTests: BitwardenTestCase { // swiftlint:disable:this type
)
vaultRepository.cipherDetailsSubject.send(cipherItem)

XCTAssertNil(subject.streamCipherDetailsTask)
let task = Task {
await subject.perform(.appeared)
}
Expand All @@ -172,6 +173,7 @@ class ViewItemProcessorTests: BitwardenTestCase { // swiftlint:disable:this type

expectedState.allUserCollections = collections

XCTAssertNotNil(subject.streamCipherDetailsTask)
XCTAssertTrue(subject.state.hasPremiumFeatures)
XCTAssertTrue(subject.state.hasMasterPassword)
XCTAssertFalse(subject.state.restrictCipherItemDeletionFlagEnabled)
Expand Down DC24 Expand Up @@ -781,6 +783,25 @@ class ViewItemProcessorTests: BitwardenTestCase { // swiftlint:disable:this type
XCTAssertEqual(coordinator.routes.last, .dismiss())
}

/// `receive` with `.disappeared` should clear streamCipherDetailsTask.
@MainActor
func test_receive_disappearPressed() {
let account = Account.fixture()
stateService.activeAccount = account

XCTAssertNil(subject.streamCipherDetailsTask)
let task = Task {
await subject.perform(.appeared)
}

waitFor(subject.state.loadingState != .loading(nil))
task.cancel()

XCTAssertNotNil(subject.streamCipherDetailsTask)
subject.receive(.disappeared)
XCTAssertNil(subject.streamCipherDetailsTask)
}

/// `perform(_:)` with `.deletePressed` presents the confirmation alert before delete the item and displays
/// generic error alert if soft deleting fails.
@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ struct ViewItemView: View {
await store.perform(.appeared)
}
}
.onDisappear {
store.send(.disappeared)
}
}

// MARK: Private Views
Expand Down
0