Store sharing key instead of just a bool

This commit is contained in:
relikd
2020-08-28 23:41:08 +02:00
parent c502484bcf
commit b03daeca66
5 changed files with 35 additions and 20 deletions

View File

@@ -26,7 +26,7 @@ extension SQLiteDatabase {
if version == 1 { if version == 1 {
transaction(""" transaction("""
ALTER TABLE rec ADD COLUMN subtitle TEXT; 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;") try run(sql: "PRAGMA user_version = 2;")
@@ -298,12 +298,12 @@ extension CreateTable {
title TEXT, title TEXT,
subtitle TEXT, subtitle TEXT,
notes 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 { struct Recording {
let id: sqlite3_int64 let id: sqlite3_int64
let start: Timestamp let start: Timestamp
@@ -312,7 +312,7 @@ struct Recording {
var title: String? = nil var title: String? = nil
var subtitle: String? = nil var subtitle: String? = nil
var notes: String? = nil var notes: String? = nil
var shared: Bool = false var sharekey: String? = nil
} }
typealias AppBundleInfo = (bundleId: String, name: String?, author: String?) 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. /// Update given recording by replacing `title`, `appid`, and `notes` with new values.
func recordingUpdate(_ r: Recording) { 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), 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) sqlite3_step(stmt)
} }
} }
@@ -362,7 +362,6 @@ extension SQLiteDatabase {
private func readRecording(_ stmt: OpaquePointer) -> Recording { private func readRecording(_ stmt: OpaquePointer) -> Recording {
let end = col_ts(stmt, 2) let end = col_ts(stmt, 2)
let opt = sqlite3_column_int(stmt, 7)
return Recording(id: sqlite3_column_int64(stmt, 0), return Recording(id: sqlite3_column_int64(stmt, 0),
start: col_ts(stmt, 1), start: col_ts(stmt, 1),
stop: end == 0 ? nil : end, stop: end == 0 ? nil : end,
@@ -370,7 +369,7 @@ extension SQLiteDatabase {
title: col_text(stmt, 4), title: col_text(stmt, 4),
subtitle: col_text(stmt, 5), subtitle: col_text(stmt, 5),
notes: col_text(stmt, 6), notes: col_text(stmt, 6),
shared: opt > 0) sharekey: col_text(stmt, 7))
} }
/// `WHERE stop IS NULL` /// `WHERE stop IS NULL`

View File

@@ -38,5 +38,6 @@ extension Recording {
} } } }
var duration: Timestamp? { get { stop == nil ? nil : stop! - start } } var duration: Timestamp? { get { stop == nil ? nil : stop! - start } }
var isLongTerm: Bool { (duration ?? 0) > Timestamp.hours(1) } var isLongTerm: Bool { (duration ?? 0) > Timestamp.hours(1) }
var isShared: Bool { sharekey?.count ?? 0 > 0}
} }

View File

@@ -76,7 +76,7 @@ class TVCPreviousRecords: UITableViewController, EditActionsRemove {
let x = dataSource[indexPath.row] let x = dataSource[indexPath.row]
cell.textLabel?.text = x.title ?? x.fallbackTitle cell.textLabel?.text = x.title ?? x.fallbackTitle
cell.textLabel?.textColor = (x.title == nil) ? .systemGray : nil 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?.image = x.isLongTerm ? nil : BundleIcon.image(x.appId)
cell.imageView?.layer.cornerRadius = 6.75 cell.imageView?.layer.cornerRadius = 6.75
cell.imageView?.layer.masksToBounds = true cell.imageView?.layer.masksToBounds = true

View File

@@ -28,6 +28,14 @@ class TVCRecordingDetails: UITableViewController, EditActionsRemove {
override func viewDidLoad() { override func viewDidLoad() {
title = record.title ?? record.fallbackTitle 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) { @IBAction private func toggleDisplayStyle(_ sender: UIBarButtonItem) {

View File

@@ -12,7 +12,9 @@ class VCShareRecording : UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
sendButton.tintColor = .gray if record.isShared {
sendButton.tintColor = .gray
}
let start = record.start let start = record.start
let comp = Calendar.current.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date(start)) let comp = Calendar.current.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date(start))
@@ -64,7 +66,7 @@ class VCShareRecording : UIViewController {
} }
@IBAction private func shareRecording(_ sender: UIBarButtonItem) { @IBAction private func shareRecording(_ sender: UIBarButtonItem) {
guard !record.shared else { guard !record.isShared else {
showAlertAlreadyShared() showAlertAlreadyShared()
return return
} }
@@ -91,22 +93,27 @@ class VCShareRecording : UIViewController {
self?.banner(.fail, "\(error?.localizedDescription ?? "Unkown error occurred")") self?.banner(.fail, "\(error?.localizedDescription ?? "Unkown error occurred")")
return return
} }
let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any] guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any],
let status = json?["status"] as? String let v = json["v"] as? Int, v > 0 else {
let v = json?["v"] as? Int ?? 0 QLog.Warning("Couldn't contribute: Not JSON or no version key")
guard v > 0, (200 ... 299) ~= response.statusCode else {
QLog.Warning("Couldn't contribute: \(status ?? "unkown reason")")
self?.banner(.fail, "Server couldn't parse request.\nTry again later.") self?.banner(.fail, "Server couldn't parse request.\nTry again later.")
return 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 // update db, mark record as shared
rec.shared = true // in case view was closed rec.sharekey = json["key"] as? String ?? "_"
self?.record = rec // in case view is still open self?.record = rec // in case view is still open
RecordingsDB.update(rec) // rec cause self may not be available RecordingsDB.update(rec) // rec cause self may not be available
self?.sendButton.tintColor = .gray
// notify user about results // notify user about results
var autoHide = true var autoHide = true
if v == 1, let urlStr = json?["url"] as? String { if v == 1, let urlStr = json["url"] as? String {
let nextUpdateIn = json?["when"] as? Int let nextUpdateIn = json["when"] as? Int
self?.showAlertAvailableSoon(urlStr, when: nextUpdateIn) self?.showAlertAvailableSoon(urlStr, when: nextUpdateIn)
autoHide = false autoHide = false
} }