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

@@ -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") }
}
}
}

View File

@@ -1,68 +0,0 @@
import AVKit
import Foundation
import SwiftUI
struct PlayerView: View {
@ObservedObject private var provider: VideoDetailsProvider
private var id: String
init(id: String) {
self.id = id
provider = VideoDetailsProvider(id)
}
var body: some View {
ZStack {
if let video = provider.video {
if video.url != nil {
PlayerViewController(video)
.edgesIgnoringSafeArea(.all)
}
if video.error {
Text("Video can not be loaded")
}
}
}
.task {
async {
provider.load()
}
}
}
}
struct PlayerViewController: UIViewControllerRepresentable {
var video: Video
init(_ video: Video) {
self.video = video
}
private var player: AVPlayer {
let item = AVPlayerItem(url: video.url!)
item.externalMetadata = [makeMetadataItem(.commonIdentifierTitle, value: video.title)]
return AVPlayer(playerItem: item)
}
private func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
let item = AVMutableMetadataItem()
item.identifier = identifier
item.value = value as? NSCopying & NSObjectProtocol
item.extendedLanguageTag = "und"
return item.copy() as! AVMetadataItem
}
func makeUIViewController(context _: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.modalPresentationStyle = .fullScreen
controller.player = player
controller.title = video.title
controller.player?.play()
return controller
}
func updateUIViewController(_: AVPlayerViewController, context _: Context) {}
}

View File

@@ -1,74 +0,0 @@
import SwiftUI
import URLImage
import URLImageStore
struct VideoThumbnailView: View {
@Environment(\.isFocused) private var focused: Bool
var video: Video
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)
.frame(width: 320, height: 180)
}
.mask(RoundedRectangle(cornerRadius: 12))
.frame(width: 320, height: 180)
HStack {
VStack(alignment: .leading) {
Text(video.title)
.foregroundColor(.primary)
.bold()
.lineLimit(1)
Text("\(video.author)")
.foregroundColor(.secondary)
.bold()
.lineLimit(1)
HStack(spacing: 8) {
Image(systemName: "calendar")
Text(video.published)
Image(systemName: "eye")
Text(video.viewsCount)
}
.foregroundColor(.secondary)
.padding(.top)
}
.padding()
Spacer()
HStack(spacing: 8) {
Image(systemName: "clock")
Text(video.playTime ?? "-")
.fontWeight(.bold)
}
.foregroundColor(.secondary)
}
.frame(minHeight: 180)
}
}
}
}
struct VideoThumbnailView_Previews: PreviewProvider {
static var previews: some View {
VideoThumbnailView(video: Video(
id: "A",
title: "A very very long text which",
thumbnailURL: URL(string: "https://invidious.home.arekf.net/vi/yXohcxCKqvo/maxres.jpg")!,
author: "Bear",
length: 240,
published: "2 days ago"
)).frame(maxWidth: 350)
}
}