Skip to content

Commit 44adf1a

Browse files
Merge pull request #1415 from wutschel/rework_torch
Maintenance: Implement KVO for torch properties and move torch methods to RemoteController
2 parents cbed86b + fbd6db9 commit 44adf1a

4 files changed

Lines changed: 57 additions & 53 deletions

File tree

XBMC Remote/RemoteController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#import "DSJSONRPC.h"
1111
#import "VolumeSliderView.h"
1212

13+
@import AVFoundation;
14+
1315
typedef NS_ENUM(NSInteger, RemotePositionType) {
1416
RemoteAtTop,
1517
RemoteAtBottom,
@@ -56,5 +58,6 @@ typedef NS_ENUM(NSInteger, RemotePositionType) {
5658

5759
@property (strong, nonatomic) id detailItem;
5860
@property (nonatomic, strong) NSTimer *holdKeyTimer;
61+
@property (nonatomic, strong) AVCaptureDevice *avCaptureDevice;
5962

6063
@end

XBMC Remote/RemoteController.m

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#define KEY_REPEAT_TIMEOUT 0.1
4949
#define GESTUREZONE_RADIUS 20.0
5050
#define GESTUREZONE_BORDERWIDTH 1.0
51+
#define KEYPATH_TORCHACTIVE @"torchActive"
52+
#define KEYPATH_TORCHAVAILABLE @"torchAvailable"
53+
54+
static void *TorchRemoteContext = &TorchRemoteContext;
5155

5256
@interface RemoteController ()
5357

@@ -1037,12 +1041,43 @@ - (void)viewWillAppear:(BOOL)animated {
10371041
selector:@selector(handleDidBecomeActive)
10381042
name:UIApplicationDidBecomeActiveNotification
10391043
object:nil];
1044+
1045+
[self.avCaptureDevice addObserver:self
1046+
forKeyPath:KEYPATH_TORCHACTIVE
1047+
options:NSKeyValueObservingOptionNew
1048+
context:TorchRemoteContext];
1049+
1050+
[self.avCaptureDevice addObserver:self
1051+
forKeyPath:KEYPATH_TORCHAVAILABLE
1052+
options:NSKeyValueObservingOptionNew
1053+
context:TorchRemoteContext];
1054+
}
1055+
1056+
- (void)observeValueForKeyPath:(NSString*)keyPath
1057+
ofObject:(id)object
1058+
change:(NSDictionary*)change
1059+
context:(void*)context {
1060+
if (context == TorchRemoteContext) {
1061+
if ([keyPath isEqualToString:KEYPATH_TORCHACTIVE]) {
1062+
torchIsOn = [change[NSKeyValueChangeNewKey] boolValue];
1063+
[self setTorchIcon:torchIsOn];
1064+
}
1065+
else if ([keyPath isEqualToString:KEYPATH_TORCHAVAILABLE]) {
1066+
torchButton.enabled = [change[NSKeyValueChangeNewKey] boolValue];
1067+
}
1068+
}
1069+
else {
1070+
[super observeValueForKeyPath:keyPath
1071+
ofObject:object
1072+
change:change
1073+
context:context];
1074+
}
10401075
}
10411076

10421077
- (void)handleDidBecomeActive {
10431078
// Update torch mode
1044-
torchIsOn = [Utilities isTorchOn];
1045-
[Utilities turnTorchOn:torchButton on:torchIsOn];
1079+
torchIsOn = self.avCaptureDevice.torchActive;
1080+
[self setTorchIcon:torchIsOn];
10461081
}
10471082

10481083
- (void)enablePopGestureRecognizer {
@@ -1074,9 +1109,19 @@ - (void)viewWillDisappear:(BOOL)animated {
10741109
self.slidingViewController.panGesture.delegate = nil;
10751110
}
10761111

1077-
- (void)turnTorchOn:(id)sender {
1112+
- (void)toggleTorch {
10781113
torchIsOn = !torchIsOn;
1079-
[Utilities turnTorchOn:sender on:torchIsOn];
1114+
1115+
AVCaptureDevice *device = self.avCaptureDevice;
1116+
[device lockForConfiguration:nil];
1117+
device.torchMode = torchIsOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff;
1118+
[device unlockForConfiguration];
1119+
}
1120+
1121+
- (void)setTorchIcon:(BOOL)torchActive {
1122+
UIImage *buttonImage = [UIImage imageNamed:torchActive ? @"torch_on" : @"torch"];
1123+
buttonImage = [Utilities colorizeImage:buttonImage withColor:ICON_TINT_COLOR];
1124+
[torchButton setImage:buttonImage forState:UIControlStateNormal];
10801125
}
10811126

10821127
- (void)dismissModal {
@@ -1102,7 +1147,7 @@ - (void)setRemoteToPosition {
11021147
}
11031148

11041149
- (void)createRemoteToolbarWidth:(CGFloat)width xMin:(CGFloat)xMin yMax:(CGFloat)yMax {
1105-
torchIsOn = [Utilities isTorchOn];
1150+
torchIsOn = self.avCaptureDevice.torchActive;
11061151
// iPhone layout has 5 buttons (Gesture > Keyboard > Info > Torch > Additional) with flex spaces around buttons.
11071152
// iPad layout has 6 buttons (Settings > Gesture > Keyboard > Info > Torch > Additional) with flex spaces around buttons.
11081153
// iPhone has an addtional button to toggle the remote's vertical position
@@ -1170,11 +1215,9 @@ - (void)createRemoteToolbarWidth:(CGFloat)width xMin:(CGFloat)xMin yMax:(CGFloat
11701215
frame.origin.x += ToolbarPadding;
11711216
torchButton.frame = frame;
11721217
torchButton.showsTouchWhenHighlighted = YES;
1173-
buttonImage = [UIImage imageNamed:torchIsOn ? @"torch_on" : @"torch"];
1174-
buttonImage = [Utilities colorizeImage:buttonImage withColor:ICON_TINT_COLOR];
1175-
[torchButton setImage:buttonImage forState:UIControlStateNormal];
1176-
[torchButton addTarget:self action:@selector(turnTorchOn:) forControlEvents:UIControlEventTouchUpInside];
1177-
torchButton.enabled = [Utilities hasTorch];
1218+
torchButton.enabled = self.avCaptureDevice.torchAvailable;
1219+
[self setTorchIcon:torchIsOn];
1220+
[torchButton addTarget:self action:@selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
11781221
[remoteToolbar addSubview:torchButton];
11791222

11801223
if (IS_IPHONE) {
@@ -1208,6 +1251,7 @@ - (void)viewDidLoad {
12081251
[super viewDidLoad];
12091252

12101253
self.edgesForExtendedLayout = UIRectEdgeNone;
1254+
self.avCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
12111255

12121256
[self loadRemoteMode];
12131257
[self configureView];

XBMC Remote/Utilities.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ typedef NS_ENUM(NSInteger, LogoBackgroundType) {
8686
+ (CGSize)getSizeOfLabel:(UILabel*)label;
8787
+ (UIImage*)applyRoundedEdgesImage:(UIImage*)image;
8888
+ (void)applyRoundedEdgesView:(UIView*)view;
89-
+ (void)turnTorchOn:(UIButton*)button on:(BOOL)torchOn;
90-
+ (BOOL)hasTorch;
91-
+ (BOOL)isTorchOn;
9289
+ (CGFloat)getBottomPadding;
9390
+ (CGFloat)getTopPadding;
9491
+ (CGFloat)getTopPaddingWithNavBar:(UINavigationController*)navCtrl;

XBMC Remote/Utilities.m

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2013 joethefox inc. All rights reserved.
77
//
88

9-
#import <AVFoundation/AVFoundation.h>
109
#import <StoreKit/StoreKit.h>
1110
#import <arpa/inet.h>
1211
#import <mach/mach.h>
@@ -951,45 +950,6 @@ + (void)applyRoundedEdgesView:(UIView*)view {
951950
}
952951
}
953952

954-
+ (void)turnTorchOn:(UIButton*)button on:(BOOL)torchOn {
955-
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
956-
if (!(device.hasTorch && device.hasFlash)) {
957-
return;
958-
}
959-
960-
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
961-
UIImage *img;
962-
[device lockForConfiguration:nil];
963-
// Set torch and select button image
964-
if (torchOn) {
965-
device.torchMode = AVCaptureTorchModeOn;
966-
settings.flashMode = AVCaptureFlashModeOn;
967-
img = [UIImage imageNamed:@"torch_on"];
968-
}
969-
else {
970-
device.torchMode = AVCaptureTorchModeOff;
971-
settings.flashMode = AVCaptureFlashModeOff;
972-
img = [UIImage imageNamed:@"torch"];
973-
}
974-
[button setImage:img forState:UIControlStateNormal];
975-
976-
[device unlockForConfiguration];
977-
}
978-
979-
+ (BOOL)hasTorch {
980-
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
981-
return device.hasTorch && device.hasFlash;
982-
}
983-
984-
+ (BOOL)isTorchOn {
985-
BOOL torchIsOn = NO;
986-
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
987-
if (device.hasTorch && device.hasFlash) {
988-
torchIsOn = device.torchActive;
989-
}
990-
return torchIsOn;
991-
}
992-
993953
+ (CGFloat)getBottomPadding {
994954
CGFloat bottomPadding = UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom;
995955
return bottomPadding;

0 commit comments

Comments
 (0)