11import Client from "../api/client" ;
22import { isUndefined } from "lodash" ;
33import { createContext , ReactNode , SetStateAction , useContext , useEffect , useState } from "react" ;
4- import { CarPlay } from "react-native-carplay" ;
5- import CarPlayNavigation from "./CarPlay/Navigation" ;
6- import CarPlayNowPlaying from "./CarPlay/NowPlaying" ;
4+ import { Platform } from 'react-native'
5+ import { CarPlayInterface } from "react-native-carplay" ;
6+
7+ // 'react-native-carplay' has also been disabled for android builds in react-native.config.js
8+ const CarPlay = Platform . OS === 'ios' ? require ( 'react-native-carplay' ) . CarPlay as CarPlayInterface : null ;
9+ const CarPlayNavigation = CarPlay ? require ( './CarPlay/Navigation' ) . CarPlayNavigation : null ;
10+ const CarPlayNowPlaying = CarPlay ? require ( './CarPlay/NowPlaying' ) . CarPlayNowPlaying : null ;
711
812interface JellifyContext {
913 loggedIn : boolean ;
@@ -22,14 +26,14 @@ const JellifyContextInitializer = () => {
2226 ) ;
2327
2428
25- const [ carPlayConnected , setCarPlayConnected ] = useState ( CarPlay . connected ) ;
29+ const [ carPlayConnected , setCarPlayConnected ] = useState ( CarPlay ? CarPlay . connected : false ) ;
2630
2731 useEffect ( ( ) => {
2832
2933 function onConnect ( ) {
3034 setCarPlayConnected ( true ) ;
3135
32- if ( loggedIn ) {
36+ if ( loggedIn && CarPlay ) {
3337 CarPlay . setRootTemplate ( CarPlayNavigation , true ) ;
3438 CarPlay . pushTemplate ( CarPlayNowPlaying , true ) ;
3539 CarPlay . enableNowPlaying ( true ) ; // https://github.com/birkir/react-native-carplay/issues/185
@@ -40,12 +44,14 @@ const JellifyContextInitializer = () => {
4044 setCarPlayConnected ( false ) ;
4145 }
4246
43- CarPlay . registerOnConnect ( onConnect ) ;
44- CarPlay . registerOnDisconnect ( onDisconnect ) ;
45- return ( ) => {
46- CarPlay . unregisterOnConnect ( onConnect )
47- CarPlay . unregisterOnDisconnect ( onDisconnect )
48- } ;
47+ if ( CarPlay ) {
48+ CarPlay . registerOnConnect ( onConnect ) ;
49+ CarPlay . registerOnDisconnect ( onDisconnect ) ;
50+ return ( ) => {
51+ CarPlay . unregisterOnConnect ( onConnect )
52+ CarPlay . unregisterOnDisconnect ( onDisconnect )
53+ } ;
54+ }
4955 } ) ;
5056
5157 return {
0 commit comments