IceCubes/IceCubesApp/App/Tabs/Settings/AddAccountsView.swift

154 lines
4.4 KiB
Swift
Raw Normal View History

2022-12-29 14:07:58 +01:00
import SwiftUI
import Network
import Models
import Env
import DesignSystem
import NukeUI
import Shimmer
struct AddAccountView: View {
@Environment(\.openURL) private var openURL
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var appAccountsManager: AppAccountsManager
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var currentInstance: CurrentInstance
@EnvironmentObject private var theme: Theme
@State private var instanceName: String = ""
@State private var instance: Instance?
@State private var isSigninIn = false
@State private var signInClient: Client?
@State private var instances: [InstanceSocial] = []
2023-01-01 09:19:00 +01:00
@State private var instanceFetchError: String?
@FocusState private var isInstanceURLFieldFocused: Bool
2022-12-29 14:07:58 +01:00
var body: some View {
NavigationStack {
Form {
TextField("Instance URL", text: $instanceName)
2022-12-29 14:07:58 +01:00
.listRowBackground(theme.primaryBackgroundColor)
.keyboardType(.URL)
.textContentType(.URL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
2023-01-01 09:19:00 +01:00
.focused($isInstanceURLFieldFocused)
if let instanceFetchError {
Text(instanceFetchError)
}
2022-12-29 14:07:58 +01:00
if let instance {
Button {
isSigninIn = true
Task {
await signIn()
}
} label: {
if isSigninIn {
ProgressView()
} else {
Text("Sign in")
}
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-07 18:12:56 +01:00
InstanceInfoSection(instance: instance)
2022-12-29 14:07:58 +01:00
} else {
instancesListView
}
}
.formStyle(.grouped)
.navigationTitle("Add account")
.navigationBarTitleDisplayMode(.inline)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2023-01-01 09:19:00 +01:00
.scrollDismissesKeyboard(.immediately)
2022-12-29 14:07:58 +01:00
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel", action: { dismiss() })
}
}
.onAppear {
2023-01-01 09:19:00 +01:00
isInstanceURLFieldFocused = true
2022-12-29 14:07:58 +01:00
let client = InstanceSocialClient()
Task {
self.instances = await client.fetchInstances()
}
}
.onChange(of: instanceName) { newValue in
let client = Client(server: newValue)
Task {
do {
self.instance = try await client.get(endpoint: Instances.instance)
2023-01-01 09:19:00 +01:00
} catch _ as DecodingError {
self.instance = nil
self.instanceFetchError = "This instance is not currently supported."
2022-12-29 14:07:58 +01:00
} catch {
self.instance = nil
}
}
}
.onOpenURL(perform: { url in
Task {
await continueSignIn(url: url)
}
})
}
}
private var instancesListView: some View {
Section("Suggestions") {
if instances.isEmpty {
ProgressView()
.listRowBackground(theme.primaryBackgroundColor)
} else {
ForEach(instanceName.isEmpty ? instances : instances.filter{ $0.name.contains(instanceName.lowercased()) }) { instance in
Button {
2022-12-29 14:07:58 +01:00
self.instanceName = instance.name
} label: {
VStack(alignment: .leading, spacing: 4) {
Text(instance.name)
.font(.headline)
.foregroundColor(.primary)
Text(instance.info?.shortDescription ?? "")
.font(.body)
.foregroundColor(.gray)
Text("\(instance.users) users ⸱ \(instance.statuses) posts")
.font(.footnote)
.foregroundColor(.gray)
}
2022-12-29 14:07:58 +01:00
}
.listRowBackground(theme.primaryBackgroundColor)
2022-12-29 14:07:58 +01:00
}
}
}
}
private func signIn() async {
do {
signInClient = .init(server: instanceName)
if let oauthURL = try await signInClient?.oauthURL() {
openURL(oauthURL)
} else {
isSigninIn = false
}
} catch {
isSigninIn = false
}
}
private func continueSignIn(url: URL) async {
guard let client = signInClient else {
isSigninIn = false
return
}
do {
let oauthToken = try await client.continueOauthFlow(url: url)
appAccountsManager.add(account: AppAccount(server: client.server, oauthToken: oauthToken))
isSigninIn = false
dismiss()
} catch {
isSigninIn = false
}
}
}