2021-06-11 18:06:26 +05:30
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
2021-06-14 23:35:02 +05:30
|
|
|
final class SearchedVideosProvider: DataProvider {
|
2021-06-11 18:06:26 +05:30
|
|
|
@Published var videos = [Video]()
|
2021-06-12 02:41:59 +05:30
|
|
|
|
2021-06-14 23:35:02 +05:30
|
|
|
var currentQuery: String = ""
|
2021-06-11 18:06:26 +05:30
|
|
|
|
2021-06-14 23:35:02 +05:30
|
|
|
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)!)"
|
2021-06-11 18:06:26 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|