yattee/Shared/Settings/ServicesSettings.swift

103 lines
3.4 KiB
Swift
Raw Normal View History

2021-10-23 12:12:53 +00:00
import Defaults
2021-10-23 11:51:02 +00:00
import SwiftUI
struct ServicesSettings: View {
2021-10-23 16:49:45 +00:00
@Default(.sponsorBlockInstance) private var sponsorBlockInstance
@Default(.sponsorBlockCategories) private var sponsorBlockCategories
2021-10-23 11:51:02 +00:00
var body: some View {
2021-10-23 12:12:53 +00:00
Section(header: Text("SponsorBlock API")) {
TextField(
"SponsorBlock API Instance",
2021-10-23 16:49:45 +00:00
text: $sponsorBlockInstance,
2021-10-23 12:12:53 +00:00
prompt: Text("SponsorBlock API URL, leave blank to disable")
)
.labelsHidden()
#if !os(macOS)
.autocapitalization(.none)
.keyboardType(.URL)
#endif
2021-10-23 16:49:45 +00:00
}
2021-10-23 12:12:53 +00:00
2021-10-27 21:11:38 +00:00
Section(header: Text("Categories to Skip")) {
2021-10-23 12:12:53 +00:00
#if os(macOS)
2021-10-23 16:49:45 +00:00
List(SponsorBlockAPI.categories, id: \.self) { category in
SponsorBlockCategorySelectionRow(
title: SponsorBlockAPI.categoryDescription(category) ?? "Unknown",
selected: sponsorBlockCategories.contains(category)
) { value in
toggleCategory(category, value: value)
}
}
.listStyle(.inset(alternatesRowBackgrounds: true))
2021-10-23 12:12:53 +00:00
Spacer()
2021-10-23 16:49:45 +00:00
#else
ForEach(SponsorBlockAPI.categories, id: \.self) { category in
SponsorBlockCategorySelectionRow(
title: SponsorBlockAPI.categoryDescription(category) ?? "Unknown",
selected: sponsorBlockCategories.contains(category)
) { value in
toggleCategory(category, value: value)
}
}
#endif
}
}
func toggleCategory(_ category: String, value: Bool) {
if let index = sponsorBlockCategories.firstIndex(where: { $0 == category }), !value {
sponsorBlockCategories.remove(at: index)
} else if value {
sponsorBlockCategories.insert(category)
}
}
struct SponsorBlockCategorySelectionRow: View {
let title: String
let selected: Bool
var action: (Bool) -> Void
@State private var toggleChecked = false
var body: some View {
Button(action: { action(!selected) }) {
HStack {
#if os(macOS)
Toggle(isOn: $toggleChecked) {
Text(self.title)
Spacer()
}
.onAppear {
toggleChecked = selected
}
.onChange(of: toggleChecked) { new in
action(new)
}
#else
Text(self.title)
Spacer()
if selected {
Image(systemName: "checkmark")
#if os(iOS)
.foregroundColor(.accentColor)
#endif
}
#endif
}
.contentShape(Rectangle())
}
#if !os(tvOS)
.buttonStyle(.plain)
2021-10-23 12:12:53 +00:00
#endif
}
2021-10-23 11:51:02 +00:00
}
}
struct ServicesSettings_Previews: PreviewProvider {
static var previews: some View {
2021-11-01 21:56:18 +00:00
VStack {
ServicesSettings()
}
2021-10-23 11:51:02 +00:00
}
}