Pickers: Change to the Picker structure

Pickers used to use a List workaround, change this to use actual
SwiftUI pickers.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-03-30 12:12:14 -04:00
parent e650bbd2bb
commit 2982c971a8
3 changed files with 45 additions and 147 deletions

View file

@ -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)
}

View file

@ -24,28 +24,41 @@ struct DefaultActionPickerView: View {
sortDescriptors: []
) var pluginLists: FetchedResults<PluginList>
@FetchRequest(
entity: KodiServer.entity(),
sortDescriptors: []
) var kodiServers: FetchedResults<KodiServer>
var kodiServers: FetchedResults<KodiServer>
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"
}
}
}

View file

@ -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 {