Subscribe/unsubscribe channels

This commit is contained in:
Arkadiusz Fal
2021-08-26 00:12:59 +02:00
parent 151121aa31
commit 1196a2a5e2
14 changed files with 218 additions and 70 deletions

View File

@@ -1,12 +1,22 @@
import AVFoundation
import Defaults
import Foundation
import SwiftyJSON
struct Channel: Codable, Defaults.Serializable {
var id: String
var name: String
var subscriptionsCount: String
static func from(video: Video) -> Channel {
Channel(id: video.channelID, name: video.author)
init(json: JSON) {
id = json["authorId"].stringValue
name = json["author"].stringValue
subscriptionsCount = json["subCountText"].stringValue
}
init(id: String, name: String, subscriptionsCount: String) {
self.id = id
self.name = name
self.subscriptionsCount = subscriptionsCount
}
}

View File

@@ -61,15 +61,19 @@ final class InvidiousAPI: Service {
configureTransformer("/auth/feed", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
if let feedVideos = content.json.dictionaryValue["videos"] {
return feedVideos.arrayValue.map { Video($0) }
return feedVideos.arrayValue.map(Video.init)
}
return []
}
configureTransformer("/auth/subscriptions", requestMethods: [.get]) { (content: Entity<JSON>) -> [Channel] in
content.json.arrayValue.map(Channel.init)
}
configureTransformer("/channels/*", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
if let channelVideos = content.json.dictionaryValue["latestVideos"] {
return channelVideos.arrayValue.map { Video($0) }
return channelVideos.arrayValue.map(Video.init)
}
return []
@@ -92,10 +96,18 @@ final class InvidiousAPI: Service {
.withParam("region", country.rawValue)
}
var subscriptions: Resource {
var feed: Resource {
resource("/auth/feed")
}
var subscriptions: Resource {
resource("/auth/subscriptions")
}
func channelSubscription(_ id: String) -> Resource {
resource("/auth/subscriptions").child(id)
}
func channelVideos(_ id: String) -> Resource {
resource("/channels/\(id)")
}

41
Model/Subscriptions.swift Normal file
View File

@@ -0,0 +1,41 @@
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()
}
}
}

View File

@@ -11,7 +11,6 @@ struct Video: Identifiable, Equatable {
var length: TimeInterval
var published: String
var views: Int
var channelID: String
var description: String
var genre: String
@@ -29,6 +28,8 @@ struct Video: Identifiable, Equatable {
var dislikes: Int?
var keywords = [String]()
var channel: Channel
init(
id: String,
title: String,
@@ -36,9 +37,9 @@ struct Video: Identifiable, Equatable {
length: TimeInterval,
published: String,
views: Int,
channelID: String,
description: String,
genre: String,
channel: Channel,
thumbnails: [Thumbnail] = [],
indexID: String? = nil,
live: Bool = false,
@@ -54,9 +55,9 @@ struct Video: Identifiable, Equatable {
self.length = length
self.published = published
self.views = views
self.channelID = channelID
self.description = description
self.genre = genre
self.channel = channel
self.thumbnails = thumbnails
self.indexID = indexID
self.live = live
@@ -83,7 +84,6 @@ struct Video: Identifiable, Equatable {
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
channelID = json["authorId"].stringValue
description = json["description"].stringValue
genre = json["genre"].stringValue
@@ -105,6 +105,7 @@ struct Video: Identifiable, Equatable {
streams.append(contentsOf: Video.extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue))
hlsUrl = json["hlsUrl"].url
channel = Channel(json: json)
}
var playTime: String? {