Skip to content

Commit

Permalink
NetP: Reenable loading toggle after 2 sec delay (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme authored Jan 9, 2024
1 parent 89ed900 commit ecde50f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo/NetworkProtectionStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import NetworkProtection

@available(iOS 15, *)
struct NetworkProtectionStatusView: View {
@ObservedObject public var statusModel: NetworkProtectionStatusViewModel
@StateObject public var statusModel: NetworkProtectionStatusViewModel

var body: some View {
List {
Expand Down
20 changes: 18 additions & 2 deletions DuckDuckGo/NetworkProtectionStatusViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
private let serverInfoObserver: ConnectionServerInfoObserver
private let errorObserver: ConnectionErrorObserver
private var cancellables: Set<AnyCancellable> = []
private var delayedToggleReenableCancellable: Cancellable?

// MARK: Error

Expand Down Expand Up @@ -146,11 +147,24 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
}

private func setUpDisableTogglePublisher() {
statusObserver.publisher
.map { $0.isLoading }
let isLoadingPublisher = statusObserver.publisher.map { $0.isLoading }

isLoadingPublisher
.receive(on: DispatchQueue.main)
.assign(to: \.shouldDisableToggle, onWeaklyHeld: self)
.store(in: &cancellables)

// Set up a delayed publisher to fire just once that reenables the toggle
// Each event cancels the previous delayed publisher
isLoadingPublisher
.filter { $0 }
.map {
Just(!$0)
.delay(for: 2.0, scheduler: DispatchQueue.main)
.assign(to: \.shouldDisableToggle, onWeaklyHeld: self)
}
.assign(to: \.delayedToggleReenableCancellable, onWeaklyHeld: self)
.store(in: &cancellables)
}

private func setUpServerInfoPublishers() {
Expand All @@ -177,6 +191,8 @@ final class NetworkProtectionStatusViewModel: ObservableObject {

@MainActor
func didToggleNetP(to enabled: Bool) async {
shouldDisableToggle = true

// This is to prevent weird looking animations on navigating to the screen.
// It makes sense as animations should mostly only happen when a user has interacted.
animationsOn = true
Expand Down

0 comments on commit ecde50f

Please sign in to comment.