Make Vibration.vibrate compatible with TurboModules (#27951)
Summary:
This PR fixes a compatibility issue with the Vibration module and TurboModules.
The TurboModules spec doesn't allow nullable arguments of type Number, causing the following problem:
![IMG_3758](https://user-images.githubusercontent.com/1247834/73803879-10be6f80-4790-11ea-92d4-a008f0007681.PNG)
[iOS] [Fixed] - Make Vibration library compatible with TurboModules.
Pull Request resolved: https://github.com/facebook/react-native/pull/27951
Test Plan:
Just submitted a PR to my own app to fix the issue [here](https://github.com/rainbow-me/rainbow/pull/340)
The problem should be reproducible on RNTester due to this line: 91f139b941/RNTester/js/examples/Vibration/VibrationExample.js (L66)
and should be working on this branch.
Reviewed By: TheSavior
Differential Revision: D19761064
Pulled By: hramos
fbshipit-source-id: 84f6b62a2734cc09d450e906b5866d4e9ce61124
This commit is contained in:
Родитель
8858d879eb
Коммит
f89c5098be
|
@ -2785,7 +2785,7 @@ namespace facebook {
|
|||
} // namespace facebook
|
||||
@protocol NativeVibrationSpec <RCTBridgeModule, RCTTurboModule>
|
||||
|
||||
- (void)vibrate:(NSNumber *)pattern;
|
||||
- (void)vibrate:(double)pattern;
|
||||
- (void)vibrateByPattern:(NSArray *)pattern
|
||||
repeat:(double)repeat;
|
||||
- (void)cancel;
|
||||
|
|
|
@ -15,7 +15,7 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
|
|||
|
||||
export interface Spec extends TurboModule {
|
||||
+getConstants: () => {||};
|
||||
+vibrate: (pattern?: ?number) => void;
|
||||
+vibrate: (pattern: number) => void;
|
||||
|
||||
// Android only
|
||||
+vibrateByPattern: (pattern: Array<number>, repeat: number) => void;
|
||||
|
|
|
@ -25,7 +25,7 @@ RCT_EXPORT_MODULE()
|
|||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(vibrate:(NSNumber *)pattern)
|
||||
RCT_EXPORT_METHOD(vibrate:(double)pattern)
|
||||
{
|
||||
[self vibrate];
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ const Platform = require('../Utilities/Platform');
|
|||
|
||||
let _vibrating: boolean = false;
|
||||
let _id: number = 0; // _id is necessary to prevent race condition.
|
||||
const _default_vibration_length = 400;
|
||||
|
||||
function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) {
|
||||
if (_vibrating) {
|
||||
|
@ -29,7 +30,7 @@ function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) {
|
|||
}
|
||||
_vibrating = true;
|
||||
if (pattern[0] === 0) {
|
||||
NativeVibration.vibrate();
|
||||
NativeVibration.vibrate(_default_vibration_length);
|
||||
pattern = pattern.slice(1);
|
||||
}
|
||||
if (pattern.length === 0) {
|
||||
|
@ -48,7 +49,7 @@ function vibrateScheduler(
|
|||
if (!_vibrating || id !== _id) {
|
||||
return;
|
||||
}
|
||||
NativeVibration.vibrate();
|
||||
NativeVibration.vibrate(_default_vibration_length);
|
||||
if (nextIndex >= pattern.length) {
|
||||
if (repeat) {
|
||||
nextIndex = 0;
|
||||
|
@ -70,7 +71,7 @@ const Vibration = {
|
|||
* See https://facebook.github.io/react-native/docs/vibration.html#vibrate
|
||||
*/
|
||||
vibrate: function(
|
||||
pattern: number | Array<number> = 400,
|
||||
pattern: number | Array<number> = _default_vibration_length,
|
||||
repeat: boolean = false,
|
||||
) {
|
||||
if (Platform.OS === 'android') {
|
||||
|
@ -86,7 +87,7 @@ const Vibration = {
|
|||
return;
|
||||
}
|
||||
if (typeof pattern === 'number') {
|
||||
NativeVibration.vibrate();
|
||||
NativeVibration.vibrate(pattern);
|
||||
} else if (Array.isArray(pattern)) {
|
||||
vibrateByPattern(pattern, repeat);
|
||||
} else {
|
||||
|
|
|
@ -31,5 +31,5 @@ public abstract class NativeVibrationSpec extends ReactContextBaseJavaModule imp
|
|||
public abstract void vibrateByPattern(ReadableArray pattern, double repeat);
|
||||
|
||||
@ReactMethod
|
||||
public abstract void vibrate(Double pattern);
|
||||
public abstract void vibrate(double pattern);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,9 @@ public class VibrationModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
|
||||
@ReactMethod
|
||||
public void vibrate(int duration) {
|
||||
public void vibrate(double durationDouble) {
|
||||
int duration = (int) durationDouble;
|
||||
|
||||
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
|
||||
if (v != null) {
|
||||
v.vibrate(duration);
|
||||
|
|
Загрузка…
Ссылка в новой задаче