-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sam/vpn-ui-improvements
* main: Subscriptions: Implement Caching for Subscription Info (#710) add history to ios (#693) Reports on toggle protections off (#696) Move vpnFirstEnabled and networkPathChange out of VPNSettings (#707) Implement simple Subscription Caching mechanism using UserDefaults (#703)
- Loading branch information
Showing
54 changed files
with
2,539 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Sources/BrowserServicesKit/ContentBlocking/ToggleProtectionsCounter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// ToggleProtectionsCounter.swift | ||
// | ||
// Copyright © 2024 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 Foundation | ||
import Persistence | ||
import Common | ||
|
||
public enum ToggleProtectionsCounterEvent { | ||
|
||
enum Parameter { | ||
|
||
static let onCountKey = "onCount" | ||
static let offCountKey = "offCount" | ||
|
||
} | ||
|
||
case toggleProtectionsCounterDaily | ||
|
||
} | ||
|
||
/// This class aggregates protection toggles and stores that count over 24 hours. | ||
public class ToggleProtectionsCounter { | ||
|
||
public enum Constant { | ||
|
||
public static let onCountKey = "ToggleProtectionsCounter_On_Count" | ||
public static let offCountKey = "ToggleProtectionsCounter_Off_Count" | ||
public static let lastSendAtKey = "ToggleProtectionsCounter_Date" | ||
public static let sendInterval: Double = 60 * 60 * 24 // 24 hours | ||
|
||
} | ||
|
||
private let store: KeyValueStoring | ||
private let sendInterval: TimeInterval | ||
private let eventReporting: EventMapping<ToggleProtectionsCounterEvent>? | ||
|
||
public init(store: KeyValueStoring = ToggleProtectionsCounterStore(), | ||
sendInterval: TimeInterval = Constant.sendInterval, | ||
eventReporting: EventMapping<ToggleProtectionsCounterEvent>?) { | ||
self.store = store | ||
self.sendInterval = sendInterval | ||
self.eventReporting = eventReporting | ||
} | ||
|
||
public func onToggleOn(currentTime: Date = Date()) { | ||
save(toggleOnCount: toggleOnCount + 1) | ||
sendEventsIfNeeded(currentTime: currentTime) | ||
} | ||
|
||
public func onToggleOff(currentTime: Date = Date()) { | ||
save(toggleOffCount: toggleOffCount + 1) | ||
sendEventsIfNeeded(currentTime: currentTime) | ||
} | ||
|
||
public func sendEventsIfNeeded(currentTime: Date = Date()) { | ||
guard let lastSendAt else { | ||
save(lastSendAt: currentTime) | ||
return | ||
} | ||
|
||
if currentTime.timeIntervalSince(lastSendAt) > sendInterval { | ||
eventReporting?.fire(.toggleProtectionsCounterDaily, parameters: [ | ||
ToggleProtectionsCounterEvent.Parameter.onCountKey: String(toggleOnCount), | ||
ToggleProtectionsCounterEvent.Parameter.offCountKey: String(toggleOffCount) | ||
]) | ||
resetStats(currentTime: currentTime) | ||
} | ||
} | ||
|
||
private func resetStats(currentTime: Date = Date()) { | ||
save(toggleOnCount: 0) | ||
save(toggleOffCount: 0) | ||
save(lastSendAt: currentTime) | ||
} | ||
|
||
// MARK: - Store | ||
|
||
private var lastSendAt: Date? { store.object(forKey: Constant.lastSendAtKey) as? Date } | ||
private var toggleOnCount: Int { store.object(forKey: Constant.onCountKey) as? Int ?? 0 } | ||
private var toggleOffCount: Int { store.object(forKey: Constant.offCountKey) as? Int ?? 0 } | ||
|
||
private func save(lastSendAt: Date) { | ||
store.set(lastSendAt, forKey: Constant.lastSendAtKey) | ||
} | ||
|
||
private func save(toggleOnCount: Int) { | ||
store.set(toggleOnCount, forKey: Constant.onCountKey) | ||
} | ||
|
||
private func save(toggleOffCount: Int) { | ||
store.set(toggleOffCount, forKey: Constant.offCountKey) | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
Sources/BrowserServicesKit/ContentBlocking/ToggleProtectionsCounterStore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// ToggleProtectionsCounterStore.swift | ||
// | ||
// Copyright © 2024 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 Foundation | ||
import Persistence | ||
|
||
public struct ToggleProtectionsCounterStore: KeyValueStoring { | ||
|
||
private var userDefaults: UserDefaults? { UserDefaults(suiteName: "com.duckduckgo.app.toggleProtectionsCounter") } | ||
|
||
public init() {} | ||
|
||
public func object(forKey defaultName: String) -> Any? { userDefaults?.object(forKey: defaultName) } | ||
public func set(_ value: Any?, forKey defaultName: String) { userDefaults?.set(value, forKey: defaultName) } | ||
public func removeObject(forKey defaultName: String) { userDefaults?.removeObject(forKey: defaultName) } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Sources/History/CoreData/BrowsingHistory.xcdatamodeld/BrowsingHistory.xcdatamodel/contents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22522" systemVersion="23B92" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier=""> | ||
<entity name="BrowsingHistoryEntryManagedObject" representedClassName="BrowsingHistoryEntryManagedObject" syncable="YES"> | ||
<attribute name="blockedTrackingEntities" optional="YES" attributeType="String"/> | ||
<attribute name="failedToLoad" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/> | ||
<attribute name="identifier" attributeType="UUID" usesScalarValueType="NO"/> | ||
<attribute name="lastVisit" attributeType="Date" usesScalarValueType="NO"/> | ||
<attribute name="numberOfTotalVisits" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" elementID="numberOfVisits"/> | ||
<attribute name="numberOfTrackersBlocked" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/> | ||
<attribute name="title" optional="YES" attributeType="String" valueTransformerName="NSStringTransformer"/> | ||
<attribute name="trackersFound" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/> | ||
<attribute name="url" attributeType="URI" valueTransformerName="NSURLTransformer"/> | ||
<relationship name="visits" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="PageVisitManagedObject" inverseName="historyEntry" inverseEntity="PageVisitManagedObject"/> | ||
</entity> | ||
<entity name="PageVisitManagedObject" representedClassName="PageVisitManagedObject" syncable="YES"> | ||
<attribute name="date" attributeType="Date" usesScalarValueType="NO"/> | ||
<relationship name="historyEntry" maxCount="1" deletionRule="Nullify" destinationEntity="BrowsingHistoryEntryManagedObject" inverseName="visits" inverseEntity="BrowsingHistoryEntryManagedObject"/> | ||
</entity> | ||
</model> |
Oops, something went wrong.