Indicate shared on recordings overview + move isShared check to sharing sheet

This commit is contained in:
relikd
2020-08-28 23:02:10 +02:00
parent 448d69c6d8
commit c502484bcf
3 changed files with 27 additions and 32 deletions

View File

@@ -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 = "at \(DateFormat.minutes(x.start)), duration: \(TimeFormat.from(x.duration ?? 0))"
cell.detailTextLabel?.text = "\(x.shared ? "" : "")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

View File

@@ -28,14 +28,6 @@ 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) {
@@ -44,20 +36,6 @@ class TVCRecordingDetails: UITableViewController, EditActionsRemove {
tableView.reloadData()
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "openContributeSegue" && record.shared {
let alert = Alert(title: nil, text: "You have shared this recording already.")
if let bid = record.appId, bid.isValidBundleId() {
alert.addAction(UIAlertAction.init(title: "Open results", style: .default, handler: { _ in
URL(string: "http://127.0.0.1/redirect.html?id=\(bid)")?.open()
}))
}
alert.presentIn(self)
return false
}
return super.shouldPerformSegue(withIdentifier: identifier, sender: sender)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let tgt = segue.destination as? VCShareRecording {
tgt.record = self.record

View File

@@ -12,7 +12,7 @@ class VCShareRecording : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
sendButton.isEnabled = !record.shared
sendButton.tintColor = .gray
let start = record.start
let comp = Calendar.current.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date(start))
@@ -64,20 +64,28 @@ class VCShareRecording : UIViewController {
}
@IBAction private func shareRecording(_ sender: UIBarButtonItem) {
guard !record.shared else {
showAlertAlreadyShared()
return
}
sender.isEnabled = false
sendActivity.startAnimating()
postToServer() { [weak self, weak sender] in
self?.sendActivity.stopAnimating()
sender?.isEnabled = true
}
}
private func postToServer(_ onceLoaded: @escaping () -> Void) {
let url = URL(string: "http://127.0.0.1/api/v1/contribute/")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData
var rec = record!
var rec = record! // store temporarily so self can be released
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async { [weak self] in
sender.isEnabled = true
self?.sendActivity.stopAnimating()
onceLoaded()
guard error == nil, let data = data,
let response = response as? HTTPURLResponse else {
self?.banner(.fail, "\(error?.localizedDescription ?? "Unkown error occurred")")
@@ -92,7 +100,6 @@ class VCShareRecording : UIViewController {
return
}
// update db, mark record as shared
sender.isEnabled = false
rec.shared = true // in case view was closed
self?.record = rec // in case view is still open
RecordingsDB.update(rec) // rec cause self may not be available
@@ -100,7 +107,7 @@ class VCShareRecording : UIViewController {
var autoHide = true
if v == 1, let urlStr = json?["url"] as? String {
let nextUpdateIn = json?["when"] as? Int
self?.showOpenResultsAlert(urlStr, when: nextUpdateIn)
self?.showAlertAvailableSoon(urlStr, when: nextUpdateIn)
autoHide = false
}
self?.banner(.ok, "Thank you for your contribution.",
@@ -113,7 +120,7 @@ class VCShareRecording : UIViewController {
NotificationBanner(msg, style: style).present(in: self, onClose: closure)
}
private func showOpenResultsAlert(_ urlStr: String, when: Int?) {
private func showAlertAvailableSoon(_ urlStr: String, when: Int?) {
var msg = "Your contribution is being processed and will be available "
if let when = when {
if when < 61 {
@@ -132,4 +139,14 @@ class VCShareRecording : UIViewController {
}
}.presentIn(self)
}
private func showAlertAlreadyShared() {
let alert = Alert(title: nil, text: "You already shared this recording.")
if let bid = record.appId, bid.isValidBundleId() {
alert.addAction(UIAlertAction.init(title: "Open results", style: .default, handler: { _ in
URL(string: "http://127.0.0.1/redirect.html?id=\(bid)")?.open()
}))
}
alert.presentIn(self)
}
}