From b03daeca66699d03df3d736b3a4a9c05c791092c Mon Sep 17 00:00:00 2001 From: relikd Date: Fri, 28 Aug 2020 23:41:08 +0200 Subject: [PATCH] Store sharing key instead of just a bool --- main/DB/DBAppOnly.swift | 15 ++++++------ main/DB/DBExtensions.swift | 1 + main/Recordings/TVCPreviousRecords.swift | 2 +- main/Recordings/TVCRecordingDetails.swift | 8 +++++++ main/Recordings/VCShareRecording.swift | 29 ++++++++++++++--------- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/main/DB/DBAppOnly.swift b/main/DB/DBAppOnly.swift index 9f62e32..38c26e0 100644 --- a/main/DB/DBAppOnly.swift +++ b/main/DB/DBAppOnly.swift @@ -26,7 +26,7 @@ extension SQLiteDatabase { if version == 1 { transaction(""" ALTER TABLE rec ADD COLUMN subtitle TEXT; - ALTER TABLE rec ADD COLUMN opt INTEGER; + ALTER TABLE rec ADD COLUMN sharekey TEXT; """) } try run(sql: "PRAGMA user_version = 2;") @@ -298,12 +298,12 @@ extension CreateTable { title TEXT, subtitle TEXT, notes TEXT, - opt INTEGER + sharekey TEXT ); """} } -let readRecordingSelect = "id, start, stop, appid, title, subtitle, notes, opt" +let readRecordingSelect = "id, start, stop, appid, title, subtitle, notes, sharekey" struct Recording { let id: sqlite3_int64 let start: Timestamp @@ -312,7 +312,7 @@ struct Recording { var title: String? = nil var subtitle: String? = nil var notes: String? = nil - var shared: Bool = false + var sharekey: String? = nil } typealias AppBundleInfo = (bundleId: String, name: String?, author: String?) @@ -341,9 +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 = ?, opt = ? WHERE id = ? LIMIT 1;", + try? run(sql: "UPDATE rec SET appid = ?, title = ?, subtitle = ?, notes = ?, sharekey = ? 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 + BindTextOrNil(r.notes), BindTextOrNil(r.sharekey), BindInt64(r.id)]) { stmt -> Void in sqlite3_step(stmt) } } @@ -362,7 +362,6 @@ 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, @@ -370,7 +369,7 @@ extension SQLiteDatabase { title: col_text(stmt, 4), subtitle: col_text(stmt, 5), notes: col_text(stmt, 6), - shared: opt > 0) + sharekey: col_text(stmt, 7)) } /// `WHERE stop IS NULL` diff --git a/main/DB/DBExtensions.swift b/main/DB/DBExtensions.swift index a3bb384..ce771fc 100644 --- a/main/DB/DBExtensions.swift +++ b/main/DB/DBExtensions.swift @@ -38,5 +38,6 @@ extension Recording { } } var duration: Timestamp? { get { stop == nil ? nil : stop! - start } } var isLongTerm: Bool { (duration ?? 0) > Timestamp.hours(1) } + var isShared: Bool { sharekey?.count ?? 0 > 0} } diff --git a/main/Recordings/TVCPreviousRecords.swift b/main/Recordings/TVCPreviousRecords.swift index f1efe7d..71312dc 100644 --- a/main/Recordings/TVCPreviousRecords.swift +++ b/main/Recordings/TVCPreviousRecords.swift @@ -76,7 +76,7 @@ class TVCPreviousRecords: UITableViewController, EditActionsRemove { let x = dataSource[indexPath.row] cell.textLabel?.text = x.title ?? x.fallbackTitle cell.textLabel?.textColor = (x.title == nil) ? .systemGray : nil - cell.detailTextLabel?.text = "\(x.shared ? "✅ " : "")at \(DateFormat.minutes(x.start)), duration: \(TimeFormat.from(x.duration ?? 0))" + cell.detailTextLabel?.text = "\(x.isShared ? "✅ " : "")at \(DateFormat.minutes(x.start)), duration: \(TimeFormat.from(x.duration ?? 0))" cell.imageView?.image = x.isLongTerm ? nil : BundleIcon.image(x.appId) cell.imageView?.layer.cornerRadius = 6.75 cell.imageView?.layer.masksToBounds = true diff --git a/main/Recordings/TVCRecordingDetails.swift b/main/Recordings/TVCRecordingDetails.swift index 82de4a1..31a8e86 100644 --- a/main/Recordings/TVCRecordingDetails.swift +++ b/main/Recordings/TVCRecordingDetails.swift @@ -28,6 +28,14 @@ class TVCRecordingDetails: UITableViewController, EditActionsRemove { override func viewDidLoad() { title = record.title ?? record.fallbackTitle + NotifyRecordingChanged.observe(call: #selector(recordingDidChange(_:)), on: self) + } + + @objc private func recordingDidChange(_ notification: Notification) { + let (rec, deleted) = notification.object as! (Recording, Bool) + if rec.id == record.id, !deleted { + record = rec // almost exclusively when 'shared' is set true + } } @IBAction private func toggleDisplayStyle(_ sender: UIBarButtonItem) { diff --git a/main/Recordings/VCShareRecording.swift b/main/Recordings/VCShareRecording.swift index 9ec921f..aab09c0 100644 --- a/main/Recordings/VCShareRecording.swift +++ b/main/Recordings/VCShareRecording.swift @@ -12,7 +12,9 @@ class VCShareRecording : UIViewController { override func viewDidLoad() { super.viewDidLoad() - sendButton.tintColor = .gray + if record.isShared { + sendButton.tintColor = .gray + } let start = record.start let comp = Calendar.current.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date(start)) @@ -64,7 +66,7 @@ class VCShareRecording : UIViewController { } @IBAction private func shareRecording(_ sender: UIBarButtonItem) { - guard !record.shared else { + guard !record.isShared else { showAlertAlreadyShared() return } @@ -91,22 +93,27 @@ class VCShareRecording : UIViewController { self?.banner(.fail, "\(error?.localizedDescription ?? "Unkown error occurred")") return } - let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any] - let status = json?["status"] as? String - let v = json?["v"] as? Int ?? 0 - guard v > 0, (200 ... 299) ~= response.statusCode else { - QLog.Warning("Couldn't contribute: \(status ?? "unkown reason")") + guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any], + let v = json["v"] as? Int, v > 0 else { + QLog.Warning("Couldn't contribute: Not JSON or no version key") self?.banner(.fail, "Server couldn't parse request.\nTry again later.") return } + guard (200 ... 299) ~= response.statusCode else { + let reason = json["status"] as? String ?? "unkown reason" + QLog.Warning("Couldn't contribute: \(reason)") + self?.banner(.fail, "Error: \(reason)") + return + } // update db, mark record as shared - rec.shared = true // in case view was closed - self?.record = rec // in case view is still open + rec.sharekey = json["key"] as? String ?? "_" + self?.record = rec // in case view is still open RecordingsDB.update(rec) // rec cause self may not be available + self?.sendButton.tintColor = .gray // notify user about results var autoHide = true - if v == 1, let urlStr = json?["url"] as? String { - let nextUpdateIn = json?["when"] as? Int + if v == 1, let urlStr = json["url"] as? String { + let nextUpdateIn = json["when"] as? Int self?.showAlertAvailableSoon(urlStr, when: nextUpdateIn) autoHide = false }