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
|
|
|
|
import MastodonSwift
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
UsernameRow(accountAvatar: account.avatar,
|
|
|
|
accountDisplayName: account.displayName,
|
|
|
|
accountUsername: account.acct,
|
|
|
|
cachedAvatar: CacheAvatarService.shared.getImage(for: account.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 18:16:08 +01:00
|
|
|
if allItemsLoaded == false && firstLoadFinished == true {
|
|
|
|
LoadingIndicator()
|
|
|
|
.onAppear {
|
|
|
|
Task {
|
|
|
|
self.page = self.page + 1
|
|
|
|
await self.loadAccounts(page: self.page)
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
2023-01-06 18:16:08 +01:00
|
|
|
}
|
|
|
|
.frame(idealWidth: .infinity, maxWidth: .infinity, alignment: .center)
|
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)
|
2023-01-06 13:05:21 +01:00
|
|
|
self.firstLoadFinished = true
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAccounts(page: Int) async {
|
|
|
|
do {
|
|
|
|
let accountsFromApi = try await AccountService.shared.getFollowers(
|
|
|
|
forAccountId: self.accountId,
|
|
|
|
andContext: self.applicationState.accountData,
|
|
|
|
page: page)
|
|
|
|
|
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
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
} catch {
|
|
|
|
print("Error \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
struct FollowersView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
FollowersView(accountId: "")
|
|
|
|
}
|
|
|
|
}
|