From 80f3503e167b76ff0afd152934c85d40cb061827 Mon Sep 17 00:00:00 2001 From: relikd Date: Wed, 8 Apr 2020 21:34:45 +0200 Subject: [PATCH] Edit delete recordings --- main/DB/DBWrapper.swift | 5 +++-- main/DB/SQDB.swift | 10 ++++++---- main/Recordings/TVCPreviousRecords.swift | 10 +++++++++- main/Recordings/TVCRecordingDetails.swift | 17 ++++++++++++++--- main/TVC Extensions/EditableRows.swift | 20 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/main/DB/DBWrapper.swift b/main/DB/DBWrapper.swift index 16a63f1..497f915 100644 --- a/main/DB/DBWrapper.swift +++ b/main/DB/DBWrapper.swift @@ -230,6 +230,7 @@ class DBWrapper { func recordingStop(_ r: inout Recording) { AppDB?.stopRecording(&r) } func recordingPersist(_ r: Recording) { AppDB?.persistRecordingLogs(r) } + func recordingDetails(_ r: Recording) -> [RecordLog] { AppDB?.getRecordingsLogs(r) ?? [] } func recordingUpdate(_ r: Recording) { AppDB?.updateRecording(r) @@ -242,8 +243,8 @@ class DBWrapper { } } - func recordingDetails(_ r: Recording) -> [(domain: String?, count: Int32)]? { - AppDB?.getRecordingsLogs(r) + func recordingDeleteDetails(_ r: Recording, domain: String?) -> Bool { + ((try? AppDB?.deleteRecordingLogs(r.id, matchingDomain: domain)) ?? 0) > 0 } diff --git a/main/DB/SQDB.swift b/main/DB/SQDB.swift index 558cbf0..c25581d 100644 --- a/main/DB/SQDB.swift +++ b/main/DB/SQDB.swift @@ -441,9 +441,9 @@ extension SQLiteDatabase { } } - private func deleteRecordingLogs(_ recId: sqlite3_int64) throws -> Int32 { - try run(sql: "DELETE FROM recLog WHERE rid = ?;", bind: { - self.bindInt64($0, 1, recId) + func deleteRecordingLogs(_ recId: sqlite3_int64, matchingDomain d: String? = nil) throws -> Int32 { + try run(sql: "DELETE FROM recLog WHERE rid = ? \(d==nil ? "" : "AND domain = ?");", bind: { + self.bindInt64($0, 1, recId) && (d==nil ? true : self.bindTextOrNil($0, 2,d)) }) { try ifStep($0, SQLITE_DONE) return sqlite3_changes(dbPointer) @@ -452,7 +452,7 @@ extension SQLiteDatabase { // MARK: read - func getRecordingsLogs(_ r: Recording) -> [(domain: String?, count: Int32)]? { + func getRecordingsLogs(_ r: Recording) -> [RecordLog]? { try? run(sql: "SELECT domain, COUNT() FROM recLog WHERE rid = ? GROUP BY domain;", bind: { self.bindInt64($0, 1, r.id) }) { @@ -460,3 +460,5 @@ extension SQLiteDatabase { } } } + +typealias RecordLog = (domain: String?, count: Int32) diff --git a/main/Recordings/TVCPreviousRecords.swift b/main/Recordings/TVCPreviousRecords.swift index dfb6685..fe7a459 100644 --- a/main/Recordings/TVCPreviousRecords.swift +++ b/main/Recordings/TVCPreviousRecords.swift @@ -1,6 +1,6 @@ import UIKit -class TVCPreviousRecords: UITableViewController { +class TVCPreviousRecords: UITableViewController, EditActionsRemove { private var dataSource: [Recording] = [] override func viewDidLoad() { @@ -72,4 +72,12 @@ class TVCPreviousRecords: UITableViewController { cell.detailTextLabel?.text = "at \(x.start.asDateTime()), duration: \(x.durationString ?? "?")" return cell } + + + // MARK: - Editing + + func editableRowCallback(_ index: IndexPath, _ action: RowAction, _ userInfo: Any?) -> Bool { + DBWrp.recordingDelete(self.dataSource[index.row]) + return true + } } diff --git a/main/Recordings/TVCRecordingDetails.swift b/main/Recordings/TVCRecordingDetails.swift index 45a94d5..2c8d88b 100644 --- a/main/Recordings/TVCRecordingDetails.swift +++ b/main/Recordings/TVCRecordingDetails.swift @@ -1,12 +1,12 @@ import UIKit -class TVCRecordingDetails: UITableViewController { +class TVCRecordingDetails: UITableViewController, EditActionsRemove { var record: Recording! - private var dataSource: [(domain: String?, count: Int32)]! + private var dataSource: [RecordLog]! override func viewDidLoad() { title = record.title ?? record.fallbackTitle - dataSource = DBWrp.recordingDetails(record) ?? [] + dataSource = DBWrp.recordingDetails(record) } @@ -23,4 +23,15 @@ class TVCRecordingDetails: UITableViewController { cell.detailTextLabel?.text = "\(x.count)" return cell } + + + // MARK: - Editing + + func editableRowCallback(_ index: IndexPath, _ action: RowAction, _ userInfo: Any?) -> Bool { + if DBWrp.recordingDeleteDetails(record, domain: self.dataSource[index.row].domain) { + self.dataSource.remove(at: index.row) + self.tableView.deleteRows(at: [index], with: .automatic) + } + return true + } } diff --git a/main/TVC Extensions/EditableRows.swift b/main/TVC Extensions/EditableRows.swift index 1aab923..7895356 100644 --- a/main/TVC Extensions/EditableRows.swift +++ b/main/TVC Extensions/EditableRows.swift @@ -123,3 +123,23 @@ extension TVCFilter : EditableRows { getRowActionsIOS11(indexPath) } } + +extension TVCPreviousRecords : EditableRows { + 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) + } +} + +extension TVCRecordingDetails : EditableRows { + 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) + } +}