2022-12-23 17:47:19 +00:00
|
|
|
import SwiftUI
|
|
|
|
import Models
|
|
|
|
import Network
|
|
|
|
|
|
|
|
public enum AccountsListMode: String {
|
|
|
|
case following, followers
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class AccountsListViewModel: ObservableObject {
|
|
|
|
var client: Client?
|
|
|
|
|
|
|
|
let accountId: String
|
|
|
|
let mode: AccountsListMode
|
|
|
|
|
|
|
|
public enum State {
|
|
|
|
public enum PagingState {
|
|
|
|
case hasNextPage, loadingNextPage, none
|
|
|
|
}
|
|
|
|
case loading
|
|
|
|
case display(accounts: [Account],
|
|
|
|
relationships: [Relationshionship],
|
|
|
|
nextPageState: PagingState)
|
|
|
|
case error(error: Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var accounts: [Account] = []
|
|
|
|
private var relationships: [Relationshionship] = []
|
|
|
|
|
|
|
|
@Published var state = State.loading
|
|
|
|
|
2022-12-24 08:21:04 +00:00
|
|
|
private var nextPageId: String?
|
|
|
|
|
2022-12-23 17:47:19 +00:00
|
|
|
init(accountId: String, mode: AccountsListMode) {
|
|
|
|
self.accountId = accountId
|
|
|
|
self.mode = mode
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetch() async {
|
|
|
|
guard let client else { return }
|
|
|
|
do {
|
|
|
|
state = .loading
|
2022-12-24 08:21:04 +00:00
|
|
|
let link: LinkHandler?
|
2022-12-23 17:47:19 +00:00
|
|
|
switch mode {
|
|
|
|
case .followers:
|
2022-12-24 08:21:04 +00:00
|
|
|
(accounts, link) = try await client.getWithLink(endpoint: Accounts.followers(id: accountId,
|
|
|
|
sinceId: nil))
|
2022-12-23 17:47:19 +00:00
|
|
|
case .following:
|
2022-12-24 08:21:04 +00:00
|
|
|
(accounts, link) = try await client.getWithLink(endpoint: Accounts.following(id: accountId,
|
|
|
|
sinceId: nil))
|
2022-12-23 17:47:19 +00:00
|
|
|
}
|
2022-12-24 08:21:04 +00:00
|
|
|
nextPageId = link?.maxId
|
2022-12-23 17:47:19 +00:00
|
|
|
relationships = try await client.get(endpoint:
|
|
|
|
Accounts.relationships(ids: accounts.map{ $0.id }))
|
|
|
|
state = .display(accounts: accounts,
|
|
|
|
relationships: relationships,
|
|
|
|
nextPageState: .hasNextPage)
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchNextPage() async {
|
2022-12-24 08:21:04 +00:00
|
|
|
guard let client, let nextPageId else { return }
|
2022-12-23 17:47:19 +00:00
|
|
|
do {
|
|
|
|
state = .display(accounts: accounts, relationships: relationships, nextPageState: .loadingNextPage)
|
|
|
|
let newAccounts: [Account]
|
2022-12-24 08:21:04 +00:00
|
|
|
let link: LinkHandler?
|
2022-12-23 17:47:19 +00:00
|
|
|
switch mode {
|
|
|
|
case .followers:
|
2022-12-24 08:21:04 +00:00
|
|
|
(newAccounts, link) = try await client.getWithLink(endpoint: Accounts.followers(id: accountId,
|
|
|
|
sinceId: nextPageId))
|
2022-12-23 17:47:19 +00:00
|
|
|
case .following:
|
2022-12-24 08:21:04 +00:00
|
|
|
(newAccounts, link) = try await client.getWithLink(endpoint: Accounts.following(id: accountId,
|
|
|
|
sinceId: nextPageId))
|
2022-12-23 17:47:19 +00:00
|
|
|
}
|
|
|
|
accounts.append(contentsOf: newAccounts)
|
|
|
|
let newRelationships: [Relationshionship] =
|
|
|
|
try await client.get(endpoint: Accounts.relationships(ids: newAccounts.map{ $0.id }))
|
|
|
|
|
|
|
|
relationships.append(contentsOf: newRelationships)
|
2022-12-24 08:21:04 +00:00
|
|
|
self.nextPageId = link?.maxId
|
2022-12-23 17:47:19 +00:00
|
|
|
state = .display(accounts: accounts,
|
|
|
|
relationships: relationships,
|
|
|
|
nextPageState: .hasNextPage)
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|