Popular videos tab

This commit is contained in:
Arkadiusz Fal
2021-06-17 12:02:39 +02:00
parent 850f4e6a02
commit 9d7abda63f
18 changed files with 842 additions and 34 deletions

View File

@@ -0,0 +1,11 @@
enum TrendingCategory: String, CaseIterable, Identifiable {
case `default`, music, gaming, movies
var id: TrendingCategory.RawValue {
rawValue
}
var name: String {
rawValue.capitalized
}
}

View File

@@ -0,0 +1,18 @@
import Alamofire
import Foundation
import SwiftyJSON
final class TrendingCountriesProvider: DataProvider {
@Published var countries = [Country]()
private var query: String = ""
func load(_ query: String) {
guard query != self.query else {
return
}
self.query = query
countries = Country.searchByName(query)
}
}

View File

@@ -0,0 +1,6 @@
import Foundation
final class TrendingState: ObservableObject {
@Published var category: TrendingCategory = .default
@Published var country: Country = .pl
}

View File

@@ -0,0 +1,31 @@
import Alamofire
import Foundation
import SwiftUI
import SwiftyJSON
final class TrendingVideosProvider: DataProvider {
@Published var videos = [Video]()
var currentCategory: TrendingCategory?
var currentCountry: Country?
func load(category: TrendingCategory, country: Country) {
if category == currentCategory, country == currentCountry {
return
}
DataProvider.request("trending?type=\(category.name)&region=\(country.rawValue)").responseJSON { response in
switch response.result {
case let .success(value):
self.videos = JSON(value).arrayValue.map { Video($0) }
case let .failure(error):
print(error)
}
}
currentCategory = category
currentCountry = country
videos = []
}
}