From 65e5f0f426131b463ac1ecb9d334acb3f5ec303d Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sat, 12 Jun 2021 00:49:42 +0200 Subject: [PATCH] Subscriptions view --- Apple TV/PopularVideosView.swift | 4 ++ Apple TV/SubscriptionsView.swift | 21 ++++++++ Apple TV/VideosView.swift | 2 +- Model/AppState.swift | 8 +-- Model/DataProvider.swift | 4 +- Model/SubscriptionVideosProvider.swift | 23 +++++++++ Pearvidious.xcodeproj/project.pbxproj | 16 ++++++ .../xcdebugger/Breakpoints_v2.xcbkptlist | 50 ------------------- Shared/ContentView.swift | 8 ++- Shared/TabSelection.swift | 2 +- 10 files changed, 78 insertions(+), 60 deletions(-) create mode 100644 Apple TV/SubscriptionsView.swift create mode 100644 Model/SubscriptionVideosProvider.swift diff --git a/Apple TV/PopularVideosView.swift b/Apple TV/PopularVideosView.swift index 9d9a6b95..55a5a749 100644 --- a/Apple TV/PopularVideosView.swift +++ b/Apple TV/PopularVideosView.swift @@ -16,6 +16,10 @@ struct PopularVideosView: View { } var videos: [Video] { + if (provider.videos.isEmpty) { + provider.load() + } + return provider.videos } } diff --git a/Apple TV/SubscriptionsView.swift b/Apple TV/SubscriptionsView.swift new file mode 100644 index 00000000..8c1d4601 --- /dev/null +++ b/Apple TV/SubscriptionsView.swift @@ -0,0 +1,21 @@ +import SwiftUI + +struct SubscriptionsView: View { + @ObservedObject private var provider = SubscriptionVideosProvider() + @ObservedObject var state: AppState + + @Binding var tabSelection: TabSelection + + var body: some View { + VideosView(state: state, tabSelection: $tabSelection, videos: videos) + .task { + async { + provider.load() + } + } + } + + var videos: [Video] { + return provider.videos + } +} diff --git a/Apple TV/VideosView.swift b/Apple TV/VideosView.swift index 8832d16d..718c60be 100644 --- a/Apple TV/VideosView.swift +++ b/Apple TV/VideosView.swift @@ -8,7 +8,7 @@ struct VideosView: View { var videos: [Video] var body: some View { - Group { + Section { List { ForEach(videos) { video in VideoThumbnailView(video: video) diff --git a/Model/AppState.swift b/Model/AppState.swift index 57c9c941..84636d78 100644 --- a/Model/AppState.swift +++ b/Model/AppState.swift @@ -2,8 +2,8 @@ import Foundation class AppState: ObservableObject { @Published var showingChannel = false - @Published var channelID: String? - @Published var channel: String? + @Published var channelID: String = "" + @Published var channel: String = "" func openChannel(from video: Video) { channel = video.author @@ -13,7 +13,7 @@ class AppState: ObservableObject { func closeChannel() { showingChannel = false - channel = nil - channelID = nil + channel = "" + channelID = "" } } diff --git a/Model/DataProvider.swift b/Model/DataProvider.swift index d94dbd11..3cab34f3 100644 --- a/Model/DataProvider.swift +++ b/Model/DataProvider.swift @@ -4,8 +4,8 @@ import Foundation class DataProvider: ObservableObject { static let instance = "https://invidious.home.arekf.net" - static func request(_ path: String) -> DataRequest { - AF.request(apiURLString(path)) + static func request(_ path: String, headers: HTTPHeaders? = nil) -> DataRequest { + AF.request(apiURLString(path), headers: headers) } static func apiURLString(_ path: String) -> String { diff --git a/Model/SubscriptionVideosProvider.swift b/Model/SubscriptionVideosProvider.swift new file mode 100644 index 00000000..dcf153b3 --- /dev/null +++ b/Model/SubscriptionVideosProvider.swift @@ -0,0 +1,23 @@ +import Alamofire +import Foundation +import SwiftyJSON + +class SubscriptionVideosProvider: DataProvider { + @Published var videos = [Video]() + + var sid: String = "RpoS7YPPK2-QS81jJF9z4KSQAjmzsOnMpn84c73-GQ8=" + + func load() { + let headers = HTTPHeaders([HTTPHeader(name: "Cookie", value: "SID=\(sid)")]) + DataProvider.request("auth/feed", headers: headers).responseJSON { response in + switch response.result { + case let .success(value): + if let feedVideos = JSON(value).dictionaryValue["videos"] { + self.videos = feedVideos.arrayValue.map { Video($0) } + } + case let .failure(error): + print(error) + } + } + } +} diff --git a/Pearvidious.xcodeproj/project.pbxproj b/Pearvidious.xcodeproj/project.pbxproj index 37ef1511..06079807 100644 --- a/Pearvidious.xcodeproj/project.pbxproj +++ b/Pearvidious.xcodeproj/project.pbxproj @@ -23,6 +23,12 @@ 37AAF2952674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; }; 37AAF2962674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; }; 37AAF29A26740A01007FC770 /* VideosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29926740A01007FC770 /* VideosView.swift */; }; + 37AAF29C26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29B26741B5F007FC770 /* SubscriptionVideosProvider.swift */; }; + 37AAF29D26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29B26741B5F007FC770 /* SubscriptionVideosProvider.swift */; }; + 37AAF29E26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29B26741B5F007FC770 /* SubscriptionVideosProvider.swift */; }; + 37AAF2A026741C97007FC770 /* SubscriptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29F26741C97007FC770 /* SubscriptionsView.swift */; }; + 37AAF2A126741C97007FC770 /* SubscriptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29F26741C97007FC770 /* SubscriptionsView.swift */; }; + 37AAF2A226741C97007FC770 /* SubscriptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29F26741C97007FC770 /* SubscriptionsView.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 */; }; @@ -89,6 +95,8 @@ 37AAF28F26740715007FC770 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = ""; }; 37AAF2932674086B007FC770 /* TabSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabSelection.swift; sourceTree = ""; }; 37AAF29926740A01007FC770 /* VideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideosView.swift; sourceTree = ""; }; + 37AAF29B26741B5F007FC770 /* SubscriptionVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionVideosProvider.swift; sourceTree = ""; }; + 37AAF29F26741C97007FC770 /* SubscriptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionsView.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 = ""; }; @@ -217,6 +225,7 @@ 37D4B159267164AE00C925CA /* Apple TV */ = { isa = PBXGroup; children = ( + 37AAF29F26741C97007FC770 /* SubscriptionsView.swift */, 37AAF27D26737323007FC770 /* PopularVideosView.swift */, 37AAF2892673AB89007FC770 /* ChannelView.swift */, 37AAF27F26737550007FC770 /* SearchView.swift */, @@ -242,6 +251,7 @@ children = ( 37AAF28F26740715007FC770 /* AppState.swift */, 37D4B19626717E1500C925CA /* Video.swift */, + 37AAF29B26741B5F007FC770 /* SubscriptionVideosProvider.swift */, 37D4B19226717CE100C925CA /* PopularVideosProvider.swift */, 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */, 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */, @@ -481,6 +491,7 @@ buildActionMask = 2147483647; files = ( 37D4B19326717CE100C925CA /* PopularVideosProvider.swift in Sources */, + 37AAF29C26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */, 37D4B0E62671614900C925CA /* ContentView.swift in Sources */, 37AAF2822673791F007FC770 /* SearchedVideosProvider.swift in Sources */, 37AAF29026740715007FC770 /* AppState.swift in Sources */, @@ -488,6 +499,7 @@ 37D4B1B02672A01000C925CA /* DataProvider.swift in Sources */, 37AAF28C2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */, 37D4B1B42672A30700C925CA /* VideoDetailsProvider.swift in Sources */, + 37AAF2A026741C97007FC770 /* SubscriptionsView.swift in Sources */, 37D4B19726717E1500C925CA /* Video.swift in Sources */, 37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */, ); @@ -498,6 +510,7 @@ buildActionMask = 2147483647; files = ( 37D4B19426717CE100C925CA /* PopularVideosProvider.swift in Sources */, + 37AAF29D26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */, 37D4B0E72671614900C925CA /* ContentView.swift in Sources */, 37AAF2832673791F007FC770 /* SearchedVideosProvider.swift in Sources */, 37AAF29126740715007FC770 /* AppState.swift in Sources */, @@ -505,6 +518,7 @@ 37D4B1B12672A01000C925CA /* DataProvider.swift in Sources */, 37AAF28D2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */, 37D4B1B52672A30700C925CA /* VideoDetailsProvider.swift in Sources */, + 37AAF2A126741C97007FC770 /* SubscriptionsView.swift in Sources */, 37D4B19826717E1500C925CA /* Video.swift in Sources */, 37D4B0E52671614900C925CA /* PearvidiousApp.swift in Sources */, ); @@ -532,6 +546,7 @@ files = ( 37AAF28026737550007FC770 /* SearchView.swift in Sources */, 37D4B19526717CE100C925CA /* PopularVideosProvider.swift in Sources */, + 37AAF29E26741B5F007FC770 /* SubscriptionVideosProvider.swift in Sources */, 37D4B1842671684E00C925CA /* PlayerView.swift in Sources */, 37D4B1802671650A00C925CA /* PearvidiousApp.swift in Sources */, 37D4B1B22672A01000C925CA /* DataProvider.swift in Sources */, @@ -544,6 +559,7 @@ 37AAF28A2673AB89007FC770 /* ChannelView.swift in Sources */, 37AAF28E2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */, 37D4B19926717E1500C925CA /* Video.swift in Sources */, + 37AAF2A226741C97007FC770 /* SubscriptionsView.swift in Sources */, 37D4B1812671653A00C925CA /* ContentView.swift in Sources */, 37AAF2842673791F007FC770 /* SearchedVideosProvider.swift in Sources */, ); diff --git a/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 75672899..15e94d43 100644 --- a/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Pearvidious.xcodeproj/xcuserdata/arek.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -3,54 +3,4 @@ uuid = "E30DA302-B258-4C14-8808-5E4CE238A4FF" type = "1" version = "2.0"> - - - - - - - - - - - - - - diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index 77a3465d..9061d134 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -3,18 +3,22 @@ import SwiftUI struct ContentView: View { @StateObject var state = AppState() - @State var tabSelection: TabSelection = .popular + @State var tabSelection: TabSelection = .subscriptions var body: some View { NavigationView { TabView(selection: $tabSelection) { + SubscriptionsView(state: state, tabSelection: $tabSelection) + .tabItem { Text("Subscriptions") } + .tag(TabSelection.subscriptions) + PopularVideosView(state: state, tabSelection: $tabSelection) .tabItem { Text("Popular") } .tag(TabSelection.popular) if state.showingChannel { ChannelView(state: state, tabSelection: $tabSelection) - .tabItem { Text("\(state.channel!) Channel") } + .tabItem { Text("\(state.channel) Channel") } .tag(TabSelection.channel) } diff --git a/Shared/TabSelection.swift b/Shared/TabSelection.swift index 3d97e7ec..eca44306 100644 --- a/Shared/TabSelection.swift +++ b/Shared/TabSelection.swift @@ -1,5 +1,5 @@ import Foundation enum TabSelection { - case popular, channel, search + case subscriptions, popular, channel, search }