|
| 1 | +// |
| 2 | +// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +// |
| 5 | + |
| 6 | +import UIKit |
| 7 | + |
| 8 | +// Unobtrusive download progress shown on top of an already visible preview |
| 9 | +@objcMembers class MediaDownloadIndicatorView: UIView { |
| 10 | + |
| 11 | + private static let preferredSize: CGFloat = 32 |
| 12 | + |
| 13 | + private var retryHandler: (() -> Void)? |
| 14 | + private var backgroundView: UIVisualEffectView? |
| 15 | + |
| 16 | + private lazy var activityIndicator = { |
| 17 | + let activityIndicator = NCActivityIndicator(frame: .init(x: 0, y: 0, width: 24, height: 24)) |
| 18 | + activityIndicator.translatesAutoresizingMaskIntoConstraints = false |
| 19 | + activityIndicator.radius = 9 |
| 20 | + activityIndicator.strokeWidth = 2 |
| 21 | + activityIndicator.indicatorMode = .indeterminate |
| 22 | + |
| 23 | + return activityIndicator |
| 24 | + }() |
| 25 | + |
| 26 | + private lazy var retryButton = { |
| 27 | + let retryButton = UIButton(type: .system) |
| 28 | + retryButton.translatesAutoresizingMaskIntoConstraints = false |
| 29 | + retryButton.setImage(UIImage(systemName: "arrow.clockwise"), for: .normal) |
| 30 | + retryButton.isHidden = true |
| 31 | + retryButton.accessibilityLabel = NSLocalizedString("Retry", comment: "Retry downloading the original file") |
| 32 | + retryButton.addAction(UIAction { [weak self] _ in |
| 33 | + self?.retryHandler?() |
| 34 | + }, for: .touchUpInside) |
| 35 | + |
| 36 | + return retryButton |
| 37 | + }() |
| 38 | + |
| 39 | + override init(frame: CGRect) { |
| 40 | + super.init(frame: frame) |
| 41 | + |
| 42 | + self.translatesAutoresizingMaskIntoConstraints = false |
| 43 | + |
| 44 | + self.addBackgroundView() |
| 45 | + |
| 46 | + // Foreground content belongs inside the effect view, that is what gets the legibility treatment |
| 47 | + let contentView = self.backgroundView?.contentView ?? self |
| 48 | + contentView.addSubview(self.activityIndicator) |
| 49 | + contentView.addSubview(self.retryButton) |
| 50 | + |
| 51 | + self.updateForegroundColor() |
| 52 | + self.observeGlassAppearance() |
| 53 | + |
| 54 | + NSLayoutConstraint.activate([ |
| 55 | + self.widthAnchor.constraint(equalToConstant: MediaDownloadIndicatorView.preferredSize), |
| 56 | + self.heightAnchor.constraint(equalToConstant: MediaDownloadIndicatorView.preferredSize), |
| 57 | + self.activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor), |
| 58 | + self.activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor), |
| 59 | + self.retryButton.leadingAnchor.constraint(equalTo: self.leadingAnchor), |
| 60 | + self.retryButton.trailingAnchor.constraint(equalTo: self.trailingAnchor), |
| 61 | + self.retryButton.topAnchor.constraint(equalTo: self.topAnchor), |
| 62 | + self.retryButton.bottomAnchor.constraint(equalTo: self.bottomAnchor) |
| 63 | + ]) |
| 64 | + |
| 65 | + self.isAccessibilityElement = true |
| 66 | + self.accessibilityLabel = NSLocalizedString("Downloading full quality media", comment: "Accessibility label of the indicator shown while the original file is downloaded") |
| 67 | + self.accessibilityTraits = .updatesFrequently |
| 68 | + } |
| 69 | + |
| 70 | + required init?(coder: NSCoder) { |
| 71 | + fatalError("init(coder:) has not been implemented") |
| 72 | + } |
| 73 | + |
| 74 | + /// |
| 75 | + /// Follows the appearance the glass picked for the brightness of the picture behind it. |
| 76 | + /// |
| 77 | + /// The glass reports that as a user interface style on its content view only, not on us. The |
| 78 | + /// indicator draws into a layer, whose color does not re-resolve on its own, so the color is |
| 79 | + /// resolved explicitly here. |
| 80 | + /// |
| 81 | + private func updateForegroundColor() { |
| 82 | + let contentTraitCollection = self.backgroundView?.contentView.traitCollection ?? self.traitCollection |
| 83 | + |
| 84 | + // Progress is ambient, the retry glyph is a control and stays at full strength |
| 85 | + self.activityIndicator.cycleColors = [UIColor.secondaryLabel.resolvedColor(with: contentTraitCollection)] |
| 86 | + self.retryButton.tintColor = UIColor.label.resolvedColor(with: contentTraitCollection) |
| 87 | + } |
| 88 | + |
| 89 | + private func observeGlassAppearance() { |
| 90 | + guard #available(iOS 17.0, *), let contentView = self.backgroundView?.contentView else { return } |
| 91 | + |
| 92 | + _ = contentView.registerForTraitChanges([UITraitUserInterfaceStyle.self]) { [weak self] (_: UIView, _: UITraitCollection) in |
| 93 | + self?.updateForegroundColor() |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private func addBackgroundView() { |
| 98 | + let backgroundView: UIVisualEffectView |
| 99 | + |
| 100 | + if #available(iOS 26.0, *) { |
| 101 | + backgroundView = self.addGlassView() |
| 102 | + } else { |
| 103 | + backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterial)) |
| 104 | + backgroundView.translatesAutoresizingMaskIntoConstraints = false |
| 105 | + self.insertSubview(backgroundView, at: 0) |
| 106 | + |
| 107 | + NSLayoutConstraint.activate([ |
| 108 | + backgroundView.leadingAnchor.constraint(equalTo: self.leadingAnchor), |
| 109 | + backgroundView.trailingAnchor.constraint(equalTo: self.trailingAnchor), |
| 110 | + backgroundView.topAnchor.constraint(equalTo: self.topAnchor), |
| 111 | + backgroundView.bottomAnchor.constraint(equalTo: self.bottomAnchor) |
| 112 | + ]) |
| 113 | + } |
| 114 | + |
| 115 | + // Glass draws outside its bounds, so it has to be clipped to the circle as well |
| 116 | + backgroundView.layer.cornerRadius = MediaDownloadIndicatorView.preferredSize / 2 |
| 117 | + backgroundView.layer.masksToBounds = true |
| 118 | + |
| 119 | + self.backgroundView = backgroundView |
| 120 | + } |
| 121 | + |
| 122 | + func showProgress() { |
| 123 | + self.retryHandler = nil |
| 124 | + self.retryButton.isHidden = true |
| 125 | + self.activityIndicator.isHidden = false |
| 126 | + self.activityIndicator.startAnimating() |
| 127 | + |
| 128 | + self.isAccessibilityElement = true |
| 129 | + } |
| 130 | + |
| 131 | + func showRetry(handler: @escaping () -> Void) { |
| 132 | + self.retryHandler = handler |
| 133 | + self.activityIndicator.stopAnimating() |
| 134 | + self.activityIndicator.isHidden = true |
| 135 | + self.retryButton.isHidden = false |
| 136 | + |
| 137 | + // The button carries the accessibility information in this mode |
| 138 | + self.isAccessibilityElement = false |
| 139 | + self.accessibilityValue = nil |
| 140 | + } |
| 141 | + |
| 142 | + func stopAnimating() { |
| 143 | + self.activityIndicator.stopAnimating() |
| 144 | + } |
| 145 | + |
| 146 | + func setProgress(_ progress: Float) { |
| 147 | + guard self.retryHandler == nil else { return } |
| 148 | + |
| 149 | + self.activityIndicator.indicatorMode = .determinate |
| 150 | + self.activityIndicator.setProgress(progress, animated: true) |
| 151 | + self.accessibilityValue = String(format: "%.0f%%", progress * 100) |
| 152 | + } |
| 153 | +} |
0 commit comments