Improve add account screen

This commit is contained in:
Thomas Ricouard 2023-01-21 16:54:43 +01:00
parent 691b3f2667
commit b577e41eae
1 changed files with 28 additions and 9 deletions

View File

@ -44,8 +44,10 @@ struct AddAccountView: View {
if let instanceFetchError {
Text(instanceFetchError)
}
if let instance {
if instance != nil || !instanceName.isEmpty {
signInSection
}
if let instance {
InstanceInfoSection(instance: instance)
} else {
instancesListView
@ -68,7 +70,10 @@ struct AddAccountView: View {
isInstanceURLFieldFocused = true
let client = InstanceSocialClient()
Task {
self.instances = await client.fetchInstances()
let instances = await client.fetchInstances()
withAnimation {
self.instances = instances
}
}
isSigninIn = false
}
@ -76,16 +81,22 @@ struct AddAccountView: View {
instanceNamePublisher.send(newValue)
}
.onReceive(instanceNamePublisher.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)) { newValue in
let newValue = newValue
.replacingOccurrences(of: "http://", with: "")
.replacingOccurrences(of: "https://", with: "")
let client = Client(server: newValue)
Task {
do {
self.instance = try await client.get(endpoint: Instances.instance)
self.instanceFetchError = nil
let instance: Instance = try await client.get(endpoint: Instances.instance)
withAnimation {
self.instance = instance
}
instanceFetchError = nil
} catch _ as DecodingError {
self.instance = nil
self.instanceFetchError = "account.add.error.instance-not-supported"
instance = nil
instanceFetchError = "account.add.error.instance-not-supported"
} catch {
self.instance = nil
instance = nil
}
}
}
@ -102,6 +113,11 @@ struct AddAccountView: View {
await continueSignIn(url: url)
}
})
.onChange(of: oauthURL, perform: { newValue in
if newValue == nil {
isSigninIn = false
}
})
.sheet(item: $oauthURL, content: { url in
SafariView(url: url)
})
@ -111,15 +127,18 @@ struct AddAccountView: View {
private var signInSection: some View {
Section {
Button {
isSigninIn = true
withAnimation {
isSigninIn = true
}
Task {
await signIn()
}
} label: {
HStack {
Spacer()
if isSigninIn {
if isSigninIn || !instanceName.isEmpty && instance == nil {
ProgressView()
.id(instanceName)
.tint(theme.labelColor)
} else {
Text("account.add.sign-in")