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

Subscription pro pixels #2531

Merged
merged 21 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
20 changes: 10 additions & 10 deletions Core/AppLastCompiledRulesStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import BrowserServicesKit
import TrackerRadarKit

struct AppLastCompiledRules: LastCompiledRules, Codable {

var name: String
var trackerData: TrackerData
var etag: String
var identifier: ContentBlockerRulesIdentifier

}

protocol Storage {
Expand All @@ -36,15 +36,15 @@ protocol Storage {
}

struct LastCompiledRulesStorage: Storage {

private enum Const {
static let filename = "LastCompiledRules"
static let path = FileManager
.default
.containerURL(forSecurityApplicationGroupIdentifier: ContentBlockerStoreConstants.groupName)!
.appendingPathComponent(filename)
}

func persist(_ data: Data) -> Bool {
do {
try data.write(to: Const.path, options: .atomic)
Expand All @@ -53,25 +53,25 @@ struct LastCompiledRulesStorage: Storage {
return false
}
}

var data: Data? {
do {
return try Data(contentsOf: Const.path)
} catch {
return nil
}
}

}

final class AppLastCompiledRulesStore: LastCompiledRulesStore {

private var storage: Storage

init(with storage: Storage = LastCompiledRulesStorage()) {
self.storage = storage
}

var rules: [LastCompiledRules] {
guard
let data = storage.data,
Expand All @@ -88,10 +88,10 @@ final class AppLastCompiledRulesStore: LastCompiledRulesStore {
etag: rules.etag,
identifier: rules.identifier)
}

if !rules.isEmpty, let encodedRules = try? JSONEncoder().encode(rules) {
_ = storage.persist(encodedRules)
}
}

}
2 changes: 1 addition & 1 deletion Core/AppPrivacyConfigurationDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final public class AppPrivacyConfigurationDataProvider: EmbeddedDataProvider {
if let url = Bundle.main.url(forResource: "ios-config", withExtension: "json") {
return url
}

return Bundle(for: self).url(forResource: "ios-config", withExtension: "json")!
}

Expand Down
2 changes: 1 addition & 1 deletion Core/AppTrackerDataSetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final public class AppTrackerDataSetProvider: EmbeddedDataProvider {
if let url = Bundle.main.url(forResource: "trackerData", withExtension: "json") {
return url
}

return Bundle(for: Self.self).url(forResource: "trackerData", withExtension: "json")!
}

Expand Down
30 changes: 15 additions & 15 deletions Core/AppTrackingProtectionAllowlistModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ public class AppTrackingProtectionAllowlistModel {
public static let groupID = "\(Global.groupIdPrefix).apptp"
public static let fileName = "appTPallowlist"
}

private let filename: String

lazy private var allowlistUrl: URL? = {
let groupContainerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.groupID)
return groupContainerUrl?.appendingPathComponent(self.filename, conformingTo: .text)
}()

var allowedDomains: Set<String>

public init(filename: String = Constants.fileName) {
self.allowedDomains = Set<String>()
self.filename = filename

readFromFile()
}

func writeToFile() {
guard let allowlistUrl = allowlistUrl else {
fatalError("Unable to get file location")
}

// Write the allowlist as a textfile with one domain per line
do {
let string = allowedDomains.joined(separator: "\n")
Expand All @@ -59,15 +59,15 @@ public class AppTrackingProtectionAllowlistModel {
print(error.localizedDescription)
}
}

public func readFromFile() {
guard let allowlistUrl = allowlistUrl else {
fatalError("Unable to get file location")
}
guard FileManager.default.fileExists(atPath: allowlistUrl.path) else {
return
}

// Read allowlist from file. Break the string into array then cast to a set.
do {
let strData = try String(contentsOf: allowlistUrl)
Expand All @@ -77,30 +77,30 @@ public class AppTrackingProtectionAllowlistModel {
print(error.localizedDescription)
}
}

public func allow(domain: String) {
allowedDomains.insert(domain)
writeToFile()
}

public func contains(domain: String) -> Bool {
var check = domain
while check.contains(".") {
if allowedDomains.contains(check) {
return true
}

check = String(check.components(separatedBy: ".").dropFirst().joined(separator: "."))
}

return false
}

public func remove(domain: String) {
allowedDomains.remove(domain)
writeToFile()
}

public func clearList() {
allowedDomains.removeAll()
writeToFile()
Expand Down
6 changes: 3 additions & 3 deletions Core/AppTrackingProtectionFeedbackModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public class AppTrackingProtectionFeedbackModel: ObservableObject {
return []
}
}

public func sendReport(appName: String, category: String, description: String) {
let date = Calendar.current.date(byAdding: .minute, value: -10, to: Date())!
let trackers = trackers(moreRecentThan: date)
let trackersString = trackers.map { $0.domain }.joined(separator: ",")

let parameters = [
"appName": appName,
"category": category,
"description": description,
"blockedTrackers": trackersString
]

Pixel.fire(pixel: .appTPBreakageReport, withAdditionalParameters: parameters)
}

Expand Down
22 changes: 11 additions & 11 deletions Core/AppTrackingProtectionListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class AppTrackingProtectionListViewModel: NSObject, ObservableObject, NSF

@Published public var debugModeEnabled = false
@Published public var isOnboarding = false

// We only want to show "Manage Trackers" and "Report an issue" if the user has enabled AppTP at least once
@UserDefaultsWrapper(key: .appTPUsed, defaultValue: false)
public var appTPUsed {
Expand All @@ -49,21 +49,21 @@ public class AppTrackingProtectionListViewModel: NSObject, ObservableObject, NSF
queue.maxConcurrentOperationCount = 1
return queue
}()

private let relativeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateStyle = .medium
formatter.doesRelativeDateFormatting = true
return formatter
}()

private let relativeTimeFormatter: RelativeDateTimeFormatter = {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .short
return formatter
}()

private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd"
Expand All @@ -76,26 +76,26 @@ public class AppTrackingProtectionListViewModel: NSObject, ObservableObject, NSF
formatter.timeStyle = .medium
return formatter
}()

private let inputFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()

public func formattedDate(_ sectionName: String) -> String {
guard let date = inputFormatter.date(from: sectionName) else {
return "Invalid Date"
}

let relativeDate = relativeFormatter.string(from: date)
if relativeDate.rangeOfCharacter(from: .decimalDigits) != nil {
return dateFormatter.string(from: date)
}

return relativeDate
}

/// Returns a relative datestring for the given timestamp. e.g. "5 min. ago"
/// If the timestamp is within 1 second of the current time this function will return `nil`
/// A `nil` return value should be considered "just now".
Expand All @@ -108,7 +108,7 @@ public class AppTrackingProtectionListViewModel: NSObject, ObservableObject, NSF
// return nil here and replace it with UserText on the view side.
return nil
}

return relativeTimeFormatter.localizedString(for: timestamp, relativeTo: Date())
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public class AppTrackingProtectionListViewModel: NSObject, ObservableObject, NSF
self.context.stalenessInterval = 0

super.init()

self.isOnboarding = !appTPUsed

setupFetchedResultsController()
Expand Down
4 changes: 2 additions & 2 deletions Core/AtbAndVariantCleanup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class AtbAndVariantCleanup {

static func cleanup(statisticsStorage: StatisticsStore = StatisticsUserDefaults(),
variantManager: VariantManager = DefaultVariantManager()) {

guard let variant = statisticsStorage.variant else { return }

// clean up ATB
if let atb = statisticsStorage.atb, atb.hasSuffix(variant) {
statisticsStorage.atb = String(atb.dropLast(variant.count))
}

// remove existing variant if not in an active experiment
if variantManager.currentVariant == nil {
statisticsStorage.variant = nil
Expand Down
12 changes: 6 additions & 6 deletions Core/BookmarkObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ private struct Constants {

public protocol Bookmark: BookmarkItem {
var url: URL? { get set }

var displayTitle: String? { get }
}

public extension Bookmark {

var displayTitle: String? {
let host = url?.host?.droppingWwwPrefix() ?? url?.absoluteString

var displayTitle = (title?.isEmpty ?? true) ? host : title

if let url = url, url.isDuckDuckGo,
let title = displayTitle, title.hasSuffix(Constants.ddgSuffix) {
displayTitle = String(title.dropLast(Constants.ddgSuffix.count))
}

return displayTitle
}
}
Expand All @@ -58,7 +58,7 @@ public protocol BookmarkFolder: BookmarkItem {
}

public extension BookmarkFolder {

var numberOfChildrenDeep: Int {
guard let children = children else { return 0 }
return children.reduce(children.count) { $0 + (($1 as? BookmarkFolder)?.numberOfChildrenDeep ?? 0) }
Expand Down
Loading
Loading