yattee/Shared/Settings/BrowsingSettings.swift

141 lines
4.5 KiB
Swift
Raw Normal View History

2021-11-04 23:25:51 +00:00
import Defaults
import SwiftUI
struct BrowsingSettings: View {
#if !os(tvOS)
@Default(.accountPickerDisplaysUsername) private var accountPickerDisplaysUsername
@Default(.roundedThumbnails) private var roundedThumbnails
#endif
@Default(.accountPickerDisplaysAnonymousAccounts) private var accountPickerDisplaysAnonymousAccounts
#if os(iOS)
@Default(.lockPortraitWhenBrowsing) private var lockPortraitWhenBrowsing
#endif
2022-06-26 12:25:54 +00:00
@Default(.thumbnailsQuality) private var thumbnailsQuality
2021-11-04 23:25:51 +00:00
@Default(.channelOnThumbnail) private var channelOnThumbnail
@Default(.timeOnThumbnail) private var timeOnThumbnail
@Default(.visibleSections) private var visibleSections
2021-11-04 23:25:51 +00:00
var body: some View {
Group {
#if os(macOS)
sections
#else
List {
sections
}
#if os(iOS)
.listStyle(.insetGrouped)
#endif
#endif
}
#if os(tvOS)
.frame(maxWidth: 1000)
#else
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
#endif
.navigationTitle("Browsing")
}
private var sections: some View {
Group {
interfaceSettings
thumbnailsSettings
visibleSectionsSettings
}
}
2021-11-07 20:51:22 +00:00
private var interfaceSettings: some View {
2022-09-04 15:28:30 +00:00
Section(header: SettingsHeader(text: "Interface".localized())) {
#if os(iOS)
Toggle("Lock portrait mode", isOn: $lockPortraitWhenBrowsing)
.onChange(of: lockPortraitWhenBrowsing) { lock in
if lock {
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
} else {
Orientation.lockOrientation(.allButUpsideDown)
}
}
#endif
2021-12-02 19:22:55 +00:00
#if !os(tvOS)
Toggle("Show account username", isOn: $accountPickerDisplaysUsername)
#endif
Toggle("Show anonymous accounts", isOn: $accountPickerDisplaysAnonymousAccounts)
}
}
private var thumbnailsSettings: some View {
2022-09-04 15:28:30 +00:00
Section(header: SettingsHeader(text: "Thumbnails".localized())) {
2022-06-26 12:25:54 +00:00
thumbnailsQualityPicker
#if !os(tvOS)
Toggle("Round corners", isOn: $roundedThumbnails)
#endif
Toggle("Show channel name", isOn: $channelOnThumbnail)
Toggle("Show video length", isOn: $timeOnThumbnail)
}
}
2022-06-26 12:25:54 +00:00
private var thumbnailsQualityPicker: some View {
Picker("Quality", selection: $thumbnailsQuality) {
ForEach(ThumbnailsQuality.allCases, id: \.self) { quality in
2022-09-04 15:28:30 +00:00
Text(quality.description)
2022-06-26 12:25:54 +00:00
}
}
2022-08-06 14:28:05 +00:00
.modifier(SettingsPickerModifier())
2022-06-26 12:25:54 +00:00
}
private var visibleSectionsSettings: some View {
2022-09-04 15:28:30 +00:00
Section(header: SettingsHeader(text: "Sections".localized())) {
#if os(macOS)
let list = ForEach(VisibleSection.allCases, id: \.self) { section in
2022-11-11 18:53:52 +00:00
MultiselectRow(
title: section.title,
selected: visibleSections.contains(section)
) { value in
toggleSection(section, value: value)
}
}
Group {
if #available(macOS 12.0, *) {
list
.listStyle(.inset(alternatesRowBackgrounds: true))
} else {
list
.listStyle(.inset)
}
Spacer()
}
#else
ForEach(VisibleSection.allCases, id: \.self) { section in
2022-11-11 18:53:52 +00:00
MultiselectRow(
title: section.title,
selected: visibleSections.contains(section)
) { value in
toggleSection(section, value: value)
}
}
#endif
2021-11-04 23:25:51 +00:00
}
}
2021-11-07 20:51:22 +00:00
private func toggleSection(_ section: VisibleSection, value: Bool) {
if value {
visibleSections.insert(section)
} else {
visibleSections.remove(section)
}
2021-11-07 20:51:22 +00:00
}
2021-11-04 23:25:51 +00:00
}
struct BrowsingSettings_Previews: PreviewProvider {
static var previews: some View {
2022-11-11 18:53:52 +00:00
VStack {
BrowsingSettings()
}
.injectFixtureEnvironmentObjects()
2021-11-04 23:25:51 +00:00
}
}