Impressia/Vernissage/Views/AccountsView.swift

143 lines
4.8 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.
// 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
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-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] = []
@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-22 13:49:19 +01:00
accountDisplayName: account.displayNameWithoutEmojis,
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()
} 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
}
2023-01-22 17:41:11 +01:00
.navigationBarTitle(self.getTitle())
2023-01-05 21:08:19 +01:00
.listStyle(PlainListStyle())
.task {
if self.accounts.isEmpty == false {
return
}
await self.loadAccounts(page: self.page)
}
}
2023-01-22 17:41:11 +01:00
private func loadAccounts(page: Int) async {
2023-01-05 21:08:19 +01:00
do {
2023-01-22 17:41:11 +01:00
let accountsFromApi = try await self.loadFromApi()
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
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"
}
}
private func loadFromApi() async throws -> [Account] {
switch self.listType {
case .followers:
2023-01-22 21:44:07 +01:00
return try await AccountService.shared.followers(
2023-01-22 17:41:11 +01:00
forAccountId: self.entityId,
andContext: self.applicationState.accountData,
page: page)
case .following:
2023-01-22 21:44:07 +01:00
return try await AccountService.shared.following(
2023-01-22 17:41:11 +01:00
forAccountId: self.entityId,
andContext: self.applicationState.accountData,
page: page)
case .favourited:
return try await StatusService.shared.favouritedBy(
statusId: self.entityId,
andContext: self.applicationState.accountData,
page: page)
case .reblogged:
return try await StatusService.shared.rebloggedBy(
statusId: self.entityId,
andContext: self.applicationState.accountData,
page: page)
}
}
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 {
group.addTask { await CacheAvatarService.shared.downloadImage(for: account.id, avatarUrl: account.avatar) }
}
}
}
2023-01-05 21:08:19 +01:00
}