1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-12 21:30:32 +05:30
yattee/Model/OpenVideosModel.swift

164 lines
4.9 KiB
Swift
Raw Permalink Normal View History

2022-11-11 02:17:27 +05:30
#if canImport(AppKit)
import AppKit
#endif
2022-11-10 22:41:28 +05:30
import Foundation
import Logging
2022-11-11 02:17:27 +05:30
#if canImport(UIKit)
import UIKit
#endif
2022-11-11 23:20:13 +05:30
import SwiftUI
2022-11-10 22:41:28 +05:30
struct OpenVideosModel {
enum PlaybackMode: String, CaseIterable {
case playNow
case shuffleAll
case playNext
case playLast
var description: String {
switch self {
case .playNow:
return "Play Now".localized()
case .shuffleAll:
return "Shuffle All".localized()
case .playNext:
return "Play Next".localized()
case .playLast:
return "Play Last".localized()
}
}
var allowsRemovingQueueItems: Bool {
self == .playNow || self == .shuffleAll
}
var allowedWhenQueueIsEmpty: Bool {
self == .playNow || self == .shuffleAll
}
}
2023-04-22 18:38:33 +05:30
static let shared = Self()
2022-11-10 22:41:28 +05:30
var player: PlayerModel! = .shared
var logger = Logger(label: "stream.yattee.open-videos")
func open(_ url: URL) {
if url.startAccessingSecurityScopedResource() {
let video = Video.local(url)
player.play([video], shuffling: false)
}
}
2022-11-11 02:17:27 +05:30
var urlsFromClipboard: [URL] {
#if os(iOS)
2022-11-11 21:03:28 +05:30
if let pasteboard = UIPasteboard.general.urls {
return pasteboard
2022-11-11 02:17:27 +05:30
}
#elseif os(macOS)
if let pasteboard = NSPasteboard.general.string(forType: .string) {
return urlsFrom(pasteboard)
}
#endif
return []
}
2023-06-17 17:39:51 +05:30
func openURLsFromClipboard(removeQueueItems: Bool = false, playbackMode: Self.PlaybackMode = .playNow) {
2022-11-11 23:20:13 +05:30
if urlsFromClipboard.isEmpty {
2022-11-19 04:36:13 +05:30
NavigationModel.shared.alert = Alert(title: Text("Could not find any links to open in your clipboard".localized()))
2022-11-11 23:20:13 +05:30
if NavigationModel.shared.presentingOpenVideos {
NavigationModel.shared.presentingAlertInOpenVideos = true
} else {
NavigationModel.shared.presentingAlert = true
}
} else {
openURLs(urlsFromClipboard, removeQueueItems: removeQueueItems, playbackMode: playbackMode)
}
2022-11-11 02:17:27 +05:30
}
2023-06-17 17:39:51 +05:30
func openURLs(_ urls: [URL], removeQueueItems: Bool = false, playbackMode: Self.PlaybackMode = .playNow) {
2022-11-11 02:50:35 +05:30
guard !urls.isEmpty else {
return
}
2022-11-11 20:49:14 +05:30
NavigationModel.shared.presentingOpenVideos = false
2022-11-10 22:41:28 +05:30
logger.info("opening \(urls.count) urls")
urls.forEach { logger.info("\($0.absoluteString)") }
if removeQueueItems, playbackMode.allowsRemovingQueueItems {
player.removeQueueItems()
logger.info("removing queue items")
}
switch playbackMode {
case .playNow:
player.playbackMode = .queue
case .shuffleAll:
player.playbackMode = .shuffle
case .playNext:
player.playbackMode = .queue
case .playLast:
player.playbackMode = .queue
}
enqueue(
urls,
prepending: playbackMode == .playNow || playbackMode == .playNext
)
NavigationModel.shared.presentingChannelSheet = false
2022-12-18 04:38:30 +05:30
2022-11-10 22:41:28 +05:30
if playbackMode == .playNow || playbackMode == .shuffleAll {
2022-11-11 17:04:55 +05:30
#if os(iOS)
if player.presentingPlayer {
player.advanceToNextItem()
} else {
player.onPresentPlayer.append { [weak player] in player?.advanceToNextItem() }
}
#else
player.advanceToNextItem()
#endif
2022-11-11 23:20:13 +05:30
player.show()
2022-11-10 22:41:28 +05:30
}
}
func enqueue(_ urls: [URL], prepending: Bool = false) {
var videos = urls.compactMap { url in
var video: Video!
if canOpenVideosByID {
let parser = URLParser(url: url)
if parser.destination == .video, let id = parser.videoID {
2022-12-09 05:45:19 +05:30
video = Video(app: .local, videoID: id)
2022-11-10 22:41:28 +05:30
logger.info("identified remote video: \(id)")
} else {
video = .local(url)
logger.info("identified local video: \(url.absoluteString)")
}
} else {
video = .local(url)
logger.info("identified local video: \(url.absoluteString)")
}
return video
}
if prepending {
videos.reverse()
}
2024-05-16 21:58:32 +05:30
for video in videos {
2022-11-10 22:41:28 +05:30
player.enqueueVideo(video, play: false, prepending: prepending, loadDetails: false)
}
}
2022-11-11 02:17:27 +05:30
func urlsFrom(_ string: String) -> [URL] {
string.split(whereSeparator: \.isNewline).compactMap { URL(string: String($0)) }
}
2022-11-10 22:41:28 +05:30
var canOpenVideosByID: Bool {
guard let app = AccountsModel.shared.current?.app else { return false }
return !AccountsModel.shared.isEmpty && app.supportsOpeningVideosByID
2022-11-10 22:41:28 +05:30
}
}