Bubble/Threaded/Data/FetchTimeline.swift

43 lines
1.1 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import Foundation
struct FetchTimeline {
var client: Client
private var datasource: [Status] = []
public var statusesState: LoadingState = .loading
private var timeline: TimelineFilter = .home
init(client: Client) {
self.client = client
}
public mutating func fetch(client: Client) async {
self.statusesState = .loading
self.datasource = await fetchNewestStatuses()
}
private mutating func fetchNewestStatuses() async -> [Status] {
do {
let statuses: [Status] = try await client.get(endpoint: timeline.endpoint(sinceId: nil, maxId: nil, minId: nil, offset: 0))
self.statusesState = .loaded
return statuses
} catch {
statusesState = .error(error: error)
print("timeline parse error: \(error)")
}
return []
}
func getStatuses() -> [Status] {
return datasource
}
enum LoadingState {
case loading
case loaded
case error(error: Error)
}
}