gh-XcodesOrg-XcodesApp/Xcodes/Frontend/Preferences/GeneralPreferencePane.swift
Brandon Evans 75457dd9eb
Show stored username in General preference pane
This reverts the change from 90c067997b so that the username is shown in situations where we don't have a valid session but could almost certainly get one. Instead, to achieve what that commit was trying to do, we'll instead remove the username from UserDefaults if auth fails with an invalid username or password error. I think this will more closely match what users expect in this UI.

I've added a comment in the UI explaining why it is the way it is. It might also be worth considering renaming AuthenticationState or its cases to better reflect that it's probably more about the (short-lived) session state than whether the user has signed in before and has stored their credentials.
2021-01-25 19:56:28 -07:00

48 lines
1.8 KiB
Swift

import AppleAPI
import Preferences
import SwiftUI
extension Preferences.PaneIdentifier {
static let general = Self("general")
}
struct GeneralPreferencePane: View {
@EnvironmentObject var appState: AppState
var body: some View {
Preferences.Container(contentWidth: 400.0) {
Preferences.Section(title: "Apple ID") {
VStack(alignment: .leading) {
// If we have saved a username then we will show it here,
// even if we don't have a valid session right now,
// because we should be able to get a valid session if needed with the password in the keychain
// and a 2FA code from the user.
// Note that AppState.authenticationState is not necessarily .authenticated in this case, though.
if let username = Current.defaults.string(forKey: "username") {
Text(username)
Button("Sign Out", action: appState.signOut)
} else {
Button("Sign In", action: { self.appState.presentingSignInAlert = true })
}
Spacer()
}
.frame(maxWidth: .infinity, alignment: .leading)
.sheet(isPresented: $appState.presentingSignInAlert) {
SignInCredentialsView(isPresented: $appState.presentingSignInAlert)
.environmentObject(appState)
}
}
}
.padding(.trailing)
}
}
struct GeneralPreferencePane_Previews: PreviewProvider {
static var previews: some View {
Group {
GeneralPreferencePane()
.environmentObject(AppState())
}
}
}