2022-12-22 10:19:56 +00:00
|
|
|
import Foundation
|
|
|
|
import Models
|
|
|
|
import Network
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
public class CurrentAccount: ObservableObject {
|
|
|
|
@Published public private(set) var account: Account?
|
2023-01-02 18:23:44 +00:00
|
|
|
@Published public private(set) var lists: [List] = []
|
2023-01-04 17:37:58 +00:00
|
|
|
@Published public private(set) var tags: [Tag] = []
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-22 10:19:56 +00:00
|
|
|
private var client: Client?
|
2023-01-17 10:36:01 +00:00
|
|
|
|
|
|
|
public static let shared = CurrentAccount()
|
|
|
|
|
|
|
|
private init() {}
|
|
|
|
|
2022-12-22 10:19:56 +00:00
|
|
|
public func setClient(client: Client) {
|
|
|
|
self.client = client
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-04 17:37:58 +00:00
|
|
|
Task(priority: .userInitiated) {
|
|
|
|
await fetchUserData()
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-04 17:37:58 +00:00
|
|
|
private func fetchUserData() async {
|
|
|
|
await withTaskGroup(of: Void.self) { group in
|
2023-01-12 17:25:37 +00:00
|
|
|
group.addTask { await self.fetchConnections() }
|
2023-01-04 17:37:58 +00:00
|
|
|
group.addTask { await self.fetchCurrentAccount() }
|
|
|
|
group.addTask { await self.fetchLists() }
|
|
|
|
group.addTask { await self.fetchFollowedTags() }
|
2022-12-22 10:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-12 17:25:37 +00:00
|
|
|
public func fetchConnections() async {
|
|
|
|
guard let client = client else { return }
|
|
|
|
do {
|
|
|
|
let connections: [String] = try await client.get(endpoint: Instances.peers)
|
|
|
|
client.addConnections(connections)
|
2023-01-17 10:36:01 +00:00
|
|
|
} catch {}
|
2023-01-12 17:25:37 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-22 10:19:56 +00:00
|
|
|
public func fetchCurrentAccount() async {
|
2022-12-29 05:55:53 +00:00
|
|
|
guard let client = client, client.isAuth else {
|
|
|
|
account = nil
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 17:37:58 +00:00
|
|
|
account = try? await client.get(endpoint: Accounts.verifyCredentials)
|
2022-12-22 10:19:56 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-02 18:23:44 +00:00
|
|
|
public func fetchLists() async {
|
|
|
|
guard let client, client.isAuth else { return }
|
|
|
|
do {
|
|
|
|
lists = try await client.get(endpoint: Lists.lists)
|
|
|
|
} catch {
|
|
|
|
lists = []
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-04 17:37:58 +00:00
|
|
|
public func fetchFollowedTags() async {
|
|
|
|
guard let client, client.isAuth else { return }
|
|
|
|
do {
|
|
|
|
tags = try await client.get(endpoint: Accounts.followedTags)
|
|
|
|
} catch {
|
|
|
|
tags = []
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-02 18:23:44 +00:00
|
|
|
public func createList(title: String) async {
|
|
|
|
guard let client else { return }
|
|
|
|
do {
|
|
|
|
let list: Models.List = try await client.post(endpoint: Lists.createList(title: title))
|
|
|
|
lists.append(list)
|
2023-01-17 10:36:01 +00:00
|
|
|
} catch {}
|
2023-01-02 18:23:44 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-02 18:23:44 +00:00
|
|
|
public func deleteList(list: Models.List) async {
|
|
|
|
guard let client else { return }
|
|
|
|
lists.removeAll(where: { $0.id == list.id })
|
|
|
|
let response = try? await client.delete(endpoint: Lists.list(id: list.id))
|
|
|
|
if response?.statusCode != 200 {
|
|
|
|
lists.append(list)
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-04 17:37:58 +00:00
|
|
|
public func followTag(id: String) async -> Tag? {
|
|
|
|
guard let client else { return nil }
|
|
|
|
do {
|
|
|
|
let tag: Tag = try await client.post(endpoint: Tags.follow(id: id))
|
|
|
|
tags.append(tag)
|
|
|
|
return tag
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-04 17:37:58 +00:00
|
|
|
public func unfollowTag(id: String) async -> Tag? {
|
|
|
|
guard let client else { return nil }
|
|
|
|
do {
|
|
|
|
let tag: Tag = try await client.post(endpoint: Tags.unfollow(id: id))
|
2023-01-17 10:36:01 +00:00
|
|
|
tags.removeAll { $0.id == tag.id }
|
2023-01-04 17:37:58 +00:00
|
|
|
return tag
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 10:19:56 +00:00
|
|
|
}
|