Files
yattee/Yattee/Views/Settings/DeveloperSettingsView.swift
Arkadiusz Fal 9fd0a71035 Add identity-scoped, account-keyed credential storage
Yattee 2 keys credentials by instance UUID with no notion of which Apple
account they belong to, and reads them back with SynchronizableAny. Apple
documents that Keychain data may stay on device when a user signs out and
chooses to keep it, so whoever signs in next would be served the previous
user's credential - guaranteed for migrated accounts, since local instances
survive the switch and the migrated account UUID is deterministically the
instance UUID.

Credentials are now keyed "<identityScope>|<accountID>", where the scope is
the CloudKit user record (identical across a user's devices, so iCloud
Keychain sync still works). Usernames get the same scope and live beside the
credential, keeping login identifiers out of synced metadata.

The legacy unscoped item is never adopted automatically. A strict
kSecAttrSynchronizable == true query was the candidate discriminator for
"this secret belongs to the current Apple account", but measured on iPadOS
27.0 a retained item keeps the attribute and still matches that query with
no Apple account signed in at all - the flag records what was once requested,
not live iCloud Keychain membership. Reads therefore never fall back to the
legacy key; adoption is an explicit user action that copies the item,
verifies the scoped read-back, then records a marker bound to the identity
and a salted fingerprint of the secret. The legacy item is retained, because
deleting a synchronizable item propagates and would log out devices still on
the previous release mid-rollout. Deleting it later needs all three of: a
marker on this device, matching identity, and the item still holding the
adopted secret.

The instance-keyed path is untouched, so no shipping call site changes
behavior. BasicAuthCredentialsManager stays instance-keyed - proxy auth
belongs to the instance, not to a user account.

While the scope is unresolved (first launch under a new Apple account,
CloudKit unreachable) reads report "can't tell yet" via isScopeResolved
rather than "signed out", and writes are refused rather than guessing a
scope. Login state is cached per scope so one identity's cache can never
answer for another.

Also adds the debug-only harness the measurement was taken with, under
Advanced > Developer > Experiments.
2026-08-23 21:20:30 +02:00

299 lines
10 KiB
Swift

//
// DeveloperSettingsView.swift
// Yattee
//
// Developer-focused settings for logging, debugging, and testing.
//
import SwiftUI
struct DeveloperSettingsView: View {
@Environment(\.appEnvironment) private var appEnvironment
@Bindable private var loggingService = LoggingService.shared
@State private var loggingEnabled = LoggingService.shared.isEnabled
// Data management state
@State private var showingDeduplicateConfirmation = false
@State private var deduplicationResult: DataManager.DeduplicationResult?
@State private var showingDeduplicationResult = false
@State private var showingResetiCloudConfirmation = false
@State private var showingResetiCloudComplete = false
var body: some View {
SettingsFormContainer {
loggingSection
if loggingEnabled {
verboseLoggingSection
}
debugSection
testingSection
dataSection
#if DEBUG
experimentsSection
#endif
}
.navigationTitle(String(localized: "settings.developer.title"))
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.confirmationDialog(
String(localized: "settings.advanced.data.removeDuplicates.title"),
isPresented: $showingDeduplicateConfirmation,
titleVisibility: .visible
) {
Button(String(localized: "settings.advanced.data.removeDuplicates.action"), role: .destructive) {
deduplicateData()
}
Button(String(localized: "common.cancel"), role: .cancel) {}
} message: {
Text(String(localized: "settings.advanced.data.removeDuplicates.message"))
}
.presentationCompactAdaptation(.sheet)
.alert(
String(localized: "settings.advanced.data.deduplicationComplete"),
isPresented: $showingDeduplicationResult
) {
Button(String(localized: "common.ok"), role: .cancel) {}
} message: {
if let result = deduplicationResult {
if result.totalRemoved > 0 {
Text(result.summary)
} else {
Text(String(localized: "settings.advanced.data.noDuplicates"))
}
}
}
.confirmationDialog(
String(localized: "settings.advanced.data.resetICloud.title"),
isPresented: $showingResetiCloudConfirmation,
titleVisibility: .visible
) {
Button(String(localized: "settings.advanced.data.resetICloud.action"), role: .destructive) {
resetiCloudData()
}
Button(String(localized: "common.cancel"), role: .cancel) {}
} message: {
Text(String(localized: "settings.advanced.data.resetICloud.message"))
}
.presentationCompactAdaptation(.sheet)
.alert(
String(localized: "settings.advanced.data.iCloudReset"),
isPresented: $showingResetiCloudComplete
) {
Button(String(localized: "common.ok"), role: .cancel) {}
} message: {
Text(String(localized: "settings.advanced.data.iCloudResetMessage"))
}
}
// MARK: - Sections
@ViewBuilder
private var loggingSection: some View {
SettingsFormSection("settings.advanced.logging.sectionTitle", footer: "settings.advanced.logging.footer") {
Toggle(isOn: $loggingEnabled) {
Label(String(localized: "settings.advanced.logging.enable"), systemImage: "doc.text")
}
.onChange(of: loggingEnabled) { _, newValue in
loggingService.isEnabled = newValue
}
if loggingEnabled {
#if os(tvOS)
NavigationLink {
LogViewerView()
} label: {
HStack {
Label(String(localized: "settings.advanced.logging.viewLogs"), systemImage: "list.bullet.rectangle")
Spacer()
Text("\(loggingService.entries.count)")
.foregroundStyle(.secondary)
}
}
#else
SettingsNavigationRow(
"settings.advanced.logging.viewLogs",
systemImage: "list.bullet.rectangle",
trailing: { Text("\(loggingService.entries.count)") }
) {
LogViewerView()
}
#endif
}
}
}
@ViewBuilder
private var verboseLoggingSection: some View {
if let settingsManager = appEnvironment?.settingsManager {
SettingsFormSection("settings.advanced.verboseLogging.sectionTitle") {
Toggle(isOn: Binding(
get: { settingsManager.verboseMPVLogging },
set: { settingsManager.verboseMPVLogging = $0 }
)) {
Label(String(localized: "settings.advanced.debug.verboseMPV"), systemImage: "doc.text.magnifyingglass")
}
Toggle(isOn: Binding(
get: { settingsManager.verboseRemoteControlLogging },
set: { settingsManager.verboseRemoteControlLogging = $0 }
)) {
Label(String(localized: "settings.advanced.debug.verboseRemote"), systemImage: "antenna.radiowaves.left.and.right")
}
}
}
}
@ViewBuilder
private var debugSection: some View {
if let settingsManager = appEnvironment?.settingsManager {
SettingsFormSection("settings.advanced.debug.sectionTitle") {
Toggle(isOn: Binding(
get: { settingsManager.showPlayerAreaDebug },
set: { settingsManager.showPlayerAreaDebug = $0 }
)) {
Label(String(localized: "settings.advanced.debug.playerAreaDebug"), systemImage: "rectangle.dashed")
}
#if os(iOS)
Toggle(isOn: Binding(
get: { settingsManager.zoomTransitionsEnabled },
set: { settingsManager.zoomTransitionsEnabled = $0 }
)) {
Label(String(localized: "settings.advanced.debug.zoomTransitions"), systemImage: "arrow.up.left.and.arrow.down.right")
}
#endif
#if os(tvOS)
Toggle(isOn: Binding(
get: { settingsManager.showTVDebugButton },
set: { settingsManager.showTVDebugButton = $0 }
)) {
Label(String(localized: "settings.advanced.debug.showTVDebugButton"), systemImage: "ant.circle")
}
#endif
}
}
}
@ViewBuilder
private var testingSection: some View {
#if !os(tvOS)
notificationTestingSection
#endif
}
@ViewBuilder
private var dataSection: some View {
SettingsFormSection("settings.advanced.data.sectionTitle", footer: "settings.advanced.data.footer") {
HStack {
Button {
showingDeduplicateConfirmation = true
} label: {
Label(String(localized: "settings.advanced.data.removeDuplicates"), systemImage: "doc.on.doc")
}
Spacer()
}
HStack {
Button(role: .destructive) {
showingResetiCloudConfirmation = true
} label: {
Label(String(localized: "settings.advanced.data.resetICloud"), systemImage: "icloud.slash")
}
Spacer()
}
}
}
#if DEBUG
@ViewBuilder
private var experimentsSection: some View {
SettingsFormSection("Experiments") {
#if os(tvOS)
NavigationLink {
KeychainProvenanceExperimentView()
} label: {
Label {
Text(verbatim: "Keychain Provenance")
} icon: {
Image(systemName: "key.viewfinder")
}
}
#else
SettingsNavigationRow("Keychain Provenance", systemImage: "key.viewfinder") {
KeychainProvenanceExperimentView()
}
#endif
}
}
#endif
// MARK: - Data Actions
private func deduplicateData() {
guard let dataManager = appEnvironment?.dataManager else { return }
deduplicationResult = dataManager.deduplicateAllData()
showingDeduplicationResult = true
}
private func resetiCloudData() {
guard let cloudKitSync = appEnvironment?.cloudKitSync else { return }
Task {
do {
try await cloudKitSync.resetSync()
showingResetiCloudComplete = true
} catch {
LoggingService.shared.logCloudKitError("Failed to reset iCloud data", error: error)
}
}
}
#if !os(tvOS)
@ViewBuilder
private var notificationTestingSection: some View {
SettingsFormSection("settings.advanced.testing.notifications.sectionTitle", footer: "settings.advanced.testing.notifications.footer") {
HStack {
Button {
sendTestNotification()
} label: {
Label(String(localized: "settings.advanced.testing.sendTestNotification"), systemImage: "bell.badge")
}
Spacer()
}
HStack {
Button {
triggerBackgroundRefresh()
} label: {
Label(String(localized: "settings.advanced.testing.triggerBackgroundRefresh"), systemImage: "arrow.clockwise")
}
Spacer()
}
}
}
private func sendTestNotification() {
Task {
await appEnvironment?.notificationManager.sendTestNotification()
}
}
private func triggerBackgroundRefresh() {
guard let appEnvironment else { return }
Task {
await appEnvironment.notificationManager.triggerBackgroundRefresh(using: appEnvironment)
}
}
#endif
}
// MARK: - Preview
#Preview {
NavigationStack {
DeveloperSettingsView()
}
}