Files
appchk-app/main/Recordings/TVCPreviousRecords.swift

99 lines
3.3 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
class TVCPreviousRecords: UITableViewController, EditActionsRemove {
private var dataSource: [Recording] = []
override func viewDidLoad() {
dataSource = RecordingsDB.list().reversed() // newest on top
NotifyRecordingChanged.observe(call: #selector(recordingDidChange(_:)), on: self)
}
func insertAndEditRecording(_ r: Recording) {
insertNewRecord(r)
editRecord(r, isNewRecording: true)
}
@objc private func recordingDidChange(_ notification: Notification) {
let (new, deleted) = notification.object as! (Recording, Bool)
if let i = dataSource.firstIndex(where: { $0.id == new.id }) {
if deleted {
dataSource.remove(at: i)
tableView.deleteRows(at: [IndexPath(row: i)], with: .automatic)
} else {
dataSource[i] = new
tableView.reloadRows(at: [IndexPath(row: i)], with: .automatic)
}
} else if !deleted {
insertNewRecord(new)
}
}
private func insertNewRecord(_ record: Recording) {
dataSource.insert(record, at: 0)
tableView.insertRows(at: [IndexPath(row: 0)], with: .top)
}
// MARK: - Table View Delegate
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
editRecord(dataSource[indexPath.row])
}
private func editRecord(_ record: Recording, isNewRecording: Bool = false) {
performSegue(withIdentifier: "editRecordSegue", sender: (record, isNewRecording))
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editRecordSegue" {
let (record, newlyCreated) = sender as! (Recording, Bool)
let target = segue.destination as! VCEditRecording
target.record = record
target.deleteOnCancel = newlyCreated
} else if segue.identifier == "openRecordDetailsSegue" {
if let i = tableView.indexPathForSelectedRow {
(segue.destination as? TVCRecordingDetails)?.record = dataSource[i.row]
}
}
}
// MARK: - Table View Data Source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PreviousRecordCell")!
let x = dataSource[indexPath.row]
cell.textLabel?.text = x.title ?? x.fallbackTitle
cell.textLabel?.textColor = (x.title == nil) ? .systemGray : nil
cell.detailTextLabel?.text = "\(x.isShared ? "" : "")at \(DateFormat.minutes(x.start)), duration: \(TimeFormat.from(x.duration))"
cell.imageView?.image = x.isLongTerm ? nil : BundleIcon.image(x.appId)
cell.imageView?.layer.cornerRadius = 6.75
cell.imageView?.layer.masksToBounds = true
if #available(iOS 11, *) {} else {
cell.textLabel?.numberOfLines = 1
cell.detailTextLabel?.numberOfLines = 1
}
return cell
}
// MARK: - Editing
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
getRowActionsIOS9(indexPath, tableView)
}
@available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
getRowActionsIOS11(indexPath)
}
func editableRowCallback(_ index: IndexPath, _ action: RowAction, _ userInfo: Any?) -> Bool {
RecordingsDB.delete(self.dataSource[index.row])
return true
}
}