Popular videos, playing from mp4

This commit is contained in:
Arkadiusz Fal
2021-06-11 00:50:10 +02:00
parent d488ee6679
commit 1772728cb8
17 changed files with 454 additions and 20 deletions

View File

@@ -1,16 +1,32 @@
//
// ContentView.swift
// Shared
//
// Created by Arkadiusz Fal on 09/06/2021.
//
import SwiftUI
struct ContentView: View {
@ObservedObject var popular = PopluarVideosProvider()
var items: [GridItem] {
Array(repeating: .init(.flexible()), count: 4)
}
var body: some View {
Text("Hello, world!")
.padding()
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()
}
}
}
}

View File

@@ -1,17 +1,15 @@
//
// PearvidiousApp.swift
// Shared
//
// Created by Arkadiusz Fal on 09/06/2021.
//
import SwiftUI
import URLImage
import URLImageStore
@main
struct PearvidiousApp: App {
var body: some Scene {
let urlImageService = URLImageService(fileStore: URLImageFileStore(),
inMemoryStore: URLImageInMemoryStore())
WindowGroup {
ContentView()
.environment(\.urlImageService, urlImageService)
}
}
}

61
Shared/PlayerView.swift Normal file
View File

@@ -0,0 +1,61 @@
import AVKit
import Foundation
import SwiftUI
struct PlayerView: View {
@ObservedObject var provider: VideoDetailsProvider
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

@@ -0,0 +1,48 @@
import SwiftUI
import URLImage
import URLImageStore
struct VideoThumbnailView: View {
@Environment(\.isFocused) var focused: Bool
var video: Video
var body: some View {
NavigationLink(destination: PlayerView(provider: VideoDetailsProvider(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)
VStack(alignment: .leading) {
Text(video.title)
.foregroundColor(.primary)
.bold()
.lineLimit(1)
Text(video.author)
.foregroundColor(.secondary)
.lineLimit(1)
}.padding()
}
}
}
}
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"
)).frame(maxWidth: 350)
}
}