2023-01-05 21:08:19 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2023-01-10 08:04:25 +01:00
|
|
|
import MastodonKit
|
2023-01-24 21:13:50 +01:00
|
|
|
import Foundation
|
2023-01-05 21:08:19 +01:00
|
|
|
|
2023-01-22 17:41:11 +01:00
|
|
|
struct AccountsView: View {
|
|
|
|
public enum ListType {
|
|
|
|
case followers
|
|
|
|
case following
|
|
|
|
case reblogged
|
|
|
|
case favourited
|
|
|
|
}
|
|
|
|
|
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 entityId: String
|
|
|
|
@State var listType: ListType
|
|
|
|
|
2023-01-05 21:08:19 +01:00
|
|
|
@State private var accounts: [Account] = []
|
2023-02-14 08:51:27 +01:00
|
|
|
@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()
|
|
|
|
.navigationBarTitle(self.getTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
2023-02-14 08:51:27 +01:00
|
|
|
await self.loadAccounts(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 {
|
|
|
|
NoDataView(imageSystemName: "person.3.sequence", text: "Unfortunately, there is no one here.")
|
|
|
|
} else {
|
|
|
|
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 {
|
2023-02-14 08:51:27 +01:00
|
|
|
self.downloadedPage = self.downloadedPage + 1
|
|
|
|
await self.loadAccounts(page: self.downloadedPage)
|
2023-02-01 18:40:28 +01:00
|
|
|
}
|
|
|
|
Spacer()
|
2023-01-18 18:41:42 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
}
|
2023-01-18 18:41:42 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.listStyle(.plain)
|
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
|
|
|
|
|
2023-02-14 08:51:27 +01:00
|
|
|
self.downloadedPage = 1
|
2023-02-01 18:40:28 +01:00
|
|
|
self.allItemsLoaded = false
|
|
|
|
self.accounts = []
|
2023-02-14 08:51:27 +01:00
|
|
|
await self.loadAccounts(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 17:41:11 +01:00
|
|
|
private func loadAccounts(page: Int) async {
|
2023-01-05 21:08:19 +01:00
|
|
|
do {
|
2023-02-14 08:51:27 +01:00
|
|
|
let accountsFromApi = try await self.loadFromApi(page: page)
|
2023-02-01 20:01:18 +01:00
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
if accountsFromApi.isEmpty {
|
2023-01-05 21:08:19 +01:00
|
|
|
self.allItemsLoaded = true
|
2023-02-01 20:01:18 +01:00
|
|
|
return
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 13:05:21 +01:00
|
|
|
await self.downloadAvatars(accounts: accountsFromApi)
|
2023-01-05 21:08:19 +01:00
|
|
|
self.accounts.append(contentsOf: accountsFromApi)
|
2023-01-18 18:41:42 +01:00
|
|
|
|
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 {
|
|
|
|
ErrorService.shared.handle(error, message: "Error during download followers from server.", showToastr: true)
|
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
|
|
|
ErrorService.shared.handle(error, message: "Error during download followers from server.", showToastr: false)
|
|
|
|
}
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 13:05:21 +01:00
|
|
|
|
2023-01-22 17:41:11 +01:00
|
|
|
private func getTitle() -> String {
|
|
|
|
switch self.listType {
|
|
|
|
case .followers:
|
|
|
|
return "Followers"
|
|
|
|
case .following:
|
|
|
|
return "Following"
|
|
|
|
case .favourited:
|
|
|
|
return "Favourited by"
|
|
|
|
case .reblogged:
|
|
|
|
return "Reboosted by"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 08:51:27 +01:00
|
|
|
private func loadFromApi(page: Int) async throws -> [Account] {
|
2023-01-22 17:41:11 +01:00
|
|
|
switch self.listType {
|
|
|
|
case .followers:
|
2023-02-03 15:16:30 +01:00
|
|
|
return try await self.client.accounts?.followers(account: self.entityId, page: page) ?? []
|
2023-01-22 17:41:11 +01:00
|
|
|
case .following:
|
2023-02-03 15:16:30 +01:00
|
|
|
return try await self.client.accounts?.following(account: self.entityId, page: page) ?? []
|
2023-01-22 17:41:11 +01:00
|
|
|
case .favourited:
|
2023-02-03 15:16:30 +01:00
|
|
|
return try await self.client.statuses?.favouritedBy(statusId: self.entityId, page: page) ?? []
|
2023-01-22 17:41:11 +01:00
|
|
|
case .reblogged:
|
2023-02-03 15:16:30 +01:00
|
|
|
return try await self.client.statuses?.rebloggedBy(statusId: self.entityId, page: page) ?? []
|
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
|
|
|
}
|