Skip to content

Commit

Permalink
Music player HUD
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jan 23, 2018
1 parent 4f19f9d commit 55763f1
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
url = https://github.com/kishikawakatsumi/UICKeyChainStore.git
[submodule "Frameworks/MBProgressHUD"]
path = Frameworks/MBProgressHUD
url = https://github.com/blinksh/MBProgressHUD.git
url = https://github.com/yury/MBProgressHUD.git
[submodule "Frameworks/PasscodeLock"]
path = Frameworks/PasscodeLock
url = https://github.com/blinksh/SwiftPasscodeLock.git
19 changes: 13 additions & 6 deletions Blink/MusicManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MusicManager : NSObject

+ (void)playPrev;
+ (void)playNext;
+ (void)pause;
+ (void)play;
+ (void)playBack;
+ (NSString *)trackInfo;
+ (MusicManager *)shared;

- (void)onShow;
- (void)onHide;

- (UIView *)hudView;

- (NSArray<UIKeyCommand *> *)keyCommands;
- (void)handleCommand:(UIKeyCommand *)cmd;

- (NSArray<NSString *> *)commands;
- (NSString *)runWithInput:(NSString *)input;

@end
171 changes: 163 additions & 8 deletions Blink/MusicManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,195 @@

#import "MusicManager.h"
#import <MediaPlayer/MediaPlayer.h>
#import "BKUserConfigurationManager.h"

@implementation MusicManager
@implementation MusicManager {
UIToolbar *_toolbar;
NSArray<UIKeyCommand *> *_keyCommands;
}

+ (MusicManager *)shared {
static MusicManager *ctrl = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ctrl = [[self alloc] init];
});
return ctrl;
}

- (instancetype)init
{
if (self = [super init]) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self
selector:@selector(_playbackStateDidChange)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
}

return self;
}

- (void)onShow
{
[[self _player] beginGeneratingPlaybackNotifications];
}

- (void)onHide
{
[[self _player] endGeneratingPlaybackNotifications];
}


- (UIView *)hudView
{
if (!_toolbar) {
_toolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
_toolbar.barStyle = UIBarStyleBlack;
}

[_toolbar setItems:[self _toolbarItems]];

return _toolbar;
}

- (NSArray<UIBarButtonItem *> *)_toolbarItems
{
UIBarButtonItem *prev = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(_playPrev)];
UIBarButtonItem *space1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space1.width = 24;
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space2.width = 24;
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(_play)];
UIBarButtonItem *pause = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(_pause)];

UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(_playNext)];

BOOL isPlaying = [self _player].playbackState == MPMusicPlaybackStatePlaying;

return @[prev, space1, isPlaying ? pause : play, space2, next];
}

- (void)_playbackStateDidChange
{
[_toolbar setItems:[self _toolbarItems]];
}

- (NSArray<UIKeyCommand *> *)keyCommands
{
if (!_keyCommands) {
UIKeyModifierFlags modifierFlags = [BKUserConfigurationManager shortCutModifierFlags];

_keyCommands =
@[
[UIKeyCommand keyCommandWithInput:@"n"
modifierFlags:modifierFlags
action:@selector(musicCommand:)
discoverabilityTitle:@"Next track"],

[UIKeyCommand keyCommandWithInput:@"s"
modifierFlags:modifierFlags
action:@selector(musicCommand:)
discoverabilityTitle:@"Pause"],

[UIKeyCommand keyCommandWithInput:@"p"
modifierFlags:modifierFlags
action:@selector(musicCommand:)
discoverabilityTitle:@"Play"],

[UIKeyCommand keyCommandWithInput:@"r"
modifierFlags:modifierFlags
action:@selector(musicCommand:)
discoverabilityTitle:@"Previous track"],

[UIKeyCommand keyCommandWithInput:@"b"
modifierFlags:modifierFlags
action:@selector(musicCommand:)
discoverabilityTitle:@"Play from beggining"],

[UIKeyCommand keyCommandWithInput:@"m"
modifierFlags:modifierFlags
action:@selector(_toggleMusicHUD)],
];
}

return _keyCommands;
}

- (void)_toggleMusicHUD
{
// See this method in SpaceController
}

- (void)musicCommand:(UIKeyCommand *)cmd
{
}

- (void)handleCommand:(UIKeyCommand *)cmd
{
[self runWithInput:cmd.input];
}

- (NSArray<NSString *> *)commands
{
return @[@"info", @"back", @"prev", @"pause", @"play", @"resume", @"next"];
}

-(NSString *)runWithInput:(NSString *)input
{
if ([input isEqualToString:@""] || [input isEqualToString:@"info"]) {
NSString *info = [self _trackInfo];
if (info) {
return [NSString stringWithFormat:@"Current track: %@", info];
}
} else if ([input isEqualToString:@"next"] || [input isEqualToString:@"n"]) {
[self _playNext];
} else if ([input isEqualToString:@"prev"] || [input isEqualToString:@"r"]) {
[self _playPrev];
} else if ([input isEqualToString:@"pause"] || [input isEqualToString:@"s"]) {
[self _pause];
} else if ([input isEqualToString:@"play"] || [input isEqualToString:@"p"] || [input isEqualToString:@"resume"]) {
[self _play];
} else if ([input isEqualToString:@"back"] || [input isEqualToString:@"b"]) {
[self _playBack];
} else {
return @"Unknown parameter";
}

return nil;
}

+ (MPMusicPlayerController *)_player
- (MPMusicPlayerController *)_player
{
return [MPMusicPlayerController systemMusicPlayer];
}

+ (void)playNext
- (void)_playNext
{
[[self _player] skipToNextItem];
}

+ (void)playPrev
- (void)_playPrev
{
[[self _player] skipToPreviousItem];
}

+ (void)playBack
- (void)_playBack
{
[[self _player] skipToBeginning];
}

+ (void)pause
- (void)_pause
{
[[self _player] pause];
}

+ (void)play
- (void)_play
{
[[self _player] play];
}

+ (NSString *)trackInfo
- (NSString *)_trackInfo
{
MPMediaItem *item = [[self _player] nowPlayingItem];
if (!item) {
Expand Down
47 changes: 42 additions & 5 deletions Blink/SpaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#import "SmartKeysController.h"
#import "TermController.h"
#import "TermInput.h"
#import "MusicManager.h"


@interface SpaceController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate,
Expand All @@ -56,6 +57,7 @@ @implementation SpaceController {

UIPageControl *_pageControl;
MBProgressHUD *_hud;
MBProgressHUD *_musicHUD;

NSMutableArray<UIKeyCommand *> *_kbdCommands;
NSMutableArray<UIKeyCommand *> *_kbdCommandsWithoutDiscoverability;
Expand Down Expand Up @@ -508,8 +510,37 @@ - (UIPageControl *)pageControl
return _pageControl;
}

- (void)_toggleMusicHUD
{
if (_musicHUD) {
[_musicHUD hideAnimated:YES];
_musicHUD = nil;
[[MusicManager shared] onHide];
return;
}

[_hud hideAnimated:NO];

_musicHUD = [MBProgressHUD showHUDAddedTo:_viewportsController.view animated:YES];
_musicHUD.mode = MBProgressHUDModeCustomView;
_musicHUD.bezelView.color = [UIColor darkGrayColor];
_musicHUD.contentColor = [UIColor whiteColor];
[_musicHUD setMargin: 0];
[[MusicManager shared] onShow];
_musicHUD.customView = [[MusicManager shared] hudView];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_toggleMusicHUD)];
[_musicHUD.backgroundView addGestureRecognizer:tapRecognizer];
}

- (void)_displayHUD
{
if (_musicHUD) {
[_musicHUD hideAnimated:YES];
_musicHUD = nil;
return;
}

if (!_hud) {
_hud = [[MBProgressHUD alloc] initWithView:_viewportsController.view];
_hud.mode = MBProgressHUDModeCustomView;
Expand Down Expand Up @@ -656,6 +687,10 @@ - (void)terminalDidResize:(TermController*)control

- (NSArray<UIKeyCommand *> *)keyCommands
{
if (_musicHUD) {
return [[MusicManager shared] keyCommands];
}

NSMutableDictionary *kbMapping = [NSMutableDictionary dictionaryWithDictionary:[BKDefaults keyboardMapping]];
if([kbMapping objectForKey:@"⌘ Cmd"] && ![[kbMapping objectForKey:@"⌘ Cmd"]isEqualToString:@"None"]){
return _kbdCommandsWithoutDiscoverability;
Expand Down Expand Up @@ -700,11 +735,8 @@ - (void)setKbdCommands
discoverabilityTitle: @"Show config"],

[UIKeyCommand keyCommandWithInput: @"m" modifierFlags: modifierFlags
action: @selector(playNext:)
discoverabilityTitle: @"Music Next track"],
[UIKeyCommand keyCommandWithInput: @"p" modifierFlags: modifierFlags
action: @selector(playPrev:)
discoverabilityTitle: @"Music Next track"],
action: @selector(_toggleMusicHUD)
discoverabilityTitle: @"Music Controls"],

[UIKeyCommand keyCommandWithInput:@"+"
modifierFlags:modifierFlags
Expand Down Expand Up @@ -956,5 +988,10 @@ - (void)resumeWith:(StateManager *)stateManager
};
}

- (void)musicCommand:(UIKeyCommand *)cmd
{
[[MusicManager shared] handleCommand:cmd];
}


@end
42 changes: 10 additions & 32 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

NSArray<NSString *> *musicActionsByPrefix(NSString *prefix)
{
NSArray<NSString *> * actions = @[@"info", @"back", @"prev", @"pause", @"play", @"resume", @"next"];
NSArray<NSString *> * actions = [[MusicManager shared] commands];

if (prefix.length == 0) {
return actions;
Expand Down Expand Up @@ -322,38 +322,15 @@ - (void)_switchTheme:(NSString *)args
}
}

- (void)_controlMusic:(NSString *)args
- (void)_controlMusic:(NSString *)input
{
if ([args isEqualToString:@""] || [args isEqualToString:@"info"]) {
__block NSString * info = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
info = [MusicManager trackInfo];
});
if (info) {
[self out:[NSString stringWithFormat:@"Current track: %@", info].UTF8String];
}
} else if ([args isEqualToString:@"next"] || [args isEqualToString:@"n"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MusicManager playNext];
});
} else if ([args isEqualToString:@"prev"] || [args isEqualToString:@"r"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MusicManager playPrev];
});
} else if ([args isEqualToString:@"pause"] || [args isEqualToString:@"p"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MusicManager pause];
});
} else if ([args isEqualToString:@"play"] || [args isEqualToString:@"resume"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MusicManager play];
});
} else if ([args isEqualToString:@"back"] || [args isEqualToString:@"b"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MusicManager playBack];
});
} else {
[self out: @"Unknown parameter".UTF8String];
__block NSString *output = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
output = [[MusicManager shared] runWithInput:input];
});

if (output) {
[self out:output.UTF8String];
}
}

Expand Down Expand Up @@ -425,6 +402,7 @@ - (void)_showHelp
@" cmd+o: Switch to other screen (Airplay mode).",
@" cmd+shift+o: Move current shell to other screen (Airplay mode).",
@" cmd+,: Open config.",
@" cmd+m: Toggle music controls. (Control with cmd+n/p/s/r).",
@" pinch: Change font size.",
@""
] componentsJoinedByString:@"\r\n"];
Expand Down

0 comments on commit 55763f1

Please sign in to comment.