Channel view

This commit is contained in:
Arkadiusz Fal 2021-06-11 23:11:59 +02:00
parent 4bc70f351d
commit 314c3b4968
13 changed files with 205 additions and 45 deletions

View File

@ -0,0 +1,52 @@
import SwiftUI
struct ChannelView: View {
@ObservedObject private var provider = ChannelVideosProvider()
@ObservedObject var state: AppState
@Binding var tabSelection: TabSelection
var body: some View {
Group {
List {
ForEach(videos) { video in
VideoThumbnailView(video: video)
.contextMenu {
Button("Close \(video.author) channel", action: {
state.closeChannel()
tabSelection = .popular
})
}
.listRowInsets(listRowInsets)
}
}
.listStyle(GroupedListStyle())
}
.task {
async {
provider.load()
}
}
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
}
var videos: [Video] {
if state.channelID != provider.channelID {
provider.videos = []
provider.channelID = state.channelID
provider.load()
}
return provider.videos
}
}
//
// struct ChannelView_Previews: PreviewProvider {
// static var previews: some View {
// ChannelView()
// }
// }

View File

@ -1,14 +1,21 @@
import Foundation
import SwiftUI
struct PopularVideosView: View {
@ObservedObject private var popular = PopluarVideosProvider()
@ObservedObject private var provider = PopularVideosProvider()
@ObservedObject var state: AppState
@Binding var tabSelection: TabSelection
var body: some View {
Group {
List {
ForEach(popular.videos) { video in
ForEach(provider.videos) { video in
VideoThumbnailView(video: video)
.contextMenu {
Button("\(video.author) Channel", action: {
state.setChannel(from: video)
tabSelection = .channel
})
}
.listRowInsets(listRowInsets)
}
}
@ -16,7 +23,7 @@ struct PopularVideosView: View {
}
.task {
async {
popular.load()
provider.load()
}
}
}

View File

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

View File

@ -1,10 +1,9 @@
import SwiftUI
struct SearchedVideosView: View {
@ObservedObject var provider: SearchedVideosProvider
@Binding var query: String
@ObservedObject var provider = SearchedVideosProvider()
@Environment(\.isSearching) var isSearching
@Binding var query: String
var body: some View {
Group {
@ -27,11 +26,12 @@ struct SearchedVideosView: View {
if let url = URLComponents(string: query),
let queryItem = url.queryItems?.first(where: { item in item.name == "v" }),
let id = queryItem.value {
let id = queryItem.value
{
newQuery = id
}
if (newQuery != provider.query) {
if newQuery != provider.query {
provider.query = newQuery
provider.load()
}
@ -40,8 +40,8 @@ struct SearchedVideosView: View {
}
}
//struct SearchedVideosView_Previews: PreviewProvider {
// struct SearchedVideosView_Previews: PreviewProvider {
// static var previews: some View {
// SearchedVideosView()
// }
//}
// }

View File

@ -10,14 +10,21 @@ struct VideoThumbnailView: View {
var body: some View {
NavigationLink(destination: PlayerView(id: video.id)) {
HStack(alignment: .top, spacing: 2) {
// to replace with AsyncImage when it is fixed with lazy views
URLImage(video.thumbnailURL) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
Section {
if let thumbnail = video.thumbnailURL {
// to replace with AsyncImage when it is fixed with lazy views
URLImage(thumbnail) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 320, height: 180)
}
.mask(RoundedRectangle(cornerRadius: 12))
.frame(width: 320, height: 180)
} else {
Image(systemName: "exclamationmark.square")
}
}
.mask(RoundedRectangle(cornerRadius: 12))
.frame(width: 320, height: 180)
HStack {
@ -68,7 +75,8 @@ struct VideoThumbnailView_Previews: PreviewProvider {
thumbnailURL: URL(string: "https://invidious.home.arekf.net/vi/yXohcxCKqvo/maxres.jpg")!,
author: "Bear",
length: 240,
published: "2 days ago"
published: "2 days ago",
channelID: ""
)).frame(maxWidth: 350)
}
}

19
Model/AppState.swift Normal file
View File

@ -0,0 +1,19 @@
import Foundation
class AppState: ObservableObject {
@Published var showingChannel = false
@Published var channelID: String?
@Published var channel: String?
func setChannel(from video: Video) {
channel = video.author
channelID = video.channelID
showingChannel = true
}
func closeChannel() {
showingChannel = false
channel = nil
channelID = nil
}
}

View File

@ -0,0 +1,24 @@
import Foundation
import SwiftyJSON
class ChannelVideosProvider: DataProvider {
@Published var videos = [Video]()
var channelID: String? = ""
func load() {
guard channelID != nil else { return }
let searchPath = "channels/\(channelID!)"
DataProvider.request(searchPath).responseJSON { response in
switch response.result {
case let .success(value):
if let channelVideos = JSON(value).dictionaryValue["latestVideos"] {
self.videos = channelVideos.arrayValue.map { Video($0) }
}
case let .failure(error):
print(error)
}
}
}
}

View File

@ -2,7 +2,7 @@ import Alamofire
import Foundation
import SwiftyJSON
final class PopluarVideosProvider: DataProvider {
final class PopularVideosProvider: DataProvider {
@Published var videos = [Video]()
func load() {

View File

@ -3,6 +3,7 @@ import SwiftyJSON
class SearchedVideosProvider: DataProvider {
@Published var videos = [Video]()
var query: String = ""
func load() {

View File

@ -5,32 +5,36 @@ import SwiftyJSON
final class Video: Identifiable, ObservableObject {
let id: String
var title: String
var thumbnailURL: URL
var thumbnailURL: URL?
var author: String
var length: TimeInterval
var published: String
var views: Int
var channelID: String
@Published var url: URL?
@Published var error: Bool = false
init(id: String, title: String, thumbnailURL: URL, author: String, length: TimeInterval, published: String, views: Int = 0) {
init(id: String, title: String, thumbnailURL: URL?, author: String, length: TimeInterval, published: String, channelID: String, views: Int = 0) {
self.id = id
self.title = title
self.thumbnailURL = thumbnailURL
self.author = author
self.length = length
self.published = published
self.channelID = channelID
self.views = views
}
init(_ json: JSON) {
func extractThumbnailURL(from details: JSON) -> URL {
func extractThumbnailURL(from details: JSON) -> URL? {
if details["videoThumbnails"].arrayValue.isEmpty {
return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")!
return nil
}
return details["videoThumbnails"][0]["url"].url!
let thumbnail = details["videoThumbnails"].arrayValue.first(where: { $0["quality"].stringValue == "medium" })!
return thumbnail["url"].url!
}
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
@ -51,6 +55,7 @@ final class Video: Identifiable, ObservableObject {
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
channelID = json["authorId"].stringValue
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
}

View File

@ -13,6 +13,16 @@
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 */; };
37AAF28A2673AB89007FC770 /* ChannelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2892673AB89007FC770 /* ChannelView.swift */; };
37AAF28C2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */; };
37AAF28D2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */; };
37AAF28E2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */; };
37AAF29026740715007FC770 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28F26740715007FC770 /* AppState.swift */; };
37AAF29126740715007FC770 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28F26740715007FC770 /* AppState.swift */; };
37AAF29226740715007FC770 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28F26740715007FC770 /* AppState.swift */; };
37AAF2942674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; };
37AAF2952674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; };
37AAF2962674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.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 */; };
@ -75,6 +85,10 @@
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>"; };
37AAF2892673AB89007FC770 /* ChannelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelView.swift; sourceTree = "<group>"; };
37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelVideosProvider.swift; sourceTree = "<group>"; };
37AAF28F26740715007FC770 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
37AAF2932674086B007FC770 /* TabSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabSelection.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>"; };
@ -164,6 +178,7 @@
isa = PBXGroup;
children = (
37D4B0C32671614700C925CA /* ContentView.swift */,
37AAF2932674086B007FC770 /* TabSelection.swift */,
37D4B0C22671614700C925CA /* PearvidiousApp.swift */,
37D4B0C42671614800C925CA /* Assets.xcassets */,
);
@ -202,11 +217,12 @@
37D4B159267164AE00C925CA /* Apple TV */ = {
isa = PBXGroup;
children = (
37AAF27D26737323007FC770 /* PopularVideosView.swift */,
37AAF2892673AB89007FC770 /* ChannelView.swift */,
37AAF27F26737550007FC770 /* SearchView.swift */,
37AAF28726737A13007FC770 /* SearchedVideosView.swift */,
37D4B1822671681B00C925CA /* PlayerView.swift */,
37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */,
37AAF27D26737323007FC770 /* PopularVideosView.swift */,
37AAF28726737A13007FC770 /* SearchedVideosView.swift */,
37AAF27F26737550007FC770 /* SearchView.swift */,
37D4B1AE26729DEB00C925CA /* Info.plist */,
37D4B15E267164AF00C925CA /* Assets.xcassets */,
);
@ -224,11 +240,13 @@
37D4B1B72672CFE300C925CA /* Model */ = {
isa = PBXGroup;
children = (
37AAF28F26740715007FC770 /* AppState.swift */,
37D4B19626717E1500C925CA /* Video.swift */,
37D4B19226717CE100C925CA /* PopularVideosProvider.swift */,
37D4B1B32672A30700C925CA /* VideoDetailsProvider.swift */,
37D4B1AF2672A01000C925CA /* DataProvider.swift */,
37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */,
37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */,
);
path = Model;
sourceTree = "<group>";
@ -465,7 +483,10 @@
37D4B19326717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B0E62671614900C925CA /* ContentView.swift in Sources */,
37AAF2822673791F007FC770 /* SearchedVideosProvider.swift in Sources */,
37AAF29026740715007FC770 /* AppState.swift in Sources */,
37AAF2942674086B007FC770 /* TabSelection.swift in Sources */,
37D4B1B02672A01000C925CA /* DataProvider.swift in Sources */,
37AAF28C2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */,
37D4B1B42672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37D4B19726717E1500C925CA /* Video.swift in Sources */,
37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */,
@ -479,7 +500,10 @@
37D4B19426717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B0E72671614900C925CA /* ContentView.swift in Sources */,
37AAF2832673791F007FC770 /* SearchedVideosProvider.swift in Sources */,
37AAF29126740715007FC770 /* AppState.swift in Sources */,
37AAF2952674086B007FC770 /* TabSelection.swift in Sources */,
37D4B1B12672A01000C925CA /* DataProvider.swift in Sources */,
37AAF28D2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */,
37D4B1B52672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37D4B19826717E1500C925CA /* Video.swift in Sources */,
37D4B0E52671614900C925CA /* PearvidiousApp.swift in Sources */,
@ -512,9 +536,13 @@
37AAF28826737A13007FC770 /* SearchedVideosView.swift in Sources */,
37D4B1802671650A00C925CA /* PearvidiousApp.swift in Sources */,
37D4B1B22672A01000C925CA /* DataProvider.swift in Sources */,
37AAF29226740715007FC770 /* AppState.swift in Sources */,
37D4B18E26717B3800C925CA /* VideoThumbnailView.swift in Sources */,
37D4B1B62672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37AAF27E26737323007FC770 /* PopularVideosView.swift in Sources */,
37AAF2962674086B007FC770 /* TabSelection.swift in Sources */,
37AAF28A2673AB89007FC770 /* ChannelView.swift in Sources */,
37AAF28E2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */,
37D4B19926717E1500C925CA /* Video.swift in Sources */,
37D4B1812671653A00C925CA /* ContentView.swift in Sources */,
37AAF2842673791F007FC770 /* SearchedVideosProvider.swift in Sources */,

View File

@ -1,14 +1,26 @@
import SwiftUI
struct ContentView: View {
@StateObject var state = AppState()
@State var tabSelection: TabSelection = .popular
var body: some View {
NavigationView {
TabView {
PopularVideosView()
TabView(selection: $tabSelection) {
PopularVideosView(state: state, tabSelection: $tabSelection)
.tabItem { Text("Popular") }
.tag(TabSelection.popular)
if state.showingChannel {
ChannelView(state: state, tabSelection: $tabSelection)
.tabItem { Text("\(state.channel!) Channel") }
.tag(TabSelection.channel)
}
SearchView()
.tabItem { Image(systemName: "magnifyingglass") }
.tag(TabSelection.search)
}
}
}

View File

@ -0,0 +1,5 @@
import Foundation
enum TabSelection {
case popular, channel, search
}