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-05 21:08:19 +01:00
|
|
|
|
|
|
|
struct FollowersView: View {
|
|
|
|
@EnvironmentObject var applicationState: ApplicationState
|
|
|
|
|
|
|
|
@State var accountId: String
|
|
|
|
@State private var accounts: [Account] = []
|
|
|
|
@State private var page = 1
|
|
|
|
@State private var allItemsLoaded = false
|
2023-01-06 13:05:21 +01:00
|
|
|
@State private var firstLoadFinished = false
|
2023-01-05 21:08:19 +01:00
|
|
|
|
|
|
|
var body: some View {
|
2023-01-06 13:05:21 +01:00
|
|
|
List {
|
|
|
|
ForEach(accounts, id: \.id) { account in
|
|
|
|
NavigationLink(destination: UserProfileView(
|
|
|
|
accountId: account.id,
|
|
|
|
accountDisplayName: account.displayName,
|
|
|
|
accountUserName: account.acct)
|
|
|
|
.environmentObject(applicationState)) {
|
2023-01-18 18:41:42 +01:00
|
|
|
UsernameRow(accountId: account.id,
|
|
|
|
accountAvatar: account.avatar,
|
2023-01-06 13:05:21 +01:00
|
|
|
accountDisplayName: account.displayName,
|
2023-01-18 18:41:42 +01:00
|
|
|
accountUsername: account.acct)
|
2023-01-06 13:05:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 18:16:08 +01:00
|
|
|
if allItemsLoaded == false && firstLoadFinished == true {
|
2023-01-18 18:41:42 +01:00
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
self.page = self.page + 1
|
|
|
|
await self.loadAccounts(page: self.page)
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.listRowSeparator(.hidden)
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
2023-01-06 13:05:21 +01:00
|
|
|
}.overlay {
|
|
|
|
if firstLoadFinished == false {
|
|
|
|
LoadingIndicator()
|
2023-01-09 12:14:33 +01:00
|
|
|
} else {
|
|
|
|
if self.accounts.isEmpty {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "person.3.sequence")
|
|
|
|
.font(.largeTitle)
|
|
|
|
.padding(.bottom, 4)
|
|
|
|
Text("Unfortunately, there is no one here.")
|
|
|
|
.font(.title3)
|
|
|
|
}.foregroundColor(.lightGrayColor)
|
|
|
|
}
|
2023-01-06 13:05:21 +01:00
|
|
|
}
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
.navigationBarTitle("Followers")
|
|
|
|
.listStyle(PlainListStyle())
|
|
|
|
.task {
|
|
|
|
if self.accounts.isEmpty == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await self.loadAccounts(page: self.page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAccounts(page: Int) async {
|
|
|
|
do {
|
|
|
|
let accountsFromApi = try await AccountService.shared.getFollowers(
|
|
|
|
forAccountId: self.accountId,
|
|
|
|
andContext: self.applicationState.accountData,
|
|
|
|
page: page)
|
2023-01-18 18:41:42 +01:00
|
|
|
|
2023-01-06 18:16:08 +01:00
|
|
|
if accountsFromApi.isEmpty || accountsFromApi.count < 10 {
|
2023-01-05 21:08:19 +01:00
|
|
|
self.allItemsLoaded = true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
self.firstLoadFinished = true
|
2023-01-05 21:08:19 +01:00
|
|
|
} catch {
|
2023-01-20 13:47:38 +01:00
|
|
|
ErrorService.shared.handle(error, message: "Error during download followers from server.", showToastr: !Task.isCancelled)
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 13:05:21 +01:00
|
|
|
|
|
|
|
func downloadAvatars(accounts: [Account]) async {
|
|
|
|
await withTaskGroup(of: Void.self) { group in
|
|
|
|
for account in accounts {
|
|
|
|
group.addTask { await CacheAvatarService.shared.downloadImage(for: account.id, avatarUrl: account.avatar) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|