DirectoryTableViewCell.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. @objcMembers public class DirectoryTableViewCell: UITableViewCell {
  7. @IBOutlet weak var fileImageView: UIImageView!
  8. @IBOutlet weak var fileNameLabel: UILabel!
  9. @IBOutlet weak var fileInfoLabel: UILabel!
  10. public static var identifier = "DirectoryCellIdentifier"
  11. public static var nibName = "DirectoryTableViewCell"
  12. public static var cellHeight = 60.0
  13. public var fileParameter: NCMessageFileParameter?
  14. internal var activityIndicator: MDCActivityIndicator?
  15. public override func awakeFromNib() {
  16. super.awakeFromNib()
  17. NotificationCenter.default.addObserver(self, selector: #selector(didChangeIsDownloading(notification:)), name: NSNotification.Name.NCChatFileControllerDidChangeIsDownloading, object: nil)
  18. NotificationCenter.default.addObserver(self, selector: #selector(didChangeDownloadProgress(notification:)), name: NSNotification.Name.NCChatFileControllerDidChangeDownloadProgress, object: nil)
  19. }
  20. public override func prepareForReuse() {
  21. super.prepareForReuse()
  22. // Fix problem of rendering downloaded image in a reused cell
  23. self.fileImageView.cancelImageDownloadTask()
  24. self.fileImageView.image = nil
  25. self.fileNameLabel.text = ""
  26. self.fileInfoLabel.text = ""
  27. self.fileParameter = nil
  28. self.accessoryView = nil
  29. self.activityIndicator = nil
  30. }
  31. func didChangeIsDownloading(notification: Notification) {
  32. DispatchQueue.main.async {
  33. // Make sure this notification is really for this cell
  34. guard let fileParameter = self.fileParameter,
  35. let receivedStatus = NCChatFileStatus.getStatus(from: notification, for: fileParameter)
  36. else { return }
  37. if receivedStatus.isDownloading, self.activityIndicator == nil {
  38. // Immediately show an indeterminate indicator as long as we don't have a progress value
  39. self.addActivityIndicator(with: 0)
  40. } else if !receivedStatus.isDownloading, self.activityIndicator != nil {
  41. self.accessoryView = nil
  42. self.activityIndicator = nil
  43. }
  44. }
  45. }
  46. func didChangeDownloadProgress(notification: Notification) {
  47. DispatchQueue.main.async {
  48. // Make sure this notification is really for this cell
  49. guard let fileParameter = self.fileParameter,
  50. let receivedStatus = NCChatFileStatus.getStatus(from: notification, for: fileParameter)
  51. else { return }
  52. if self.activityIndicator != nil {
  53. // Switch to determinate-mode and show progress
  54. if receivedStatus.canReportProgress {
  55. self.activityIndicator?.indicatorMode = .determinate
  56. self.activityIndicator?.setProgress(Float(receivedStatus.downloadProgress), animated: true)
  57. }
  58. } else {
  59. // Make sure we have an activity indicator added to this cell
  60. self.addActivityIndicator(with: Float(receivedStatus.downloadProgress))
  61. }
  62. }
  63. }
  64. func addActivityIndicator(with progress: Float) {
  65. let indicator = MDCActivityIndicator(frame: .init(x: 0, y: 0, width: 20, height: 20))
  66. self.activityIndicator = indicator
  67. indicator.radius = 7.0
  68. indicator.cycleColors = [.lightGray]
  69. if progress > 0 {
  70. indicator.indicatorMode = .determinate
  71. indicator.setProgress(progress, animated: false)
  72. }
  73. indicator.startAnimating()
  74. self.accessoryView = indicator
  75. }
  76. }