2023-04-25 15:47:21 +02:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the Apache License 2.0.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import PixelfedKit
|
|
|
|
import ClientKit
|
|
|
|
import Foundation
|
|
|
|
import ServicesKit
|
|
|
|
import EnvironmentKit
|
|
|
|
import WidgetsKit
|
|
|
|
|
2023-10-19 13:24:02 +02:00
|
|
|
@MainActor
|
2023-04-25 15:47:21 +02:00
|
|
|
struct FollowRequestsView: View {
|
2023-10-19 13:24:02 +02:00
|
|
|
@Environment(ApplicationState.self) var applicationState
|
|
|
|
@Environment(RouterPath.self) var routerPath
|
|
|
|
@Environment(Client.self) var client
|
2023-04-25 15:47:21 +02:00
|
|
|
|
|
|
|
@State private var accounts: [Account] = []
|
|
|
|
@State private var downloadedPage = 1
|
|
|
|
@State private var allItemsLoaded = false
|
|
|
|
@State private var state: ViewState = .loading
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
self.mainBody()
|
|
|
|
.navigationTitle("followingRequests.navigationBar.title")
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
await self.loadData(page: self.downloadedPage)
|
|
|
|
}
|
|
|
|
case .loaded:
|
|
|
|
if self.accounts.isEmpty {
|
|
|
|
NoDataView(imageSystemName: "person.3.sequence", text: "accounts.title.noAccounts")
|
|
|
|
} else {
|
|
|
|
self.list()
|
|
|
|
}
|
|
|
|
case .error(let error):
|
|
|
|
ErrorView(error: error) {
|
|
|
|
self.state = .loading
|
|
|
|
|
|
|
|
self.downloadedPage = 1
|
|
|
|
self.allItemsLoaded = false
|
|
|
|
self.accounts = []
|
|
|
|
await self.loadData(page: self.downloadedPage)
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func list() -> some View {
|
|
|
|
List {
|
|
|
|
ForEach(accounts, id: \.id) { account in
|
|
|
|
NavigationLink(value: RouteurDestinations.userProfile(
|
|
|
|
accountId: account.id,
|
|
|
|
accountDisplayName: account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: account.acct)
|
|
|
|
) {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
UserAvatar(accountAvatar: account.avatar, size: .list)
|
|
|
|
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text(account.displayName ?? account.username)
|
|
|
|
.foregroundColor(.mainTextColor)
|
|
|
|
Text("@\(account.acct)")
|
2023-09-19 19:32:27 +02:00
|
|
|
.foregroundColor(.customGrayColor)
|
2023-04-25 15:47:21 +02:00
|
|
|
.font(.footnote)
|
|
|
|
}
|
|
|
|
.padding(.leading, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let note = account.note, !note.asMarkdown.isEmpty {
|
|
|
|
MarkdownFormattedText(note.asMarkdown)
|
|
|
|
.font(.footnote)
|
|
|
|
.environment(\.openURL, OpenURLAction { url in
|
|
|
|
routerPath.handle(url: url)
|
|
|
|
})
|
|
|
|
.padding(.vertical, 4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.swipeActions {
|
2023-04-26 16:40:46 +02:00
|
|
|
Button(role: .destructive) {
|
|
|
|
Task {
|
|
|
|
await self.approve(account: account)
|
|
|
|
}
|
2023-04-25 15:47:21 +02:00
|
|
|
} label: {
|
|
|
|
Label("followingRequests.title.approve", systemImage: "checkmark")
|
|
|
|
}
|
|
|
|
|
|
|
|
Button(role: .destructive) {
|
2023-04-26 16:40:46 +02:00
|
|
|
Task {
|
|
|
|
await self.reject(account: account)
|
|
|
|
}
|
2023-04-25 15:47:21 +02:00
|
|
|
} label: {
|
|
|
|
Label("followingRequests.title.reject", systemImage: "xmark")
|
|
|
|
}
|
|
|
|
.tint(.dangerColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if allItemsLoaded == false {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
self.downloadedPage = self.downloadedPage + 1
|
|
|
|
await self.loadData(page: self.downloadedPage)
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listStyle(.plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadData(page: Int) async {
|
|
|
|
do {
|
|
|
|
try await self.loadAccounts(page: page)
|
2023-10-21 08:18:36 +02:00
|
|
|
|
|
|
|
withAnimation {
|
|
|
|
self.state = .loaded
|
|
|
|
}
|
2023-04-25 15:47:21 +02:00
|
|
|
} catch {
|
|
|
|
if !Task.isCancelled {
|
|
|
|
ErrorService.shared.handle(error, message: "accounts.error.loadingAccountsFailed", showToastr: true)
|
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
|
|
|
ErrorService.shared.handle(error, message: "accounts.error.loadingAccountsFailed", showToastr: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadAccounts(page: Int) async throws {
|
|
|
|
let accountsFromApi = try await self.loadFromApi(page: page)
|
|
|
|
|
|
|
|
if accountsFromApi.isEmpty {
|
|
|
|
self.allItemsLoaded = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await self.downloadAvatars(accounts: accountsFromApi)
|
|
|
|
self.accounts.append(contentsOf: accountsFromApi)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadFromApi(page: Int) async throws -> [Account] {
|
|
|
|
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
|
|
|
|
if page == 1 {
|
|
|
|
let results = try await self.client.followRequests?.followRequests(limit: 100, page: page)
|
|
|
|
return results ?? []
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func approve(account: Account) async {
|
|
|
|
do {
|
|
|
|
_ = try await self.client.followRequests?.authorizeRequest(id: account.id)
|
2023-04-26 16:40:46 +02:00
|
|
|
self.accounts.removeAll { $0.id == account.id }
|
2023-04-25 15:47:21 +02:00
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "followingRequests.error.approve", showToastr: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reject(account: Account) async {
|
|
|
|
do {
|
|
|
|
_ = try await self.client.followRequests?.rejectRequest(id: account.id)
|
2023-04-26 16:40:46 +02:00
|
|
|
self.accounts.removeAll { $0.id == account.id }
|
2023-04-25 15:47:21 +02:00
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "followingRequests.error.reject", showToastr: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func downloadAvatars(accounts: [Account]) async {
|
|
|
|
await withTaskGroup(of: Void.self) { group in
|
|
|
|
for account in accounts {
|
|
|
|
group.addTask { await CacheImageService.shared.download(url: account.avatar) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|