| fix: make Vibration API consistent with Android
Co-authored-by: zheng-jun-feng<949815885@qq.com>
# message auto-generated for no-merge-commit merge:
merge 0.77.1-rc.1-ohos into 0.77.1-rc.1-ohos
fix: make Vibration API consistent with Android
Created-by: zheng-jun-feng
Commit-by: zheng-jun-feng
Merged-by: huangyouhua
Description: <!-- PLEASE REMOVE COMMENTS BEFORE MERGING TO AVOID INCLUDING THEM IN GIT HISTORY -->
## Description
<!--
Briefly describe the purpose of the MR, for example:
This PR mainly implements the function of remote debugging within the Metro LAN.
-->
https://gitcode.com/openharmony-sig/ohos_react_native/pull/997
In the above commit, we discussed a scenario where 72 is inconsistent with Android, and we treated it as a bug to fix.
Fixed commit:
https://gitcode.com/openharmony-sig/ohos_react_native/pull/1069
Now 72 has been aligned with Android, so 77 also needs to be aligned with Android.
## Changes
<!--
List main changes in bullet points that can be included in the changelog. Those changes should be written for
application developers and react third-party library authors. Internal changes shouldn't be listed. Please start each bullet point with:
- added
- fixed
- deprecated
- removed
If it is a change to add a new attribute, use [added] prefix, for example:
- [added] FlatList component adds inverted attribute
-->
## Test Plan
<!--
How to test this PR. For example:
1. Open the Tester project
2. Execute the npm run start command
...
-->
demo:
```
import React, { useState, useEffect } from 'react';
import { StyleSheet, Button, View, Vibration, Text } from 'react-native';
const App = () => {
const [vibrationPattern] = useState([5000, 3000, 5000, 1000]);
//On Android, the odd indices of the pattern array represent the vibration duration, while the even ones represent the separation time.
const [time, setTime] = useState(0);
const [isVibrating, setIsVibrating] = useState(false);
useEffect(() => {
let interval = null;
if (isVibrating) {
interval = setInterval(() => {
setTime(prevTime => prevTime + 1);
}, 1000);
} else {
clearInterval(interval);
setTime(0);
}
return () => clearInterval(interval);
}, [isVibrating]);
const startVibrationWithPattern = () => {
Vibration.vibrate(vibrationPattern, true);
setIsVibrating(true);
};
const stopVibrationAndResetTimer = () => {
Vibration.cancel();
setIsVibrating(false);
};
const vibrateOnce = () => {
Vibration.vibrate(400);
};
const vibrateWithPattern = () => {
Vibration.vibrate(vibrationPattern, false);
};
return (
<View style={styles.container}>
{/* stopwatch */}
<Text style={styles.timerText}>{时间: ${Math.floor(time / 60)}:${(time % 60).toString().padStart(2, '0')}}</Text>
{/* Vibration control */}
<Button title="Vibration for 0.4 seconds" onPress={vibrateOnce} />
<View style={styles.buttonSpacer} />
<Button title="Vibration by mode" onPress={vibrateWithPattern} />
<View style={styles.buttonSpacer} />
<Button title="Repetitive vibration mode" onPress={startVibrationWithPattern} />
<View style={styles.buttonSpacer} />
<Button title="Cancel vibration" onPress={stopVibrationAndResetTimer} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
buttonSpacer: {
marginVertical: 15,
},
timerText: {
fontSize: 24,
marginBottom: 20,
}
});
export default App;
```
## Checklist
<!--
Please conduct self inspection according to the following checklist before merging
After completing the self check, fill in "x" in [], For example:
- [x] Does not involve incompatible changes; if involved, has been reviewed accordingly.
-->
- [x] Does not involve incompatible changes; if involved, has been reviewed accordingly.
- [x] Does not impact performance, or performance testing has been conducted without degradation.
- [x] Complies with the relevant coding standards.
- [x] Does not involve documentation updates, or the documentation has been updated.
- [x] Meets testability requirements with necessary self-test cases, appropriate logging, or trace information added.
- [x] No illegal file inclusions exist, such as images or code.
See merge request: openharmony-sig/ohos_react_native!1081 | 11 个月前 |