Skip to content

Commit

Permalink
feat: timestamp (#174)
Browse files Browse the repository at this point in the history
## 📜 Description

Added `timestamp` property to events that are delivered via
EventEmitter.

## 💡 Motivation and Context

Since these events arrive asynchronously it's important to understand
the delay. Below is a table with typical delay across different devices:

|Device|AVG delay|
|--------------------------|-----------|
|Xiaomi Redmi Note 5 Pro  |       2-5ms|
|iPhone 6s                            |       4-8ms|
|Pixel 7 Pro                           |        0-1ms|

## 📢 Changelog

### JS
- added new property to `KeyboardEvents` module.

### iOS
- added new property to `KeyboardEvents` module.

### Android
- added new property to `KeyboardEvents` module.

## 🤔 How Has This Been Tested?

Tested manually on:
- Pixel 7 Pro (Android 13);
- iPhone 6s (iOS 15.5);
- iPhone 11 (iOS 16.5);
- Xiaomi Redmi Note 5 Pro (Android 9).

## 📸 Screenshots (if appropriate):

<img
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/c2c7e8c9-e769-4d38-96da-410672967326"
width="250px">

## 📝 Checklist

- [x] CI successfully passed
  • Loading branch information
kirillzyusko committed Jul 13, 2023
1 parent 88ff6e9 commit 43ddde7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
12 changes: 8 additions & 4 deletions FabricExample/src/screens/Examples/Events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,35 @@ function EventsListener() {

useEffect(() => {
const show = KeyboardEvents.addListener('keyboardWillShow', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'info',
text1: '⬆️ ⌨️ Keyboard will show',
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const shown = KeyboardEvents.addListener('keyboardDidShow', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'success',
text1: '⌨️ Keyboard is shown',
text2: `👋 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `👋 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const hide = KeyboardEvents.addListener('keyboardWillHide', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'info',
text1: '⬇️ ⌨️ Keyboard will hide',
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const hid = KeyboardEvents.addListener('keyboardDidHide', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'error',
text1: '⌨️ Keyboard is hidden',
text2: `🤐 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `🤐 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class KeyboardAnimationCallback(
val params: WritableMap = Arguments.createMap()
params.putDouble("height", height)
params.putInt("duration", duration)
params.putDouble("timestamp", System.currentTimeMillis().toDouble())

return params
}
Expand Down
12 changes: 8 additions & 4 deletions example/src/screens/Examples/Events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,35 @@ function EventsListener() {

useEffect(() => {
const show = KeyboardEvents.addListener('keyboardWillShow', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'info',
text1: '⬆️ ⌨️ Keyboard will show',
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const shown = KeyboardEvents.addListener('keyboardDidShow', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'success',
text1: '⌨️ Keyboard is shown',
text2: `👋 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `👋 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const hide = KeyboardEvents.addListener('keyboardWillHide', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'info',
text1: '⬇️ ⌨️ Keyboard will hide',
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `📲 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});
const hid = KeyboardEvents.addListener('keyboardDidHide', (e) => {
const delay = new Date().getTime() - e.timestamp;
Toast.show({
type: 'error',
text1: '⌨️ Keyboard is hidden',
text2: `🤐 Height: ${e.height}, duration: ${e.duration}ms`,
text2: `🤐 Height: ${e.height}, duration: ${e.duration}ms, delay: ${delay}ms`,
});
});

Expand Down
6 changes: 6 additions & 0 deletions ios/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ public extension CGFloat {
return interpolatedValue
}
}

public extension Date {
static var currentTimeStamp: Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
}
4 changes: 4 additions & 0 deletions ios/KeyboardMovementObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public class KeyboardMovementObserver: NSObject {
var data = [AnyHashable: Any]()
data["height"] = keyboardHeight
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp

onEvent("onKeyboardMoveStart", Float(keyboardHeight) as NSNumber, 1, duration as NSNumber)
onNotify("KeyboardController::keyboardWillShow", data)
Expand All @@ -163,6 +164,7 @@ public class KeyboardMovementObserver: NSObject {
var data = [AnyHashable: Any]()
data["height"] = 0
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp

onEvent("onKeyboardMoveStart", 0, 0, duration as NSNumber)
onNotify("KeyboardController::keyboardWillHide", data)
Expand All @@ -182,6 +184,7 @@ public class KeyboardMovementObserver: NSObject {
var data = [AnyHashable: Any]()
data["height"] = keyboardHeight
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp

onEvent("onKeyboardMoveEnd", keyboardHeight as NSNumber, 1, duration as NSNumber)
onNotify("KeyboardController::keyboardDidShow", data)
Expand All @@ -198,6 +201,7 @@ public class KeyboardMovementObserver: NSObject {
var data = [AnyHashable: Any]()
data["height"] = 0
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp

onEvent("onKeyboardMoveEnd", 0 as NSNumber, 0, duration as NSNumber)
onNotify("KeyboardController::keyboardDidHide", data)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type KeyboardControllerEvents =
export type KeyboardEventData = {
height: number;
duration: number;
timestamp: number;
};

// package types
Expand Down

0 comments on commit 43ddde7

Please sign in to comment.