@iternio/react-native-auto-play 0.1.8 → 0.1.10

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.
@@ -5,12 +5,14 @@ import com.margelo.nitro.core.Promise
5
5
  import com.margelo.nitro.swe.iternio.reactnativeautoplay.template.AndroidAutoTemplate
6
6
  import com.margelo.nitro.swe.iternio.reactnativeautoplay.template.MessageTemplate
7
7
  import com.margelo.nitro.swe.iternio.reactnativeautoplay.utils.ThreadUtil
8
+ import java.util.concurrent.ConcurrentHashMap
9
+ import java.util.concurrent.CopyOnWriteArrayList
8
10
 
9
11
  class HybridAutoPlay : HybridAutoPlaySpec() {
10
12
  override fun addListener(
11
13
  eventType: EventName, callback: () -> Unit
12
14
  ): () -> Unit {
13
- val callbacks = listeners.getOrPut(eventType) { mutableListOf() }
15
+ val callbacks = listeners.getOrPut(eventType) { CopyOnWriteArrayList() }
14
16
  callbacks.add(callback)
15
17
 
16
18
  if (eventType == EventName.DIDCONNECT && AndroidAutoSession.getIsConnected()) {
@@ -30,7 +32,7 @@ class HybridAutoPlay : HybridAutoPlaySpec() {
30
32
  moduleName: String, callback: (VisibilityState) -> Unit
31
33
  ): () -> Unit {
32
34
  val callbacks = renderStateListeners.getOrPut(moduleName) {
33
- mutableListOf()
35
+ CopyOnWriteArrayList()
34
36
  }
35
37
  callbacks.add(callback)
36
38
 
@@ -56,7 +58,7 @@ class HybridAutoPlay : HybridAutoPlaySpec() {
56
58
  moduleName: String, callback: (SafeAreaInsets) -> Unit
57
59
  ): () -> Unit {
58
60
  val callbacks = safeAreaInsetsListeners.getOrPut(moduleName) {
59
- mutableListOf()
61
+ CopyOnWriteArrayList()
60
62
  }
61
63
  callbacks.add(callback)
62
64
 
@@ -224,7 +226,6 @@ class HybridAutoPlay : HybridAutoPlaySpec() {
224
226
  }
225
227
 
226
228
 
227
-
228
229
  override fun addListenerVoiceInput(callback: (Location?, String?) -> Unit): () -> Unit {
229
230
  voiceInputListeners.add(callback)
230
231
 
@@ -236,15 +237,15 @@ class HybridAutoPlay : HybridAutoPlaySpec() {
236
237
  companion object {
237
238
  const val TAG = "HybridAutoPlay"
238
239
 
239
- private val listeners = mutableMapOf<EventName, MutableList<() -> Unit>>()
240
+ private val listeners = ConcurrentHashMap<EventName, CopyOnWriteArrayList<() -> Unit>>()
240
241
 
241
242
  private val renderStateListeners =
242
- mutableMapOf<String, MutableList<(VisibilityState) -> Unit>>()
243
+ ConcurrentHashMap<String, CopyOnWriteArrayList<(VisibilityState) -> Unit>>()
243
244
 
244
- private val voiceInputListeners = mutableListOf<(Location?, String?) -> Unit>()
245
+ private val voiceInputListeners = CopyOnWriteArrayList<(Location?, String?) -> Unit>()
245
246
 
246
247
  private val safeAreaInsetsListeners =
247
- mutableMapOf<String, MutableList<(SafeAreaInsets) -> Unit>>()
248
+ ConcurrentHashMap<String, CopyOnWriteArrayList<(SafeAreaInsets) -> Unit>>()
248
249
 
249
250
 
250
251
  fun removeListeners(templateId: String) {
@@ -2,12 +2,14 @@ package com.margelo.nitro.swe.iternio.reactnativeautoplay
2
2
 
3
3
  import com.margelo.nitro.core.Promise
4
4
  import com.margelo.nitro.swe.iternio.reactnativeautoplay.utils.ThreadUtil
5
+ import java.util.concurrent.ConcurrentHashMap
6
+ import java.util.concurrent.CopyOnWriteArrayList
5
7
 
6
8
  class HybridCluster : HybridClusterSpec() {
7
9
  override fun addListener(
8
10
  eventType: ClusterEventName, callback: (String) -> Unit
9
11
  ): () -> Unit {
10
- val callbacks = listeners.getOrPut(eventType) { mutableListOf() }
12
+ val callbacks = listeners.getOrPut(eventType) { CopyOnWriteArrayList() }
11
13
  callbacks.add(callback)
12
14
 
13
15
  eventQueue[eventType]?.forEach {
@@ -67,8 +69,8 @@ class HybridCluster : HybridClusterSpec() {
67
69
  const val TAG = "HybridCluster"
68
70
 
69
71
  private val listeners =
70
- mutableMapOf<ClusterEventName, MutableList<(clusterId: String) -> Unit>>()
71
- private val eventQueue = mutableMapOf<ClusterEventName, Array<String>>()
72
+ ConcurrentHashMap<ClusterEventName, CopyOnWriteArrayList<(clusterId: String) -> Unit>>()
73
+ private val eventQueue = ConcurrentHashMap<ClusterEventName, Array<String>>()
72
74
 
73
75
  private val colorSchemeListeners =
74
76
  mutableListOf<(clusterId: String, colorScheme: ColorScheme) -> Unit>()
@@ -533,15 +533,6 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
533
533
  startedTrip trip: CPTrip,
534
534
  using routeChoice: CPRouteChoice
535
535
  ) {
536
- if let onTripStarted = self.onTripStarted {
537
- let tripId = trip.id
538
- let routeId = routeChoice.id
539
-
540
- onTripStarted(tripId, routeId)
541
- }
542
-
543
- hideTripSelector()
544
-
545
536
  let trip = CPTrip(
546
537
  origin: trip.origin,
547
538
  destination: trip.destination,
@@ -550,6 +541,15 @@ class MapTemplate: NSObject, AutoPlayTemplate, AutoPlayHeaderProviding,
550
541
  )
551
542
 
552
543
  startNavigation(trip: trip)
544
+
545
+ if let onTripStarted = self.onTripStarted {
546
+ let tripId = trip.id
547
+ let routeId = routeChoice.id
548
+
549
+ onTripStarted(tripId, routeId)
550
+ }
551
+
552
+ hideTripSelector()
553
553
  }
554
554
 
555
555
  func updateVisibleTravelEstimate(
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ import type { AutoPlay as NitroAutoPlay } from './specs/AutoPlay.nitro';
2
+ export declare const HybridAutoPlay: NitroAutoPlay;
package/lib/hybrid.js ADDED
@@ -0,0 +1,2 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ export const HybridAutoPlay = NitroModules.createHybridObject('AutoPlay');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iternio/react-native-auto-play",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Android Auto and Apple CarPlay for react-native",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -1,8 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export {};