mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
36 lines
974 B
Swift
36 lines
974 B
Swift
import Foundation
|
|
import SwiftyJSON
|
|
|
|
final class SearchedVideosProvider: DataProvider {
|
|
@Published var videos = [Video]()
|
|
|
|
var currentQuery: String = ""
|
|
|
|
func load(_ query: String) {
|
|
var newQuery = query
|
|
|
|
if let url = URLComponents(string: query),
|
|
let queryItem = url.queryItems?.first(where: { item in item.name == "v" }),
|
|
let id = queryItem.value
|
|
{
|
|
newQuery = id
|
|
}
|
|
|
|
if newQuery == currentQuery {
|
|
return
|
|
}
|
|
|
|
currentQuery = newQuery
|
|
|
|
let searchPath = "search?q=\(currentQuery.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!)"
|
|
DataProvider.request(searchPath).responseJSON { response in
|
|
switch response.result {
|
|
case let .success(value):
|
|
self.videos = JSON(value).arrayValue.map { Video($0) }
|
|
case let .failure(error):
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
}
|