1
0
mirror of https://github.com/yattee/yattee.git synced 2025-04-27 23:40:35 +05:30
yattee/Model/Subscriptions.swift
2021-08-26 00:12:59 +02:00

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()
}
}
}