session-ios/Session/Conversations/Message Cells/Content Views/MediaView.swift

490 lines
17 KiB
Swift
Raw Normal View History

//
2019-02-28 21:31:35 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
2021-01-29 01:46:32 +01:00
@objc(OWSMediaView)
public class MediaView: UIView {
2018-11-08 17:14:30 +01:00
private enum MediaError {
case missing
case invalid
case failed
}
2018-11-08 17:14:30 +01:00
// MARK: -
private let mediaCache: NSCache<NSString, AnyObject>
2018-11-09 22:58:58 +01:00
@objc
public let attachment: TSAttachment
private let isOutgoing: Bool
2018-11-08 17:14:30 +01:00
private let maxMessageWidth: CGFloat
2020-02-04 00:25:37 +01:00
private var loadBlock: (() -> Void)?
private var unloadBlock: (() -> Void)?
2018-12-07 21:56:02 +01:00
2018-12-07 22:24:46 +01:00
// MARK: - LoadState
2018-12-07 21:46:43 +01:00
// The loadState property allows us to:
//
// * Make sure we only have one load attempt
// enqueued at a time for a given piece of media.
// * We never retry media that can't be loaded.
// * We skip media loads which are no longer
// necessary by the time they reach the front
// of the queue.
2018-12-07 22:24:46 +01:00
enum LoadState {
case unloaded
case loading
case loaded
case failed
}
// Thread-safe access to load state.
//
// We use a "box" class so that we can capture a reference
// to this box (rather than self) and a) safely access
// if off the main thread b) not prevent deallocation of
// self.
private class ThreadSafeLoadState {
private var value: LoadState
required init(_ value: LoadState) {
self.value = value
}
func get() -> LoadState {
objc_sync_enter(self)
let valueCopy = value
objc_sync_exit(self)
return valueCopy
}
func set(_ newValue: LoadState) {
objc_sync_enter(self)
value = newValue
objc_sync_exit(self)
}
}
private let threadSafeLoadState = ThreadSafeLoadState(.unloaded)
// Convenience accessors.
private var loadState: LoadState {
get {
return threadSafeLoadState.get()
}
set {
threadSafeLoadState.set(newValue)
}
}
// MARK: - Initializers
@objc
public required init(mediaCache: NSCache<NSString, AnyObject>,
attachment: TSAttachment,
2018-11-08 17:14:30 +01:00
isOutgoing: Bool,
2021-01-29 01:46:32 +01:00
maxMessageWidth: CGFloat) {
self.mediaCache = mediaCache
self.attachment = attachment
self.isOutgoing = isOutgoing
2018-11-08 17:14:30 +01:00
self.maxMessageWidth = maxMessageWidth
super.init(frame: .zero)
2021-01-29 01:46:32 +01:00
backgroundColor = Colors.unimportant
2018-11-06 15:31:29 +01:00
clipsToBounds = true
createContents()
}
@available(*, unavailable, message: "use other init() instead.")
required public init?(coder aDecoder: NSCoder) {
notImplemented()
}
2018-12-07 22:24:46 +01:00
deinit {
AssertIsOnMainThread()
loadState = .unloaded
}
// MARK: -
private func createContents() {
AssertIsOnMainThread()
guard let attachmentStream = attachment as? TSAttachmentStream else {
2018-11-08 17:14:30 +01:00
addDownloadProgressIfNecessary()
return
}
guard !isFailedDownload else {
2018-11-13 19:14:24 +01:00
configure(forError: .failed)
return
}
if attachmentStream.isAnimated {
configureForAnimatedImage(attachmentStream: attachmentStream)
} else if attachmentStream.isImage {
configureForStillImage(attachmentStream: attachmentStream)
} else if attachmentStream.isVideo {
configureForVideo(attachmentStream: attachmentStream)
} else {
2019-02-28 21:49:51 +01:00
owsFailDebug("Attachment has unexpected type.")
2018-11-13 19:14:24 +01:00
configure(forError: .invalid)
2018-11-08 17:14:30 +01:00
}
}
private func addDownloadProgressIfNecessary() {
guard !isFailedDownload else {
2018-11-13 19:14:24 +01:00
configure(forError: .failed)
return
}
2018-11-08 17:14:30 +01:00
guard let attachmentPointer = attachment as? TSAttachmentPointer else {
owsFailDebug("Attachment has unexpected type.")
2018-11-13 19:14:24 +01:00
configure(forError: .invalid)
2018-11-08 17:14:30 +01:00
return
}
guard attachmentPointer.pointerType == .incoming else {
// TODO: Show "restoring" indicator and possibly progress.
configure(forError: .missing)
return
}
backgroundColor = (Theme.isDarkThemeEnabled ? .ows_gray90 : .ows_gray05)
2021-01-29 01:46:32 +01:00
let loader = MediaLoaderView()
addSubview(loader)
loader.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.bottom, UIView.HorizontalEdge.right ], to: self)
}
2018-11-08 22:55:54 +01:00
private func addUploadProgressIfNecessary(_ subview: UIView) -> Bool {
2020-02-04 00:25:37 +01:00
guard isOutgoing else { return false }
guard let attachmentStream = attachment as? TSAttachmentStream else { return false }
guard !attachmentStream.isUploaded else { return false }
2021-01-29 01:46:32 +01:00
let loader = MediaLoaderView()
addSubview(loader)
loader.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.bottom, UIView.HorizontalEdge.right ], to: self)
2018-11-08 22:55:54 +01:00
return true
}
private func configureForAnimatedImage(attachmentStream: TSAttachmentStream) {
guard let cacheKey = attachmentStream.uniqueId else {
owsFailDebug("Attachment stream missing unique ID.")
return
}
let animatedImageView = YYAnimatedImageView()
// We need to specify a contentMode since the size of the image
// might not match the aspect ratio of the view.
animatedImageView.contentMode = .scaleAspectFill
// Use trilinear filters for better scaling quality at
// some performance cost.
animatedImageView.layer.minificationFilter = .trilinear
animatedImageView.layer.magnificationFilter = .trilinear
2021-01-29 01:46:32 +01:00
animatedImageView.backgroundColor = Colors.unimportant
addSubview(animatedImageView)
animatedImageView.autoPinEdgesToSuperviewEdges()
_ = addUploadProgressIfNecessary(animatedImageView)
2018-11-08 21:40:43 +01:00
loadBlock = { [weak self] in
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
2018-12-12 16:06:21 +01:00
guard let strongSelf = self else {
return
}
if animatedImageView.image != nil {
2018-12-07 21:46:43 +01:00
owsFailDebug("Unexpectedly already loaded.")
return
}
2018-12-12 16:06:21 +01:00
strongSelf.tryToLoadMedia(loadMediaBlock: { () -> AnyObject? in
2018-11-06 15:46:44 +01:00
guard attachmentStream.isValidImage else {
Logger.warn("Ignoring invalid attachment.")
2018-11-06 15:46:44 +01:00
return nil
}
guard let filePath = attachmentStream.originalFilePath else {
owsFailDebug("Attachment stream missing original file path.")
return nil
}
let animatedImage = YYImage(contentsOfFile: filePath)
return animatedImage
},
2018-12-07 21:46:43 +01:00
applyMediaBlock: { (media) in
AssertIsOnMainThread()
guard let image = media as? YYImage else {
2018-12-07 21:56:02 +01:00
owsFailDebug("Media has unexpected type: \(type(of: media))")
2018-12-07 21:46:43 +01:00
return
}
animatedImageView.image = image
},
cacheKey: cacheKey)
}
unloadBlock = {
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
animatedImageView.image = nil
}
}
private func configureForStillImage(attachmentStream: TSAttachmentStream) {
guard let cacheKey = attachmentStream.uniqueId else {
owsFailDebug("Attachment stream missing unique ID.")
return
}
let stillImageView = UIImageView()
// We need to specify a contentMode since the size of the image
// might not match the aspect ratio of the view.
stillImageView.contentMode = .scaleAspectFill
// Use trilinear filters for better scaling quality at
// some performance cost.
stillImageView.layer.minificationFilter = .trilinear
stillImageView.layer.magnificationFilter = .trilinear
2021-01-29 01:46:32 +01:00
stillImageView.backgroundColor = Colors.unimportant
addSubview(stillImageView)
stillImageView.autoPinEdgesToSuperviewEdges()
_ = addUploadProgressIfNecessary(stillImageView)
loadBlock = { [weak self] in
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
if stillImageView.image != nil {
2018-12-07 21:46:43 +01:00
owsFailDebug("Unexpectedly already loaded.")
return
}
2018-12-07 21:46:43 +01:00
self?.tryToLoadMedia(loadMediaBlock: { () -> AnyObject? in
2018-11-06 15:46:44 +01:00
guard attachmentStream.isValidImage else {
Logger.warn("Ignoring invalid attachment.")
2018-11-06 15:46:44 +01:00
return nil
}
return attachmentStream.thumbnailImageLarge(success: { (image) in
2018-12-07 21:46:43 +01:00
AssertIsOnMainThread()
2018-12-07 21:56:02 +01:00
stillImageView.image = image
}, failure: {
Logger.error("Could not load thumbnail")
})
},
2018-12-07 21:46:43 +01:00
applyMediaBlock: { (media) in
AssertIsOnMainThread()
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
guard let image = media as? UIImage else {
2018-12-07 21:56:02 +01:00
owsFailDebug("Media has unexpected type: \(type(of: media))")
2018-12-07 21:46:43 +01:00
return
}
stillImageView.image = image
},
cacheKey: cacheKey)
}
unloadBlock = {
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
stillImageView.image = nil
}
}
private func configureForVideo(attachmentStream: TSAttachmentStream) {
guard let cacheKey = attachmentStream.uniqueId else {
owsFailDebug("Attachment stream missing unique ID.")
return
}
let stillImageView = UIImageView()
// We need to specify a contentMode since the size of the image
// might not match the aspect ratio of the view.
stillImageView.contentMode = .scaleAspectFill
// Use trilinear filters for better scaling quality at
// some performance cost.
stillImageView.layer.minificationFilter = .trilinear
stillImageView.layer.magnificationFilter = .trilinear
2021-01-29 01:46:32 +01:00
stillImageView.backgroundColor = Colors.unimportant
addSubview(stillImageView)
stillImageView.autoPinEdgesToSuperviewEdges()
2018-11-08 22:55:54 +01:00
if !addUploadProgressIfNecessary(stillImageView) {
let videoPlayIcon = UIImage(named: "CirclePlay")
2018-11-08 22:55:54 +01:00
let videoPlayButton = UIImageView(image: videoPlayIcon)
videoPlayButton.set(.width, to: 72)
videoPlayButton.set(.height, to: 72)
2018-11-08 22:55:54 +01:00
stillImageView.addSubview(videoPlayButton)
videoPlayButton.autoCenterInSuperview()
}
loadBlock = { [weak self] in
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
if stillImageView.image != nil {
2018-12-07 21:46:43 +01:00
owsFailDebug("Unexpectedly already loaded.")
return
}
2018-12-07 21:46:43 +01:00
self?.tryToLoadMedia(loadMediaBlock: { () -> AnyObject? in
2018-11-06 15:46:44 +01:00
guard attachmentStream.isValidVideo else {
Logger.warn("Ignoring invalid attachment.")
2018-11-06 15:46:44 +01:00
return nil
}
return attachmentStream.thumbnailImageMedium(success: { (image) in
2018-12-07 21:46:43 +01:00
AssertIsOnMainThread()
2018-12-07 21:56:02 +01:00
stillImageView.image = image
}, failure: {
Logger.error("Could not load thumbnail")
})
},
2018-12-07 21:46:43 +01:00
applyMediaBlock: { (media) in
AssertIsOnMainThread()
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
guard let image = media as? UIImage else {
2018-12-07 21:56:02 +01:00
owsFailDebug("Media has unexpected type: \(type(of: media))")
2018-12-07 21:46:43 +01:00
return
}
stillImageView.image = image
},
cacheKey: cacheKey)
}
unloadBlock = {
2018-12-10 16:12:21 +01:00
AssertIsOnMainThread()
stillImageView.image = nil
}
}
2018-11-09 19:58:31 +01:00
private var isFailedDownload: Bool {
2018-11-08 22:11:53 +01:00
guard let attachmentPointer = attachment as? TSAttachmentPointer else {
return false
}
return attachmentPointer.state == .failed
}
2018-11-13 19:14:24 +01:00
private func configure(forError error: MediaError) {
backgroundColor = (Theme.isDarkThemeEnabled ? .ows_gray90 : .ows_gray05)
2018-11-08 22:11:53 +01:00
let icon: UIImage
2019-03-30 15:50:52 +01:00
switch error {
2018-11-13 19:14:24 +01:00
case .failed:
guard let asset = UIImage(named: "media_retry") else {
owsFailDebug("Missing image")
return
}
icon = asset
case .invalid:
2018-11-08 22:11:53 +01:00
guard let asset = UIImage(named: "media_invalid") else {
owsFailDebug("Missing image")
return
}
icon = asset
case .missing:
return
2018-11-08 22:11:53 +01:00
}
let iconView = UIImageView(image: icon.withRenderingMode(.alwaysTemplate))
2021-01-29 01:46:32 +01:00
iconView.tintColor = Colors.text.withAlphaComponent(Values.mediumOpacity)
addSubview(iconView)
2018-11-08 22:11:53 +01:00
iconView.autoCenterInSuperview()
2018-11-08 17:14:30 +01:00
}
private func tryToLoadMedia(loadMediaBlock: @escaping () -> AnyObject?,
2018-12-07 21:46:43 +01:00
applyMediaBlock: @escaping (AnyObject) -> Void,
cacheKey: String) {
AssertIsOnMainThread()
2018-12-07 21:46:43 +01:00
// It's critical that we update loadState once
// our load attempt is complete.
2018-12-07 21:56:02 +01:00
let loadCompletion: (AnyObject?) -> Void = { [weak self] (possibleMedia) in
2018-12-07 21:46:43 +01:00
AssertIsOnMainThread()
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
guard let strongSelf = self else {
return
}
guard strongSelf.loadState == .loading else {
Logger.verbose("Skipping obsolete load.")
return
}
guard let media = possibleMedia else {
strongSelf.loadState = .failed
// TODO:
// [self showAttachmentErrorViewWithMediaView:mediaView];
return
}
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
applyMediaBlock(media)
strongSelf.loadState = .loaded
}
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
guard loadState == .loading else {
owsFailDebug("Unexpected load state: \(loadState)")
return
}
2018-12-07 21:46:43 +01:00
let mediaCache = self.mediaCache
if let media = mediaCache.object(forKey: cacheKey as NSString) {
Logger.verbose("media cache hit")
2018-12-10 20:55:06 +01:00
loadCompletion(media)
2018-12-07 21:46:43 +01:00
return
}
2018-12-07 21:46:43 +01:00
Logger.verbose("media cache miss")
2018-12-07 21:56:02 +01:00
2018-12-07 22:24:46 +01:00
let threadSafeLoadState = self.threadSafeLoadState
2021-01-29 01:46:32 +01:00
MediaView.loadQueue.async {
2018-12-07 22:24:46 +01:00
guard threadSafeLoadState.get() == .loading else {
Logger.verbose("Skipping obsolete load.")
return
}
guard let media = loadMediaBlock() else {
Logger.error("Failed to load media.")
2018-12-07 21:56:02 +01:00
DispatchQueue.main.async {
loadCompletion(nil)
}
2018-12-07 21:56:02 +01:00
return
}
DispatchQueue.main.async {
mediaCache.setObject(media, forKey: cacheKey as NSString)
2018-12-07 22:24:46 +01:00
loadCompletion(media)
2018-12-07 21:46:43 +01:00
}
}
2018-12-07 21:46:43 +01:00
}
// We use this queue to perform the media loads.
// These loads are expensive, so we want to:
//
// * Do them off the main thread.
// * Only do one at a time.
// * Avoid this work if possible (obsolete loads for
// views that are no longer visible, redundant loads
// of media already being loaded, don't retry media
// that can't be loaded, etc.).
2018-12-13 17:02:23 +01:00
// * Do them in _reverse_ order. More recently enqueued
// loads more closely reflect the current view state.
// By processing in reverse order, we improve our
// "skip rate" of obsolete loads.
private static let loadQueue = ReverseDispatchQueue(label: "org.signal.asyncMediaLoadQueue")
2018-12-07 21:46:43 +01:00
@objc
public func loadMedia() {
AssertIsOnMainThread()
2018-12-07 21:46:43 +01:00
switch loadState {
case .unloaded:
loadState = .loading
2018-12-07 21:56:02 +01:00
2018-12-07 21:46:43 +01:00
guard let loadBlock = loadBlock else {
return
}
loadBlock()
case .loading, .loaded, .failed:
break
}
}
@objc
public func unloadMedia() {
AssertIsOnMainThread()
2018-12-07 21:46:43 +01:00
loadState = .unloaded
2018-12-07 21:56:02 +01:00
guard let unloadBlock = unloadBlock else {
return
}
unloadBlock()
}
}