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

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.
@@ -41,6 +41,7 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
41
41
  var onTripStarted: ((_ tripId: String, _ routeId: String) -> Void)?
42
42
  var navigationSession: CPNavigationSession?
43
43
  var navigationAlert: NavigationAlertWrapper?
44
+ var currentTripId: String?
44
45
 
45
46
  var tripSelectorVisible = false
46
47
 
@@ -463,7 +464,7 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
463
464
  let selectedTrip = selectedTripId.flatMap { tripId in
464
465
  tripPreviews.first(where: { $0.id == tripId })
465
466
  }
466
-
467
+
467
468
  template.showTripPreviews(
468
469
  tripPreviews,
469
470
  selectedTrip: selectedTrip,
@@ -494,6 +495,7 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
494
495
  }
495
496
 
496
497
  func hideTripSelector() {
498
+ currentTripId = nil;
497
499
  template.hideTripPreviews()
498
500
 
499
501
  tripSelectorVisible = false
@@ -509,6 +511,13 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
509
511
  using routeChoice: CPRouteChoice
510
512
  ) {
511
513
  let tripId = trip.id
514
+
515
+ if (currentTripId != nil && currentTripId == tripId) {
516
+ return
517
+ }
518
+
519
+ currentTripId = trip.id
520
+
512
521
  let routeId = routeChoice.id
513
522
  self.onTripSelected?(tripId, routeId)
514
523
 
@@ -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
+ }
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';
@@ -0,0 +1,8 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+ import type { CleanupCallback } from '../types/Event';
3
+ export interface VoiceInput extends HybridObject<{
4
+ android: 'kotlin';
5
+ ios: 'swift';
6
+ }> {
7
+ registerVoiceInputListener(callback: (voiceInputResult?: string, error?: string) => void): CleanupCallback;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
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.5",
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';