geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
import HydraulophoneCore
import SwiftUI
struct SettingsView: View {
@EnvironmentObject private var model: AppModel
var body: some View {
ObservedSettingsView(settings: model.settings)
}
}
private struct ObservedSettingsView: View {
@EnvironmentObject private var model: AppModel
@ObservedObject private var settings: AppSettingsStore
@State private var isClearingCatalogCache = false
@State private var catalogCacheMessage: String?
@State private var isClearingTrackCache = false
@State private var confirmingSignOut = false
init(settings: AppSettingsStore) {
_settings = ObservedObject(wrappedValue: settings)
}
var body: some View {
TrackNavigationStack {
Form {
Section {
Toggle(
"Offline Only",
isOn: Binding(
get: { model.isOfflineOnly },
set: { model.setOfflineOnly($0) }))
NavigationLink {
DownloadsView(downloads: model.downloads)
} label: {
Label("Downloads", systemImage: "arrow.down.circle")
}
} footer: {
Text(
"Offline Only shows saved music and pauses network activity. Downloads stay on this device until you remove them or sign out."
)
}
Section {
Toggle("Normalize Volume", isOn: $settings.normalizeVolume)
.accessibilityHint("Reduces volume differences between songs.")
} footer: {
Text("Balances song volume using available loudness information.")
}
audioQualitySection
networkSection
trackCacheSection
if model.isLastFMConfigured {
lastFMSection
}
catalogSection
accountSection
}
.navigationTitle("Settings")
.navigationBarTitleDisplayMode(.inline)
}
.tint(HydraulophoneTheme.accent)
.task { await model.refreshTrackCacheStatistics() }
.confirmationDialog("Sign out of Hydraulophone?", isPresented: $confirmingSignOut) {
Button("Sign Out", role: .destructive) { _ = model.signOut() }
} message: {
Text("This removes downloaded music from this device. Your online library is kept.")
}
}
private var trackCacheSection: some View {
Section {
Picker(
"Size limit",
selection: Binding(
get: { settings.trackCacheLimit }, set: { model.setTrackCacheLimit($0) }
)
) {
ForEach(AppSettingsStore.trackCacheLimits, id: \.self) { bytes in
Text(
bytes == 0 ? "Off" : ByteCountFormatter.string(fromByteCount: bytes, countStyle: .file)
)
.tag(bytes)
}
}
LabeledContent(
"Used",
value: ByteCountFormatter.string(fromByteCount: model.trackCacheBytes, countStyle: .file))
LabeledContent("Complete tracks", value: "\(model.cachedTrackCount)")
Button("Clear track cache", role: .destructive) {
isClearingTrackCache = true
Task {
await model.clearTrackCache()
isClearingTrackCache = false
}
}
.disabled(isClearingTrackCache)
.frame(minHeight: 44)
if isClearingTrackCache { ProgressView("Clearing track cache…") }
} header: {
Text("Track cache")
} footer: {
Text(
"Tracks are saved as you listen, and the next song is cached ahead on Wi-Fi. Older tracks are removed when the cache fills. High replaces a cached Standard copy."
)
}
}
private var audioQualitySection: some View {
Section {
qualityChoice(
.standard,
title: "Standard",
details: "AAC · about 128 kb/s",
isDisabled: false
)
qualityChoice(
.high,
title: "High",
details: "Opus · about 200 kb/s on average",
isDisabled: !model.canSelectHighQuality
)
} header: {
Text("Audio quality")
}
}
private var networkSection: some View {
Section {
Toggle(
"Use High on Cellular or Hotspots",
isOn: Binding(
get: { settings.allowHighOnExpensiveNetworks },
set: { model.setAllowHighOnExpensiveNetworks($0) }
)
)
.accessibilityHint(
"Allows High streaming on cellular networks and personal hotspots."
)
} header: {
Text("Network")
} footer: {
Text(
"Uses Standard on cellular or hotspot connections unless enabled. Low Data Mode always uses Standard."
)
}
}
private var catalogSection: some View {
Section {
Button {
startCatalogCacheClear()
} label: {
HStack {
if isClearingCatalogCache {
ProgressView()
.controlSize(.small)
Text("Clearing catalog cache…")
} else {
Label("Clear catalog cache", systemImage: "trash")
}
}
.frame(minHeight: 44, alignment: .leading)
}
.disabled(isClearingCatalogCache)
.accessibilityLabel(
isClearingCatalogCache
? "Clearing catalog cache"
: "Clear catalog cache"
)
if let catalogCacheMessage {
Label(catalogCacheMessage, systemImage: "checkmark.circle")
.font(.footnote)
.foregroundStyle(.secondary)
}
} header: {
Text("Catalog")
} footer: {
Text(
"Catalog pages are cached only in memory, not saved for offline listening. "
+ "Clearing them does not remove your account or stop playback."
)
}
}
private var lastFMSection: some View {
Section("Last.fm") {
switch model.lastFMAuthorizationState {
case .notConfigured:
EmptyView()
case .signedOut:
if model.lastFMNeedsFinishLogin {
Button("Finish Last.fm login") {
model.finishLastFMLogin()
}
.frame(minHeight: 44, alignment: .leading)
.disabled(model.isLastFMOperationInProgress || model.isOfflineOnly)
} else {
Button("Log in to Last.fm") {
model.beginLastFMLogin()
}
.frame(minHeight: 44, alignment: .leading)
.disabled(model.isLastFMOperationInProgress || model.isOfflineOnly)
}
case .connected(let username):
Label("Connected as \(username)", systemImage: "checkmark.circle")
Button("Log out of Last.fm", role: .destructive) {
model.signOutLastFM()
}
.frame(minHeight: 44, alignment: .leading)
.disabled(model.isLastFMOperationInProgress)
}
if model.isLastFMOperationInProgress {
ProgressView("Updating Last.fm account…")
}
if let message = model.lastFMErrorMessage {
Label(message, systemImage: "exclamationmark.triangle")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
}
private var accountSection: some View {
Section("Account") {
if model.isRestoringSession || model.isSessionTransitioning {
ProgressView("Updating account…")
} else if model.session == nil {
Text("Sign in is required to browse and play music.")
.foregroundStyle(.secondary)
Button("Sign in") {
model.showingLogin = true
}
.frame(minHeight: 44, alignment: .leading)
} else {
Label("Signed in", systemImage: "checkmark.circle")
Button("Sign out", role: .destructive) {
confirmingSignOut = true
}
.frame(minHeight: 44, alignment: .leading)
}
if let message = model.authErrorMessage {
Label(message, systemImage: "exclamationmark.triangle")
.foregroundStyle(.secondary)
if model.session == nil {
Button("Retry secure sign-out") {
_ = model.signOut()
}
.disabled(model.isSessionTransitioning)
}
}
}
}
private func qualityChoice(
_ preference: AudioQualityPreference,
title: String,
details: String,
isDisabled: Bool
) -> some View {
let isSelected = settings.preferredQuality.rawValue == preference.rawValue
return Button {
model.setPreferredQuality(preference)
} label: {
HStack(alignment: .top, spacing: 12) {
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.body.weight(.medium))
Text(details)
.font(.footnote)
.foregroundStyle(.secondary)
}
Spacer(minLength: 12)
Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")
.foregroundStyle(isSelected ? HydraulophoneTheme.accent : .secondary)
.accessibilityHidden(true)
}
.frame(minHeight: 44, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.disabled(isDisabled)
.accessibilityLabel("\(title), \(details)")
.accessibilityValue(isSelected ? "Selected" : "Not selected")
.accessibilityHint(isDisabled ? "High quality is currently unavailable." : "")
}
private func startCatalogCacheClear() {
guard !isClearingCatalogCache else { return }
isClearingCatalogCache = true
catalogCacheMessage = nil
Task { @MainActor in
await model.clearCatalogCache()
guard !Task.isCancelled else { return }
isClearingCatalogCache = false
catalogCacheMessage = "Catalog cache cleared."
}
}
}
#Preview("Settings — offline") {
SettingsView()
.environmentObject(
AppModel(service: OfflineFixtureMusicService())
)
}