2022-08-17 04:04:25 +05:30
|
|
|
import Defaults
|
2022-08-14 22:36:22 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2024-04-26 15:57:25 +05:30
|
|
|
struct FormatState: Equatable {
|
|
|
|
let format: QualityProfile.Format
|
|
|
|
var isActive: Bool
|
|
|
|
}
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
struct QualityProfileForm: View {
|
2022-08-15 03:45:18 +05:30
|
|
|
@Binding var qualityProfileID: QualityProfile.ID?
|
2022-08-14 22:36:22 +05:30
|
|
|
|
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
|
|
|
@State private var valid = false
|
|
|
|
|
2023-09-24 20:51:54 +05:30
|
|
|
@State private var initialized = false
|
2022-08-14 22:36:22 +05:30
|
|
|
@State private var name = ""
|
|
|
|
@State private var backend = PlayerBackendType.mpv
|
2022-08-15 03:46:02 +05:30
|
|
|
@State private var resolution = ResolutionSetting.hd1080p60
|
2022-08-14 22:36:22 +05:30
|
|
|
@State private var formats = [QualityProfile.Format]()
|
2024-04-26 15:57:25 +05:30
|
|
|
@State private var orderedFormats: [FormatState] = []
|
2022-08-14 22:36:22 +05:30
|
|
|
|
2022-08-17 04:04:25 +05:30
|
|
|
@Default(.qualityProfiles) private var qualityProfiles
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
var qualityProfile: QualityProfile! {
|
|
|
|
if let id = qualityProfileID {
|
|
|
|
return QualityProfilesModel.shared.find(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-26 15:57:25 +05:30
|
|
|
// swiftlint:disable trailing_closure
|
2022-08-14 22:36:22 +05:30
|
|
|
var body: some View {
|
2022-08-15 03:45:18 +05:30
|
|
|
VStack {
|
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
|
|
|
footer
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
2022-08-15 03:45:18 +05:30
|
|
|
.frame(maxWidth: 1000)
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
2022-08-15 03:45:18 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.padding(20)
|
|
|
|
#endif
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
.onAppear(perform: initializeForm)
|
2024-04-26 15:57:25 +05:30
|
|
|
.onChange(of: backend, perform: { _ in backendChanged(self.backend); updateActiveFormats(); validate() })
|
|
|
|
.onChange(of: name, perform: { _ in validate() })
|
|
|
|
.onChange(of: resolution, perform: { _ in validate() })
|
|
|
|
.onChange(of: orderedFormats, perform: { _ in validate() })
|
2022-08-14 22:36:22 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
|
|
|
#else
|
2024-07-06 16:15:44 +05:30
|
|
|
.frame(width: 400, height: 450)
|
2022-08-14 22:36:22 +05:30
|
|
|
.padding(.vertical, 10)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-04-26 15:57:25 +05:30
|
|
|
// swiftlint:enable trailing_closure
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
var header: some View {
|
2022-11-26 00:01:48 +05:30
|
|
|
HStack {
|
2022-08-14 22:36:22 +05:30
|
|
|
Text(editing ? "Edit Quality Profile" : "Add Quality Profile")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Cancel") {
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
}
|
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.cancelAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
var form: some View {
|
2022-08-15 03:45:18 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
ScrollView {
|
|
|
|
VStack {
|
|
|
|
formFields
|
|
|
|
}
|
|
|
|
.padding(.horizontal, 20)
|
|
|
|
}
|
|
|
|
#else
|
2022-08-14 22:36:22 +05:30
|
|
|
Form {
|
|
|
|
formFields
|
|
|
|
#if os(macOS)
|
|
|
|
.padding(.horizontal)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var formFields: some View {
|
|
|
|
Group {
|
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
nameHeader
|
|
|
|
TextField("Name", text: $name, onCommit: validate)
|
|
|
|
.labelsHidden()
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
Section(header: Text("Resolution")) {
|
|
|
|
qualityButton
|
|
|
|
}
|
2022-08-21 02:35:40 +05:30
|
|
|
Section(header: Text("Backend")) {
|
|
|
|
backendPicker
|
|
|
|
}
|
2022-08-14 22:36:22 +05:30
|
|
|
#else
|
|
|
|
backendPicker
|
|
|
|
qualityPicker
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
Section(header: Text("Preferred Formats"), footer: formatsFooter) {
|
|
|
|
formatsPicker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var nameHeader: some View {
|
|
|
|
#if os(macOS)
|
|
|
|
Text("Name")
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatsFooter: some View {
|
2024-04-26 15:57:25 +05:30
|
|
|
VStack(alignment: .leading) {
|
2024-05-19 04:16:01 +05:30
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
Text("Formats can be reordered and will be selected in this order.")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
} else if #available(iOS 14.0, *) {
|
|
|
|
Text("Formats will be selected in the order they are listed.")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
} else {
|
|
|
|
Text("Formats will be selected in the order they are listed.")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
}
|
|
|
|
|
2024-05-13 11:24:24 +05:30
|
|
|
Text("**Note:** HLS is an adaptive format where specific resolution settings don't apply.")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
.padding(.top)
|
|
|
|
Text("Yattee attempts to match the quality that is closest to the set resolution, but exact results cannot be guaranteed.")
|
2024-04-26 15:57:25 +05:30
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
.padding(.top, 0.1)
|
|
|
|
}
|
|
|
|
.padding(.top, 2)
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
2022-08-15 03:45:18 +05:30
|
|
|
@ViewBuilder var qualityPicker: some View {
|
|
|
|
let picker = Picker("Resolution", selection: $resolution) {
|
2022-08-14 22:36:22 +05:30
|
|
|
ForEach(availableResolutions, id: \.self) { resolution in
|
|
|
|
Text(resolution.description).tag(resolution)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.modifier(SettingsPickerModifier())
|
2022-08-15 03:45:18 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
2022-08-17 04:04:25 +05:30
|
|
|
HStack {
|
2022-08-15 03:45:18 +05:30
|
|
|
Text("Resolution")
|
|
|
|
Spacer()
|
|
|
|
Menu {
|
|
|
|
picker
|
|
|
|
} label: {
|
|
|
|
Text(resolution.description)
|
|
|
|
.frame(minWidth: 120, alignment: .trailing)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2022-08-21 02:35:40 +05:30
|
|
|
picker
|
2022-08-15 03:45:18 +05:30
|
|
|
#endif
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
var qualityButton: some View {
|
|
|
|
Button(resolution.description) {
|
|
|
|
resolution = resolution.next()
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(availableResolutions, id: \.self) { resolution in
|
|
|
|
Button(resolution.description) {
|
|
|
|
self.resolution = resolution
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
var availableResolutions: [ResolutionSetting] {
|
|
|
|
ResolutionSetting.allCases.filter { !isResolutionDisabled($0) }
|
|
|
|
}
|
|
|
|
|
2022-08-15 03:45:18 +05:30
|
|
|
@ViewBuilder var backendPicker: some View {
|
|
|
|
let picker = Picker("Backend", selection: $backend) {
|
2022-08-14 22:36:22 +05:30
|
|
|
ForEach(PlayerBackendType.allCases, id: \.self) { backend in
|
|
|
|
Text(backend.label).tag(backend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.modifier(SettingsPickerModifier())
|
2022-08-15 03:45:18 +05:30
|
|
|
#if os(iOS)
|
2022-08-17 04:04:25 +05:30
|
|
|
HStack {
|
2022-08-15 03:45:18 +05:30
|
|
|
Text("Backend")
|
|
|
|
Spacer()
|
|
|
|
Menu {
|
|
|
|
picker
|
|
|
|
} label: {
|
|
|
|
Text(backend.label)
|
|
|
|
.frame(minWidth: 120, alignment: .trailing)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2022-08-21 02:35:40 +05:30
|
|
|
picker
|
2022-08-15 03:45:18 +05:30
|
|
|
#endif
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
2024-04-26 15:57:25 +05:30
|
|
|
var filteredFormatList: some View {
|
|
|
|
ForEach(Array(orderedFormats.enumerated()), id: \.element.format) { idx, element in
|
|
|
|
let format = element.format
|
|
|
|
MultiselectRow(
|
|
|
|
title: format.description,
|
|
|
|
selected: element.isActive
|
|
|
|
) { value in
|
|
|
|
orderedFormats[idx].isActive = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onMove { source, destination in
|
|
|
|
orderedFormats.move(fromOffsets: source, toOffset: destination)
|
|
|
|
validate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
@ViewBuilder var formatsPicker: some View {
|
|
|
|
#if os(macOS)
|
2024-04-26 15:57:25 +05:30
|
|
|
let list = filteredFormatList
|
2022-08-14 22:36:22 +05:30
|
|
|
|
2023-10-15 17:05:23 +05:30
|
|
|
Group {
|
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
list
|
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
|
|
|
} else {
|
|
|
|
list
|
|
|
|
.listStyle(.inset)
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 22:36:22 +05:30
|
|
|
Spacer()
|
|
|
|
#else
|
2024-04-26 15:57:25 +05:30
|
|
|
filteredFormatList
|
2022-08-14 22:36:22 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFormatSelected(_ format: QualityProfile.Format) -> Bool {
|
2024-04-26 15:57:25 +05:30
|
|
|
return orderedFormats.first { $0.format == format }?.isActive ?? false
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func toggleFormat(_ format: QualityProfile.Format, value: Bool) {
|
2024-04-26 15:57:25 +05:30
|
|
|
if let index = orderedFormats.firstIndex(where: { $0.format == format }) {
|
|
|
|
orderedFormats[index].isActive = value
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
2024-04-26 15:57:25 +05:30
|
|
|
validate() // Check validity after a toggle operation
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var footer: some View {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button("Save", action: submitForm)
|
|
|
|
.disabled(!valid)
|
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.frame(minHeight: 35)
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.top, 30)
|
|
|
|
#endif
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
var editing: Bool {
|
|
|
|
!qualityProfile.isNil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFormatDisabled(_ format: QualityProfile.Format) -> Bool {
|
|
|
|
guard backend == .appleAVPlayer else { return false }
|
|
|
|
|
2024-08-31 01:34:31 +05:30
|
|
|
let avPlayerFormats = [.stream, QualityProfile.Format.hls]
|
2022-08-14 22:36:22 +05:30
|
|
|
|
|
|
|
return !avPlayerFormats.contains(format)
|
|
|
|
}
|
|
|
|
|
2024-04-26 15:57:25 +05:30
|
|
|
func updateActiveFormats() {
|
|
|
|
for (index, format) in orderedFormats.enumerated() where isFormatDisabled(format.format) {
|
|
|
|
orderedFormats[index].isActive = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 22:36:22 +05:30
|
|
|
func isResolutionDisabled(_ resolution: ResolutionSetting) -> Bool {
|
|
|
|
guard backend == .appleAVPlayer else { return false }
|
|
|
|
|
2024-09-09 16:29:39 +05:30
|
|
|
let hd720p30 = Stream.Resolution.predefined(.hd720p30)
|
|
|
|
|
|
|
|
return resolution.value > hd720p30
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func initializeForm() {
|
2024-04-26 15:57:25 +05:30
|
|
|
if editing {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
self.name = qualityProfile.name ?? ""
|
|
|
|
self.backend = qualityProfile.backend
|
|
|
|
self.resolution = qualityProfile.resolution
|
|
|
|
self.orderedFormats = qualityProfile.order.map { order in
|
|
|
|
let format = QualityProfile.Format.allCases[order]
|
|
|
|
let isActive = qualityProfile.formats.contains(format)
|
|
|
|
return FormatState(format: format, isActive: isActive)
|
|
|
|
}
|
|
|
|
self.initialized = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = ""
|
|
|
|
backend = .mpv
|
|
|
|
resolution = .hd720p60
|
|
|
|
orderedFormats = QualityProfile.Format.allCases.map {
|
|
|
|
FormatState(format: $0, isActive: true)
|
|
|
|
}
|
|
|
|
initialized = true
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func backendChanged(_: PlayerBackendType) {
|
2024-04-26 15:57:25 +05:30
|
|
|
let defaultFormats = QualityProfile.Format.allCases.map {
|
|
|
|
FormatState(format: $0, isActive: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if backend == .appleAVPlayer {
|
|
|
|
orderedFormats = orderedFormats.filter { !isFormatDisabled($0.format) }
|
|
|
|
} else {
|
|
|
|
orderedFormats = defaultFormats
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
2022-08-19 03:57:03 +05:30
|
|
|
if isResolutionDisabled(resolution),
|
|
|
|
let newResolution = availableResolutions.first
|
|
|
|
{
|
2022-08-14 22:36:22 +05:30
|
|
|
resolution = newResolution
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate() {
|
2024-04-26 15:57:25 +05:30
|
|
|
if !initialized {
|
|
|
|
valid = false
|
|
|
|
} else if editing {
|
|
|
|
let savedOrderFormats = qualityProfile.order.map { order in
|
|
|
|
let format = QualityProfile.Format.allCases[order]
|
|
|
|
let isActive = qualityProfile.formats.contains(format)
|
|
|
|
return FormatState(format: format, isActive: isActive)
|
|
|
|
}
|
|
|
|
valid = name != qualityProfile.name
|
|
|
|
|| backend != qualityProfile.backend
|
|
|
|
|| resolution != qualityProfile.resolution
|
|
|
|
|| orderedFormats != savedOrderFormats
|
|
|
|
} else { valid = true }
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func submitForm() {
|
|
|
|
guard valid else { return }
|
|
|
|
|
2024-08-18 18:16:51 +05:30
|
|
|
let activeFormats = orderedFormats.filter(\.isActive).map(\.format)
|
2022-08-14 22:36:22 +05:30
|
|
|
|
|
|
|
let formProfile = QualityProfile(
|
|
|
|
id: qualityProfile?.id ?? UUID().uuidString,
|
|
|
|
name: name,
|
|
|
|
backend: backend,
|
|
|
|
resolution: resolution,
|
2024-04-26 15:57:25 +05:30
|
|
|
formats: activeFormats,
|
|
|
|
order: orderedFormats.map { QualityProfile.Format.allCases.firstIndex(of: $0.format)! }
|
2022-08-14 22:36:22 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
if editing {
|
|
|
|
QualityProfilesModel.shared.update(qualityProfile, formProfile)
|
|
|
|
} else {
|
2022-08-17 04:04:25 +05:30
|
|
|
let wasEmpty = qualityProfiles.isEmpty
|
2022-08-14 22:36:22 +05:30
|
|
|
QualityProfilesModel.shared.add(formProfile)
|
2022-08-17 04:04:25 +05:30
|
|
|
|
|
|
|
if wasEmpty {
|
|
|
|
QualityProfilesModel.shared.applyToAll(formProfile)
|
|
|
|
}
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct QualityProfileForm_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-08-15 03:45:18 +05:30
|
|
|
QualityProfileForm(qualityProfileID: .constant(QualityProfile.defaultProfile.id))
|
|
|
|
.environment(\.navigationStyle, .tab)
|
2022-08-14 22:36:22 +05:30
|
|
|
}
|
|
|
|
}
|