Hide the ALT button when alt text is unavailable
This commit is contained in:
parent
ed580541f0
commit
501e17bf18
|
@ -8,12 +8,12 @@
|
|||
import SwiftUI
|
||||
|
||||
class AltViewController: UIViewController {
|
||||
var alt: String?
|
||||
private var alt: String
|
||||
let label = UILabel()
|
||||
|
||||
convenience init(alt: String?, sourceView: UIView?) {
|
||||
self.init(nibName: nil, bundle: nil)
|
||||
init(alt: String, sourceView: UIView?) {
|
||||
self.alt = alt
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
self.modalPresentationStyle = .popover
|
||||
self.popoverPresentationController?.delegate = self
|
||||
self.popoverPresentationController?.permittedArrowDirections = .up
|
||||
|
@ -21,10 +21,6 @@ class AltViewController: UIViewController {
|
|||
self.overrideUserInterfaceStyle = .dark
|
||||
}
|
||||
|
||||
@objc override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
|
||||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
|
||||
}
|
||||
|
||||
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
@ -37,7 +33,7 @@ class AltViewController: UIViewController {
|
|||
label.lineBreakMode = .byWordWrapping
|
||||
label.lineBreakStrategy = .standard
|
||||
label.font = .preferredFont(forTextStyle: .callout)
|
||||
label.text = alt ?? "ummmmmmm tbd but you shouldn’t see this"
|
||||
label.text = alt
|
||||
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
view.addSubview(label)
|
||||
|
|
|
@ -110,10 +110,6 @@ extension MediaPreviewImageViewController: MediaPreviewPage {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
var altText: String? {
|
||||
viewModel.item.altText
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ImageAnalysisInteractionDelegate
|
||||
|
|
|
@ -141,6 +141,20 @@ extension MediaPreviewViewController {
|
|||
}
|
||||
.store(in: &disposeBag)
|
||||
|
||||
viewModel.$altText
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] altText in
|
||||
guard let self else { return }
|
||||
UIView.animate(withDuration: 0.3) {
|
||||
if altText == nil {
|
||||
self.altButton.alpha = 0
|
||||
} else {
|
||||
self.altButton.alpha = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &disposeBag)
|
||||
|
||||
viewModel.$showingChrome
|
||||
.receive(on: DispatchQueue.main)
|
||||
.removeDuplicates()
|
||||
|
@ -184,7 +198,8 @@ extension MediaPreviewViewController {
|
|||
|
||||
@objc private func altButtonPressed(_ sender: UIButton) {
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
||||
present(AltViewController(alt: viewModel.viewControllers[viewModel.currentPage].altText, sourceView: sender), animated: true)
|
||||
guard let alt = viewModel.altText else { return }
|
||||
present(AltViewController(alt: alt, sourceView: sender), animated: true)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import MastodonCore
|
|||
|
||||
protocol MediaPreviewPage: UIViewController {
|
||||
func setShowingChrome(_ showingChrome: Bool)
|
||||
var altText: String? { get }
|
||||
}
|
||||
|
||||
final class MediaPreviewViewModel: NSObject {
|
||||
|
@ -28,6 +27,7 @@ final class MediaPreviewViewModel: NSObject {
|
|||
|
||||
@Published var currentPage: Int
|
||||
@Published var showingChrome = true
|
||||
@Published var altText: String?
|
||||
|
||||
// output
|
||||
let viewControllers: [MediaPreviewPage]
|
||||
|
@ -43,8 +43,11 @@ final class MediaPreviewViewModel: NSObject {
|
|||
self.item = item
|
||||
var currentPage = 0
|
||||
var viewControllers: [MediaPreviewPage] = []
|
||||
var getAltText = { (page: Int) -> String? in nil }
|
||||
switch item {
|
||||
case .attachment(let previewContext):
|
||||
getAltText = { previewContext.attachments[$0].altDescription }
|
||||
|
||||
currentPage = previewContext.initialIndex
|
||||
for (i, attachment) in previewContext.attachments.enumerated() {
|
||||
switch attachment.kind {
|
||||
|
@ -117,6 +120,10 @@ final class MediaPreviewViewModel: NSObject {
|
|||
self.transitionItem = transitionItem
|
||||
super.init()
|
||||
|
||||
self.$currentPage
|
||||
.map(getAltText)
|
||||
.assign(to: &$altText)
|
||||
|
||||
for viewController in viewControllers {
|
||||
self.$showingChrome
|
||||
.sink { [weak viewController] showingChrome in
|
||||
|
|
|
@ -111,10 +111,6 @@ extension MediaPreviewVideoViewController: MediaPreviewPage {
|
|||
func setShowingChrome(_ showingChrome: Bool) {
|
||||
// TODO: does this do anything?
|
||||
}
|
||||
|
||||
var altText: String? {
|
||||
viewModel.item.altText
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AVPlayerViewControllerDelegate
|
||||
|
|
|
@ -125,13 +125,6 @@ extension MediaPreviewVideoViewModel {
|
|||
case .gif(let mediaContext): return mediaContext.assetURL
|
||||
}
|
||||
}
|
||||
|
||||
var altText: String? {
|
||||
switch self {
|
||||
case .video(let mediaContext): return mediaContext.altText
|
||||
case .gif(let mediaContext): return mediaContext.altText
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RemoteVideoContext {
|
||||
|
|
Loading…
Reference in New Issue