Impressia/Vernissage/Views/AccountsPhotoView.swift

133 lines
4.4 KiB
Swift
Raw Normal View History

//
// 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.
//
import SwiftUI
import PixelfedKit
2023-04-07 14:20:12 +02:00
import ClientKit
import Foundation
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-03-08 12:07:43 +01:00
struct AccountsPhotoView: View {
public enum ListType: Hashable {
case trending
case search(query: String)
2023-04-20 15:03:43 +02:00
public var title: LocalizedStringKey {
switch self {
case .trending:
return "trendingAccounts.navigationBar.title"
case .search:
return "trendingAccounts.navigationBar.title"
}
}
2023-03-08 12:07:43 +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
@State private var accounts: [Account] = []
@State private var state: ViewState = .loading
var body: some View {
self.mainBody()
2023-04-20 15:03:43 +02:00
.navigationTitle(self.listType.title)
}
@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")
} else {
self.list()
}
case .error(let error):
ErrorView(error: error) {
self.state = .loading
self.accounts = []
await self.loadData()
}
.padding()
}
}
@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))
}
}
}
}
}
private func loadData() async {
do {
2023-03-08 12:07:43 +01:00
self.accounts = try await self.loadAccounts()
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)
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-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 []
}
2023-03-08 12:07:43 +01:00
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 ?? []
}
}
}