Convert CogStatus enum to NS_ENUM

CQTexperiment
Dzmitry Neviadomski 2021-02-07 00:20:03 +03:00
parent 2b916704a0
commit 9840d87127
10 changed files with 73 additions and 61 deletions

View File

@ -165,9 +165,9 @@
int lastStatus = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastPlaybackStatus"]; int lastStatus = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastPlaybackStatus"];
int lastIndex = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastTrackPlaying"]; int lastIndex = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastTrackPlaying"];
if (lastStatus != kCogStatusStopped && lastIndex >= 0) if (lastStatus != CogStatusStopped && lastIndex >= 0)
{ {
[playbackController playEntryAtIndex:lastIndex startPaused:(lastStatus == kCogStatusPaused)]; [playbackController playEntryAtIndex:lastIndex startPaused:(lastStatus == CogStatusPaused)];
[playbackController seek:[NSNumber numberWithDouble:[[NSUserDefaults standardUserDefaults] floatForKey:@"lastTrackPosition"]]]; [playbackController seek:[NSNumber numberWithDouble:[[NSUserDefaults standardUserDefaults] floatForKey:@"lastTrackPosition"]]];
} }
} }
@ -239,16 +239,16 @@
- (void)applicationWillTerminate:(NSNotification *)aNotification - (void)applicationWillTerminate:(NSNotification *)aNotification
{ {
int currentStatus = [playbackController playbackStatus]; CogStatus currentStatus = [playbackController playbackStatus];
int lastTrackPlaying = -1; int lastTrackPlaying = -1;
double lastTrackPosition = 0; double lastTrackPosition = 0;
if (currentStatus == kCogStatusStopping) if (currentStatus == CogStatusStopping)
currentStatus = kCogStatusStopped; currentStatus = CogStatusStopped;
[[NSUserDefaults standardUserDefaults] setInteger:currentStatus forKey:@"lastPlaybackStatus"]; [[NSUserDefaults standardUserDefaults] setInteger:currentStatus forKey:@"lastPlaybackStatus"];
if (currentStatus != kCogStatusStopped) if (currentStatus != CogStatusStopped)
{ {
PlaylistEntry * pe = [playlistController currentEntry]; PlaylistEntry * pe = [playlistController currentEntry];
lastTrackPlaying = [pe index]; lastTrackPlaying = [pe index];
@ -372,7 +372,7 @@
[userDefaultsValuesDict setObject:@"cubic" forKey:@"resampling"]; [userDefaultsValuesDict setObject:@"cubic" forKey:@"resampling"];
[userDefaultsValuesDict setObject:[NSNumber numberWithInteger:kCogStatusStopped] forKey:@"lastPlaybackStatus"]; [userDefaultsValuesDict setObject:[NSNumber numberWithInteger:CogStatusStopped] forKey:@"lastPlaybackStatus"];
[userDefaultsValuesDict setObject:[NSNumber numberWithInteger:-1] forKey:@"lastTrackPlaying"]; [userDefaultsValuesDict setObject:[NSNumber numberWithInteger:-1] forKey:@"lastTrackPlaying"];
[userDefaultsValuesDict setObject:[NSNumber numberWithDouble:0] forKey:@"lastTrackPosition"]; [userDefaultsValuesDict setObject:[NSNumber numberWithDouble:0] forKey:@"lastTrackPosition"];

View File

@ -49,14 +49,17 @@ static NSString *getBadgeName(NSString *baseName, BOOL colorfulIcons)
BOOL colorfulIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"colorfulDockIcons"]; BOOL colorfulIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"colorfulDockIcons"];
if (playbackStatus == kCogStatusPlaying) { switch (playbackStatus) {
badgeImage = [NSImage imageNamed:getBadgeName(@"playDockBadge", colorfulIcons)]; case CogStatusPlaying:
} badgeImage = [NSImage imageNamed:getBadgeName(@"playDockBadge", colorfulIcons)];
else if (playbackStatus == kCogStatusPaused) { break;
badgeImage = [NSImage imageNamed:getBadgeName(@"pauseDockBadge", colorfulIcons)]; case CogStatusPaused:
} badgeImage = [NSImage imageNamed:getBadgeName(@"pauseDockBadge", colorfulIcons)];
else { break;
badgeImage = [NSImage imageNamed:getBadgeName(@"stopDockBadge", colorfulIcons)];
default:
badgeImage = [NSImage imageNamed:getBadgeName(@"stopDockBadge", colorfulIcons)];
break;
} }
NSSize badgeSize = [badgeImage size]; NSSize badgeSize = [badgeImage size];

View File

@ -3,6 +3,7 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "CogAudio/AudioPlayer.h" #import "CogAudio/AudioPlayer.h"
#import "CogAudio/Status.h"
#import "TrackingSlider.h" #import "TrackingSlider.h"
#import "AudioScrobbler.h" #import "AudioScrobbler.h"
@ -34,13 +35,13 @@ extern NSDictionary * makeRGInfo(PlaylistEntry *pe);
AudioPlayer *audioPlayer; AudioPlayer *audioPlayer;
int playbackStatus; CogStatus playbackStatus;
double position; double position;
BOOL seekable; BOOL seekable;
BOOL fading; BOOL fading;
} }
@property int playbackStatus; @property CogStatus playbackStatus;
- (IBAction)changeVolume:(id)sender; - (IBAction)changeVolume:(id)sender;
- (IBAction)volumeDown:(id)sender; - (IBAction)volumeDown:(id)sender;

View File

@ -44,7 +44,7 @@ NSString *CogPlaybackDidStopNotficiation = @"CogPlaybackDidStopNotficiation";
audioPlayer = [[AudioPlayer alloc] init]; audioPlayer = [[AudioPlayer alloc] init];
[audioPlayer setDelegate:self]; [audioPlayer setDelegate:self];
[self setPlaybackStatus: kCogStatusStopped]; [self setPlaybackStatus: CogStatusStopped];
} }
return self; return self;
@ -73,7 +73,7 @@ NSString *CogPlaybackDidStopNotficiation = @"CogPlaybackDidStopNotficiation";
- (IBAction)playPauseResume:(id)sender - (IBAction)playPauseResume:(id)sender
{ {
if (playbackStatus == kCogStatusStopped || playbackStatus == kCogStatusStopping) if (playbackStatus == CogStatusStopped || playbackStatus == CogStatusStopping)
{ {
[self play:self]; [self play:self];
} }
@ -87,7 +87,7 @@ NSString *CogPlaybackDidStopNotficiation = @"CogPlaybackDidStopNotficiation";
- (IBAction)pauseResume:(id)sender - (IBAction)pauseResume:(id)sender
{ {
if (playbackStatus == kCogStatusPaused) if (playbackStatus == CogStatusPaused)
[self resume:self]; [self resume:self];
else else
[self pause:self]; [self pause:self];
@ -96,7 +96,7 @@ NSString *CogPlaybackDidStopNotficiation = @"CogPlaybackDidStopNotficiation";
- (IBAction)pause:(id)sender - (IBAction)pause:(id)sender
{ {
[audioPlayer pause]; [audioPlayer pause];
[self setPlaybackStatus: kCogStatusPaused]; [self setPlaybackStatus: CogStatusPaused];
[self sendMetaData]; [self sendMetaData];
} }
@ -160,7 +160,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
- (void)playEntry:(PlaylistEntry *)pe startPaused:(BOOL)paused - (void)playEntry:(PlaylistEntry *)pe startPaused:(BOOL)paused
{ {
if (playbackStatus != kCogStatusStopped && playbackStatus != kCogStatusStopping) if (playbackStatus != CogStatusStopped && playbackStatus != CogStatusStopping)
[self stop:self]; [self stop:self];
DLog(@"PLAYLIST CONTROLLER: %@", [playlistController class]); DLog(@"PLAYLIST CONTROLLER: %@", [playlistController class]);
@ -382,7 +382,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
NSNumber *originalVolume = [NSNumber numberWithDouble: [audioPlayer volume]]; NSNumber *originalVolume = [NSNumber numberWithDouble: [audioPlayer volume]];
NSTimer *fadeTimer; NSTimer *fadeTimer;
if (playbackStatus == kCogStatusPlaying) { if (playbackStatus == CogStatusPlaying) {
fadeTimer = [NSTimer timerWithTimeInterval:time fadeTimer = [NSTimer timerWithTimeInterval:time
target:self target:self
selector:@selector(audioFadeDown:) selector:@selector(audioFadeDown:)
@ -551,7 +551,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
- (void)audioPlayer:(AudioPlayer *)player didChangeStatus:(NSNumber *)s userInfo:(id)userInfo - (void)audioPlayer:(AudioPlayer *)player didChangeStatus:(NSNumber *)s userInfo:(id)userInfo
{ {
int status = [s intValue]; int status = [s intValue];
if (status == kCogStatusStopped || status == kCogStatusPaused) if (status == CogStatusStopped || status == CogStatusPaused)
{ {
if (positionTimer) if (positionTimer)
{ {
@ -559,7 +559,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
positionTimer = NULL; positionTimer = NULL;
} }
if (status == kCogStatusStopped) if (status == CogStatusStopped)
{ {
[self setPosition:0]; [self setPosition:0];
[self setSeekable:NO]; // the player stopped, disable the slider [self setSeekable:NO]; // the player stopped, disable the slider
@ -571,7 +571,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
[[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidPauseNotficiation object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidPauseNotficiation object:nil];
} }
} }
else if (status == kCogStatusPlaying) else if (status == CogStatusPlaying)
{ {
if (!positionTimer) { if (!positionTimer) {
positionTimer = [NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(updatePosition:) userInfo:nil repeats:YES]; positionTimer = [NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(updatePosition:) userInfo:nil repeats:YES];
@ -581,7 +581,7 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
[[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidResumeNotficiation object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:CogPlaybackDidResumeNotficiation object:nil];
} }
if (status == kCogStatusStopped) { if (status == CogStatusStopped) {
DLog(@"DONE!"); DLog(@"DONE!");
[playlistController setCurrentEntry:nil]; [playlistController setCurrentEntry:nil];
[self setSeekable:NO]; // the player stopped, disable the slider [self setSeekable:NO]; // the player stopped, disable the slider
@ -591,11 +591,11 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
[self setSeekable:YES]; [self setSeekable:YES];
} }
if (status == kCogStatusStopped) { if (status == CogStatusStopped) {
status = kCogStatusStopping; status = CogStatusStopping;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
if ([self playbackStatus] == kCogStatusStopping) if ([self playbackStatus] == CogStatusStopping)
[self setPlaybackStatus:kCogStatusStopped]; [self setPlaybackStatus:CogStatusStopped];
}); });
} }
@ -648,12 +648,17 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe)
[songInfo setObject:[NSNumber numberWithFloat:[entry index]] forKey:MPMediaItemPropertyPersistentID]; [songInfo setObject:[NSNumber numberWithFloat:[entry index]] forKey:MPMediaItemPropertyPersistentID];
} }
if (playbackStatus == kCogStatusPlaying) { switch (playbackStatus) {
defaultCenter.playbackState = MPNowPlayingPlaybackStatePlaying; case CogStatusPlaying:
} else if (playbackStatus == kCogStatusPaused) { defaultCenter.playbackState = MPNowPlayingPlaybackStatePlaying;
defaultCenter.playbackState = MPNowPlayingPlaybackStatePaused; break;
} else { case CogStatusPaused:
defaultCenter.playbackState = MPNowPlayingPlaybackStateStopped; defaultCenter.playbackState = MPNowPlayingPlaybackStatePaused;
break;
default:
defaultCenter.playbackState = MPNowPlayingPlaybackStateStopped;
break;
} }
[defaultCenter setNowPlayingInfo:songInfo]; [defaultCenter setNowPlayingInfo:songInfo];

View File

@ -107,21 +107,21 @@
[bufferChain launchThreads]; [bufferChain launchThreads];
if (paused) if (paused)
[self setPlaybackStatus:kCogStatusPaused waitUntilDone:YES]; [self setPlaybackStatus:CogStatusPaused waitUntilDone:YES];
} }
- (void)stop - (void)stop
{ {
//Set shouldoContinue to NO on allll things //Set shouldoContinue to NO on allll things
[self setShouldContinue:NO]; [self setShouldContinue:NO];
[self setPlaybackStatus:kCogStatusStopped waitUntilDone:YES]; [self setPlaybackStatus:CogStatusStopped waitUntilDone:YES];
} }
- (void)pause - (void)pause
{ {
[output pause]; [output pause];
[self setPlaybackStatus:kCogStatusPaused waitUntilDone:YES]; [self setPlaybackStatus:CogStatusPaused waitUntilDone:YES];
} }
- (void)resume - (void)resume
@ -135,7 +135,7 @@
[output resume]; [output resume];
[self setPlaybackStatus:kCogStatusPlaying waitUntilDone:YES]; [self setPlaybackStatus:CogStatusPlaying waitUntilDone:YES];
} }
- (void)seekToTime:(double)time - (void)seekToTime:(double)time
@ -208,7 +208,7 @@
{ {
initialBufferFilled = YES; initialBufferFilled = YES;
if (outputLaunched == NO && startedPaused == NO) { if (outputLaunched == NO && startedPaused == NO) {
[self setPlaybackStatus:kCogStatusPlaying]; [self setPlaybackStatus:CogStatusPlaying];
[output launchThread]; [output launchThread];
outputLaunched = YES; outputLaunched = YES;
} }

View File

@ -7,10 +7,11 @@
* *
*/ */
enum #import <Foundation/Foundation.h>
{
kCogStatusStopped = 0, typedef NS_ENUM(NSInteger, CogStatus) {
kCogStatusPaused, CogStatusStopped = 0,
kCogStatusPlaying, CogStatusPaused,
kCogStatusStopping, CogStatusPlaying,
CogStatusStopping,
}; };

View File

@ -64,7 +64,7 @@
8E6096C009F314CF006D8BD7 /* mpc_reader.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = mpc_reader.c; path = Files/src/mpc_reader.c; sourceTree = "<group>"; }; 8E6096C009F314CF006D8BD7 /* mpc_reader.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = mpc_reader.c; path = Files/src/mpc_reader.c; sourceTree = "<group>"; };
8E6096C109F314CF006D8BD7 /* requant.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = requant.c; path = Files/src/requant.c; sourceTree = "<group>"; }; 8E6096C109F314CF006D8BD7 /* requant.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = requant.c; path = Files/src/requant.c; sourceTree = "<group>"; };
8E6096C309F314CF006D8BD7 /* streaminfo.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = streaminfo.c; path = Files/src/streaminfo.c; sourceTree = "<group>"; }; 8E6096C309F314CF006D8BD7 /* streaminfo.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = streaminfo.c; path = Files/src/streaminfo.c; sourceTree = "<group>"; };
8E6096C409F314CF006D8BD7 /* synth_filter.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = synth_filter.c; path = Files/src/synth_filter.c; sourceTree = "<group>"; }; 8E6096C409F314CF006D8BD7 /* synth_filter.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = synth_filter.c; path = Files/src/synth_filter.c; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */

View File

@ -362,8 +362,8 @@
} }
if (action == @selector(scrollToCurrentEntry:) && if (action == @selector(scrollToCurrentEntry:) &&
(([playbackController playbackStatus] == kCogStatusStopped) || (([playbackController playbackStatus] == CogStatusStopped) ||
([playbackController playbackStatus] == kCogStatusStopping))) ([playbackController playbackStatus] == CogStatusStopping)))
return NO; return NO;
return [super validateUserInterfaceItem:anItem]; return [super validateUserInterfaceItem:anItem];

View File

@ -17,17 +17,19 @@ class PlaybackStatusToHiddenTransformer : ValueTransformer {
} }
override func transformedValue(_ value: Any?) -> Any? { override func transformedValue(_ value: Any?) -> Any? {
switch value { guard let intValue = value as? Int,
case kCogStatusStopped as Int: let status = CogStatus(rawValue: intValue) else {
return true;
}
switch status {
case .stopped:
return true return true
case kCogStatusPaused as Int, case .paused,
kCogStatusPlaying as Int, .playing,
kCogStatusStopping as Int: .stopping:
return false return false
case .none: @unknown default:
return true return false;
case .some(_):
return true
} }
} }
} }

View File

@ -45,7 +45,7 @@ static NSString *PlaybackButtonsPlaybackStatusObservationContext = @"PlaybackBut
NSImage *image = nil; NSImage *image = nil;
if (playbackStatus == kCogStatusPlaying) { if (playbackStatus == CogStatusPlaying) {
image = [NSImage imageNamed:@"pauseTemplate"]; image = [NSImage imageNamed:@"pauseTemplate"];
} }
else { else {