IceCubesApp/Packages/Account/Sources/Account/AccountsLIst/AccountsListViewModel.swift

114 lines
4.2 KiB
Swift
Raw Normal View History

2022-12-23 17:47:19 +00:00
import Models
import Network
2023-01-17 10:36:01 +00:00
import SwiftUI
2022-12-23 17:47:19 +00:00
2022-12-24 12:41:25 +00:00
public enum AccountsListMode {
case following(accountId: String), followers(accountId: String)
case favouritedBy(statusId: String), rebloggedBy(statusId: String)
2023-01-17 10:36:01 +00:00
2022-12-24 12:41:25 +00:00
var title: String {
switch self {
case .following:
return "Following"
case .followers:
return "Followers"
case .favouritedBy:
return "Favourited by"
case .rebloggedBy:
return "Boosted by"
}
}
2022-12-23 17:47:19 +00:00
}
@MainActor
class AccountsListViewModel: ObservableObject {
var client: Client?
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
let mode: AccountsListMode
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
public enum State {
public enum PagingState {
case hasNextPage, loadingNextPage, none
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
case loading
case display(accounts: [Account],
relationships: [Relationshionship],
nextPageState: PagingState)
case error(error: Error)
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
private var accounts: [Account] = []
private var relationships: [Relationshionship] = []
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
@Published var state = State.loading
2023-01-17 10:36:01 +00:00
private var nextPageId: String?
2023-01-17 10:36:01 +00:00
2022-12-24 12:41:25 +00:00
init(mode: AccountsListMode) {
2022-12-23 17:47:19 +00:00
self.mode = mode
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
func fetch() async {
guard let client else { return }
do {
state = .loading
let link: LinkHandler?
2022-12-23 17:47:19 +00:00
switch mode {
2022-12-24 12:41:25 +00:00
case let .followers(accountId):
(accounts, link) = try await client.getWithLink(endpoint: Accounts.followers(id: accountId,
2022-12-24 12:41:25 +00:00
maxId: nil))
case let .following(accountId):
(accounts, link) = try await client.getWithLink(endpoint: Accounts.following(id: accountId,
2022-12-24 12:41:25 +00:00
maxId: nil))
case let .rebloggedBy(statusId):
(accounts, link) = try await client.getWithLink(endpoint: Statuses.rebloggedBy(id: statusId,
maxId: nil))
case let .favouritedBy(statusId):
(accounts, link) = try await client.getWithLink(endpoint: Statuses.favouritedBy(id: statusId,
maxId: nil))
2022-12-23 17:47:19 +00:00
}
nextPageId = link?.maxId
2022-12-23 17:47:19 +00:00
relationships = try await client.get(endpoint:
2023-01-17 10:36:01 +00:00
Accounts.relationships(ids: accounts.map { $0.id }))
2022-12-23 17:47:19 +00:00
state = .display(accounts: accounts,
relationships: relationships,
2022-12-24 14:09:17 +00:00
nextPageState: link?.maxId != nil ? .hasNextPage : .none)
2023-01-17 10:36:01 +00:00
} catch {}
2022-12-23 17:47:19 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
func fetchNextPage() async {
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]
let link: LinkHandler?
2022-12-23 17:47:19 +00:00
switch mode {
2022-12-24 12:41:25 +00:00
case let .followers(accountId):
(newAccounts, link) = try await client.getWithLink(endpoint: Accounts.followers(id: accountId,
2022-12-24 12:41:25 +00:00
maxId: nextPageId))
case let .following(accountId):
(newAccounts, link) = try await client.getWithLink(endpoint: Accounts.following(id: accountId,
2022-12-24 12:41:25 +00:00
maxId: nextPageId))
case let .rebloggedBy(statusId):
(newAccounts, link) = try await client.getWithLink(endpoint: Statuses.rebloggedBy(id: statusId,
maxId: nextPageId))
case let .favouritedBy(statusId):
2023-01-17 10:36:01 +00:00
(newAccounts, link) = try await client.getWithLink(endpoint: Statuses.favouritedBy(id: statusId,
maxId: nextPageId))
2022-12-23 17:47:19 +00:00
}
accounts.append(contentsOf: newAccounts)
let newRelationships: [Relationshionship] =
2023-01-17 10:36:01 +00:00
try await client.get(endpoint: Accounts.relationships(ids: newAccounts.map { $0.id }))
2022-12-23 17:47:19 +00:00
relationships.append(contentsOf: newRelationships)
self.nextPageId = link?.maxId
2022-12-23 17:47:19 +00:00
state = .display(accounts: accounts,
relationships: relationships,
2022-12-24 14:09:17 +00:00
nextPageState: link?.maxId != nil ? .hasNextPage : .none)
2022-12-23 17:47:19 +00:00
} catch {
print(error)
}
}
}