|
1 | 1 | #import "Haptics.h" |
| 2 | +#import <React/RCTLog.h> |
2 | 3 |
|
3 | 4 | @implementation Haptics |
| 5 | + |
4 | 6 | RCT_EXPORT_MODULE() |
5 | 7 |
|
| 8 | ++ (BOOL)requiresMainQueueSetup |
| 9 | +{ |
| 10 | + return YES; |
| 11 | +} |
| 12 | + |
| 13 | +- (instancetype)init |
| 14 | +{ |
| 15 | + self = [super init]; |
| 16 | + if (self) { |
| 17 | + _notificationGenerator = [UINotificationFeedbackGenerator new]; |
| 18 | + [_notificationGenerator prepare]; |
| 19 | + |
| 20 | + _selectionGenerator = [UISelectionFeedbackGenerator new]; |
| 21 | + [_selectionGenerator prepare]; |
| 22 | + |
| 23 | + _impactStyles = @{ |
| 24 | + @"light": @(UIImpactFeedbackStyleLight), |
| 25 | + @"medium": @(UIImpactFeedbackStyleMedium), |
| 26 | + @"heavy": @(UIImpactFeedbackStyleHeavy), |
| 27 | + @"soft": @(UIImpactFeedbackStyleSoft), |
| 28 | + @"rigid": @(UIImpactFeedbackStyleRigid) |
| 29 | + }; |
| 30 | + |
| 31 | + _notificationTypes = @{ |
| 32 | + @"success": @(UINotificationFeedbackTypeSuccess), |
| 33 | + @"warning": @(UINotificationFeedbackTypeWarning), |
| 34 | + @"error": @(UINotificationFeedbackTypeError) |
| 35 | + }; |
| 36 | + } |
| 37 | + return self; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)impact:(NSString *)style |
| 41 | + resolve:(nonnull RCTPromiseResolveBlock)resolve |
| 42 | + reject:(nonnull RCTPromiseRejectBlock)reject |
| 43 | +{ |
| 44 | + NSNumber *styleValue = self.impactStyles[style]; |
| 45 | + |
| 46 | + if (!styleValue) { |
| 47 | + reject(@"E_INVALID_STYLE", [NSString stringWithFormat:@"Invalid impact style '%@'", style], nil); |
| 48 | + return; |
| 49 | + } |
| 50 | + UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] |
| 51 | + initWithStyle:(UIImpactFeedbackStyle)styleValue.integerValue]; |
| 52 | + [generator prepare]; |
| 53 | + [generator impactOccurred]; |
| 54 | + |
| 55 | + resolve(nil); |
| 56 | +} |
| 57 | + |
| 58 | +- (void)notification:(NSString *)type |
| 59 | + resolve:(nonnull RCTPromiseResolveBlock)resolve |
| 60 | + reject:(nonnull RCTPromiseRejectBlock)reject |
| 61 | +{ |
| 62 | + NSNumber *typeValue = self.notificationTypes[type]; |
| 63 | + |
| 64 | + if (!typeValue) { |
| 65 | + reject(@"E_INVALID_TYPE", [NSString stringWithFormat:@"Invalid notification type '%@'", type], nil); |
| 66 | + return; |
| 67 | + } |
| 68 | + [self.notificationGenerator notificationOccurred:(UINotificationFeedbackType)typeValue.integerValue]; |
| 69 | + resolve(nil); |
| 70 | +} |
| 71 | + |
| 72 | +- (void)selection:(nonnull RCTPromiseResolveBlock)resolve |
| 73 | + reject:(nonnull RCTPromiseRejectBlock)reject { |
| 74 | + [self.selectionGenerator prepare]; |
| 75 | + [self.selectionGenerator selectionChanged]; |
| 76 | + resolve(nil); |
| 77 | +} |
| 78 | + |
| 79 | +- (void)androidHaptics:(nonnull NSString *)type |
| 80 | + resolve:(nonnull RCTPromiseResolveBlock)resolve |
| 81 | + reject:(nonnull RCTPromiseRejectBlock)reject { |
| 82 | + resolve(nil); |
| 83 | +} |
| 84 | + |
6 | 85 | - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: |
7 | 86 | (const facebook::react::ObjCTurboModule::InitParams &)params |
8 | 87 | { |
|
0 commit comments