From 7fcfdb373bed3403beb0debffaf8ec74e775649a Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 19 Jun 2022 01:42:27 -0700 Subject: [PATCH] [Playback Status] Remember last play position Remember last play position in the playlist, rather than using an index variable to store its position and play time. Still store whether the player was last playing in a configuration variable, though. Signed-off-by: Christopher Snowhill --- Application/AppController.m | 44 +++++++++++++++++++------------- Application/PlaybackController.m | 35 ++++++++++++++++--------- Playlist/PlaylistController.m | 26 ++++++++++++++++--- 3 files changed, 72 insertions(+), 33 deletions(-) diff --git a/Application/AppController.m b/Application/AppController.m index 58a6201a4..026c245d6 100644 --- a/Application/AppController.m +++ b/Application/AppController.m @@ -36,6 +36,8 @@ void *kAppControllerContext = &kAppControllerContext; +BOOL kAppControllerShuttingDown = NO; + @implementation AppController { BOOL _isFullToolbarStyle; } @@ -219,12 +221,29 @@ void *kAppControllerContext = &kAppControllerContext; [[playlistController undoManager] enableUndoRegistration]; - if([[NSUserDefaults standardUserDefaults] boolForKey:@"resumePlaybackOnStartup"]) { - int lastStatus = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"lastPlaybackStatus"]; - int lastIndex = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"lastTrackPlaying"]; + int lastStatus = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"lastPlaybackStatus"]; - if(lastStatus != CogStatusStopped && lastIndex >= 0) { - [playbackController playEntryAtIndex:lastIndex startPaused:(lastStatus == CogStatusPaused) andSeekTo:@([[NSUserDefaults standardUserDefaults] doubleForKey:@"lastTrackPosition"])]; + if(lastStatus != CogStatusStopped) { + NSPredicate *deletedPredicate = [NSPredicate predicateWithFormat:@"deLeted == NO || deLeted == nil"]; + NSPredicate *currentPredicate = [NSPredicate predicateWithFormat:@"current == YES"]; + + NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[deletedPredicate, currentPredicate]]; + + NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"]; + request.predicate = predicate; + + NSError *error = nil; + NSArray *results = [playlistController.persistentContainer.viewContext executeFetchRequest:request error:&error]; + + if(results && [results count] == 1) { + PlaylistEntry *pe = results[0]; + if([[NSUserDefaults standardUserDefaults] boolForKey:@"resumePlaybackOnStartup"]) { + [playbackController playEntryAtIndex:pe.index startPaused:(lastStatus == CogStatusPaused) andSeekTo:@(pe.currentPosition)]; + } else { + pe.current = NO; + pe.currentPosition = 0.0; + [playlistController commitPersistentStore]; + } } } @@ -387,22 +406,13 @@ void *kAppControllerContext = &kAppControllerContext; } - (void)applicationWillTerminate:(NSNotification *)aNotification { + kAppControllerShuttingDown = YES; + CogStatus currentStatus = [playbackController playbackStatus]; - NSInteger lastTrackPlaying = -1; - double lastTrackPosition = 0; if(currentStatus == CogStatusStopping) currentStatus = CogStatusStopped; - if(currentStatus != CogStatusStopped) { - PlaylistEntry *pe = [playlistController currentEntry]; - lastTrackPlaying = [pe index]; - lastTrackPosition = [pe currentPosition]; - } - - [[NSUserDefaults standardUserDefaults] setInteger:lastTrackPlaying forKey:@"lastTrackPlaying"]; - [[NSUserDefaults standardUserDefaults] setDouble:lastTrackPosition forKey:@"lastTrackPosition"]; - [playbackController stop:self]; [[NSUserDefaults standardUserDefaults] setInteger:currentStatus forKey:@"lastPlaybackStatus"]; @@ -546,8 +556,6 @@ void *kAppControllerContext = &kAppControllerContext; [userDefaultsValuesDict setObject:@"cubic" forKey:@"resampling"]; [userDefaultsValuesDict setObject:@(CogStatusStopped) forKey:@"lastPlaybackStatus"]; - [userDefaultsValuesDict setObject:@(-1) forKey:@"lastTrackPlaying"]; - [userDefaultsValuesDict setObject:@(0.0) forKey:@"lastTrackPosition"]; [userDefaultsValuesDict setObject:@"dls appl" forKey:@"midiPlugin"]; diff --git a/Application/PlaybackController.m b/Application/PlaybackController.m index ea3f5fbaf..45b119944 100644 --- a/Application/PlaybackController.m +++ b/Application/PlaybackController.m @@ -21,6 +21,8 @@ @import Firebase; +extern BOOL kAppControllerShuttingDown; + @implementation PlaybackController #define DEFAULT_SEEK 5 @@ -257,7 +259,10 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { [self setPosition:pos]; - [[playlistController currentEntry] setCurrentPosition:pos]; + if(!kAppControllerShuttingDown) { + PlaylistEntry *pe = [playlistController currentEntry]; + if(pe) pe.currentPosition = pos; + } } - (IBAction)seek:(id)sender { @@ -269,7 +274,10 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { [self setPosition:time]; - [[playlistController currentEntry] setCurrentPosition:time]; + if(!kAppControllerShuttingDown) { + PlaylistEntry *pe = [playlistController currentEntry]; + if(pe) pe.currentPosition = time; + } } - (IBAction)seek:(id)sender toTime:(NSTimeInterval)position { @@ -281,7 +289,10 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { [self setPosition:time]; - [[playlistController currentEntry] setCurrentPosition:time]; + if(!kAppControllerShuttingDown) { + PlaylistEntry *pe = [playlistController currentEntry]; + if(pe) pe.currentPosition = time; + } } - (IBAction)spam:(id)sender { @@ -744,23 +755,23 @@ NSDictionary *makeRGInfo(PlaylistEntry *pe) { } - (void)setPosition:(double)p { - if(p > lastPosition && (p - lastPosition) >= 10.0) { - PlaylistEntry *pe = [playlistController currentEntry]; - NSInteger lastTrackPlaying = [pe index]; + position = p; + if(kAppControllerShuttingDown) return; + + PlaylistEntry *pe = [playlistController currentEntry]; + if(pe) pe.currentPosition = p; + + if(p > lastPosition && (p - lastPosition) >= 10.0) { [[NSUserDefaults standardUserDefaults] setInteger:CogStatusPlaying forKey:@"lastPlaybackStatus"]; - [[NSUserDefaults standardUserDefaults] setInteger:lastTrackPlaying forKey:@"lastTrackPlaying"]; - [[NSUserDefaults standardUserDefaults] setDouble:p forKey:@"lastTrackPosition"]; // If we handle this here, then it will send on all seek operations, which also reset lastPosition [self sendMetaData]; lastPosition = p; + + [playlistController commitPersistentStore]; } - - position = p; - - [[playlistController currentEntry] setCurrentPosition:p]; } - (double)position { diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 14f3fa4b1..72779949e 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -25,6 +25,8 @@ #define UNDO_STACK_LIMIT 0 +extern BOOL kAppControllerShuttingDown; + @implementation PlaylistController @synthesize currentEntry; @@ -1099,6 +1101,8 @@ static void *playlistControllerContext = &playlistControllerContext; [queueItem setQueuePosition:i]; } + [self commitPersistentStore]; + return pe; } @@ -1256,6 +1260,7 @@ static void *playlistControllerContext = &playlistControllerContext; for(i = 0; i < [shuffleList count]; i++) { [shuffleList[i] setShuffleIndex:i]; } + [self commitPersistentStore]; } - (void)addShuffledListToBack { @@ -1277,6 +1282,7 @@ static void *playlistControllerContext = &playlistControllerContext; for(i = ([shuffleList count] - [newList count]); i < [shuffleList count]; i++) { [shuffleList[i] setShuffleIndex:(int)i]; } + [self commitPersistentStore]; } - (void)resetShuffleList { @@ -1311,6 +1317,7 @@ static void *playlistControllerContext = &playlistControllerContext; for(i = 0, j = [shuffleList count]; i < j; ++i) { [shuffleList[i] setShuffleIndex:(int)i]; } + [self commitPersistentStore]; } else { [shuffleList insertObject:currentEntry atIndex:0]; [currentEntry setShuffleIndex:0]; @@ -1326,15 +1333,25 @@ static void *playlistControllerContext = &playlistControllerContext; [shuffleList[i] setShuffleIndex:(int)i]; } } + [self commitPersistentStore]; } } } - (void)setCurrentEntry:(PlaylistEntry *)pe { - currentEntry.current = NO; - currentEntry.stopAfter = NO; + if(pe == currentEntry || kAppControllerShuttingDown) return; - pe.current = YES; + if(currentEntry) { + currentEntry.current = NO; + currentEntry.stopAfter = NO; + currentEntry.currentPosition = 0.0; + } + + if(pe) { + pe.current = YES; + } + + [self commitPersistentStore]; NSMutableIndexSet *refreshSet = [[NSMutableIndexSet alloc] init]; @@ -1532,6 +1549,7 @@ static void *playlistControllerContext = &playlistControllerContext; - (IBAction)stopAfterCurrent:(id)sender { currentEntry.stopAfter = !currentEntry.stopAfter; + [self commitPersistentStore]; NSIndexSet *refreshSet = [NSIndexSet indexSetWithIndex:[currentEntry index]]; @@ -1548,6 +1566,8 @@ static void *playlistControllerContext = &playlistControllerContext; [refreshSet addIndex:pe.index]; } + [self commitPersistentStore]; + // Refresh entire row of all affected items to update tooltips unsigned long columns = [[self.tableView tableColumns] count]; [self.tableView reloadDataForRowIndexes:refreshSet columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, columns)]];