@iternio/react-native-auto-play 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * An effect hook that only runs when the CarPlay/Android Auto screen is visible to the user and dependencies have changed.
3
+ * It behaves like `useEffect`, but the effect function is only executed if the screen is visible.
4
+ *
5
+ * @param moduleName The name of the root module to listen to for focus changes - one of AutoPlayModules or a cluster uuid.
6
+ * @param effect The effect function to run.
7
+ * @param deps An array of dependencies for the effect.
8
+ */
9
+ export declare function useFocusedEffect(moduleName: string, effect: () => void | (() => void), deps: readonly unknown[]): void;
@@ -0,0 +1,32 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { HybridAutoPlay } from '..';
3
+ /**
4
+ * An effect hook that only runs when the CarPlay/Android Auto screen is visible to the user and dependencies have changed.
5
+ * It behaves like `useEffect`, but the effect function is only executed if the screen is visible.
6
+ *
7
+ * @param moduleName The name of the root module to listen to for focus changes - one of AutoPlayModules or a cluster uuid.
8
+ * @param effect The effect function to run.
9
+ * @param deps An array of dependencies for the effect.
10
+ */
11
+ export function useFocusedEffect(moduleName, effect, deps) {
12
+ const [isFocused, setIsFocused] = useState(false);
13
+ const effectRef = useRef(effect);
14
+ useEffect(() => {
15
+ effectRef.current = effect;
16
+ }, [effect]);
17
+ useEffect(() => {
18
+ return HybridAutoPlay.addListenerRenderState(moduleName, (state) => {
19
+ if (state === 'willAppear' || state === 'willDisappear') {
20
+ // react on actual visibility changes only
21
+ return;
22
+ }
23
+ setIsFocused(state === 'didAppear');
24
+ });
25
+ }, [moduleName]);
26
+ useEffect(() => {
27
+ if (!isFocused) {
28
+ return;
29
+ }
30
+ return effectRef.current();
31
+ }, [isFocused, ...deps]);
32
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A hook to determine if the CarPlay/Android Auto screen is currently focused (visible).
3
+ *
4
+ * @param moduleName The name of the module to listen to.
5
+ * @returns `true` if the screen is focused, `false` otherwise.
6
+ */
7
+ export declare function useIsAutoPlayFocused(moduleName: string): boolean;
@@ -0,0 +1,20 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { HybridAutoPlay } from '..';
3
+ /**
4
+ * A hook to determine if the CarPlay/Android Auto screen is currently focused (visible).
5
+ *
6
+ * @param moduleName The name of the module to listen to.
7
+ * @returns `true` if the screen is focused, `false` otherwise.
8
+ */
9
+ export function useIsAutoPlayFocused(moduleName) {
10
+ const [isFocused, setIsFocused] = useState(false);
11
+ useEffect(() => {
12
+ const remove = HybridAutoPlay.addListenerRenderState(moduleName, (state) => {
13
+ setIsFocused(state === 'didAppear');
14
+ });
15
+ return () => {
16
+ remove();
17
+ };
18
+ }, [moduleName]);
19
+ return isFocused;
20
+ }
package/lib/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export declare enum AutoPlayModules {
13
13
  }
14
14
  export * from './components/SafeAreaView';
15
15
  export * from './hooks/useAndroidAutoTelemetry';
16
+ export * from './hooks/useFocusedEffect';
16
17
  export * from './hooks/useMapTemplate';
17
18
  export * from './hooks/useSafeAreaInsets';
18
19
  export * from './hooks/useVoiceInput';
package/lib/index.js CHANGED
@@ -18,6 +18,7 @@ export var AutoPlayModules;
18
18
  })(AutoPlayModules || (AutoPlayModules = {}));
19
19
  export * from './components/SafeAreaView';
20
20
  export * from './hooks/useAndroidAutoTelemetry';
21
+ export * from './hooks/useFocusedEffect';
21
22
  export * from './hooks/useMapTemplate';
22
23
  export * from './hooks/useSafeAreaInsets';
23
24
  export * from './hooks/useVoiceInput';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iternio/react-native-auto-play",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Android Auto and Apple CarPlay for react-native",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -0,0 +1,42 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { HybridAutoPlay } from '..';
3
+
4
+ /**
5
+ * An effect hook that only runs when the CarPlay/Android Auto screen is visible to the user and dependencies have changed.
6
+ * It behaves like `useEffect`, but the effect function is only executed if the screen is visible.
7
+ *
8
+ * @param moduleName The name of the root module to listen to for focus changes - one of AutoPlayModules or a cluster uuid.
9
+ * @param effect The effect function to run.
10
+ * @param deps An array of dependencies for the effect.
11
+ */
12
+ export function useFocusedEffect(
13
+ moduleName: string,
14
+ effect: () => void | (() => void),
15
+ deps: readonly unknown[]
16
+ ) {
17
+ const [isFocused, setIsFocused] = useState(false);
18
+ const effectRef = useRef(effect);
19
+
20
+ useEffect(() => {
21
+ effectRef.current = effect;
22
+ }, [effect]);
23
+
24
+ useEffect(() => {
25
+ return HybridAutoPlay.addListenerRenderState(moduleName, (state) => {
26
+ if (state === 'willAppear' || state === 'willDisappear') {
27
+ // react on actual visibility changes only
28
+ return;
29
+ }
30
+
31
+ setIsFocused(state === 'didAppear');
32
+ });
33
+ }, [moduleName]);
34
+
35
+ useEffect(() => {
36
+ if (!isFocused) {
37
+ return;
38
+ }
39
+
40
+ return effectRef.current();
41
+ }, [isFocused, ...deps]);
42
+ }
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export enum AutoPlayModules {
25
25
 
26
26
  export * from './components/SafeAreaView';
27
27
  export * from './hooks/useAndroidAutoTelemetry';
28
+ export * from './hooks/useFocusedEffect';
28
29
  export * from './hooks/useMapTemplate';
29
30
  export * from './hooks/useSafeAreaInsets';
30
31
  export * from './hooks/useVoiceInput';