Skip to content
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

Use VPNSettings for cohort storage #581

Merged
merged 3 commits into from
Dec 1, 2023
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
3 changes: 2 additions & 1 deletion Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,8 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
.setNotifyStatusChanges,
.setRegistrationKeyValidity,
.setSelectedEnvironment,
.setShowInMenuBar:
.setShowInMenuBar,
.setVPNFirstEnabled:
// Intentional no-op, as some setting changes don't require any further operation
break
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// UserDefaults+showInMenuBar.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Combine
import Foundation

extension UserDefaults {
private var vpnFirstEnabledKey: String {
"vpnFirstEnabled"
}

@objc
dynamic var vpnFirstEnabled: Date? {
get {
value(forKey: vpnFirstEnabledKey) as? Date
}

set {
set(newValue, forKey: vpnFirstEnabledKey)
}
}

var vpnFirstEnabledPublisher: AnyPublisher<Date?, Never> {
publisher(for: \.vpnFirstEnabled).eraseToAnyPublisher()
}
}
32 changes: 29 additions & 3 deletions Sources/NetworkProtection/Settings/VPNSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import Combine
import Foundation

// swiftlint:disable type_body_length
// swiftlint:disable type_body_length file_length

/// Persists and publishes changes to tunnel settings.
///
Expand All @@ -40,6 +40,7 @@ public final class VPNSettings {
case setSelectedLocation(_ selectedLocation: SelectedLocation)
case setSelectedEnvironment(_ selectedEnvironment: SelectedEnvironment)
case setShowInMenuBar(_ showInMenuBar: Bool)
case setVPNFirstEnabled(_ vpnFirstEnabled: Date?)
}

public enum RegistrationKeyValidity: Codable {
Expand Down Expand Up @@ -131,6 +132,10 @@ public final class VPNSettings {
Change.setShowInMenuBar(showInMenuBar)
}.eraseToAnyPublisher()

let vpnFirstEnabledPublisher = vpnFirstEnabledPublisher.map { vpnFirstEnabled in
Change.setVPNFirstEnabled(vpnFirstEnabled)
}.eraseToAnyPublisher()

return Publishers.MergeMany(
connectOnLoginPublisher,
includeAllNetworksPublisher,
Expand All @@ -140,7 +145,8 @@ public final class VPNSettings {
serverChangePublisher,
locationChangePublisher,
environmentChangePublisher,
showInMenuBarPublisher).eraseToAnyPublisher()
showInMenuBarPublisher,
vpnFirstEnabledPublisher).eraseToAnyPublisher()
}()

public init(defaults: UserDefaults) {
Expand All @@ -163,6 +169,7 @@ public final class VPNSettings {

// MARK: - Applying Changes

// swiftlint:disable cyclomatic_complexity
public func apply(change: Change) {
switch change {
case .setConnectOnLogin(let connectOnLogin):
Expand All @@ -185,8 +192,11 @@ public final class VPNSettings {
self.selectedEnvironment = selectedEnvironment
case .setShowInMenuBar(let showInMenuBar):
self.showInMenuBar = showInMenuBar
case .setVPNFirstEnabled(let vpnFirstEnabled):
self.vpnFirstEnabled = vpnFirstEnabled
}
}
// swiftlint:enable cyclomatic_complexity

// MARK: - Connect on Login

Expand Down Expand Up @@ -375,6 +385,22 @@ public final class VPNSettings {
}
}
}

// MARK: - First time VPN is enabled

public var vpnFirstEnabledPublisher: AnyPublisher<Date?, Never> {
defaults.vpnFirstEnabledPublisher
}

public var vpnFirstEnabled: Date? {
get {
defaults.vpnFirstEnabled
}

set {
defaults.vpnFirstEnabled = newValue
}
}
}

// swiftlint:enable type_body_length
// swiftlint:enable type_body_length file_length