[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 <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-19 01:42:27 -07:00
parent cd14377dcd
commit 7fcfdb373b
3 changed files with 72 additions and 33 deletions

View File

@ -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"];

View File

@ -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 {

View File

@ -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)]];