Impressia/Vernissage/Views/AccountsView.swift

194 lines
7.1 KiB
Swift
Raw Normal View History

2023-01-05 21:08:19 +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-01-05 21:08:19 +01:00
//
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
import Foundation
2023-01-05 21:08:19 +01:00
2023-01-22 17:41:11 +01:00
struct AccountsView: View {
2023-03-07 16:45:44 +01:00
public enum ListType: Hashable {
case followers(entityId: String)
case following(entityId: String)
case reblogged(entityId: String)
case favourited(entityId: String)
2023-03-27 14:52:53 +02:00
case blocks
case mutes
2023-03-07 16:45:44 +01:00
case search(query: String)
2023-01-22 17:41:11 +01:00
}
2023-01-05 21:08:19 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject var client: Client
2023-01-05 21:08:19 +01:00
2023-01-22 17:41:11 +01:00
@State var listType: ListType
2023-01-05 21:08:19 +01:00
@State private var accounts: [Account] = []
@State private var downloadedPage = 1
2023-01-05 21:08:19 +01:00
@State private var allItemsLoaded = false
2023-02-01 18:40:28 +01:00
@State private var state: ViewState = .loading
2023-01-05 21:08:19 +01:00
var body: some View {
2023-02-01 18:40:28 +01:00
self.mainBody()
2023-02-21 08:36:14 +01:00
.navigationTitle(self.getTitle())
2023-02-01 18:40:28 +01:00
}
@ViewBuilder
private func mainBody() -> some View {
switch state {
case .loading:
LoadingIndicator()
.task {
await self.loadData(page: self.downloadedPage)
2023-01-23 18:01:27 +01:00
}
2023-02-01 18:40:28 +01:00
case .loaded:
if self.accounts.isEmpty {
2023-03-13 13:53:36 +01:00
NoDataView(imageSystemName: "person.3.sequence", text: "accounts.title.noAccounts")
2023-02-01 18:40:28 +01:00
} else {
self.list()
2023-01-05 21:08:19 +01:00
}
2023-02-01 18:40:28 +01:00
case .error(let error):
ErrorView(error: error) {
self.state = .loading
self.downloadedPage = 1
2023-02-01 18:40:28 +01:00
self.allItemsLoaded = false
self.accounts = []
await self.loadData(page: self.downloadedPage)
2023-01-06 13:05:21 +01:00
}
2023-02-01 18:40:28 +01:00
.padding()
2023-01-05 21:08:19 +01:00
}
}
@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)
) {
UsernameRow(accountId: account.id,
accountAvatar: account.avatar,
accountDisplayName: account.displayNameWithoutEmojis,
accountUsername: account.acct)
}
}
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 {
2023-01-05 21:08:19 +01:00
do {
try await self.loadAccounts(page: page)
2023-02-01 18:40:28 +01:00
self.state = .loaded
2023-01-05 21:08:19 +01:00
} catch {
2023-02-01 18:40:28 +01:00
if !Task.isCancelled {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "accounts.error.loadingAccountsFailed", showToastr: true)
2023-02-01 18:40:28 +01:00
self.state = .error(error)
} else {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "accounts.error.loadingAccountsFailed", showToastr: false)
2023-02-01 18:40:28 +01:00
}
2023-01-05 21:08:19 +01:00
}
}
2023-01-06 13:05:21 +01:00
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)
}
2023-01-22 17:41:11 +01:00
private func getTitle() -> String {
switch self.listType {
case .followers:
2023-03-13 13:53:36 +01:00
return NSLocalizedString("accounts.navigationBar.followers", comment: "Followers")
2023-01-22 17:41:11 +01:00
case .following:
2023-03-13 13:53:36 +01:00
return NSLocalizedString("accounts.navigationBar.following", comment: "Following")
2023-01-22 17:41:11 +01:00
case .favourited:
2023-03-13 13:53:36 +01:00
return NSLocalizedString("accounts.navigationBar.favouritedBy", comment: "Favourited by")
2023-01-22 17:41:11 +01:00
case .reblogged:
2023-03-13 13:53:36 +01:00
return NSLocalizedString("accounts.navigationBar.reboostedBy", comment: "Reboosted by")
2023-03-27 14:52:53 +02:00
case .blocks:
return NSLocalizedString("accounts.navigationBar.blocked", comment: "Blocked")
case .mutes:
return NSLocalizedString("accounts.navigationBar.mutes", comment: "Mutes")
2023-03-07 16:45:44 +01:00
case .search(let query):
return query
2023-01-22 17:41:11 +01:00
}
}
private func loadFromApi(page: Int) async throws -> [Account] {
2023-01-22 17:41:11 +01:00
switch self.listType {
2023-03-07 16:45:44 +01:00
case .followers(let entityId):
return try await self.client.accounts?.followers(account: entityId, page: page) ?? []
case .following(let entityId):
return try await self.client.accounts?.following(account: entityId, page: page) ?? []
case .favourited(let entityId):
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
if page == 1 {
2023-03-07 16:45:44 +01:00
return try await self.client.statuses?.favouritedBy(statusId: entityId, limit: 40, page: page) ?? []
} else {
return []
}
2023-03-07 16:45:44 +01:00
case .reblogged(let entityId):
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
if page == 1 {
return try await self.client.statuses?.rebloggedBy(statusId: entityId, limit: 40, page: page) ?? []
} else {
return []
}
2023-03-27 14:52:53 +02:00
case .blocks:
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
if page == 1 {
return try await self.client.blocks?.blocks(limit: 40, page: page) ?? []
} else {
return []
}
case .mutes:
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
if page == 1 {
return try await self.client.mutes?.mutes(limit: 40, page: page) ?? []
} else {
return []
}
2023-03-07 16:45:44 +01:00
case .search(let query):
// TODO: Workaround for not working paging for favourites/reblogged issues: https://github.com/pixelfed/pixelfed/issues/4182.
if page == 1 {
2023-03-07 16:45:44 +01:00
let results = try await self.client.search?.search(query: query, resultsType: .accounts, limit: 40, page: page)
return results?.accounts ?? []
} else {
return []
}
2023-01-22 17:41:11 +01:00
}
}
private func downloadAvatars(accounts: [Account]) async {
2023-01-06 13:05:21 +01:00
await withTaskGroup(of: Void.self) { group in
for account in accounts {
2023-01-28 21:09:31 +01:00
group.addTask { await CacheImageService.shared.download(url: account.avatar) }
2023-01-06 13:05:21 +01:00
}
}
}
2023-01-05 21:08:19 +01:00
}