mirror of
https://github.com/yattee/yattee.git
synced 2025-08-08 19:54:07 +00:00
Add to playlist from video player, state fixes
This commit is contained in:
@@ -21,6 +21,7 @@ extension Defaults.Keys {
|
||||
])
|
||||
static let lastAccountID = Key<Account.ID?>("lastAccountID")
|
||||
static let lastInstanceID = Key<Instance.ID?>("lastInstanceID")
|
||||
static let lastUsedPlaylistID = Key<Playlist.ID?>("lastPlaylistID")
|
||||
|
||||
static let sponsorBlockInstance = Key<String>("sponsorBlockInstance", default: "https://sponsor.ajay.app")
|
||||
static let sponsorBlockCategories = Key<Set<String>>("sponsorBlockCategories", default: Set(SponsorBlockAPI.categories))
|
||||
|
@@ -59,6 +59,7 @@ struct ContentView: View {
|
||||
.environmentObject(instances)
|
||||
.environmentObject(navigation)
|
||||
.environmentObject(player)
|
||||
.environmentObject(playlists)
|
||||
.environmentObject(subscriptions)
|
||||
.environmentObject(thumbnailsModel)
|
||||
}
|
||||
@@ -70,6 +71,7 @@ struct ContentView: View {
|
||||
.environmentObject(instances)
|
||||
.environmentObject(navigation)
|
||||
.environmentObject(player)
|
||||
.environmentObject(playlists)
|
||||
.environmentObject(subscriptions)
|
||||
.environmentObject(thumbnailsModel)
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ struct VideoDetails: View {
|
||||
|
||||
@State private var subscribed = false
|
||||
@State private var confirmationShown = false
|
||||
@State private var presentingAddToPlaylist = false
|
||||
|
||||
@State private var currentPage = Page.details
|
||||
|
||||
@@ -18,6 +19,7 @@ struct VideoDetails: View {
|
||||
|
||||
@EnvironmentObject<AccountsModel> private var accounts
|
||||
@EnvironmentObject<PlayerModel> private var player
|
||||
@EnvironmentObject<PlaylistsModel> private var playlists
|
||||
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
||||
|
||||
init(
|
||||
@@ -266,11 +268,25 @@ struct VideoDetails: View {
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button {
|
||||
presentingAddToPlaylist = true
|
||||
} label: {
|
||||
Label("Add to Playlist", systemImage: "text.badge.plus")
|
||||
.labelStyle(.iconOnly)
|
||||
.help("Add to Playlist...")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
.frame(maxHeight: 35)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $presentingAddToPlaylist) {
|
||||
if let video = video {
|
||||
AddToPlaylistView(video: video)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var detailsPage: some View {
|
||||
@@ -351,7 +367,7 @@ struct VideoDetails: View {
|
||||
|
||||
struct VideoDetails_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
VideoDetails(sidebarQueue: .constant(false))
|
||||
VideoDetails(sidebarQueue: .constant(true))
|
||||
.injectFixtureEnvironmentObjects()
|
||||
}
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ struct AddToPlaylistView: View {
|
||||
}
|
||||
.onAppear {
|
||||
model.load {
|
||||
if let playlist = model.all.first {
|
||||
if let playlist = model.find(id: Defaults[.lastUsedPlaylistID]) ?? model.all.first {
|
||||
selectedPlaylistID = playlist.id
|
||||
}
|
||||
}
|
||||
@@ -117,22 +117,22 @@ struct AddToPlaylistView: View {
|
||||
HStack {
|
||||
Spacer()
|
||||
Button("Add to Playlist", action: addToPlaylist)
|
||||
.disabled(selectedPlaylist.isNil)
|
||||
.padding(.top, 30)
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.defaultAction)
|
||||
#endif
|
||||
.disabled(currentPlaylist.isNil)
|
||||
.padding(.top, 30)
|
||||
}
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
private var selectPlaylistButton: some View {
|
||||
Button(currentPlaylist?.title ?? "Select playlist") {
|
||||
guard currentPlaylist != nil else {
|
||||
Button(selectedPlaylist?.title ?? "Select playlist") {
|
||||
guard selectedPlaylist != nil else {
|
||||
return
|
||||
}
|
||||
|
||||
selectedPlaylistID = model.all.next(after: currentPlaylist!)!.id
|
||||
selectedPlaylistID = model.all.next(after: selectedPlaylist!)!.id
|
||||
}
|
||||
.contextMenu {
|
||||
ForEach(model.all) { playlist in
|
||||
@@ -146,16 +146,18 @@ struct AddToPlaylistView: View {
|
||||
}
|
||||
|
||||
private func addToPlaylist() {
|
||||
guard currentPlaylist != nil else {
|
||||
guard let id = selectedPlaylist?.id else {
|
||||
return
|
||||
}
|
||||
|
||||
model.addVideo(playlistID: currentPlaylist!.id, videoID: video.videoID) {
|
||||
Defaults[.lastUsedPlaylistID] = id
|
||||
|
||||
model.addVideo(playlistID: id, videoID: video.videoID) {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private var currentPlaylist: Playlist? {
|
||||
private var selectedPlaylist: Playlist? {
|
||||
model.find(id: selectedPlaylistID) ?? model.all.first
|
||||
}
|
||||
}
|
||||
|
40
Shared/Throttle.swift
Normal file
40
Shared/Throttle.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
import Foundation
|
||||
|
||||
final class Throttle {
|
||||
let interval: TimeInterval
|
||||
private(set) var lastExecutedAt: Date?
|
||||
|
||||
private let syncQueue = DispatchQueue(label: "net.yatee.app.throttle")
|
||||
|
||||
init(interval: TimeInterval) {
|
||||
self.interval = interval
|
||||
}
|
||||
|
||||
@discardableResult func execute(_ action: () -> Void) -> Bool {
|
||||
let executed = syncQueue.sync { () -> Bool in
|
||||
let now = Date()
|
||||
|
||||
let timeInterval = now.timeIntervalSince(lastExecutedAt ?? .distantPast)
|
||||
|
||||
if timeInterval > interval {
|
||||
lastExecutedAt = now
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if executed {
|
||||
action()
|
||||
}
|
||||
|
||||
return executed
|
||||
}
|
||||
|
||||
func reset() {
|
||||
syncQueue.sync {
|
||||
lastExecutedAt = nil
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user