Skip to content

Commit

Permalink
Move custom url to a separate screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Dec 15, 2023
1 parent 283e94c commit 5a1d922
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 135 deletions.
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@
CBD4F140279EBFB300B20FD7 /* SwiftUICollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1AEFB02799AA940031AE3D /* SwiftUICollectionViewCell.swift */; };
CBDD5DDF29A6736A00832877 /* APIHeadersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBDD5DDE29A6736A00832877 /* APIHeadersTests.swift */; };
CBDD5DE129A6741300832877 /* MockBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBDD5DE029A6741300832877 /* MockBundle.swift */; };
CBFCB30E2B2CD47800253E9E /* ConfigurationURLDebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBFCB30D2B2CD47800253E9E /* ConfigurationURLDebugViewController.swift */; };
D63657192A7BAE7C001AF19D /* EmailManagerRequestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63657182A7BAE7C001AF19D /* EmailManagerRequestDelegate.swift */; };
EA39B7E2268A1A35000C62CD /* privacy-reference-tests in Resources */ = {isa = PBXBuildFile; fileRef = EA39B7E1268A1A35000C62CD /* privacy-reference-tests */; };
EAB19EDA268963510015D3EA /* DomainMatchingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB19ED9268963510015D3EA /* DomainMatchingTests.swift */; };
Expand Down Expand Up @@ -2307,6 +2308,7 @@
CBF14FC227970072001D94D0 /* HomeMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMessageView.swift; sourceTree = "<group>"; };
CBF14FC427970AB0001D94D0 /* HomeMessageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMessageViewModel.swift; sourceTree = "<group>"; };
CBF14FC627970C8A001D94D0 /* HomeMessageCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMessageCollectionViewCell.swift; sourceTree = "<group>"; };
CBFCB30D2B2CD47800253E9E /* ConfigurationURLDebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationURLDebugViewController.swift; sourceTree = "<group>"; };
D63657182A7BAE7C001AF19D /* EmailManagerRequestDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmailManagerRequestDelegate.swift; sourceTree = "<group>"; };
EA39B7E1268A1A35000C62CD /* privacy-reference-tests */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = "privacy-reference-tests"; path = "submodules/privacy-reference-tests"; sourceTree = SOURCE_ROOT; };
EAB19ED9268963510015D3EA /* DomainMatchingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DomainMatchingTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3741,6 +3743,7 @@
F46FEC5627987A5F0061D9DF /* KeychainItemsDebugViewController.swift */,
983D71B02A286E810072E26D /* SyncDebugViewController.swift */,
EE72CA842A862D000043B5B3 /* NetworkProtectionDebugViewController.swift */,
CBFCB30D2B2CD47800253E9E /* ConfigurationURLDebugViewController.swift */,
);
name = Debug;
sourceTree = "<group>";
Expand Down Expand Up @@ -6258,6 +6261,7 @@
85582E0029D7409700E9AE35 /* SyncSettingsViewController+PDFRendering.swift in Sources */,
EE0153EF2A70021E002A8B26 /* NetworkProtectionInviteView.swift in Sources */,
9888F77B2224980500C46159 /* FeedbackViewController.swift in Sources */,
CBFCB30E2B2CD47800253E9E /* ConfigurationURLDebugViewController.swift in Sources */,
982686AD2600C0850011A8D6 /* ActionMessageView.swift in Sources */,
F446B9B5251150AC00324016 /* HomeMessageViewSectionRenderer.swift in Sources */,
98D98A8225ED88E300D8E3DF /* BrowsingMenuSeparatorViewCell.swift in Sources */,
Expand Down
113 changes: 0 additions & 113 deletions DuckDuckGo/ConfigurationDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,23 @@
import UIKit
import BackgroundTasks
import Core
import Configuration

class ConfigurationDebugViewController: UITableViewController {

private let titles = [
Sections.customURLs: "Custom URLs",
Sections.refreshInformation: "Background Refresh Info",
Sections.queuedTasks: "Queued Tasks (Earliest Execution Date)",
Sections.etags: "ETags"
]

enum Sections: Int, CaseIterable {

case customURLs
case refreshInformation
case queuedTasks
case etags

}

enum CustomURLsRows: Int, CaseIterable {

case privacyConfigURL

var title: String {
switch self {
case .privacyConfigURL: return "Privacy Config"
}
}

}

enum RefreshInformationRows: Int, CaseIterable {

case lastRefreshDate
Expand All @@ -77,18 +62,6 @@ class ConfigurationDebugViewController: UITableViewController {

}

private var customURLProvider = CustomConfigurationURLProvider()

@UserDefaultsWrapper(key: .privacyConfigCustomURL, defaultValue: nil)
private var privacyConfigCustomURL: String? {
didSet {
customURLProvider.customPrivacyConfigurationURL = privacyConfigCustomURL.flatMap { URL(string: $0) }
Configuration.setURLProvider(customURLProvider)
lastConfigurationRefreshDate = Date.distantPast
fetchAssets()
}
}

private func fetchAssets() {
AppConfigurationFetch().start(isDebug: true) { [weak tableView] result in
switch result {
Expand All @@ -105,18 +78,6 @@ class ConfigurationDebugViewController: UITableViewController {
}
}

private func customURL(for row: CustomURLsRows) -> String? {
switch row {
case .privacyConfigURL: return privacyConfigCustomURL
}
}

private func setCustomURL(_ urlString: String?, for row: CustomURLsRows) {
switch row {
case .privacyConfigURL: privacyConfigCustomURL = urlString
}
}

@UserDefaultsWrapper(key: .lastConfigurationRefreshDate, defaultValue: .distantPast)
private var lastConfigurationRefreshDate: Date
private var queuedTasks: [BGTaskRequest] = []
Expand Down Expand Up @@ -155,12 +116,6 @@ class ConfigurationDebugViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
switch Sections(rawValue: indexPath.section) {

case .customURLs:
let row = CustomURLsRows(rawValue: indexPath.row)!
cell.textLabel?.text = row.title
cell.detailTextLabel?.text = customURL(for: row)
cell.accessoryView = configureSwitchView(for: row)
case .refreshInformation:
switch RefreshInformationRows(rawValue: indexPath.row) {
case .lastRefreshDate:
Expand Down Expand Up @@ -205,25 +160,8 @@ class ConfigurationDebugViewController: UITableViewController {
}
// swiftlint:enable cyclomatic_complexity

private func configureSwitchView(for row: CustomURLsRows) -> UISwitch {
let switchView = UISwitch(frame: .zero)
switchView.isOn = customURL(for: row) != nil
let action = UIAction() { [weak self] _ in
guard let self else { return }
if switchView.isOn {
presentCustomURLAlert(for: row)
} else {
setCustomURL(nil, for: row)
tableView.reloadData()
}
}
switchView.addAction(action, for: .valueChanged)
return switchView
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Sections(rawValue: section) {
case .customURLs: return CustomURLsRows.allCases.count
case .refreshInformation: return RefreshInformationRows.allCases.count
case .queuedTasks: return queuedTasks.isEmpty ? 1 : queuedTasks.count
case .etags: return ETagRows.allCases.count
Expand All @@ -234,8 +172,6 @@ class ConfigurationDebugViewController: UITableViewController {
// swiftlint:disable:next cyclomatic_complexity
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch Sections(rawValue: indexPath.section) {
case .customURLs:
presentCustomURLAlert(for: CustomURLsRows(rawValue: indexPath.row)!)
case .refreshInformation:
switch RefreshInformationRows(rawValue: indexPath.row) {
case .resetLastRefreshDate:
Expand All @@ -261,53 +197,4 @@ class ConfigurationDebugViewController: UITableViewController {
tableView.deselectRow(at: indexPath, animated: true)
}

func presentCustomURLAlert(for row: CustomURLsRows) {
let alert = UIAlertController(title: row.title, message: "Provide custom URL", preferredStyle: .alert)
alert.addTextField { textField in
textField.tag = row.rawValue
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
self.tableView.reloadData()
}
alert.addAction(cancelAction)

let submitAction = UIAlertAction(title: "Override", style: .default) { _ in
self.setCustomURL(alert.textFields?.first?.text, for: row)
self.tableView.reloadData()
}
alert.addAction(submitAction)
let cell = self.tableView.cellForRow(at: IndexPath(row: row.rawValue,
section: Sections.customURLs.rawValue))!
present(controller: alert, fromView: cell)
}

}

struct CustomConfigurationURLProvider: ConfigurationURLProviding {

var customBloomFilterSpecURL: URL?
var customBloomFilterBinaryURL: URL?
var customBloomFilterExcludedDomainsURL: URL?
var customPrivacyConfigurationURL: URL?
var customTrackerDataSetURL: URL?
var customSurrogatesURL: URL?
var customFBConfigURL: URL?

let defaultProvider = AppConfigurationURLProvider()

func url(for configuration: Configuration) -> URL {
let defaultURL = defaultProvider.url(for: configuration)
let customURL: URL?
switch configuration {
case .bloomFilterSpec: customURL = customBloomFilterSpecURL
case .bloomFilterBinary: customURL = customBloomFilterBinaryURL
case .bloomFilterExcludedDomains:customURL = customBloomFilterExcludedDomainsURL
case .privacyConfiguration:customURL = customPrivacyConfigurationURL
case .trackerDataSet:customURL = customTrackerDataSetURL
case .surrogates:customURL = customSurrogatesURL
case .FBConfig: customURL = nil
}
return customURL ?? defaultURL
}

}
Loading

0 comments on commit 5a1d922

Please sign in to comment.