2023-03-04 14:08:39 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2023-03-04 14:08:39 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import PixelfedKit
|
|
|
|
import Foundation
|
|
|
|
|
2023-03-08 12:07:43 +01:00
|
|
|
struct AccountsPhotoView: View {
|
|
|
|
public enum ListType: Hashable {
|
|
|
|
case trending
|
|
|
|
case search(query: String)
|
|
|
|
}
|
|
|
|
|
2023-03-04 14:08:39 +01:00
|
|
|
@EnvironmentObject var applicationState: ApplicationState
|
|
|
|
@EnvironmentObject var client: Client
|
|
|
|
@EnvironmentObject var routerPath: RouterPath
|
|
|
|
|
2023-03-08 12:07:43 +01:00
|
|
|
@State public var listType: ListType
|
|
|
|
|
2023-03-04 14:08:39 +01:00
|
|
|
@State private var accounts: [Account] = []
|
|
|
|
@State private var state: ViewState = .loading
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
self.mainBody()
|
2023-03-13 13:53:36 +01:00
|
|
|
.navigationTitle("trendingAccounts.navigationBar.title")
|
2023-03-04 14:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
await self.loadData()
|
|
|
|
}
|
|
|
|
case .loaded:
|
|
|
|
if self.accounts.isEmpty {
|
2023-03-13 13:53:36 +01:00
|
|
|
NoDataView(imageSystemName: "person.3.sequence", text: "trendingAccounts.title.noAccounts")
|
2023-03-04 14:08:39 +01:00
|
|
|
} else {
|
2023-03-29 09:35:01 +02:00
|
|
|
self.list()
|
2023-03-04 14:08:39 +01:00
|
|
|
}
|
|
|
|
case .error(let error):
|
|
|
|
ErrorView(error: error) {
|
|
|
|
self.state = .loading
|
|
|
|
|
|
|
|
self.accounts = []
|
|
|
|
await self.loadData()
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
@ViewBuilder
|
|
|
|
private func list() -> some View {
|
|
|
|
List {
|
|
|
|
ForEach(self.accounts, id: \.id) { account in
|
|
|
|
Section {
|
|
|
|
ImagesGrid(gridType: .account(accountId: account.id,
|
|
|
|
accountDisplayName: account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: account.acct))
|
|
|
|
} header: {
|
|
|
|
HStack {
|
|
|
|
UsernameRow(
|
|
|
|
accountId: account.id,
|
|
|
|
accountAvatar: account.avatar,
|
|
|
|
accountDisplayName: account.displayNameWithoutEmojis,
|
|
|
|
accountUsername: account.acct)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.textCase(.none)
|
|
|
|
.listRowInsets(EdgeInsets())
|
|
|
|
.padding(.vertical, 12)
|
|
|
|
.onTapGesture {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: account.id,
|
|
|
|
accountDisplayName: account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: account.acct))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 14:08:39 +01:00
|
|
|
private func loadData() async {
|
|
|
|
do {
|
2023-03-08 12:07:43 +01:00
|
|
|
self.accounts = try await self.loadAccounts()
|
2023-03-04 14:08:39 +01:00
|
|
|
self.state = .loaded
|
|
|
|
} catch {
|
|
|
|
if !Task.isCancelled {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingAccounts.error.loadingAccountsFailed", showToastr: true)
|
2023-03-04 14:08:39 +01:00
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingAccounts.error.loadingAccountsFailed", showToastr: false)
|
2023-03-04 14:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 12:07:43 +01:00
|
|
|
private func loadAccounts() async throws -> [Account] {
|
|
|
|
switch self.listType {
|
|
|
|
case .trending:
|
|
|
|
do {
|
|
|
|
let accountsFromApi = try await self.client.trends?.accounts()
|
|
|
|
return accountsFromApi ?? []
|
|
|
|
} catch NetworkError.notSuccessResponse(let response) {
|
|
|
|
// TODO: This code can be removed when other Pixelfed server will support trending accounts.
|
|
|
|
if response.statusCode() == HTTPStatusCode.notFound {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
throw NetworkError.notSuccessResponse(response)
|
|
|
|
}
|
|
|
|
case .search(let query):
|
|
|
|
let results = try await self.client.search?.search(query: query, resultsType: .accounts, limit: 40, page: 1)
|
|
|
|
return results?.accounts ?? []
|
|
|
|
}
|
2023-03-04 14:08:39 +01:00
|
|
|
}
|
|
|
|
}
|