Add current user to SettingsView

This commit is contained in:
Shav Kinderlehrer 2024-01-07 16:58:38 -05:00
parent 4d40b0c083
commit b9b61fa7eb
3 changed files with 35 additions and 12 deletions

View File

@ -12,16 +12,18 @@ class AuthStateController: ObservableObject {
@Published var serverUrl: URL?
@Published var authToken: String?
@Published var userId: String?
@Published var username: String?
private let defaults = UserDefaults.standard
static let shared = AuthStateController()
init(loggedIn: Bool = false, serverUrl: URL? = nil, authToken: String? = nil, userId: String? = nil) {
init(loggedIn: Bool = false, serverUrl: URL? = nil, authToken: String? = nil, userId: String? = nil, username: String? = nil) {
self.loggedIn = loggedIn
self.serverUrl = serverUrl
self.authToken = authToken
self.userId = userId
self.username = username
}
func load() {
@ -35,6 +37,9 @@ class AuthStateController: ObservableObject {
if let oldUserId = defaults.string(forKey: "AuthState_userId") {
self.userId = oldUserId
}
if let oldUsername = defaults.string(forKey: "AuthState_username") {
self.username = oldUsername
}
}
func save() {
@ -42,5 +47,6 @@ class AuthStateController: ObservableObject {
defaults.set(self.serverUrl, forKey: "AuthState_serverUrl")
defaults.set(self.authToken, forKey: "AuthState_authToken")
defaults.set(self.userId, forKey: "AuthState_userId")
defaults.set(self.username, forKey: "AuthState_username")
}
}

View File

@ -109,6 +109,7 @@ extension JellyfinClientController {
self.authState.loggedIn = true
self.authState.authToken = res.value.accessToken
self.authState.userId = res.value.user?.id
self.authState.username = username
self.authState.save()
self.setToken(token: self.authState.authToken ?? "")

View File

@ -18,17 +18,14 @@ struct SettingsView: View {
NavigationStack {
Form {
Section() {
AppearancePicker()
} header: {
Text("Accessibility")
}
Section {
NavigationLink {
ConsoleView()
.closeButtonHidden()
} label: {
Text("Logs")
LabeledContent("Username") {
Text("\(authState.username ?? "---")")
.textSelection(.enabled)
}
LabeledContent("Auth Token") {
Text("\(authState.authToken ?? "---")")
.textSelection(.enabled)
}
Button(role: .destructive) {
@ -37,6 +34,25 @@ struct SettingsView: View {
} label: {
Text("Sign out")
}
} header: {
Text("Current User")
}
Section() {
AppearancePicker()
} header: {
Text("Accessibility")
}
Section() {
NavigationLink {
ConsoleView()
.closeButtonHidden()
} label: {
Text("Logs")
}
} header: {
Text("Debug")
}
}
.navigationTitle("Settings")