diff --git a/main/Base.lproj/Main.storyboard b/main/Base.lproj/Main.storyboard index 7b1ea0c..81b3fcb 100644 --- a/main/Base.lproj/Main.storyboard +++ b/main/Base.lproj/Main.storyboard @@ -178,7 +178,7 @@ - + @@ -202,7 +202,7 @@ - + @@ -339,7 +339,13 @@ - + + + + + + + diff --git a/main/Extensions/AlertSheet.swift b/main/Extensions/AlertSheet.swift index 91fab91..2bbdce4 100644 --- a/main/Extensions/AlertSheet.swift +++ b/main/Extensions/AlertSheet.swift @@ -2,19 +2,32 @@ import UIKit // MARK: Basic Alerts +/// - Parameters: +/// - buttonText: Default: "Dismiss" func Alert(title: String?, text: String?, buttonText: String = "Dismiss") -> UIAlertController { let alert = UIAlertController(title: title, message: text, preferredStyle: .alert) alert.addAction(UIAlertAction(title: buttonText, style: .cancel, handler: nil)) return alert } +/// - Parameters: +/// - buttonText: Default: "Dismiss" func ErrorAlert(_ error: Error, buttonText: String = "Dismiss") -> UIAlertController { return Alert(title: "Error", text: error.localizedDescription, buttonText: buttonText) } -func AskAlert(title: String?, text: String?, buttonText: String = "Continue", buttonStyle: UIAlertAction.Style = .default, action: @escaping () -> Void) -> UIAlertController { +/// - Parameters: +/// - buttonText: Default: "Dismiss" +func ErrorAlert(_ errorDescription: String, buttonText: String = "Dismiss") -> UIAlertController { + return Alert(title: "Error", text: errorDescription, buttonText: buttonText) +} + +/// - Parameters: +/// - buttonText: Default: "Continue" +/// - buttonStyle: Default: `.default` +func AskAlert(title: String?, text: String?, buttonText: String = "Continue", buttonStyle: UIAlertAction.Style = .default, action: @escaping (UIAlertController) -> Void) -> UIAlertController { let alert = Alert(title: title, text: text, buttonText: "Cancel") - alert.addAction(UIAlertAction(title: buttonText, style: buttonStyle) { _ in action() }) + alert.addAction(UIAlertAction(title: buttonText, style: buttonStyle) { _ in action(alert) }) return alert } diff --git a/main/Extensions/Generic.swift b/main/Extensions/Generic.swift index b9888c5..226c86b 100644 --- a/main/Extensions/Generic.swift +++ b/main/Extensions/Generic.swift @@ -55,15 +55,11 @@ extension String { ending = rld + "." + ending } return (domain: ending, host: parts.joined(separator: ".")) - -// var allDots = enumerated().compactMap { $1 == "." ? $0 : nil } -// let d1 = allDots.popLast() // we dont care about TLD -// guard let d2 = allDots.popLast() else { -// return (domain: self, host: nil) // no subdomains, just plain SLD -// } -// // TODO: check third level domains -//// let d3 = allDots.popLast() -// return (String(suffix(count - d2 - 1)), String(prefix(d2))) + } + /// Returns `true` if String matches list of known second level domains (e.g., `co.uk`). + func isKnownSLD() -> Bool { + let parts = components(separatedBy: ".") + return parts.count == 2 && listOfSLDs[parts.last!]?[parts.first!] ?? false } } diff --git a/main/Settings/TVCFilter.swift b/main/Settings/TVCFilter.swift index 47d48d9..2125f38 100644 --- a/main/Settings/TVCFilter.swift +++ b/main/Settings/TVCFilter.swift @@ -18,6 +18,27 @@ class TVCFilter: UITableViewController, EditActionsRemove { tableView.reloadData() } + @IBAction private func addNewFilter() { + let desc: String + switch currentFilter { + case .blocked: desc = "Enter the domain name you wish to block." + case .ignored: desc = "Enter the domain name you wish to ignore." + default: return + } + let alert = AskAlert(title: "Create new filter", text: desc, buttonText: "Add") { + guard let dom = $0.textFields?.first?.text else { + return + } + guard dom.contains("."), !dom.isKnownSLD() else { + ErrorAlert("Entered domain is not valid. Filter can't match country TLD only.").presentIn(self) + return + } + DBWrp.updateFilter(dom, add: self.currentFilter) + } + alert.addTextField { $0.placeholder = "cdn.domain.tld" } + alert.presentIn(self) + } + // MARK: - Table View Delegate override func tableView(_ _: UITableView, numberOfRowsInSection _: Int) -> Int { dataSource.count } diff --git a/main/Settings/TVCSettings.swift b/main/Settings/TVCSettings.swift index f8bf742..d2026b9 100644 --- a/main/Settings/TVCSettings.swift +++ b/main/Settings/TVCSettings.swift @@ -56,7 +56,7 @@ class TVCSettings: UITableViewController { AskAlert(title: "Clear results?", text: """ You are about to delete all results that have been logged in the past. Your preference for blocked and ignored domains is preserved. Continue? - """, buttonText: "Delete", buttonStyle: .destructive) { + """, buttonText: "Delete", buttonStyle: .destructive) { _ in DBWrp.deleteHistory() }.presentIn(self) }