From 05a82600ae2ecd72117591a4842408dc2decadd3 Mon Sep 17 00:00:00 2001 From: AntonMeier Date: Wed, 18 May 2016 13:42:29 +0200 Subject: [PATCH] Added a fliclib wrapper that can be imported if you want to build for simulator --- README.md | 14 ++- simulator-wrapper/.DS_Store | Bin 0 -> 10244 bytes simulator-wrapper/SCLFlicButtonSimulator.h | 102 ++++++++++++++++++++ simulator-wrapper/SCLFlicButtonSimulator.m | 76 +++++++++++++++ simulator-wrapper/SCLFlicManagerSimulator.h | 57 +++++++++++ simulator-wrapper/SCLFlicManagerSimulator.m | 101 +++++++++++++++++++ simulator-wrapper/fliclibWrapper.h | 17 ++++ 7 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 simulator-wrapper/.DS_Store create mode 100644 simulator-wrapper/SCLFlicButtonSimulator.h create mode 100644 simulator-wrapper/SCLFlicButtonSimulator.m create mode 100644 simulator-wrapper/SCLFlicManagerSimulator.h create mode 100644 simulator-wrapper/SCLFlicManagerSimulator.m create mode 100644 simulator-wrapper/fliclibWrapper.h diff --git a/README.md b/README.md index 1896603..206cdad 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,16 @@ The fliclib framework for iOS. This framework works in collaboration with the Flic app so please make sure that you have it installed before you begin. Notice: The instructions and documentation available at our website have not been updated to work with the latest release of the framework. This will be done soon. -For instructions on how to implement this go to https://flic.io/partners/developers/ios-tutorial, or take a look at one of our example projects here on github. \ No newline at end of file +For instructions on how to implement this go to https://flic.io/partners/developers/ios-tutorial, or take a look at one of our example projects here on github. + +**Simulator Support** + +The framework itself does not contain slices for x86 and i386 (iOS Simulator). The reason for this is that if we were to add those slices to the framework then +they would have to be removed before App Store submission, which is not a very straightforward thing to do. So, for those who want to run the framework in the simulator +we instead provide a wrapper that adds a fake fliclib implementation when building for x86 and i386. + +To use this wrapper follow these steps: + +1. Include the files inside the `simulator-wrapper` folder to your project. +2. On all the places in your code where you need to import fliclib you should import the wrapper instead. In other words, replace `#import ` with `#import "fliclibWrapper.h"`. Of course, the file path will depend on how you have configured your project. For example, if you have the *fliclib-ios* repository as a git submodule in your project root then it might look like this `#import "../fliclib-ios/simulator-wrapper/fliclibWrapper.h"`. +3. Make sure that both `SCLFlicManagerSimulator.m` and `SCLFlicButtonSimulator.m` are added to *Compile Sources* in your project *Build Phases*. \ No newline at end of file diff --git a/simulator-wrapper/.DS_Store b/simulator-wrapper/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6eeb4ca9c1d081436c7a68e9ad76f6dc38f28231 GIT binary patch literal 10244 zcmeI1PgBA$5XE2A1EU^1>a9mFi24aCq9@b?I$lNmhf>=SnDN-NpW3&Zz@X^VcF^&# zVUkypY*T(q!t4UTHODu5Km?%1D!Q@G=8GcZQs>H2=vk#Y+5-dhag9s7pgT9)I*fo3 zFak!v2pEC?K!Cs5Qfg~ntu+Ehzz9qU$oUYlipD{6uX=Q_(GY;T!fq|RW*ne4QP4PO z?o~~BPai$FVCq7M;k-G=BbiQ(gXUhnIh;3#3oE-2p*UTgc9Bkpi@jQF1dPBu0g2r= zaHo-b??e2aA;T>r`5ioDqwDr~IC|mSH(X$-{X;YuJ(j6H;Cebj6I?rS&bXQjR?G?se?*33sWF zT>BYMC2W7bN@Mzzvl)&Fp~epxo(6`6ym`M(vN=Q>$H6mAe!3A~!n#8u3Ez7fsrg91 z3B^9D!gS)Z(zX?RnAkom+-GI|AWWhbg z)|EVI`|&qCIlCjvds6nK6(e8-jDQg^0!E-Zfn{$RN&f%n`~Ux|FJP8NzzF<%0&b<% zX*KznA-6vFz>;f^Sg%;6$h_REny}GyJXO>2d?V@jZC07PD=LN8anRf=dszPb&j88d c^-z>kv-p2D)4IrPY5BNMa?lq4FEH=@4niSJ(f|Me literal 0 HcmV?d00001 diff --git a/simulator-wrapper/SCLFlicButtonSimulator.h b/simulator-wrapper/SCLFlicButtonSimulator.h new file mode 100644 index 0000000..a2d1125 --- /dev/null +++ b/simulator-wrapper/SCLFlicButtonSimulator.h @@ -0,0 +1,102 @@ +// +// @file SCLFlicButtonSimulator.h +// @framework fliclib +// +// Created by Anton Meier on 2016-06-17. +// Copyright (c) 2016 Shortcut Labs. All rights reserved. +// + +#import +@class UIColor; + +typedef NS_ENUM(NSInteger, SCLFlicButtonConnectionState) { + SCLFlicButtonConnectionStateConnected = 0, + SCLFlicButtonConnectionStateConnecting, + SCLFlicButtonConnectionStateDisconnected, + SCLFlicButtonConnectionStateDisconnecting, +}; + +typedef NS_ENUM(NSInteger, SCLFlicButtonLEDIndicateCount) { + SCLFlicButtonLEDIndicateCount1 = 1, + SCLFlicButtonLEDIndicateCount2, + SCLFlicButtonLEDIndicateCount3, + SCLFlicButtonLEDIndicateCount4, + SCLFlicButtonLEDIndicateCount5, +}; + +typedef NS_ENUM(NSInteger, SCLFlicButtonTriggerBehavior) { + SCLFlicButtonTriggerBehaviorClickAndHold = 0, + SCLFlicButtonTriggerBehaviorClickAndDoubleClick, + SCLFlicButtonTriggerBehaviorClickAndDoubleClickAndHold, +}; + +typedef NS_ENUM(NSInteger, SCLFlicError) { + SCLFlicErrorUnknown = 0, + SCLFlicErrorCouldNotCompleteTask = 1, + SCLFlicErrorConnectionFailed = 2, + SCLFlicErrorCouldNotUpdateRSSI = 3, + SCLFlicErrorButtonIsPrivate = 10, + SCLFlicErrorCryptographicFailure = 11, + SCLFlicErrorMissingData = 13, + SCLFlicErrorInvalidSignature = 14, + SCLFlicErrorButtonAlreadyGrabbed = 15, + SCLFlicErrorBluetoothErrorUnknown = 100, + SCLFlicErrorBluetoothErrorInvalidParameters = 101, + SCLFlicErrorBluetoothErrorInvalidHandle = 102, + SCLFlicErrorBluetoothErrorNotConnected = 103, + SCLFlicErrorBluetoothErrorOutOfSpace = 104, + SCLFlicErrorBluetoothErrorOperationCancelled = 105, + SCLFlicErrorBluetoothErrorConnectionLost = 106, + SCLFlicErrorBluetoothErrorPeripheralDisconnected = 107, + SCLFlicErrorBluetoothErrorUUIDNotAllowed = 108, + SCLFlicErrorBluetoothErrorAlreadyAdvertising = 109, + SCLFlicErrorBluetoothErrorConnectionFailed = 110, + SCLFlicErrorBluetoothErrorConnectionLimitReached = 111, + SCLFlicErrorFlicRefusedConnection = 200, +}; + +@protocol SCLFlicButtonDelegate; + +@interface SCLFlicButton : NSObject { + +} + +@property(weak, nonatomic, nullable) id delegate; +@property (readonly, nonatomic, strong, nonnull) NSUUID *buttonIdentifier; +@property (readonly, nonatomic, strong, nonnull) NSString *buttonPublicKey; +@property (atomic, readonly, strong, nonnull) NSString *name; +@property (atomic, readonly, strong, nonnull) UIColor *color; +@property (atomic, readonly, strong, nonnull) NSString *userAssignedName; +@property (atomic, readonly) SCLFlicButtonConnectionState connectionState; +@property (nonatomic, readwrite) BOOL lowLatency; +@property (nonatomic, readwrite) SCLFlicButtonTriggerBehavior triggerBehavior; +@property (nonatomic, readonly) int pressCount; +@property (readonly) BOOL isReady; + + +- (void) connect; +- (void) disconnect; +- (void) indicateLED: (SCLFlicButtonLEDIndicateCount) count; +- (void) readRSSI; + +@end + +@protocol SCLFlicButtonDelegate + +@required + +@optional + +- (void) flicButton:(SCLFlicButton * _Nonnull) button didReceiveButtonDown:(BOOL) queued age: (NSInteger) age; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didReceiveButtonUp:(BOOL) queued age: (NSInteger) age; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didReceiveButtonClick:(BOOL) queued age: (NSInteger) age; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didReceiveButtonDoubleClick:(BOOL) queued age: (NSInteger) age; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didReceiveButtonHold:(BOOL) queued age: (NSInteger) age; +- (void) flicButtonDidConnect:(SCLFlicButton * _Nonnull) button; +- (void) flicButtonIsReady:(SCLFlicButton * _Nonnull) button; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didDisconnectWithError:(NSError * _Nullable) error; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didFailToConnectWithError:(NSError * _Nullable) error; +- (void) flicButton:(SCLFlicButton * _Nonnull) button didUpdateRSSI:(NSNumber * _Nonnull) RSSI error:(NSError * _Nullable) error; + +@end + diff --git a/simulator-wrapper/SCLFlicButtonSimulator.m b/simulator-wrapper/SCLFlicButtonSimulator.m new file mode 100644 index 0000000..63fb597 --- /dev/null +++ b/simulator-wrapper/SCLFlicButtonSimulator.m @@ -0,0 +1,76 @@ +// +// @file SCLFlicButtonSimulator.m +// @framework fliclib +// +// Created by Anton Meier on 2016-06-17. +// Copyright (c) 2016 Shortcut Labs. All rights reserved. +// + +#import "SCLFlicButtonSimulator.h" +#if TARGET_OS_SIMULATOR + +#import + +@implementation SCLFlicButton + +- (instancetype) init { + self = [super init]; + if ( self != nil ) { + NSLog(@"SCLFlicButtonSimulator init"); + _isReady = YES; + _lowLatency = NO; + _buttonIdentifier = [NSUUID UUID]; + _name=[NSString stringWithFormat:@"FLIC: %@", _buttonIdentifier.UUIDString ]; + _connectionState=SCLFlicButtonConnectionStateConnected; + _triggerBehavior = SCLFlicButtonTriggerBehaviorClickAndHold; + _pressCount = 0; + _buttonPublicKey = _buttonIdentifier.UUIDString; + _userAssignedName = @"My Flic"; + _color = [UIColor whiteColor]; + + // Uncomment to recieve a button down 2 seconds after grab. + /* + __weak SCLFlicButton *flicButton = self; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + if ([flicButton.delegate respondsToSelector:@selector(flicButton:didReceiveButtonDown:age:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [flicButton.delegate flicButton:self didReceiveButtonDown:NO age:0]; }); + } + }); + */ + return self; + } + return nil; +} + +- (void) connect { + _connectionState = SCLFlicButtonConnectionStateConnected; + if ([self.delegate respondsToSelector:@selector(flicButtonDidConnect:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicButtonDidConnect:self]; }); + } + _isReady = YES; + if ([self.delegate respondsToSelector:@selector(flicButtonIsReady:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicButtonIsReady:self]; }); + } +} + +- (void) disconnect { + _connectionState = SCLFlicButtonConnectionStateConnected; + _isReady = NO; + if ([self.delegate respondsToSelector:@selector(flicButton:didDisconnectWithError:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicButton:self didDisconnectWithError:nil]; }); + } +} + +- (void) indicateLED: (SCLFlicButtonLEDIndicateCount) count { + +} + +- (void) readRSSI { + if ([self.delegate respondsToSelector:@selector(flicButton:didUpdateRSSI:error:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicButton:self didUpdateRSSI:[NSNumber numberWithInt: -1] error:nil]; }); + } +} + +@end + +#endif diff --git a/simulator-wrapper/SCLFlicManagerSimulator.h b/simulator-wrapper/SCLFlicManagerSimulator.h new file mode 100644 index 0000000..5d08070 --- /dev/null +++ b/simulator-wrapper/SCLFlicManagerSimulator.h @@ -0,0 +1,57 @@ +// +// @file SCLFlicManagerSimulator.h +// @framework fliclib +// +// Created by Anton Meier on 2016-05-17. +// Copyright (c) 2016 Shortcut Labs. All rights reserved. +// + +#import +#import "SCLFlicButtonSimulator.h" + +typedef NS_ENUM(NSInteger, SCLFlicManagerBluetoothState) { + SCLFlicManagerBluetoothStatePoweredOn = 0, + SCLFlicManagerBluetoothStatePoweredOff, + SCLFlicManagerBluetoothStateResetting, + SCLFlicManagerBluetoothStateUnsupported, + SCLFlicManagerBluetoothStateUnauthorized, + SCLFlicManagerBluetoothStateUnknown, +}; + +@protocol SCLFlicManagerDelegate; + +@interface SCLFlicManager : NSObject { + +} + +@property(weak, nonatomic, nullable) id delegate; +@property(weak, nonatomic, nullable) id defaultButtonDelegate; +@property (nonatomic, readonly) SCLFlicManagerBluetoothState bluetoothState; +@property (readonly, getter=isEnabled) BOOL enabled; + ++ (instancetype _Nullable) sharedManager; ++ (instancetype _Nullable) configureWithDelegate:(id _Nullable) delegate defaultButtonDelegate: (id _Nullable) buttonDelegate appID: (NSString * _Nonnull) appID appSecret: (NSString * _Nonnull) appSecret backgroundExecution: (BOOL) bExecution; +- (NSDictionary * _Nonnull) knownButtons; +- (void) forgetButton:(SCLFlicButton * _Nonnull) button; +- (void) disable; +- (void) enable; +- (void) grabFlicFromFlicAppWithCallbackUrlScheme: (NSString * _Nonnull) scheme; +- (BOOL) handleOpenURL: (NSURL * _Nonnull) url; +- (void) onLocationChange; + +@end + + +@protocol SCLFlicManagerDelegate + +@required + +- (void) flicManager:(SCLFlicManager * _Nonnull) manager didGrabFlicButton:(SCLFlicButton * _Nullable) button withError: (NSError * _Nullable) error; + +@optional + +- (void) flicManager:(SCLFlicManager * _Nonnull) manager didChangeBluetoothState: (SCLFlicManagerBluetoothState) state; +- (void) flicManagerDidRestoreState:(SCLFlicManager * _Nonnull) manager; +- (void) flicManager:(SCLFlicManager * _Nonnull) manager didForgetButton:(NSUUID * _Nonnull) buttonIdentifier error:(NSError * _Nullable)error; + +@end diff --git a/simulator-wrapper/SCLFlicManagerSimulator.m b/simulator-wrapper/SCLFlicManagerSimulator.m new file mode 100644 index 0000000..822c167 --- /dev/null +++ b/simulator-wrapper/SCLFlicManagerSimulator.m @@ -0,0 +1,101 @@ +// +// @file SCLFlicManagerSimulator.m +// @framework fliclib +// +// Created by Anton Meier on 2016-05-17. +// Copyright (c) 2016 Shortcut Labs. All rights reserved. +// + +#import "SCLFlicManagerSimulator.h" +#if TARGET_OS_SIMULATOR + +@interface SCLFlicManager () + +@property (nonatomic, strong) NSMutableDictionary *buttonDictionary; + +@end + +@interface SCLFlicButton () + +- (instancetype) init; + +@end + +@implementation SCLFlicManager + +static SCLFlicManager *_sharedManager = nil; + ++ (SCLFlicManager *) sharedManager; { + return _sharedManager; +} + ++ (instancetype _Nullable) configureWithDelegate:(id _Nullable) delegate defaultButtonDelegate: (id _Nullable) buttonDelegate appID: (NSString * _Nonnull) appID appSecret: (NSString * _Nonnull) appSecret backgroundExecution: (BOOL) bExecution; { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _sharedManager = [[SCLFlicManager alloc] initWithDelegate:delegate buttonDelegate:buttonDelegate]; + if ([SCLFlicManager sharedManager].delegate && [[SCLFlicManager sharedManager].delegate respondsToSelector:@selector(flicManagerDidRestoreState:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [[SCLFlicManager sharedManager].delegate flicManagerDidRestoreState:[SCLFlicManager sharedManager]]; }); + } + if ([SCLFlicManager sharedManager].delegate && [[SCLFlicManager sharedManager].delegate respondsToSelector:@selector(flicManager:didChangeBluetoothState:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [[SCLFlicManager sharedManager].delegate flicManager:[SCLFlicManager sharedManager] didChangeBluetoothState:[SCLFlicManager sharedManager].bluetoothState]; }); + } + }); + + return _sharedManager; +} + +- (instancetype) initWithDelegate: (id _Nullable) delegate buttonDelegate: (id _Nullable) buttonDelegate { + self = [super init]; + if ( self != nil ) { + NSLog(@"SCLFlicManagerSimulator init"); + _enabled = YES; + _delegate = delegate; + _buttonDictionary = [NSMutableDictionary dictionary]; + _defaultButtonDelegate = buttonDelegate; + _bluetoothState = SCLFlicManagerBluetoothStatePoweredOn; + return self; + } + return nil; +} + +- (void) forgetButton:(SCLFlicButton *) button { + if ([self.buttonDictionary objectForKey:button.buttonIdentifier]) { + [self.buttonDictionary removeObjectForKey:button.buttonIdentifier]; + if (self.delegate && [self.delegate respondsToSelector:@selector(flicManager:didForgetButton:error:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicManager:self didForgetButton:button.buttonIdentifier error:nil]; }); + } + } +} + +- (void) disable { + _enabled = NO; +} + +- (void) enable { + _enabled = YES; +} + +- (NSDictionary*) knownButtons { + return [self.buttonDictionary copy]; +} + +- (void) onLocationChange { + +} + +- (BOOL) handleOpenURL: (NSURL * _Nonnull) url; { + return YES; +} + +- (void) grabFlicFromFlicAppWithCallbackUrlScheme: (NSString *) scheme { + SCLFlicButton *button = [[SCLFlicButton alloc] init]; + button.delegate = self.defaultButtonDelegate; + [self.buttonDictionary setObject:button forKey:button.buttonIdentifier]; + if (self.delegate && [self.delegate respondsToSelector:@selector(flicManager:didGrabFlicButton:withError:)]) { + dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate flicManager:self didGrabFlicButton:button withError:nil]; }); + } +} + +@end + +#endif diff --git a/simulator-wrapper/fliclibWrapper.h b/simulator-wrapper/fliclibWrapper.h new file mode 100644 index 0000000..2324f48 --- /dev/null +++ b/simulator-wrapper/fliclibWrapper.h @@ -0,0 +1,17 @@ +// +// fliclibWrapper.h +// fliclib +// +// Created by Anton Meier on 2016-04-20. +// Copyright (c) 2016 Shortcut Labs. All rights reserved. +// + +#if TARGET_OS_SIMULATOR + +#import "SCLFlicManagerSimulator.h" + +#else + +#import + +#endif