From 2982c971a8da71e831f31503e2dab72a01a4ed20 Mon Sep 17 00:00:00 2001 From: kingbri Date: Thu, 30 Mar 2023 12:12:14 -0400 Subject: [PATCH] Pickers: Change to the Picker structure Pickers used to use a List workaround, change this to use actual SwiftUI pickers. Signed-off-by: kingbri --- .../Source/SourceSettingsMethodView.swift | 47 ++---- .../Settings/DefaultActionPickerView.swift | 134 ++++-------------- Ferrite/Views/SettingsView.swift | 11 +- 3 files changed, 45 insertions(+), 147 deletions(-) diff --git a/Ferrite/Views/ComponentViews/Plugin/Source/SourceSettingsMethodView.swift b/Ferrite/Views/ComponentViews/Plugin/Source/SourceSettingsMethodView.swift index b7e27aa..c1de425 100644 --- a/Ferrite/Views/ComponentViews/Plugin/Source/SourceSettingsMethodView.swift +++ b/Ferrite/Views/ComponentViews/Plugin/Source/SourceSettingsMethodView.swift @@ -12,50 +12,21 @@ struct SourceSettingsMethodView: View { var body: some View { Section(header: InlineHeader("Fetch method")) { - if selectedSource.jsonParser != nil { - Button { - selectedSource.preferredParser = SourcePreferredParser.siteApi.rawValue - } label: { - HStack { - Text("Website API") - Spacer() - if SourcePreferredParser.siteApi.rawValue == selectedSource.preferredParser { - Image(systemName: "checkmark") - .foregroundColor(.blue) - } - } + Picker("", selection: $selectedSource.preferredParser) { + if selectedSource.jsonParser != nil { + Text("Website API").tag(SourcePreferredParser.siteApi.rawValue) } - } - if selectedSource.rssParser != nil { - Button { - selectedSource.preferredParser = SourcePreferredParser.rss.rawValue - } label: { - HStack { - Text("RSS") - Spacer() - if SourcePreferredParser.rss.rawValue == selectedSource.preferredParser { - Image(systemName: "checkmark") - .foregroundColor(.blue) - } - } + if selectedSource.rssParser != nil { + Text("RSS").tag(SourcePreferredParser.rss.rawValue) } - } - if selectedSource.htmlParser != nil { - Button { - selectedSource.preferredParser = SourcePreferredParser.scraping.rawValue - } label: { - HStack { - Text("Web scraping") - Spacer() - if SourcePreferredParser.scraping.rawValue == selectedSource.preferredParser { - Image(systemName: "checkmark") - .foregroundColor(.blue) - } - } + if selectedSource.htmlParser != nil { + Text("Web scraping").tag(SourcePreferredParser.scraping.rawValue) } } + .pickerStyle(.inline) + .labelsHidden() } .tint(.primary) } diff --git a/Ferrite/Views/ComponentViews/Settings/DefaultActionPickerView.swift b/Ferrite/Views/ComponentViews/Settings/DefaultActionPickerView.swift index 749d960..0aa8b00 100644 --- a/Ferrite/Views/ComponentViews/Settings/DefaultActionPickerView.swift +++ b/Ferrite/Views/ComponentViews/Settings/DefaultActionPickerView.swift @@ -24,28 +24,41 @@ struct DefaultActionPickerView: View { sortDescriptors: [] ) var pluginLists: FetchedResults - @FetchRequest( - entity: KodiServer.entity(), - sortDescriptors: [] - ) var kodiServers: FetchedResults + var kodiServers: FetchedResults var body: some View { List { - DefaultChoiceButton(defaultAction: $defaultAction, selectedOption: .none) - DefaultChoiceButton(defaultAction: $defaultAction, selectedOption: .share) + Picker("", selection: $defaultAction) { + Text("Let me choose").tag(DefaultAction.none) + Text("Share link").tag(DefaultAction.share) - if actionRequirement == .debrid, !kodiServers.isEmpty { - DefaultChoiceButton(defaultAction: $defaultAction, selectedOption: .kodi) - } + if actionRequirement == .debrid, !kodiServers.isEmpty { + Text("Open in Kodi").tag(DefaultAction.kodi) + } - // Handle custom here - ForEach(actions.filter { $0.requires.contains(actionRequirement.rawValue) }, id: \.id) { action in - CustomChoiceButton( - action: action, - defaultAction: $defaultAction, - associatedPluginList: pluginLists.first(where: { $0.id == action.listId }) - ) + ForEach(actions.filter { $0.requires.contains(actionRequirement.rawValue) }, id: \.id) { action in + VStack(alignment: .leading, spacing: 5) { + Text(action.name) + + Group { + if let associatedPluginList = pluginLists.first(where: { $0.id == action.listId }) { + Text("List: \(associatedPluginList.name)") + + Text(associatedPluginList.id.uuidString) + .font(.caption) + } else { + Text("No plugin list found") + .font(.caption) + } + } + .foregroundColor(.secondary) + .lineLimit(1) + } + .tag(DefaultAction.custom(name: action.name, listId: action.listId?.uuidString ?? "")) + } } + .pickerStyle(.inline) + .labelsHidden() } .listStyle(.insetGrouped) .inlinedList(inset: -20) @@ -53,92 +66,3 @@ struct DefaultActionPickerView: View { .navigationBarTitleDisplayMode(.inline) } } - -private struct CustomChoiceButton: View { - @EnvironmentObject var logManager: LoggingManager - - @ObservedObject var action: Action - - @Binding var defaultAction: DefaultAction - - var associatedPluginList: PluginList? - - var body: some View { - Button { - if let actionListId = action.listId?.uuidString { - defaultAction = .custom(name: action.name, listId: actionListId) - } else { - logManager.error( - "Default action: This action doesn't have a corresponding plugin list! Please uninstall the action" - ) - } - } label: { - HStack { - VStack(alignment: .leading, spacing: 5) { - Text(action.name) - - Group { - if let associatedPluginList { - Text("List: \(associatedPluginList.name)") - - Text(associatedPluginList.id.uuidString) - .font(.caption) - } else { - Text("No plugin list found") - .font(.caption) - } - } - .foregroundColor(.secondary) - .lineLimit(1) - } - Spacer() - - if - case let .custom(name, listId) = defaultAction, - action.listId?.uuidString == listId, - action.name == name - { - Image(systemName: "checkmark") - .foregroundColor(.blue) - } - } - } - .tint(.primary) - } -} - -private struct DefaultChoiceButton: View { - @Binding var defaultAction: DefaultAction - let selectedOption: DefaultAction - - var body: some View { - Button { - defaultAction = selectedOption - } label: { - HStack { - Text(fetchButtonName()) - Spacer() - - if defaultAction == selectedOption { - Image(systemName: "checkmark") - .foregroundColor(.blue) - } - } - } - .tint(.primary) - } - - func fetchButtonName() -> String { - switch selectedOption { - case .none: - return "Let me choose" - case .share: - return "Share link" - case .kodi: - return "Open in Kodi" - case .custom: - // This should not be called - return "Custom button" - } - } -} diff --git a/Ferrite/Views/SettingsView.swift b/Ferrite/Views/SettingsView.swift index 5919468..eda26ec 100644 --- a/Ferrite/Views/SettingsView.swift +++ b/Ferrite/Views/SettingsView.swift @@ -80,11 +80,13 @@ struct SettingsView: View { } Section(header: InlineHeader("Default actions")) { - if debridManager.enabledDebrids.count > 0 { + // TODO: Uncomment + //if debridManager.enabledDebrids.count > 0 { NavigationLink { DefaultActionPickerView( actionRequirement: .debrid, - defaultAction: $defaultDebridAction.value + defaultAction: $defaultDebridAction.value, + kodiServers: kodiServers ) } label: { HStack { @@ -106,12 +108,13 @@ struct SettingsView: View { .foregroundColor(.secondary) } } - } + //} NavigationLink { DefaultActionPickerView( actionRequirement: .magnet, - defaultAction: $defaultMagnetAction.value + defaultAction: $defaultMagnetAction.value, + kodiServers: kodiServers ) } label: { HStack {