diff --git a/Shared/PlayerView.swift b/Apple TV/PlayerView.swift similarity index 100% rename from Shared/PlayerView.swift rename to Apple TV/PlayerView.swift diff --git a/Apple TV/PopularVideosView.swift b/Apple TV/PopularVideosView.swift new file mode 100644 index 00000000..75c0fbc7 --- /dev/null +++ b/Apple TV/PopularVideosView.swift @@ -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) + } +} diff --git a/Apple TV/SearchView.swift b/Apple TV/SearchView.swift new file mode 100644 index 00000000..456a666a --- /dev/null +++ b/Apple TV/SearchView.swift @@ -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() + } +} diff --git a/Apple TV/SearchedVideosView.swift b/Apple TV/SearchedVideosView.swift new file mode 100644 index 00000000..a5e6ea54 --- /dev/null +++ b/Apple TV/SearchedVideosView.swift @@ -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() +// } +//} diff --git a/Shared/VideoThumbnailView.swift b/Apple TV/VideoThumbnailView.swift similarity index 100% rename from Shared/VideoThumbnailView.swift rename to Apple TV/VideoThumbnailView.swift diff --git a/Model/SearchedVideosProvider.swift b/Model/SearchedVideosProvider.swift new file mode 100644 index 00000000..8376c677 --- /dev/null +++ b/Model/SearchedVideosProvider.swift @@ -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) + } + } + } +} diff --git a/Model/Video.swift b/Model/Video.swift index cd5a55a5..0e72f5c9 100644 --- a/Model/Video.swift +++ b/Model/Video.swift @@ -25,26 +25,34 @@ final class Video: Identifiable, ObservableObject { } init(_ json: JSON) { + func extractThumbnailURL(from details: JSON) -> URL { + if details["videoThumbnails"].arrayValue.isEmpty { + return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")! + } + + return details["videoThumbnails"][0]["url"].url! + } + + func extractFormatStreamURL(from streams: [JSON]) -> URL? { + if streams.isEmpty { + error = true + return nil + } + + let stream = streams.last! + + return stream["url"].url + } + id = json["videoId"].stringValue title = json["title"].stringValue - thumbnailURL = json["videoThumbnails"][0]["url"].url! + thumbnailURL = extractThumbnailURL(from: json) author = json["author"].stringValue length = json["lengthSeconds"].doubleValue published = json["publishedText"].stringValue views = json["viewCount"].intValue - url = formatStreamURL(from: json["formatStreams"].arrayValue) - } - - func formatStreamURL(from streams: [JSON]) -> URL? { - if streams.isEmpty { - error = true - return nil - } - - let stream = streams.last! - - return stream["url"].url + url = extractFormatStreamURL(from: json["formatStreams"].arrayValue) } var playTime: String? { diff --git a/Pearvidious.xcodeproj/project.pbxproj b/Pearvidious.xcodeproj/project.pbxproj index 32f853d8..e1d05722 100644 --- a/Pearvidious.xcodeproj/project.pbxproj +++ b/Pearvidious.xcodeproj/project.pbxproj @@ -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 = ""; }; + 37AAF27F26737550007FC770 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = ""; }; + 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosProvider.swift; sourceTree = ""; }; + 37AAF28726737A13007FC770 /* SearchedVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosView.swift; sourceTree = ""; }; 37D4B0C22671614700C925CA /* PearvidiousApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PearvidiousApp.swift; sourceTree = ""; }; 37D4B0C32671614700C925CA /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 37D4B0C42671614800C925CA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -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 = ""; @@ -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; }; diff --git a/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcschemes/xcschememanagement.plist b/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcschemes/xcschememanagement.plist index 65ca5537..8e1975e9 100644 --- a/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ Pearvidious (Apple TV).xcscheme_^#shared#^_ orderHint - 2 + 1 Pearvidious (iOS).xcscheme_^#shared#^_ orderHint - 1 + 2 Pearvidious (macOS).xcscheme_^#shared#^_ diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index 2dac3f39..a07a5af9 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -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()) - } - .tabItem { Text("Popular") } - } - } - .task { - async { - popular.load() + PopularVideosView() + .tabItem { Text("Popular") } + + SearchView() + .tabItem { Image(systemName: "magnifyingglass") } } } }