Minor fixes, split files into folders

This commit is contained in:
Arkadiusz Fal
2021-08-20 00:38:31 +02:00
parent c1d9e02475
commit ea634390a6
26 changed files with 101 additions and 46 deletions

View File

@@ -0,0 +1,96 @@
import SwiftUI
struct DetailBadge: View {
enum Style {
case `default`, prominent, outstanding, informational
}
struct StyleModifier: ViewModifier {
let style: Style
func body(content: Content) -> some View {
Group {
switch style {
case .prominent:
content.modifier(ProminentStyleModifier())
case .outstanding:
content.modifier(OutstandingStyleModifier())
case .informational:
content.modifier(InformationalStyleModifier())
default:
content.modifier(DefaultStyleModifier())
}
}
}
}
struct DefaultStyleModifier: ViewModifier {
func body(content: Content) -> some View {
content
.background(.thinMaterial)
}
}
struct ProminentStyleModifier: ViewModifier {
var font: Font {
Font.system(.body).weight(.semibold)
}
func body(content: Content) -> some View {
content
.font(font)
.modifier(DefaultStyleModifier())
}
}
struct OutstandingStyleModifier: ViewModifier {
var backgroundColor: Color {
Color("DetailBadgeOutstandingStyleBackgroundColor")
}
func body(content: Content) -> some View {
content
.textCase(.uppercase)
.background(backgroundColor)
.foregroundColor(.white)
}
}
struct InformationalStyleModifier: ViewModifier {
var backgroundColor: Color {
Color("DetailBadgeInformationalStyleBackgroundColor")
}
func body(content: Content) -> some View {
content
.background(backgroundColor)
.foregroundColor(.white)
}
}
var text: String
var style: Style = .default
var body: some View {
Text(text)
.lineLimit(1)
.truncationMode(.middle)
.padding(10)
.modifier(StyleModifier(style: style))
.mask(RoundedRectangle(cornerRadius: 12))
}
}
struct DetailBadge_Previews: PreviewProvider {
static var previews: some View {
VStack {
DetailBadge(text: "Live", style: .outstanding)
DetailBadge(text: "Premieres", style: .informational)
DetailBadge(text: "Booyah", style: .prominent)
DetailBadge(
text: "Donec in neque mi. Phasellus quis sapien metus. Ut felis ante, posuere."
)
}
.frame(maxWidth: 500)
}
}

View File

@@ -0,0 +1,258 @@
import Defaults
import SwiftUI
struct VideoView: View {
@EnvironmentObject<NavigationState> private var navigationState
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
var video: Video
var layout: ListingLayout
var body: some View {
Button(action: { navigationState.playVideo(video) }) {
VStack {
if layout == .cells {
#if os(iOS)
if verticalSizeClass == .compact {
horizontalRow
.padding(.vertical, 4)
} else {
verticalRow
}
#else
verticalRow
#endif
} else {
horizontalRow
}
}
#if os(macOS)
.background()
#endif
}
.modifier(ButtonStyleModifier(layout: layout))
.contentShape(RoundedRectangle(cornerRadius: 12))
.contextMenu { VideoContextMenuView(video: video) }
}
var horizontalRow: some View {
HStack(alignment: .top, spacing: 2) {
Section {
#if os(tvOS)
thumbnailImage(quality: .medium)
#else
thumbnail
#endif
}
.frame(maxWidth: 320)
VStack(alignment: .leading, spacing: 0) {
videoDetail(video.title)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
videoDetail(video.author)
Spacer()
additionalDetails
}
.padding()
.frame(minHeight: 180)
#if os(tvOS)
if video.playTime != nil || video.live || video.upcoming {
Spacer()
VStack(alignment: .center) {
Spacer()
if let time = video.playTime {
HStack(spacing: 4) {
Image(systemName: "clock")
Text(time)
.fontWeight(.bold)
}
.foregroundColor(.secondary)
} else if video.live {
DetailBadge(text: "Live", style: .outstanding)
} else if video.upcoming {
DetailBadge(text: "Upcoming", style: .informational)
}
Spacer()
}
}
#endif
}
.padding(.trailing)
}
var verticalRow: some View {
VStack(alignment: .leading) {
thumbnail
VStack(alignment: .leading) {
videoDetail(video.title, lineLimit: additionalDetailsAvailable ? 2 : 3)
#if os(tvOS)
.frame(minHeight: additionalDetailsAvailable ? 80 : 120, alignment: .top)
#elseif os(macOS)
.frame(minHeight: 30, alignment: .top)
#else
.frame(minHeight: 50, alignment: .top)
#endif
.padding(.bottom)
if additionalDetailsAvailable {
additionalDetails
.padding(.bottom, 10)
} else {
Spacer()
}
}
#if os(tvOS)
.padding(.horizontal, 8)
#endif
}
}
var additionalDetailsAvailable: Bool {
video.publishedDate != nil || video.views != 0
}
var additionalDetails: some View {
HStack(spacing: 8) {
if let date = video.publishedDate {
Image(systemName: "calendar")
Text(date)
}
if video.views != 0 {
Image(systemName: "eye")
Text(video.viewsCount)
}
}
.foregroundColor(.secondary)
}
var thumbnail: some View {
ZStack(alignment: .leading) {
thumbnailImage(quality: .maxres)
VStack {
HStack(alignment: .top) {
if video.live {
DetailBadge(text: "Live", style: .outstanding)
} else if video.upcoming {
DetailBadge(text: "Upcoming", style: .informational)
}
Spacer()
DetailBadge(text: video.author, style: .prominent)
}
.padding(10)
Spacer()
HStack(alignment: .top) {
Spacer()
if let time = video.playTime {
DetailBadge(text: time, style: .prominent)
}
}
.padding(10)
}
}
.padding([.leading, .top, .trailing], 4)
.frame(maxWidth: 600)
}
func thumbnailImage(quality: Thumbnail.Quality) -> some View {
Group {
if let url = video.thumbnailURL(quality: quality) {
AsyncImage(url: url) { image in
image
.resizable()
} placeholder: {
ProgressView()
.aspectRatio(contentMode: .fill)
}
.mask(RoundedRectangle(cornerRadius: 12))
} else {
Image(systemName: "exclamationmark.square")
}
}
.frame(minWidth: 320, maxWidth: .infinity, minHeight: 180, maxHeight: .infinity)
#if os(tvOS)
.frame(minHeight: layout == .cells ? 320 : 200)
#endif
.aspectRatio(1.777, contentMode: .fit)
}
func videoDetail(_ text: String, lineLimit: Int = 1) -> some View {
Text(text)
.fontWeight(.bold)
.lineLimit(lineLimit)
.truncationMode(.middle)
}
struct ButtonStyleModifier: ViewModifier {
var layout: ListingLayout
func body(content: Content) -> some View {
Section {
#if os(tvOS)
if layout == .cells {
content.buttonStyle(.plain)
} else {
content
}
#else
content.buttonStyle(.plain)
#endif
}
}
}
}
struct VideoListRowPreview: PreviewProvider {
static var previews: some View {
#if os(tvOS)
List {
ForEach(Video.allFixtures) { video in
VideoView(video: video, layout: .list)
}
}
.listStyle(GroupedListStyle())
HStack {
ForEach(Video.allFixtures) { video in
VideoView(video: video, layout: .cells)
}
}
.frame(maxHeight: 600)
#else
List {
ForEach(Video.allFixtures) { video in
VideoView(video: video, layout: .list)
}
}
#if os(macOS)
.frame(minHeight: 800)
#endif
#if os(iOS)
List {
ForEach(Video.allFixtures) { video in
VideoView(video: video, layout: .list)
}
}
.previewInterfaceOrientation(.landscapeRight)
#endif
#endif
}
}

View File

@@ -0,0 +1,77 @@
import Defaults
import SwiftUI
struct VideosCellsView: View {
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
var videos = [Video]()
var body: some View {
ScrollViewReader { scrollView in
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
LazyVGrid(columns: items, alignment: .center) {
ForEach(videos) { video in
VideoView(video: video, layout: .cells)
#if os(tvOS)
.padding(.horizontal)
#endif
}
}
.padding()
}
.onChange(of: videos) { [videos] newVideos in
#if !os(tvOS)
guard !videos.isEmpty, let video = newVideos.first else {
return
}
scrollView.scrollTo(video.id, anchor: .top)
#endif
}
#if os(tvOS)
.padding(.horizontal, 10)
#endif
}
.edgesIgnoringSafeArea(.horizontal)
}
var items: [GridItem] {
#if os(tvOS)
videos.count < 3 ? Array(repeating: GridItem(.fixed(540)), count: [videos.count, 1].max()!) : adaptiveItem
#else
adaptiveItem
#endif
}
var adaptiveItem: [GridItem] {
[GridItem(.adaptive(minimum: adaptiveGridItemMinimumSize))]
}
var adaptiveGridItemMinimumSize: CGFloat {
#if os(iOS)
return verticalSizeClass == .regular ? 320 : 800
#elseif os(tvOS)
return 540
#else
return 320
#endif
}
var scrollViewShowsIndicators: Bool {
#if !os(tvOS)
true
#else
false
#endif
}
}
struct VideoCellsView_Previews: PreviewProvider {
static var previews: some View {
VideosView(videos: Video.allFixtures)
.frame(minWidth: 1000)
.environmentObject(NavigationState())
}
}

View File

@@ -0,0 +1,36 @@
import Defaults
import SwiftUI
struct VideosListView: View {
var videos: [Video]
var body: some View {
Section {
ScrollViewReader { scrollView in
List {
ForEach(videos) { video in
VideoView(video: video, layout: .list)
.listRowInsets(EdgeInsets())
}
.onChange(of: videos) { videos in
#if !os(tvOS)
guard let video = videos.first else {
return
}
scrollView.scrollTo(video.id, anchor: .top)
#endif
}
}
}
.listStyle(GroupedListStyle())
}
}
}
struct VideosListView_Previews: PreviewProvider {
static var previews: some View {
VideosListView(videos: Video.allFixtures)
}
}

View File

@@ -0,0 +1,32 @@
import Defaults
import SwiftUI
struct VideosView: View {
@EnvironmentObject<NavigationState> private var navigationState
@State private var profile = Profile()
#if os(tvOS)
@Default(.layout) private var layout
#endif
var videos: [Video]
var body: some View {
VStack {
#if os(tvOS)
if layout == .cells {
VideosCellsView(videos: videos)
} else {
VideosListView(videos: videos)
}
#else
VideosCellsView(videos: videos)
#endif
}
#if os(macOS)
.background()
.frame(minWidth: 360)
#endif
}
}