1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Views/OpenVideosView.swift

220 lines
6.5 KiB
Swift
Raw Normal View History

2022-11-10 22:41:28 +05:30
import SwiftUI
struct OpenVideosView: View {
@State private var presentingFileImporter = false
2022-11-11 02:50:35 +05:30
@State private var urlsToOpenText = ""
2022-11-10 22:41:28 +05:30
@State private var playbackMode = OpenVideosModel.PlaybackMode.playNow
@State private var removeQueueItems = false
@ObservedObject private var navigation = NavigationModel.shared
2022-11-10 22:41:28 +05:30
@Environment(\.openURL) private var openURL
@Environment(\.presentationMode) private var presentationMode
var body: some View {
#if os(macOS)
openVideos
2022-11-11 23:20:13 +05:30
.frame(minWidth: 600, maxWidth: 800, minHeight: 350, maxHeight: 500)
2022-11-10 22:41:28 +05:30
#else
NavigationView {
2022-11-13 04:37:23 +05:30
ScrollView(.vertical, showsIndicators: false) {
openVideos
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
closeButton
2022-11-10 22:41:28 +05:30
}
2022-11-13 04:37:23 +05:30
}
.navigationTitle("Open Videos")
2022-11-11 20:49:14 +05:30
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
2022-11-10 22:41:28 +05:30
}
#endif
}
var closeButton: some View {
Button(action: { presentationMode.wrappedValue.dismiss() }) {
2022-12-12 03:45:56 +05:30
Label("Done", systemImage: "xmark")
}
#if os(macOS)
.labelStyle(.titleOnly)
#endif
#if !os(tvOS)
.keyboardShortcut(.cancelAction)
#endif
}
2022-11-10 22:41:28 +05:30
var openVideos: some View {
VStack(alignment: .leading) {
#if os(macOS)
closeButton
#endif
2022-11-10 22:41:28 +05:30
ZStack(alignment: .topLeading) {
#if os(tvOS)
2022-11-11 23:20:13 +05:30
TextField("URL to Open", text: $urlsToOpenText)
2022-11-10 22:41:28 +05:30
#else
TextEditor(text: $urlsToOpenText)
.padding(2)
.border(Color(white: 0.8), width: 1)
2022-11-11 23:20:13 +05:30
.frame(minHeight: 100, maxHeight: 250)
2022-11-10 22:41:28 +05:30
#if !os(macOS)
.keyboardType(.URL)
#endif
#endif
}
2022-11-11 23:20:13 +05:30
Group {
#if os(tvOS)
Text("Enter link to open")
#else
Text("Enter links to open, one per line")
#endif
2022-11-10 22:41:28 +05:30
}
2022-11-11 23:20:13 +05:30
.font(.caption2)
.foregroundColor(.secondary)
playbackModeControl
2022-11-10 22:41:28 +05:30
Toggle(isOn: $removeQueueItems) {
2022-11-11 23:20:13 +05:30
Text("Clear Queue before opening")
2022-11-10 22:41:28 +05:30
}
.disabled(!playbackMode.allowsRemovingQueueItems)
.padding(.bottom)
HStack {
Group {
2022-11-11 23:20:13 +05:30
#if os(tvOS)
Spacer()
#endif
2022-11-11 02:17:27 +05:30
openURLsButton
2022-11-11 23:20:13 +05:30
Spacer()
2022-11-10 22:41:28 +05:30
2022-11-11 02:58:55 +05:30
#if !os(tvOS)
openFromClipboardButton
#endif
2022-11-10 22:41:28 +05:30
}
}
2022-11-11 02:17:27 +05:30
.padding(.bottom, 10)
2022-11-11 02:58:55 +05:30
#if !os(tvOS)
openFilesButton
#endif
2022-11-10 22:41:28 +05:30
Spacer()
}
.padding()
2022-11-11 23:20:13 +05:30
.alert(isPresented: $navigation.presentingAlertInOpenVideos) { navigation.alert }
2022-11-10 22:41:28 +05:30
#if !os(tvOS)
.fileImporter(
isPresented: $presentingFileImporter,
allowedContentTypes: [.audiovisualContent],
allowsMultipleSelection: true
) { result in
do {
let selectedFiles = try result.get()
let urlsToOpen = selectedFiles.map { url in
if let bookmarkURL = URLBookmarkModel.shared.loadBookmark(url) {
return bookmarkURL
}
if url.startAccessingSecurityScopedResource() {
URLBookmarkModel.shared.saveBookmark(url)
}
return url
}
2022-11-14 04:18:55 +05:30
openURLs(urlsToOpen)
2022-11-10 22:41:28 +05:30
} catch {
navigation.alert = Alert(title: Text("Could not open Files"))
navigation.presentingAlertInOpenVideos = true
2022-11-10 22:41:28 +05:30
}
presentationMode.wrappedValue.dismiss()
}
#endif
}
2022-11-11 23:20:13 +05:30
var playbackModeControl: some View {
HStack {
#if !os(tvOS)
Text("Playback Mode")
Spacer()
#endif
#if os(iOS)
Menu {
playbackModePicker
} label: {
Text(playbackMode.description)
}
#else
playbackModePicker
#if !os(tvOS)
.frame(maxWidth: 200)
#endif
#endif
}
.transaction { t in t.animation = .none }
.padding(.bottom, 5)
.frame(maxWidth: .infinity, alignment: .center)
}
var playbackModePicker: some View {
Picker("Playback Mode", selection: $playbackMode) {
ForEach(OpenVideosModel.PlaybackMode.allCases, id: \.rawValue) { mode in
Text(mode.description).tag(mode)
}
}
.labelsHidden()
}
2022-11-11 02:17:27 +05:30
var openURLsButton: some View {
2022-12-16 14:03:06 +05:30
AccentButton(text: "Open", imageSystemName: "network") {
2022-11-11 02:17:27 +05:30
openURLs(urlsToOpenFromText)
}
.disabled(urlsToOpenFromText.isEmpty)
2022-11-11 23:20:13 +05:30
#if os(tvOS)
.frame(maxWidth: 600)
#else
2022-11-11 02:17:27 +05:30
.keyboardShortcut(.defaultAction)
#endif
}
var openFromClipboardButton: some View {
2022-12-16 14:03:06 +05:30
AccentButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
2022-11-11 02:17:27 +05:30
OpenVideosModel.shared.openURLsFromClipboard(
removeQueueItems: removeQueueItems,
playbackMode: playbackMode
)
}
}
var openFilesButton: some View {
2022-12-16 14:03:06 +05:30
AccentButton(text: "Open Files", imageSystemName: "folder") {
2022-11-11 02:17:27 +05:30
presentingFileImporter = true
}
}
2022-11-10 22:41:28 +05:30
var urlsToOpenFromText: [URL] {
2022-11-11 02:17:27 +05:30
OpenVideosModel.shared.urlsFrom(urlsToOpenText)
2022-11-10 22:41:28 +05:30
}
func openURLs(_ urls: [URL]) {
OpenVideosModel.shared.openURLs(urls, removeQueueItems: removeQueueItems, playbackMode: playbackMode)
presentationMode.wrappedValue.dismiss()
}
}
struct OpenVideosView_Previews: PreviewProvider {
static var previews: some View {
OpenVideosView()
2022-11-11 23:20:13 +05:30
.injectFixtureEnvironmentObjects()
2022-11-10 22:41:28 +05:30
#if os(iOS)
.navigationViewStyle(.stack)
#endif
}
}