Improve listing

This commit is contained in:
Arkadiusz Fal 2021-06-11 02:05:59 +02:00
parent 1772728cb8
commit 5efb3a798f
7 changed files with 144 additions and 29 deletions

View File

@ -1,14 +1 @@
parent_config: https://raw.githubusercontent.com/sindresorhus/swiftlint-config/main/.swiftlint.yml parent_config: https://raw.githubusercontent.com/sindresorhus/swiftlint-config/main/.swiftlint.yml
excluded:
- .build
identifier_name:
excluded:
- db
- id
- vm
type_name:
excluded:
- VM

View File

@ -5,7 +5,7 @@ class DataProvider: ObservableObject {
static let instance = "https://invidious.home.arekf.net" static let instance = "https://invidious.home.arekf.net"
static func request(_ path: String) -> DataRequest { static func request(_ path: String) -> DataRequest {
return AF.request(apiURLString(path)) AF.request(apiURLString(path))
} }
static func apiURLString(_ path: String) -> String { static func apiURLString(_ path: String) -> String {

View File

@ -2,20 +2,26 @@ import Alamofire
import Foundation import Foundation
import SwiftyJSON import SwiftyJSON
class Video: Identifiable, ObservableObject { final class Video: Identifiable, ObservableObject {
let id: String let id: String
var title: String var title: String
var thumbnailURL: URL var thumbnailURL: URL
var author: String var author: String
var length: TimeInterval
var published: String
var views: Int
@Published var url: URL? @Published var url: URL?
@Published var error: Bool = false @Published var error: Bool = false
init(id: String, title: String, thumbnailURL: URL, author: String) { init(id: String, title: String, thumbnailURL: URL, author: String, length: TimeInterval, published: String, views: Int = 0) {
self.id = id self.id = id
self.title = title self.title = title
self.thumbnailURL = thumbnailURL self.thumbnailURL = thumbnailURL
self.author = author self.author = author
self.length = length
self.published = published
self.views = views
} }
init(_ json: JSON) { init(_ json: JSON) {
@ -23,6 +29,10 @@ class Video: Identifiable, ObservableObject {
title = json["title"].stringValue title = json["title"].stringValue
thumbnailURL = json["videoThumbnails"][0]["url"].url! thumbnailURL = json["videoThumbnails"][0]["url"].url!
author = json["author"].stringValue author = json["author"].stringValue
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
url = formatStreamURL(from: json["formatStreams"].arrayValue) url = formatStreamURL(from: json["formatStreams"].arrayValue)
} }
@ -36,4 +46,33 @@ class Video: Identifiable, ObservableObject {
return stream["url"].url return stream["url"].url
} }
var playTime: String? {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
formatter.zeroFormattingBehavior = [.pad]
return formatter.string(from: length)
}
var viewsCount: String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1
var number: NSNumber
var unit: String
if views < 1_000_000 {
number = NSNumber(value: Double(views) / 1000.0)
unit = "K"
} else {
number = NSNumber(value: Double(views) / 1_000_000.0)
unit = "M"
}
return "\(formatter.string(from: number)!)\(unit)"
}
} }

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "E30DA302-B258-4C14-8808-5E4CE238A4FF"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6F232B4B-8357-4EFC-81EA-3D0D2ADA975C"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/VideoProvider.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "18"
endingLineNumber = "18"
landmarkName = "init()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6611D00B-216F-4D19-8477-50314F55BDD4"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/VideoDetailsProvider.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "19"
endingLineNumber = "19"
landmarkName = "load()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "14CD2BD6-9051-4DD5-9A50-EB8B50C5E6C0"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/PlayerView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "19"
endingLineNumber = "19"
landmarkName = "body"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -1,7 +1,7 @@
import SwiftUI import SwiftUI
struct ContentView: View { struct ContentView: View {
@ObservedObject var popular = PopluarVideosProvider() @ObservedObject private var popular = PopluarVideosProvider()
var items: [GridItem] { var items: [GridItem] {
Array(repeating: .init(.flexible()), count: 4) Array(repeating: .init(.flexible()), count: 4)

View File

@ -3,7 +3,14 @@ import Foundation
import SwiftUI import SwiftUI
struct PlayerView: View { struct PlayerView: View {
@ObservedObject var provider: VideoDetailsProvider @ObservedObject private var provider: VideoDetailsProvider
private var id: String
init(id: String) {
self.id = id
provider = VideoDetailsProvider(id)
}
var body: some View { var body: some View {
ZStack { ZStack {

View File

@ -3,12 +3,12 @@ import URLImage
import URLImageStore import URLImageStore
struct VideoThumbnailView: View { struct VideoThumbnailView: View {
@Environment(\.isFocused) var focused: Bool @Environment(\.isFocused) private var focused: Bool
var video: Video var video: Video
var body: some View { var body: some View {
NavigationLink(destination: PlayerView(provider: VideoDetailsProvider(video.id))) { NavigationLink(destination: PlayerView(id: video.id)) {
HStack(alignment: .top, spacing: 2) { HStack(alignment: .top, spacing: 2) {
// to replace with AsyncImage when it is fixed with lazy views // to replace with AsyncImage when it is fixed with lazy views
URLImage(video.thumbnailURL) { image in URLImage(video.thumbnailURL) { image in
@ -20,17 +20,41 @@ struct VideoThumbnailView: View {
.mask(RoundedRectangle(cornerRadius: 12)) .mask(RoundedRectangle(cornerRadius: 12))
.frame(width: 320, height: 180) .frame(width: 320, height: 180)
VStack(alignment: .leading) { HStack {
Text(video.title) VStack(alignment: .leading) {
.foregroundColor(.primary) Text(video.title)
.bold() .foregroundColor(.primary)
.lineLimit(1) .bold()
.lineLimit(1)
Text(video.author) 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) .foregroundColor(.secondary)
.lineLimit(1) .padding(.top)
}
.padding()
}.padding() Spacer()
HStack(spacing: 8) {
Image(systemName: "clock")
Text(video.playTime ?? "-")
.fontWeight(.bold)
}
.foregroundColor(.secondary)
}
.frame(minHeight: 180)
} }
} }
} }
@ -42,7 +66,9 @@ struct VideoThumbnailView_Previews: PreviewProvider {
id: "A", id: "A",
title: "A very very long text which", title: "A very very long text which",
thumbnailURL: URL(string: "https://invidious.home.arekf.net/vi/yXohcxCKqvo/maxres.jpg")!, thumbnailURL: URL(string: "https://invidious.home.arekf.net/vi/yXohcxCKqvo/maxres.jpg")!,
author: "Bear" author: "Bear",
length: 240,
published: "2 days ago"
)).frame(maxWidth: 350) )).frame(maxWidth: 350)
} }
} }