feat: Show download progress on shared media

Assisted-by: ClaudeCode:claude-opus-5.5
Signed-off-by: Marcel Müller <[email protected]>
This commit is contained in:
Marcel Müller committed 2026-10-14 14:37:36 +02:00
1 parent 8fb63dc81c
commit 2f84db7ef0
4 files changed
+125 -40

No files matched your search

@@ -0,0 +1,48 @@
//
// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
// SPDX-License-Identifier: GPL-3.0-or-later
//
import Foundation
/// Follows the download of one file through the notifications NCChatFileController posts
class ChatFileDownloadObserver {
enum Change {
case isDownloading(Bool)
/// nil while the size of the download is unknown
case progress(Float?)
}
var fileParameter: NCMessageFileParameter?
private let onChange: (Change) -> Void
private var observers: [NSObjectProtocol] = []
init(onChange: @escaping (Change) -> Void) {
self.onChange = onChange
// Download progress can be reported off the main queue
self.observers.append(NotificationCenter.default.addObserver(forName: .NCChatFileControllerDidChangeIsDownloading, object: nil, queue: .main) { [weak self] notification in
guard let status = self?.status(from: notification) else { return }
self?.onChange(.isDownloading(status.isDownloading))
})
self.observers.append(NotificationCenter.default.addObserver(forName: .NCChatFileControllerDidChangeDownloadProgress, object: nil, queue: .main) { [weak self] notification in
guard let status = self?.status(from: notification) else { return }
self?.onChange(.progress(status.canReportProgress ? Float(status.downloadProgress) : nil))
})
}
deinit {
self.observers.forEach { NotificationCenter.default.removeObserver($0) }
}
private func status(from notification: Notification) -> NCChatFileStatus? {
guard let fileParameter = self.fileParameter else { return nil }
return NCChatFileStatus.getStatus(from: notification, for: fileParameter)
}
}
@@ -157,6 +157,10 @@ import SwiftyGif
return videoPlayButton
}()
private lazy var downloadObserver = ChatFileDownloadObserver { [weak self] change in
self?.downloadDidChange(change)
}
init(message: NCChatMessage, account: TalkAccount) {
self.message = message
self.account = account
@@ -193,7 +197,7 @@ import SwiftyGif
self.navigationItem.title = self.message.file()?.name
NotificationCenter.default.addObserver(self, selector: #selector(didChangeDownloadProgress(notification:)), name: NSNotification.Name.NCChatFileControllerDidChangeDownloadProgress, object: nil)
self.downloadObserver.fileParameter = self.message.file()
self.startLoading()
@@ -662,17 +666,12 @@ import SwiftyGif
}
}
func didChangeDownloadProgress(notification: Notification) {
DispatchQueue.main.async {
guard let fileParameter = self.message.file(),
let receivedStatus = NCChatFileStatus.getStatus(from: notification, for: fileParameter),
receivedStatus.canReportProgress
else { return }
private func downloadDidChange(_ change: ChatFileDownloadObserver.Change) {
guard case .progress(let progress?) = change else { return }
self.activityIndicator.indicatorMode = .determinate
self.activityIndicator.setProgress(Float(receivedStatus.downloadProgress), animated: true)
self.downloadIndicator.setProgress(Float(receivedStatus.downloadProgress))
}
self.activityIndicator.indicatorMode = .determinate
self.activityIndicator.setProgress(progress, animated: true)
self.downloadIndicator.setProgress(progress)
}
// MARK: - NCChatFileController delegate
+22 -28
View File
@@ -9,9 +9,17 @@ import SDWebImage
class SharedItemTableViewCell: UITableViewCell {
var fileParameter: NCMessageFileParameter?
var fileParameter: NCMessageFileParameter? {
get { self.downloadObserver.fileParameter }
set { self.downloadObserver.fileParameter = newValue }
}
private var activityIndicator: NCActivityIndicator?
private lazy var downloadObserver = ChatFileDownloadObserver { [weak self] change in
self?.downloadDidChange(change)
}
let titleLabel: UILabel = {
let label = UILabel()
label.font = .preferredFont(forTextStyle: .body)
@@ -32,9 +40,6 @@ class SharedItemTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(self, selector: #selector(didChangeIsDownloading(notification:)), name: NSNotification.Name.NCChatFileControllerDidChangeIsDownloading, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didChangeDownloadProgress(notification:)), name: NSNotification.Name.NCChatFileControllerDidChangeDownloadProgress, object: nil)
}
required init?(coder: NSCoder) {
@@ -61,35 +66,24 @@ class SharedItemTableViewCell: UITableViewCell {
return parts.joined(separator: " ⸱ ")
}
@objc private func didChangeIsDownloading(notification: Notification) {
DispatchQueue.main.async {
guard let fileParameter = self.fileParameter,
let receivedStatus = NCChatFileStatus.getStatus(from: notification, for: fileParameter)
else { return }
if receivedStatus.isDownloading, self.activityIndicator == nil {
self.addActivityIndicator(with: 0)
} else if !receivedStatus.isDownloading, self.activityIndicator != nil {
self.accessoryView = nil
self.activityIndicator = nil
}
}
}
@objc private func didChangeDownloadProgress(notification: Notification) {
DispatchQueue.main.async {
guard let fileParameter = self.fileParameter,
let receivedStatus = NCChatFileStatus.getStatus(from: notification, for: fileParameter)
else { return }
private func downloadDidChange(_ change: ChatFileDownloadObserver.Change) {
switch change {
case .isDownloading(true) where self.activityIndicator == nil:
self.addActivityIndicator(with: 0)
case .isDownloading(false):
self.accessoryView = nil
self.activityIndicator = nil
case .progress(let progress):
if let activityIndicator = self.activityIndicator {
if receivedStatus.canReportProgress {
if let progress {
activityIndicator.indicatorMode = .determinate
activityIndicator.setProgress(Float(receivedStatus.downloadProgress), animated: true)
activityIndicator.setProgress(progress, animated: true)
}
} else {
self.addActivityIndicator(with: Float(receivedStatus.downloadProgress))
self.addActivityIndicator(with: progress ?? 0)
}
default:
break
}
}
+45 -1
View File
@@ -15,6 +15,17 @@ class SharedMediaCell: UICollectionViewCell {
private var fileId: String?
private var previewRequest: SDWebImageCombinedOperation?
private var isVideo = false
private lazy var downloadObserver = ChatFileDownloadObserver { [weak self] change in
self?.downloadDidChange(change)
}
private let downloadIndicator: MediaDownloadIndicatorView = {
let downloadIndicator = MediaDownloadIndicatorView(frame: .zero)
downloadIndicator.isHidden = true
return downloadIndicator
}()
private let imageView: UIImageView = {
let imageView = UIImageView()
@@ -68,6 +79,7 @@ class SharedMediaCell: UICollectionViewCell {
self.contentView.addSubview(self.fileIconView)
self.contentView.addSubview(self.playIconView)
self.contentView.addSubview(self.captionView)
self.contentView.addSubview(self.downloadIndicator)
self.captionView.addSubview(self.actorLabel)
for view in [self.imageView, self.fileIconView, self.playIconView, self.captionView, self.actorLabel] {
@@ -90,6 +102,9 @@ class SharedMediaCell: UICollectionViewCell {
self.playIconView.widthAnchor.constraint(equalToConstant: 44),
self.playIconView.heightAnchor.constraint(equalToConstant: 44),
self.downloadIndicator.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
self.downloadIndicator.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor),
self.captionView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
self.captionView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
self.captionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
@@ -128,6 +143,8 @@ class SharedMediaCell: UICollectionViewCell {
self.fileId = nil
self.imageView.image = nil
self.fileIconView.isHidden = true
self.downloadObserver.fileParameter = nil
self.showsDownload(false)
}
func configure(with message: NCChatMessage, aspectRatio: CGFloat, showsServerPreview: Bool, account: TalkAccount) {
@@ -137,7 +154,10 @@ class SharedMediaCell: UICollectionViewCell {
self.fileId = file?.parameterId
self.actorLabel.text = actorDisplayName
self.captionView.isHidden = actorDisplayName.isEmpty
self.playIconView.isHidden = !NCUtils.isVideo(fileType: file?.mimetype ?? "")
self.isVideo = NCUtils.isVideo(fileType: file?.mimetype ?? "")
self.downloadObserver.fileParameter = file
// A download started before this cell showed the file only reports its next change
self.showsDownload(file.flatMap { ChatFileDownloader.shared.downloadStatus(forFileId: $0.parameterId)?.isDownloading } ?? false)
self.accessibilityLabel = [file?.name ?? "", actorDisplayName].filter { !$0.isEmpty }.joined(separator: ", ")
guard let file, file.previewAvailable else {
@@ -170,6 +190,30 @@ class SharedMediaCell: UICollectionViewCell {
}
}
private func downloadDidChange(_ change: ChatFileDownloadObserver.Change) {
switch change {
case .isDownloading(let isDownloading):
self.showsDownload(isDownloading)
case .progress(let progress):
self.showsDownload(true)
if let progress {
self.downloadIndicator.setProgress(progress)
}
}
}
private func showsDownload(_ showsDownload: Bool) {
if showsDownload, self.downloadIndicator.isHidden {
self.downloadIndicator.showProgress()
} else if !showsDownload {
self.downloadIndicator.stopAnimating()
}
self.downloadIndicator.isHidden = !showsDownload
self.playIconView.isHidden = showsDownload || !self.isVideo
}
private func showFileIcon(for file: NCMessageFileParameter?) {
self.imageView.image = nil
self.fileIconView.image = UIImage(named: NCUtils.previewImage(forMimeType: file?.mimetype))