mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-18 11:15:36 -05:00
## Summary Most, but not all, of the changes necessary to support multi-server throughout the app and all its features. ## Screenshots | Light | Dark | | ----- | ---- | |  |  | |  |  | |  |  | |  |  | |  |  | ## Any other notes - Encapsulates all connectivity, token & server-specific knowledge in a Server model object which gets passed around. - Updates various places throughout the app to know about and use Server rather than accessing said information through non-server-specific methods. - Visually requests/notes server in places where it's ambiguous. For example, the Open Page widget will gain a subtitle if multiple servers are set up. - Allows switching which server is shown in the WebViews. Note that this doesn't take into account multi-window support on iPad/macOS yet. Most things will migrate successfully however adding an additional server causes things like Shortcuts to start erroring requiring you specify which to use in the particular Shortcut. Future work necessary: - Model objects currently clobber each other if their identifiers match. For example, both servers having a zone named `home` means one of them wins the fight for which is known to the app. - Being remotely logged out on any account causes the app to require onboarding again, when instead it should only do that if the last known server is logged out.
113 lines
3.9 KiB
Swift
113 lines
3.9 KiB
Swift
import MobileCoreServices
|
|
import PromiseKit
|
|
import Shared
|
|
import UIKit
|
|
import UserNotifications
|
|
import UserNotificationsUI
|
|
|
|
class PlayerAttachmentViewController: UIViewController, NotificationCategory {
|
|
enum PlayerAttachmentError: Error {
|
|
case noAttachment
|
|
}
|
|
|
|
let api: HomeAssistantAPI
|
|
let attachmentURL: URL
|
|
let needsEndSecurityScoped: Bool
|
|
|
|
required init(api: HomeAssistantAPI, notification: UNNotification, attachmentURL: URL?) throws {
|
|
guard let attachmentURL = attachmentURL else {
|
|
throw PlayerAttachmentError.noAttachment
|
|
}
|
|
|
|
self.needsEndSecurityScoped = attachmentURL.startAccessingSecurityScopedResource()
|
|
|
|
if Current.isCatalyst,
|
|
attachmentURL.isFileURL,
|
|
!FileManager.default.isReadableFile(atPath: attachmentURL.path) {
|
|
// if it's a file URL, on macOS we may not have access to the attachment on disk, so make sure
|
|
// FB9638431
|
|
throw PlayerAttachmentError.noAttachment
|
|
}
|
|
|
|
self.api = api
|
|
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()
|
|
}
|
|
}
|