mirror of
https://github.com/yattee/yattee.git
synced 2025-04-27 23:40:35 +05:30
42 lines
1.0 KiB
Swift
42 lines
1.0 KiB
Swift
import Foundation
|
|
import Siesta
|
|
import SwiftUI
|
|
|
|
final class Subscriptions: ObservableObject {
|
|
@Published var channels = [Channel]()
|
|
|
|
var resource: Resource {
|
|
InvidiousAPI.shared.subscriptions
|
|
}
|
|
|
|
init() {
|
|
load()
|
|
}
|
|
|
|
func subscribe(_ channelID: String) {
|
|
performChannelSubscriptionRequest(channelID, method: .post)
|
|
}
|
|
|
|
func unsubscribe(_ channelID: String) {
|
|
performChannelSubscriptionRequest(channelID, method: .delete)
|
|
}
|
|
|
|
func subscribed(_ channelID: String) -> Bool {
|
|
channels.contains { $0.id == channelID }
|
|
}
|
|
|
|
fileprivate func load() {
|
|
resource.load().onSuccess { resource in
|
|
if let channels: [Channel] = resource.typedContent() {
|
|
self.channels = channels
|
|
}
|
|
}
|
|
}
|
|
|
|
fileprivate func performChannelSubscriptionRequest(_ channelID: String, method: RequestMethod) {
|
|
InvidiousAPI.shared.channelSubscription(channelID).request(method).onCompletion { _ in
|
|
self.load()
|
|
}
|
|
}
|
|
}
|