Add search

This commit is contained in:
Arkadiusz Fal 2021-06-11 14:36:26 +02:00
parent 5efb3a798f
commit 4bc70f351d
10 changed files with 162 additions and 46 deletions

View File

@ -0,0 +1,27 @@
import Foundation
import SwiftUI
struct PopularVideosView: View {
@ObservedObject private var popular = PopluarVideosProvider()
var body: some View {
Group {
List {
ForEach(popular.videos) { video in
VideoThumbnailView(video: video)
.listRowInsets(listRowInsets)
}
}
.listStyle(GroupedListStyle())
}
.task {
async {
popular.load()
}
}
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
}
}

19
Apple TV/SearchView.swift Normal file
View File

@ -0,0 +1,19 @@
import SwiftUI
struct SearchView: View {
@State var query = ""
@ObservedObject private var provider = SearchedVideosProvider()
var body: some View {
VStack {
SearchedVideosView(provider: provider, query: $query)
.searchable(text: $query)
}
}
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
SearchView()
}
}

View File

@ -0,0 +1,47 @@
import SwiftUI
struct SearchedVideosView: View {
@ObservedObject var provider: SearchedVideosProvider
@Binding var query: String
@Environment(\.isSearching) var isSearching
var body: some View {
Group {
List {
ForEach(videos) { video in
VideoThumbnailView(video: video)
.listRowInsets(listRowInsets)
}
}
.listStyle(GroupedListStyle())
}
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
}
var videos: [Video] {
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 != provider.query) {
provider.query = newQuery
provider.load()
}
return provider.videos
}
}
//struct SearchedVideosView_Previews: PreviewProvider {
// static var previews: some View {
// SearchedVideosView()
// }
//}

View File

@ -0,0 +1,19 @@
import Foundation
import SwiftyJSON
class SearchedVideosProvider: DataProvider {
@Published var videos = [Video]()
var query: String = ""
func load() {
let searchPath = "search?q=\(query.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)
}
}
}
}

View File

@ -25,18 +25,15 @@ final class Video: Identifiable, ObservableObject {
}
init(_ json: JSON) {
id = json["videoId"].stringValue
title = json["title"].stringValue
thumbnailURL = json["videoThumbnails"][0]["url"].url!
author = json["author"].stringValue
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
url = formatStreamURL(from: json["formatStreams"].arrayValue)
func extractThumbnailURL(from details: JSON) -> URL {
if details["videoThumbnails"].arrayValue.isEmpty {
return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")!
}
func formatStreamURL(from streams: [JSON]) -> URL? {
return details["videoThumbnails"][0]["url"].url!
}
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
if streams.isEmpty {
error = true
return nil
@ -47,6 +44,17 @@ final class Video: Identifiable, ObservableObject {
return stream["url"].url
}
id = json["videoId"].stringValue
title = json["title"].stringValue
thumbnailURL = extractThumbnailURL(from: json)
author = json["author"].stringValue
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
}
var playTime: String? {
let formatter = DateComponentsFormatter()

View File

@ -7,6 +7,12 @@
objects = {
/* Begin PBXBuildFile section */
37AAF27E26737323007FC770 /* PopularVideosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF27D26737323007FC770 /* PopularVideosView.swift */; };
37AAF28026737550007FC770 /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF27F26737550007FC770 /* SearchView.swift */; };
37AAF2822673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF2832673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF2842673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF28826737A13007FC770 /* SearchedVideosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28726737A13007FC770 /* SearchedVideosView.swift */; };
37D4B0D92671614900C925CA /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0D82671614900C925CA /* Tests_iOS.swift */; };
37D4B0E32671614900C925CA /* Tests_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0E22671614900C925CA /* Tests_macOS.swift */; };
37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0C22671614700C925CA /* PearvidiousApp.swift */; };
@ -19,12 +25,8 @@
37D4B176267164B000C925CA /* PearvidiousUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B175267164B000C925CA /* PearvidiousUITests.swift */; };
37D4B1802671650A00C925CA /* PearvidiousApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0C22671614700C925CA /* PearvidiousApp.swift */; };
37D4B1812671653A00C925CA /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0C32671614700C925CA /* ContentView.swift */; };
37D4B1832671681B00C925CA /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B1822671681B00C925CA /* PlayerView.swift */; };
37D4B1842671684E00C925CA /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B1822671681B00C925CA /* PlayerView.swift */; };
37D4B1852671684F00C925CA /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B1822671681B00C925CA /* PlayerView.swift */; };
37D4B1862671691600C925CA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 37D4B0C42671614800C925CA /* Assets.xcassets */; };
37D4B18C26717B3800C925CA /* VideoThumbnailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */; };
37D4B18D26717B3800C925CA /* VideoThumbnailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */; };
37D4B18E26717B3800C925CA /* VideoThumbnailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */; };
37D4B19126717C6900C925CA /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 37D4B19026717C6900C925CA /* Alamofire */; };
37D4B19326717CE100C925CA /* PopularVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B19226717CE100C925CA /* PopularVideosProvider.swift */; };
@ -69,6 +71,10 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
37AAF27D26737323007FC770 /* PopularVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopularVideosView.swift; sourceTree = "<group>"; };
37AAF27F26737550007FC770 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; };
37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosProvider.swift; sourceTree = "<group>"; };
37AAF28726737A13007FC770 /* SearchedVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosView.swift; sourceTree = "<group>"; };
37D4B0C22671614700C925CA /* PearvidiousApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PearvidiousApp.swift; sourceTree = "<group>"; };
37D4B0C32671614700C925CA /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
37D4B0C42671614800C925CA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -158,8 +164,6 @@
isa = PBXGroup;
children = (
37D4B0C32671614700C925CA /* ContentView.swift */,
37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */,
37D4B1822671681B00C925CA /* PlayerView.swift */,
37D4B0C22671614700C925CA /* PearvidiousApp.swift */,
37D4B0C42671614800C925CA /* Assets.xcassets */,
);
@ -198,6 +202,11 @@
37D4B159267164AE00C925CA /* Apple TV */ = {
isa = PBXGroup;
children = (
37D4B1822671681B00C925CA /* PlayerView.swift */,
37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */,
37AAF27D26737323007FC770 /* PopularVideosView.swift */,
37AAF28726737A13007FC770 /* SearchedVideosView.swift */,
37AAF27F26737550007FC770 /* SearchView.swift */,
37D4B1AE26729DEB00C925CA /* Info.plist */,
37D4B15E267164AF00C925CA /* Assets.xcassets */,
);
@ -219,6 +228,7 @@
37D4B19226717CE100C925CA /* PopularVideosProvider.swift */,
37D4B1B32672A30700C925CA /* VideoDetailsProvider.swift */,
37D4B1AF2672A01000C925CA /* DataProvider.swift */,
37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */,
);
path = Model;
sourceTree = "<group>";
@ -453,10 +463,9 @@
buildActionMask = 2147483647;
files = (
37D4B19326717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B1832671681B00C925CA /* PlayerView.swift in Sources */,
37D4B0E62671614900C925CA /* ContentView.swift in Sources */,
37AAF2822673791F007FC770 /* SearchedVideosProvider.swift in Sources */,
37D4B1B02672A01000C925CA /* DataProvider.swift in Sources */,
37D4B18C26717B3800C925CA /* VideoThumbnailView.swift in Sources */,
37D4B1B42672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37D4B19726717E1500C925CA /* Video.swift in Sources */,
37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */,
@ -468,10 +477,9 @@
buildActionMask = 2147483647;
files = (
37D4B19426717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B1852671684F00C925CA /* PlayerView.swift in Sources */,
37D4B0E72671614900C925CA /* ContentView.swift in Sources */,
37AAF2832673791F007FC770 /* SearchedVideosProvider.swift in Sources */,
37D4B1B12672A01000C925CA /* DataProvider.swift in Sources */,
37D4B18D26717B3800C925CA /* VideoThumbnailView.swift in Sources */,
37D4B1B52672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37D4B19826717E1500C925CA /* Video.swift in Sources */,
37D4B0E52671614900C925CA /* PearvidiousApp.swift in Sources */,
@ -498,14 +506,18 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
37AAF28026737550007FC770 /* SearchView.swift in Sources */,
37D4B19526717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B1842671684E00C925CA /* PlayerView.swift in Sources */,
37AAF28826737A13007FC770 /* SearchedVideosView.swift in Sources */,
37D4B1802671650A00C925CA /* PearvidiousApp.swift in Sources */,
37D4B1B22672A01000C925CA /* DataProvider.swift in Sources */,
37D4B18E26717B3800C925CA /* VideoThumbnailView.swift in Sources */,
37D4B1B62672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37AAF27E26737323007FC770 /* PopularVideosView.swift in Sources */,
37D4B19926717E1500C925CA /* Video.swift in Sources */,
37D4B1812671653A00C925CA /* ContentView.swift in Sources */,
37AAF2842673791F007FC770 /* SearchedVideosProvider.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -7,12 +7,12 @@
<key>Pearvidious (Apple TV).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>Pearvidious (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<key>Pearvidious (macOS).xcscheme_^#shared#^_</key>
<dict>

View File

@ -1,30 +1,14 @@
import SwiftUI
struct ContentView: View {
@ObservedObject private var popular = PopluarVideosProvider()
var items: [GridItem] {
Array(repeating: .init(.flexible()), count: 4)
}
var body: some View {
NavigationView {
TabView {
Group {
List {
ForEach(popular.videos) { video in
VideoThumbnailView(video: video)
.listRowInsets(EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30))
}
}
.listStyle(GroupedListStyle())
}
PopularVideosView()
.tabItem { Text("Popular") }
}
}
.task {
async {
popular.load()
SearchView()
.tabItem { Image(systemName: "magnifyingglass") }
}
}
}