Simple view display switching

This commit is contained in:
Arkadiusz Fal
2021-06-26 13:37:24 +02:00
parent b336d2c512
commit 15bfaf7497
12 changed files with 74 additions and 32 deletions

View File

@@ -8,15 +8,13 @@ struct PlayerViewController: UIViewControllerRepresentable {
@ObservedObject private var state: PlayerState
@ObservedObject private var profile = Profile()
var video: Video
init(video: Video) {
self.video = video
state = PlayerState(video)
loadStream(video.defaultStreamForProfile(profile), loadBest: profile.defaultStreamResolution == .hd720pFirstThenBest)
loadStream(video.defaultStreamForProfile(state.profile), loadBest: state.profile.defaultStreamResolution == .hd720pFirstThenBest)
}
fileprivate func loadStream(_ stream: Stream?, loadBest: Bool = false) {

View File

@@ -64,8 +64,13 @@ struct PlaylistsView: View {
extension Array where Element: Equatable {
func next(after element: Element) -> Element? {
let idx = firstIndex(of: element)!
let next = index(after: idx)
let idx = firstIndex(of: element)
if idx == nil {
return first
}
let next = index(after: idx!)
return self[next == endIndex ? startIndex : next]
}

View File

@@ -7,11 +7,6 @@ struct PopularVideosView: View {
var body: some View {
VideosView(tabSelection: $tabSelection, videos: videos)
.task {
Task {
provider.load()
}
}
}
var videos: [Video] {

View File

@@ -2,6 +2,7 @@ import SwiftUI
struct SearchView: View {
@ObservedObject private var provider = SearchedVideosProvider()
@EnvironmentObject private var profile: Profile
@EnvironmentObject private var state: AppState
@Binding var tabSelection: TabSelection
@@ -11,6 +12,7 @@ struct SearchView: View {
var body: some View {
VideosView(tabSelection: $tabSelection, videos: videos)
.environmentObject(state)
.environmentObject(profile)
.searchable(text: $query)
}

View File

@@ -1,21 +1,19 @@
import SwiftUI
struct SubscriptionsView: View {
@ObservedObject private var provider = SubscriptionVideosProvider()
@EnvironmentObject private var state: AppState
@Binding var tabSelection: TabSelection
@ObservedObject private var provider = SubscriptionVideosProvider()
var body: some View {
VideosView(tabSelection: $tabSelection, videos: videos)
.task {
Task {
provider.load()
}
}
}
var videos: [Video] {
provider.videos
if provider.videos.isEmpty {
provider.load()
}
return provider.videos
}
}

View File

@@ -1,18 +1,22 @@
import SwiftUI
struct VideosView: View {
@EnvironmentObject private var state: AppState
@EnvironmentObject private var profile: Profile
@Binding var tabSelection: TabSelection
var videos: [Video]
@State private var showingViewOptions = false
var body: some View {
Group {
if state.profile.listing == .list {
Section {
if self.profile.listing == .list {
VideosListView(tabSelection: $tabSelection, videos: videos)
} else {
VideosCellsView(videos: videos, columns: state.profile.cellsColumns)
VideosCellsView(videos: videos, columns: self.profile.cellsColumns)
}
}
.fullScreenCover(isPresented: $showingViewOptions) { ViewOptionsView() }
.onPlayPauseCommand { showingViewOptions.toggle() }
}
}

View File

@@ -0,0 +1,32 @@
import SwiftUI
struct ViewOptionsView: View {
@EnvironmentObject private var profile: Profile
@Environment(\.presentationMode) private var presentationMode
var body: some View {
ZStack {
VisualEffectView(effect: UIBlurEffect(style: .dark))
VStack {
Spacer()
ScrollView(.vertical) {
Button(profile.listing == .list ? "Cells" : "List") {
profile.listing = (profile.listing == .list ? .cells : .list)
presentationMode.wrappedValue.dismiss()
}
Button("Close") {
presentationMode.wrappedValue.dismiss()
}
.frame(width: 800)
}
Spacer()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}