2023-01-17 11:36:01 +01:00
|
|
|
import Combine
|
2023-01-06 12:14:05 +01:00
|
|
|
import DesignSystem
|
2023-01-17 11:36:01 +01:00
|
|
|
import Env
|
|
|
|
import Models
|
|
|
|
import Network
|
2023-01-06 12:14:05 +01:00
|
|
|
import NukeUI
|
|
|
|
import Shimmer
|
2023-01-17 11:36:01 +01:00
|
|
|
import SwiftUI
|
2023-01-06 12:14:05 +01:00
|
|
|
|
2023-01-06 17:14:34 +01:00
|
|
|
struct AddRemoteTimelineView: View {
|
2023-01-06 12:14:05 +01:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 17:14:34 +01:00
|
|
|
@EnvironmentObject private var preferences: UserPreferences
|
2023-01-06 12:14:05 +01:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
@State private var instanceName: String = ""
|
|
|
|
@State private var instance: Instance?
|
|
|
|
@State private var instances: [InstanceSocial] = []
|
2023-01-16 06:43:53 +01:00
|
|
|
|
|
|
|
private let instanceNamePublisher = PassthroughSubject<String, Never>()
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
@FocusState private var isInstanceURLFieldFocused: Bool
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
var body: some View {
|
|
|
|
NavigationStack {
|
|
|
|
Form {
|
2023-01-19 18:14:08 +01:00
|
|
|
TextField("timeline.add.url", text: $instanceName)
|
2023-01-06 12:14:05 +01:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
.keyboardType(.URL)
|
|
|
|
.textContentType(.URL)
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.autocorrectionDisabled()
|
|
|
|
.focused($isInstanceURLFieldFocused)
|
|
|
|
if let instance {
|
2023-01-19 18:14:08 +01:00
|
|
|
Label("timeline.\(instance.title)-is-valid", systemImage: "checkmark.seal.fill")
|
2023-01-06 12:14:05 +01:00
|
|
|
.foregroundColor(.green)
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
guard instance != nil else { return }
|
2023-01-06 17:14:34 +01:00
|
|
|
preferences.remoteLocalTimelines.append(instanceName)
|
2023-01-06 12:14:05 +01:00
|
|
|
dismiss()
|
|
|
|
} label: {
|
2023-01-19 18:14:08 +01:00
|
|
|
Text("timeline.add.action.add")
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
instancesListView
|
|
|
|
}
|
|
|
|
.formStyle(.grouped)
|
2023-01-19 18:14:08 +01:00
|
|
|
.navigationTitle("timeline.add-remote.title")
|
2023-01-06 12:14:05 +01:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
.background(theme.secondaryBackgroundColor)
|
|
|
|
.scrollDismissesKeyboard(.immediately)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
2023-01-19 18:14:08 +01:00
|
|
|
Button("action.cancel", action: { dismiss() })
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 06:43:53 +01:00
|
|
|
.onChange(of: instanceName) { newValue in
|
|
|
|
instanceNamePublisher.send(newValue)
|
|
|
|
}
|
|
|
|
.onReceive(instanceNamePublisher.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)) { newValue in
|
2023-01-06 12:14:05 +01:00
|
|
|
Task {
|
|
|
|
let client = Client(server: newValue)
|
|
|
|
instance = try? await client.get(endpoint: Instances.instance)
|
|
|
|
}
|
2023-01-16 06:43:53 +01:00
|
|
|
}
|
2023-01-06 12:14:05 +01:00
|
|
|
.onAppear {
|
|
|
|
isInstanceURLFieldFocused = true
|
|
|
|
let client = InstanceSocialClient()
|
|
|
|
Task {
|
|
|
|
self.instances = await client.fetchInstances()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-06 12:14:05 +01:00
|
|
|
private var instancesListView: some View {
|
2023-01-19 18:14:08 +01:00
|
|
|
Section("instance.suggestions") {
|
2023-01-06 12:14:05 +01:00
|
|
|
if instances.isEmpty {
|
|
|
|
ProgressView()
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
} else {
|
2023-01-17 11:36:01 +01:00
|
|
|
ForEach(instanceName.isEmpty ? instances : instances.filter { $0.name.contains(instanceName.lowercased()) }) { instance in
|
2023-01-06 12:14:05 +01:00
|
|
|
Button {
|
|
|
|
self.instanceName = instance.name
|
|
|
|
} label: {
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
Text(instance.name)
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledHeadline)
|
2023-01-06 12:14:05 +01:00
|
|
|
.foregroundColor(.primary)
|
|
|
|
Text(instance.info?.shortDescription ?? "")
|
2023-01-17 19:41:46 +01:00
|
|
|
.font(.scaledBody)
|
2023-01-06 12:14:05 +01:00
|
|
|
.foregroundColor(.gray)
|
2023-01-19 18:14:08 +01:00
|
|
|
|
|
|
|
(Text("instance.list.users-\(instance.users)")
|
|
|
|
+ Text(" ⸱ ")
|
|
|
|
+ Text("instance.list.posts-\(instance.statuses)"))
|
|
|
|
.font(.scaledFootnote)
|
|
|
|
.foregroundColor(.gray)
|
2023-01-06 12:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|