mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Channel view
This commit is contained in:
19
Model/AppState.swift
Normal file
19
Model/AppState.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
import Foundation
|
||||
|
||||
class AppState: ObservableObject {
|
||||
@Published var showingChannel = false
|
||||
@Published var channelID: String?
|
||||
@Published var channel: String?
|
||||
|
||||
func setChannel(from video: Video) {
|
||||
channel = video.author
|
||||
channelID = video.channelID
|
||||
showingChannel = true
|
||||
}
|
||||
|
||||
func closeChannel() {
|
||||
showingChannel = false
|
||||
channel = nil
|
||||
channelID = nil
|
||||
}
|
||||
}
|
24
Model/ChannelVideosProvider.swift
Normal file
24
Model/ChannelVideosProvider.swift
Normal file
@@ -0,0 +1,24 @@
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
class ChannelVideosProvider: DataProvider {
|
||||
@Published var videos = [Video]()
|
||||
|
||||
var channelID: String? = ""
|
||||
|
||||
func load() {
|
||||
guard channelID != nil else { return }
|
||||
|
||||
let searchPath = "channels/\(channelID!)"
|
||||
DataProvider.request(searchPath).responseJSON { response in
|
||||
switch response.result {
|
||||
case let .success(value):
|
||||
if let channelVideos = JSON(value).dictionaryValue["latestVideos"] {
|
||||
self.videos = channelVideos.arrayValue.map { Video($0) }
|
||||
}
|
||||
case let .failure(error):
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@ import Alamofire
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
final class PopluarVideosProvider: DataProvider {
|
||||
final class PopularVideosProvider: DataProvider {
|
||||
@Published var videos = [Video]()
|
||||
|
||||
func load() {
|
||||
|
@@ -3,6 +3,7 @@ import SwiftyJSON
|
||||
|
||||
class SearchedVideosProvider: DataProvider {
|
||||
@Published var videos = [Video]()
|
||||
|
||||
var query: String = ""
|
||||
|
||||
func load() {
|
||||
|
@@ -5,34 +5,38 @@ import SwiftyJSON
|
||||
final class Video: Identifiable, ObservableObject {
|
||||
let id: String
|
||||
var title: String
|
||||
var thumbnailURL: URL
|
||||
var thumbnailURL: URL?
|
||||
var author: String
|
||||
var length: TimeInterval
|
||||
var published: String
|
||||
var views: Int
|
||||
|
||||
var channelID: String
|
||||
|
||||
@Published var url: URL?
|
||||
@Published var error: Bool = false
|
||||
|
||||
init(id: String, title: String, thumbnailURL: URL, author: String, length: TimeInterval, published: String, views: Int = 0) {
|
||||
init(id: String, title: String, thumbnailURL: URL?, author: String, length: TimeInterval, published: String, channelID: String, views: Int = 0) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.thumbnailURL = thumbnailURL
|
||||
self.author = author
|
||||
self.length = length
|
||||
self.published = published
|
||||
self.channelID = channelID
|
||||
self.views = views
|
||||
}
|
||||
|
||||
init(_ json: JSON) {
|
||||
func extractThumbnailURL(from details: JSON) -> URL {
|
||||
func extractThumbnailURL(from details: JSON) -> URL? {
|
||||
if details["videoThumbnails"].arrayValue.isEmpty {
|
||||
return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")!
|
||||
return nil
|
||||
}
|
||||
|
||||
return details["videoThumbnails"][0]["url"].url!
|
||||
|
||||
let thumbnail = details["videoThumbnails"].arrayValue.first(where: { $0["quality"].stringValue == "medium" })!
|
||||
return thumbnail["url"].url!
|
||||
}
|
||||
|
||||
|
||||
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
|
||||
if streams.isEmpty {
|
||||
error = true
|
||||
@@ -43,7 +47,7 @@ final class Video: Identifiable, ObservableObject {
|
||||
|
||||
return stream["url"].url
|
||||
}
|
||||
|
||||
|
||||
id = json["videoId"].stringValue
|
||||
title = json["title"].stringValue
|
||||
thumbnailURL = extractThumbnailURL(from: json)
|
||||
@@ -51,6 +55,7 @@ final class Video: Identifiable, ObservableObject {
|
||||
length = json["lengthSeconds"].doubleValue
|
||||
published = json["publishedText"].stringValue
|
||||
views = json["viewCount"].intValue
|
||||
channelID = json["authorId"].stringValue
|
||||
|
||||
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
|
||||
}
|
||||
|
Reference in New Issue
Block a user