1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Shared/Settings/ServicesSettings.swift

132 lines
4.3 KiB
Swift
Raw Normal View History

2021-10-23 17:42:53 +05:30
import Defaults
2021-10-23 17:21:02 +05:30
import SwiftUI
struct ServicesSettings: View {
2021-10-23 22:19:45 +05:30
@Default(.sponsorBlockInstance) private var sponsorBlockInstance
@Default(.sponsorBlockCategories) private var sponsorBlockCategories
2021-12-05 01:05:41 +05:30
@Default(.commentsInstanceID) private var commentsInstanceID
2021-10-23 22:19:45 +05:30
2021-10-23 17:21:02 +05:30
var body: some View {
2021-12-05 01:05:41 +05:30
Section(header: SettingsHeader(text: "Comments")) {
commentsInstancePicker
}
2021-11-05 03:31:27 +05:30
Section(header: SettingsHeader(text: "SponsorBlock API")) {
2021-10-23 17:42:53 +05:30
TextField(
"SponsorBlock API Instance",
2021-11-28 20:07:55 +05:30
text: $sponsorBlockInstance
2021-10-23 17:42:53 +05:30
)
.labelsHidden()
#if !os(macOS)
.autocapitalization(.none)
.keyboardType(.URL)
#endif
2021-10-23 22:19:45 +05:30
}
2021-10-23 17:42:53 +05:30
2021-11-05 03:31:27 +05:30
Section(header: SettingsHeader(text: "Categories to Skip")) {
2021-10-23 17:42:53 +05:30
#if os(macOS)
2021-12-03 00:52:55 +05:30
let list = ForEach(SponsorBlockAPI.categories, id: \.self) { category in
2021-10-23 22:19:45 +05:30
SponsorBlockCategorySelectionRow(
title: SponsorBlockAPI.categoryDescription(category) ?? "Unknown",
selected: sponsorBlockCategories.contains(category)
) { value in
toggleCategory(category, value: value)
}
}
2021-11-28 20:07:55 +05:30
Group {
if #available(macOS 12.0, *) {
list
.listStyle(.inset(alternatesRowBackgrounds: true))
} else {
list
.listStyle(.inset)
}
}
2021-10-23 17:42:53 +05:30
Spacer()
2021-10-23 22:19:45 +05:30
#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
}
}
2021-12-05 01:05:41 +05:30
private var commentsInstancePicker: some View {
Picker("Comments", selection: $commentsInstanceID) {
2021-12-05 22:40:33 +05:30
Text("Disabled").tag(Optional(""))
2021-12-05 01:05:41 +05:30
ForEach(InstancesModel.all.filter { $0.app.supportsComments }) { instance in
Text(instance.description).tag(Optional(instance.id))
}
}
.labelsHidden()
#if os(iOS)
.pickerStyle(.automatic)
#elseif os(tvOS)
.pickerStyle(.inline)
#endif
}
2021-10-23 22:19:45 +05:30
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)
2021-11-08 21:59:35 +05:30
.buttonStyle(.plain)
2021-10-23 17:42:53 +05:30
#endif
}
2021-10-23 17:21:02 +05:30
}
}
struct ServicesSettings_Previews: PreviewProvider {
static var previews: some View {
2021-11-02 03:26:18 +05:30
VStack {
ServicesSettings()
}
2021-10-23 17:21:02 +05:30
}
}