Refactoring I.

- Revamp whole DB to Display flow
- Filter Pipeline, arbitrary filtering and sorting
- Binary tree arrays for faster lookup & manipulation
- DB: introducing custom functions
- DB scheme: split req into heap & cache
- cache written by GlassVPN only
- heap written by Main App only
- Introducing DB separation: DBCore, DBCommon, DBAppOnly
- Introducing DB data sources: TestDataSource, GroupedDomainDataSource, RecordingsDB, DomainFilter
- Background sync: Move entries from cache to heap and notify all observers
- GlassVPN: Binary tree filter lookup
- GlassVPN: Reusing prepared statement
This commit is contained in:
relikd
2020-06-02 21:45:08 +02:00
parent 10b43a0f67
commit b17fb3c354
36 changed files with 2214 additions and 1482 deletions

View File

@@ -1,23 +1,36 @@
import UIKit
class TVCFilter: UITableViewController, EditActionsRemove {
var currentFilter: FilterOptions = .none
var currentFilter: FilterOptions = .none // set by segue
private var dataSource: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// if #available(iOS 10.0, *) {
// tableView.refreshControl = UIRefreshControl(call: #selector(reloadDataSource), on: self)
// }
NotifyDNSFilterChanged.observe(call: #selector(reloadDataSource), on: self)
NotifyDNSFilterChanged.observe(call: #selector(didChangeDomainFilter), on: self)
reloadDataSource()
}
@objc func reloadDataSource() {
dataSource = DBWrp.dataF_list(currentFilter)
func reloadDataSource() {
dataSource = DomainFilter.list(where: currentFilter)
tableView.reloadData()
}
@objc func didChangeDomainFilter(_ notification: Notification) {
guard let domain = notification.object as? String else {
reloadDataSource()
return
}
if DomainFilter[domain]?.contains(currentFilter) ?? false {
let i = dataSource.binTreeIndex(of: domain, compare: (<))!
if i >= dataSource.count || dataSource[i] != domain {
dataSource.insert(domain, at: i)
tableView.safeInsertRow(i)
}
} else if let i = dataSource.binTreeRemove(domain, compare: (<)) {
tableView.safeDeleteRows([i])
}
}
@IBAction private func addNewFilter() {
let desc: String
switch currentFilter {
@@ -33,7 +46,7 @@ class TVCFilter: UITableViewController, EditActionsRemove {
ErrorAlert("Entered domain is not valid. Filter can't match country TLD only.").presentIn(self)
return
}
DBWrp.updateFilter(dom, add: self.currentFilter)
DomainFilter.update(dom, add: self.currentFilter)
}
alert.addTextField {
$0.placeholder = "cdn.domain.tld"
@@ -42,7 +55,7 @@ class TVCFilter: UITableViewController, EditActionsRemove {
alert.presentIn(self)
}
// MARK: - Table View Delegate
// MARK: - Table View Data Source
override func tableView(_ _: UITableView, numberOfRowsInSection _: Int) -> Int { dataSource.count }
@@ -57,11 +70,17 @@ class TVCFilter: UITableViewController, EditActionsRemove {
// MARK: - Editing
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
getRowActionsIOS9(indexPath)
}
@available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
getRowActionsIOS11(indexPath)
}
func editableRowCallback(_ index: IndexPath, _ action: RowAction, _ userInfo: Any?) -> Bool {
let domain = dataSource[index.row]
DBWrp.updateFilter(domain, remove: currentFilter)
dataSource.remove(at: index.row)
tableView.deleteRows(at: [index], with: .automatic)
DomainFilter.update(domain, remove: currentFilter)
return true
}

View File

@@ -16,11 +16,9 @@ class TVCSettings: UITableViewController {
}
@objc func reloadDataSource() {
let (blocked, ignored) = DBWrp.dataF_counts()
DispatchQueue.main.async {
self.cellDomainsIgnored.detailTextLabel?.text = "\(ignored) Domains"
self.cellDomainsBlocked.detailTextLabel?.text = "\(blocked) Domains"
}
let (blocked, ignored) = DomainFilter.counts()
cellDomainsIgnored.detailTextLabel?.text = "\(ignored) Domains"
cellDomainsBlocked.detailTextLabel?.text = "\(blocked) Domains"
}
@IBAction func toggleVPNProxy(_ sender: UISwitch) {
@@ -28,28 +26,8 @@ class TVCSettings: UITableViewController {
}
@IBAction func exportDB(_ sender: Any) {
// TODO: export partly?
// TODO: show header-banner of success
// Share Sheet
let sheet = UIActivityViewController(activityItems: [URL.internalDB()], applicationActivities: nil)
self.present(sheet, animated: true)
// Save to Files app
// self.present(UIDocumentPickerViewController(url: FileManager.default.internalDB(), in: .exportToService), animated: true)
// Shows Alert and exports to Documents directory
// AskAlert(title: "Export results?", text: """
// This action will copy the internal database to the app's local Documents directory. You can use the Files app to access the database file.
//
// Note: This will make your DNS requests available to other apps!
// """, buttonText: "Export") {
// do {
// let dest = try SQLiteDatabase.export()
// let folder = dest.deletingLastPathComponent()
// let out = folder.lastPathComponent + "/" + dest.lastPathComponent
// Alert(title: "Successful", text: "File exported to '\(out)'", buttonText: "OK").presentIn(self)
// } catch {
// ErrorAlert(error).presentIn(self)
// }
// }.presentIn(self)
}
@IBAction func resetTutorialAlerts(_ sender: UIButton) {
@@ -64,7 +42,10 @@ class TVCSettings: UITableViewController {
"You are about to delete all results that have been logged in the past. " +
"Your preferences for blocked and ignored domains are preserved.\n" +
"Continue?", buttonText: "Delete", buttonStyle: .destructive) { _ in
DBWrp.deleteHistory()
DispatchQueue.global().async {
try? AppDB?.dnsLogsDeleteAll()
NotifyLogHistoryReset.postAsyncMain()
}
}.presentIn(self)
}