2023-01-17 11:36:01 +01:00
|
|
|
import AppAccount
|
|
|
|
import Combine
|
2022-12-29 14:07:58 +01:00
|
|
|
import DesignSystem
|
2023-01-17 11:36:01 +01:00
|
|
|
import Env
|
|
|
|
import Models
|
|
|
|
import Network
|
2022-12-29 14:07:58 +01:00
|
|
|
import NukeUI
|
|
|
|
import Shimmer
|
2023-01-17 11:36:01 +01:00
|
|
|
import SwiftUI
|
2022-12-29 14:07:58 +01:00
|
|
|
|
|
|
|
struct AddAccountView: View {
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-01-10 12:57:45 +01:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
@EnvironmentObject private var appAccountsManager: AppAccountsManager
|
|
|
|
@EnvironmentObject private var currentAccount: CurrentAccount
|
|
|
|
@EnvironmentObject private var currentInstance: CurrentInstance
|
2023-01-08 14:16:43 +01:00
|
|
|
@EnvironmentObject private var pushNotifications: PushNotificationsService
|
2022-12-29 14:07:58 +01:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
@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?
|
2023-01-16 06:43:53 +01:00
|
|
|
|
|
|
|
private let instanceNamePublisher = PassthroughSubject<String, Never>()
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-01 09:19:00 +01:00
|
|
|
@FocusState private var isInstanceURLFieldFocused: Bool
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
var body: some View {
|
|
|
|
NavigationStack {
|
|
|
|
Form {
|
2022-12-30 21:13:31 +01:00
|
|
|
TextField("Instance URL", text: $instanceName)
|
2022-12-29 14:07:58 +01:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2022-12-30 21:13:31 +01:00
|
|
|
.keyboardType(.URL)
|
|
|
|
.textContentType(.URL)
|
|
|
|
.textInputAutocapitalization(.never)
|
2023-01-06 12:14:05 +01:00
|
|
|
.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 {
|
2023-01-10 12:57:45 +01:00
|
|
|
signInSection
|
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 {
|
2023-01-12 06:30:43 +01:00
|
|
|
if !appAccountsManager.availableAccounts.isEmpty {
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
|
|
Button("Cancel", action: { dismiss() })
|
|
|
|
}
|
2022-12-29 14:07:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.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()
|
|
|
|
}
|
2023-01-10 12:57:45 +01:00
|
|
|
isSigninIn = false
|
2022-12-29 14:07:58 +01:00
|
|
|
}
|
|
|
|
.onChange(of: instanceName) { newValue in
|
2023-01-16 06:43:53 +01:00
|
|
|
instanceNamePublisher.send(newValue)
|
|
|
|
}
|
|
|
|
.onReceive(instanceNamePublisher.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)) { newValue in
|
2022-12-29 14:07:58 +01:00
|
|
|
let client = Client(server: newValue)
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
self.instance = try await client.get(endpoint: Instances.instance)
|
2023-01-10 12:57:45 +01:00
|
|
|
self.instanceFetchError = nil
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 12:57:45 +01:00
|
|
|
.onChange(of: scenePhase, perform: { scenePhase in
|
|
|
|
switch scenePhase {
|
|
|
|
case .active:
|
|
|
|
isSigninIn = false
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2022-12-29 14:07:58 +01:00
|
|
|
.onOpenURL(perform: { url in
|
|
|
|
Task {
|
|
|
|
await continueSignIn(url: url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-10 12:57:45 +01:00
|
|
|
private var signInSection: some View {
|
|
|
|
Section {
|
|
|
|
Button {
|
|
|
|
isSigninIn = true
|
|
|
|
Task {
|
|
|
|
await signIn()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
if isSigninIn {
|
|
|
|
ProgressView()
|
|
|
|
.tint(theme.labelColor)
|
|
|
|
} else {
|
|
|
|
Text("Sign in")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledHeadline)
|
2023-01-10 12:57:45 +01:00
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.tintColor)
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
private var instancesListView: some View {
|
|
|
|
Section("Suggestions") {
|
|
|
|
if instances.isEmpty {
|
2023-01-10 12:57:45 +01:00
|
|
|
placeholderRow
|
2022-12-29 14:07:58 +01:00
|
|
|
} else {
|
2023-01-17 11:36:01 +01:00
|
|
|
ForEach(instanceName.isEmpty ? instances : instances.filter { $0.name.contains(instanceName.lowercased()) }) { instance in
|
2022-12-31 05:36:21 +01:00
|
|
|
Button {
|
2022-12-29 14:07:58 +01:00
|
|
|
self.instanceName = instance.name
|
2022-12-31 05:36:21 +01:00
|
|
|
} label: {
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
Text(instance.name)
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledHeadline)
|
2022-12-31 05:36:21 +01:00
|
|
|
.foregroundColor(.primary)
|
|
|
|
Text(instance.info?.shortDescription ?? "")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledBody)
|
2022-12-31 05:36:21 +01:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
Text("\(instance.users) users ⸱ \(instance.statuses) posts")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledFootnote)
|
2022-12-31 05:36:21 +01:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
}
|
2022-12-29 14:07:58 +01:00
|
|
|
}
|
2022-12-31 05:36:21 +01:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2022-12-29 14:07:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-10 12:57:45 +01:00
|
|
|
private var placeholderRow: some View {
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
Text("Loading...")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledHeadline)
|
2023-01-10 12:57:45 +01:00
|
|
|
.foregroundColor(.primary)
|
|
|
|
Text("Loading, loading, loading ....")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledBody)
|
2023-01-10 12:57:45 +01:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
Text("Loading ...")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledFootnote)
|
2023-01-10 12:57:45 +01:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
}
|
|
|
|
.redacted(reason: .placeholder)
|
|
|
|
.shimmering()
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
private func signIn() async {
|
|
|
|
do {
|
|
|
|
signInClient = .init(server: instanceName)
|
|
|
|
if let oauthURL = try await signInClient?.oauthURL() {
|
2023-01-08 19:56:16 +01:00
|
|
|
await UIApplication.shared.open(oauthURL)
|
2022-12-29 14:07:58 +01:00
|
|
|
} else {
|
|
|
|
isSigninIn = false
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
isSigninIn = false
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2022-12-29 14:07:58 +01:00
|
|
|
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))
|
2023-01-08 10:22:52 +01:00
|
|
|
Task {
|
|
|
|
await pushNotifications.updateSubscriptions(accounts: appAccountsManager.pushAccounts)
|
|
|
|
}
|
2022-12-29 14:07:58 +01:00
|
|
|
isSigninIn = false
|
|
|
|
dismiss()
|
|
|
|
} catch {
|
|
|
|
isSigninIn = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|