mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-19 08:31:12 -06:00
## Summary Allows attachments to be loaded on-the-fly in the content extension if they are not present. ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant#510 ## Any other notes - Adds `lazy` as an option to the `attachment` dictionary. When true, this prevents the attachment from being downloaded except when displayed in the content extension. This is similar, but not identical, to hide-thumbnail. Hiding the thumbnail still uses the internal storage of the notifications which are limited to e.g. 50mb. However, we'd still want to pre-load those attachments even if the thumbnail is hidden. - Retries non-lazy attachments which failed due to a network error when the content extension opens.
103 lines
3.5 KiB
Swift
103 lines
3.5 KiB
Swift
import MobileCoreServices
|
|
import PromiseKit
|
|
import Shared
|
|
import UIKit
|
|
import UserNotifications
|
|
import UserNotificationsUI
|
|
|
|
class PlayerAttachmentViewController: UIViewController, NotificationCategory {
|
|
enum PlayerAttachmentError: Error {
|
|
case noAttachment
|
|
}
|
|
|
|
let attachmentURL: URL
|
|
let needsEndSecurityScoped: Bool
|
|
|
|
required init(notification: UNNotification, attachmentURL: URL?) throws {
|
|
guard let attachmentURL = attachmentURL else {
|
|
throw PlayerAttachmentError.noAttachment
|
|
}
|
|
|
|
self.needsEndSecurityScoped = attachmentURL.startAccessingSecurityScopedResource()
|
|
|
|
self.attachmentURL = attachmentURL
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
if needsEndSecurityScoped {
|
|
videoViewController?.url.stopAccessingSecurityScopedResource()
|
|
}
|
|
}
|
|
|
|
var videoViewController: CameraStreamHLSViewController? {
|
|
willSet {
|
|
videoViewController?.url.stopAccessingSecurityScopedResource()
|
|
videoViewController?.willMove(toParent: nil)
|
|
newValue.flatMap { addChild($0) }
|
|
}
|
|
didSet {
|
|
oldValue?.view.removeFromSuperview()
|
|
oldValue?.removeFromParent()
|
|
|
|
if let videoViewController = videoViewController {
|
|
view.addSubview(videoViewController.view)
|
|
videoViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
NSLayoutConstraint.activate([
|
|
videoViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
|
|
videoViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
videoViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
videoViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
|
|
videoViewController.didMove(toParent: self)
|
|
}
|
|
}
|
|
}
|
|
|
|
func start() -> Promise<Void> {
|
|
let controller = with(CameraStreamHLSViewController(url: attachmentURL)) {
|
|
var lastState: CameraStreamHandlerState?
|
|
$0.didUpdateState = { [extensionContext] state in
|
|
guard lastState != state else {
|
|
return
|
|
}
|
|
|
|
switch state {
|
|
case .playing:
|
|
// if this happens too fast (which happens for local files) the extension context ignores it
|
|
// so trigger a short delay as well
|
|
extensionContext?.mediaPlayingStarted()
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
|
|
if lastState == .playing {
|
|
extensionContext?.mediaPlayingStarted()
|
|
}
|
|
}
|
|
case .paused:
|
|
extensionContext?.mediaPlayingPaused()
|
|
}
|
|
|
|
lastState = state
|
|
}
|
|
}
|
|
videoViewController = controller
|
|
return controller.promise
|
|
}
|
|
|
|
var mediaPlayPauseButtonType: UNNotificationContentExtensionMediaPlayPauseButtonType { .overlay }
|
|
var mediaPlayPauseButtonFrame: CGRect? { nil }
|
|
|
|
func mediaPlay() {
|
|
videoViewController?.play()
|
|
}
|
|
|
|
func mediaPause() {
|
|
videoViewController?.pause()
|
|
}
|
|
}
|