From 448d69c6d8404e8b7fdb413d87fbf790ce407d77 Mon Sep 17 00:00:00 2001 From: relikd Date: Fri, 28 Aug 2020 22:05:49 +0200 Subject: [PATCH] Show "no results" in recordings + mark recording as shared --- main/AppDelegate.swift | 4 +++ main/DB/DBAppOnly.swift | 27 ++++++++++----- main/DB/DBCore.swift | 4 +++ main/Extensions/AlertSheet.swift | 8 ++--- main/Extensions/String.swift | 7 ++++ main/GUI/Base.lproj/Recordings.storyboard | 24 ++++++++++++-- main/Recordings/TVCRecordingDetails.swift | 40 +++++++++++++++++++---- main/Recordings/VCShareRecording.swift | 16 ++++++--- 8 files changed, 103 insertions(+), 27 deletions(-) diff --git a/main/AppDelegate.swift b/main/AppDelegate.swift index 45001a7..6b4f0c0 100644 --- a/main/AppDelegate.swift +++ b/main/AppDelegate.swift @@ -33,3 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // This is a known issue and tolerated. } } + +extension URL { + @discardableResult func open() -> Bool { UIApplication.shared.openURL(self) } +} diff --git a/main/DB/DBAppOnly.swift b/main/DB/DBAppOnly.swift index 7e10a08..9f62e32 100644 --- a/main/DB/DBAppOnly.swift +++ b/main/DB/DBAppOnly.swift @@ -22,9 +22,12 @@ extension SQLiteDatabase { } if version != 2 { // version 0 -> 1: req(domain) -> heap(fqdn, domain) - // version 1 -> 2: rec(+subtitle) + // version 1 -> 2: rec(+subtitle, +opt) if version == 1 { - try run(sql: "ALTER TABLE rec ADD COLUMN subtitle TEXT;") + transaction(""" + ALTER TABLE rec ADD COLUMN subtitle TEXT; + ALTER TABLE rec ADD COLUMN opt INTEGER; + """) } try run(sql: "PRAGMA user_version = 2;") } @@ -294,11 +297,13 @@ extension CreateTable { appid TEXT, title TEXT, subtitle TEXT, - notes TEXT + notes TEXT, + opt INTEGER ); """} } +let readRecordingSelect = "id, start, stop, appid, title, subtitle, notes, opt" struct Recording { let id: sqlite3_int64 let start: Timestamp @@ -307,6 +312,7 @@ struct Recording { var title: String? = nil var subtitle: String? = nil var notes: String? = nil + var shared: Bool = false } typealias AppBundleInfo = (bundleId: String, name: String?, author: String?) @@ -335,8 +341,9 @@ extension SQLiteDatabase { /// Update given recording by replacing `title`, `appid`, and `notes` with new values. func recordingUpdate(_ r: Recording) { - try? run(sql: "UPDATE rec SET appid = ?, title = ?, subtitle = ?, notes = ? WHERE id = ? LIMIT 1;", - bind: [BindTextOrNil(r.appId), BindTextOrNil(r.title), BindTextOrNil(r.subtitle), BindTextOrNil(r.notes), BindInt64(r.id)]) { stmt -> Void in + try? run(sql: "UPDATE rec SET appid = ?, title = ?, subtitle = ?, notes = ?, opt = ? WHERE id = ? LIMIT 1;", + bind: [BindTextOrNil(r.appId), BindTextOrNil(r.title), BindTextOrNil(r.subtitle), + BindTextOrNil(r.notes), r.shared ? BindInt32(1) : BindNull(), BindInt64(r.id)]) { stmt -> Void in sqlite3_step(stmt) } } @@ -355,18 +362,20 @@ extension SQLiteDatabase { private func readRecording(_ stmt: OpaquePointer) -> Recording { let end = col_ts(stmt, 2) + let opt = sqlite3_column_int(stmt, 7) return Recording(id: sqlite3_column_int64(stmt, 0), start: col_ts(stmt, 1), stop: end == 0 ? nil : end, appId: col_text(stmt, 3), title: col_text(stmt, 4), subtitle: col_text(stmt, 5), - notes: col_text(stmt, 6)) + notes: col_text(stmt, 6), + shared: opt > 0) } /// `WHERE stop IS NULL` func recordingGetOngoing() -> Recording? { - try? run(sql: "SELECT id, start, stop, appid, title, subtitle, notes FROM rec WHERE stop IS NULL LIMIT 1;") { + try? run(sql: "SELECT \(readRecordingSelect) FROM rec WHERE stop IS NULL LIMIT 1;") { try ifStep($0, SQLITE_ROW) return readRecording($0) } @@ -382,14 +391,14 @@ extension SQLiteDatabase { /// `WHERE stop IS NOT NULL` func recordingGetAll() -> [Recording]? { - try? run(sql: "SELECT id, start, stop, appid, title, subtitle, notes FROM rec WHERE stop IS NOT NULL;") { + try? run(sql: "SELECT \(readRecordingSelect) FROM rec WHERE stop IS NOT NULL;") { allRows($0) { readRecording($0) } } } /// `WHERE id = ?` private func recordingGet(withID: sqlite3_int64) throws -> Recording { - try run(sql: "SELECT id, start, stop, appid, title, subtitle, notes FROM rec WHERE id = ? LIMIT 1;", bind: [BindInt64(withID)]) { + try run(sql: "SELECT \(readRecordingSelect) FROM rec WHERE id = ? LIMIT 1;", bind: [BindInt64(withID)]) { try ifStep($0, SQLITE_ROW) return readRecording($0) } diff --git a/main/DB/DBCore.swift b/main/DB/DBCore.swift index b737d8e..69e43d0 100644 --- a/main/DB/DBCore.swift +++ b/main/DB/DBCore.swift @@ -169,6 +169,10 @@ protocol DBBinding { func bind(_ stmt: OpaquePointer!, _ col: Int32) -> Int32 } +struct BindNull : DBBinding { + func bind(_ stmt: OpaquePointer!, _ col: Int32) -> Int32 { sqlite3_bind_null(stmt, col) } +} + struct BindInt32 : DBBinding { let raw: Int32 init(_ value: Int32) { raw = value } diff --git a/main/Extensions/AlertSheet.swift b/main/Extensions/AlertSheet.swift index a290e49..92fb286 100644 --- a/main/Extensions/AlertSheet.swift +++ b/main/Extensions/AlertSheet.swift @@ -31,8 +31,8 @@ func ErrorAlert(_ errorDescription: String, buttonText: String = "Dismiss") -> U /// - 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") +func AskAlert(title: String?, text: String?, buttonText: String = "Continue", cancelButton: String = "Cancel", buttonStyle: UIAlertAction.Style = .default, action: @escaping (UIAlertController) -> Void) -> UIAlertController { + let alert = Alert(title: title, text: text, buttonText: cancelButton) alert.addAction(UIAlertAction(title: buttonText, style: buttonStyle) { _ in action(alert) }) return alert } @@ -42,9 +42,7 @@ func NotificationsDisabledAlert(presentIn viewController: UIViewController) { AskAlert(title: "Notifications Disabled", text: "Go to System Settings > Notifications > AppCheck to re-enable notifications.", buttonText: "Open settings") { _ in - if let url = URL(string: UIApplication.openSettingsURLString) { - UIApplication.shared.openURL(url) - } + URL(string: UIApplication.openSettingsURLString)?.open() }.presentIn(viewController) } diff --git a/main/Extensions/String.swift b/main/Extensions/String.swift index 463dd9b..d21775f 100644 --- a/main/Extensions/String.swift +++ b/main/Extensions/String.swift @@ -25,6 +25,13 @@ extension String { let parts = components(separatedBy: ".") return parts.count == 2 && listOfSLDs[parts.last!]?[parts.first!] ?? false } + + func isValidBundleId() -> Bool { + let regex = try! NSRegularExpression(pattern: #"^[A-Za-z0-9\.\-]{1,155}$"#, options: .anchorsMatchLines) + let range = NSRange(location: 0, length: self.utf16.count) + let matches = regex.matches(in: self, options: .anchored, range: range) + return matches.count == 1 + } } private var listOfSLDs: [String : [String : Bool]] = { diff --git a/main/GUI/Base.lproj/Recordings.storyboard b/main/GUI/Base.lproj/Recordings.storyboard index 8018105..123f843 100644 --- a/main/GUI/Base.lproj/Recordings.storyboard +++ b/main/GUI/Base.lproj/Recordings.storyboard @@ -212,6 +212,23 @@ + + + + + + + + + + + @@ -222,7 +239,7 @@ - + @@ -274,8 +291,8 @@ -