11package com.expofp
22
33import android.app.Application
4- import android.util.Log
5- import android.view.View
6- import com.expofp.common.GlobalLocationProvider
7- import com.expofp.crowdconnected.CrowdConnectedProvider
8- import com.expofp.crowdconnected.Mode
9- import com.expofp.fplan.FplanView
10- import com.expofp.fplan.models.FplanViewState
4+ import com.expofp.fplan.api.app.ExpoFpPlan
5+ import com.expofp.fplan.api.app.model.ExpoFpLinkType
6+ import com.expofp.fplan.api.locationProvider.IExpoFpLocationProvider
7+ import com.expofp.fplan.ui.ExpoFpView
118import com.facebook.react.bridge.ReadableMap
129import com.facebook.react.uimanager.SimpleViewManager
1310import com.facebook.react.uimanager.ThemedReactContext
1411import com.facebook.react.uimanager.annotations.ReactProp
15- import com.expofp.R
16- import com.expofp.fplan.contracts.DownloadOfflinePlanCallback
17- import com.expofp.fplan.models.OfflinePlanInfo
12+ import com.expofp.crowdconnected.ExpoFpCrowdConnectedLocationProvider
13+ // import com.expofp.crowdconnectedbackground.ExpoFpCrowdConnectedBackgroundLocationProvider
14+ import com.expofp.crowdconnected.ExpoFpCrowdConnectedLocationProviderSettings
15+ import com.expofp.crowdconnected.ExpoFpCrowdConnectedNavigationType
1816
19- class ExpofpViewManager : SimpleViewManager <View >() {
17+ class ExpofpViewManager : SimpleViewManager <ExpoFpView >() {
2018 private var reactContext: ThemedReactContext ? = null
2119
2220 override fun getName () = " ExpofpView"
2321
24- override fun createViewInstance (reactContext : ThemedReactContext ): View {
22+ override fun createViewInstance (reactContext : ThemedReactContext ): ExpoFpView {
2523 this .reactContext = reactContext
26- var view = FplanView (reactContext)
27-
28- return view;
24+ ExpoFpPlan .initialize(reactContext.applicationContext)
25+ return ExpoFpView (reactContext)
2926 }
3027
31- override fun onDropViewInstance (view : View ) {
32- (view as ? FplanView )?.destroy()
28+ override fun onDropViewInstance (view : ExpoFpView ) {
3329 super .onDropViewInstance(view)
3430 }
3531
3632 private fun getExpoKeyFromUrl (url : String ): String {
3733 return url.substringAfter(" https://" ).substringBefore(" .expofp.com" )
3834 }
3935
40- private fun openMapForUrl (view : FplanView , url : String ) {
41- val expoKey = getExpoKeyFromUrl(url)
42- val settings = com.expofp.fplan.models.Settings ().withGlobalLocationProvider()
43-
44- val offlinePlanManager = FplanView .getOfflinePlanManager(reactContext)
45- val latestOfflinePlan = offlinePlanManager.allOfflinePlansFromCache
46- .filter { offlinePlanInfo -> offlinePlanInfo.expoKey == expoKey }
47- .maxByOrNull { offlinePlanInfo -> offlinePlanInfo.version }
48-
49- if (latestOfflinePlan != null ) {
50- Log .d(" ExpofpModule" , latestOfflinePlan.expoKey)
51- view.openOfflinePlan(latestOfflinePlan, " " , settings)
52- return
53- }
54-
55- val ctx = this .reactContext ? : run {
56- view.load(url, settings)
57- return
58- }
59-
60- val am = ctx.assets
61- val cachePlanExists = try {
62- am.open(" ${expoKey} .zip" ).close()
63- true
64- } catch (e: Exception ) {
65- false
66- }
67-
68- if (cachePlanExists) {
69- try {
70- Log .d(" ExpofpModule" , " openZipFromAssets: ${expoKey} .zip" )
71- view.openZipFromAssets(" ${expoKey} .zip" , " " , settings, ctx)
72- return
73- } catch (e: Exception ) {
74- Log .d(" ExpofpModule" , " failed to open asset zip, loading url: $url " )
75- view.load(url, settings)
76- return
77- }
78- }
79-
80- Log .d(" ExpofpModule" , " asset zip not found, loading url: $url " )
81- view.load(url, settings)
82- }
83-
84- private fun triggerOfflinePlanDownload (expoKey : String ) {
85- val offlinePlanManager = FplanView .getOfflinePlanManager(reactContext)
86- offlinePlanManager.downloadOfflinePlanToCache(expoKey, object : DownloadOfflinePlanCallback {
87- override fun onCompleted (offlinePlanInfo : OfflinePlanInfo ) {
88- Log .d(" ExpofpModule" , " downloaded offline plan: ${offlinePlanInfo.expoKey} v${offlinePlanInfo.version} " )
89- }
90-
91- override fun onError (message : String ) {
92- Log .e(" ExpofpModule" , " offline plan download failed: $message " )
93- }
94- })
36+ private fun createCrowdConnectedProvider (settingsMap : ReadableMap ): IExpoFpLocationProvider ? {
37+ val context = reactContext?.applicationContext ? : return null
38+ val application = (context as ? Application ) ? : return null
39+
40+ val cc = if (settingsMap.hasKey(" crowdConnected" )) settingsMap.getMap(" crowdConnected" ) else settingsMap
41+ if (cc == null ) return null
42+
43+ val appKey = if (cc.hasKey(" appKey" )) cc.getString(" appKey" ) else null
44+ val token = if (cc.hasKey(" token" )) cc.getString(" token" ) else null
45+ val secret = if (cc.hasKey(" secret" )) cc.getString(" secret" ) else null
46+ if (appKey.isNullOrEmpty() || token.isNullOrEmpty() || secret.isNullOrEmpty()) return null
47+ val aliases = mutableMapOf<String , String >()
48+ aliases[" onesignal_user_id" ] = cc.getString(" oneSignalUserId" ) ? : " "
49+ val settings = ExpoFpCrowdConnectedLocationProviderSettings (
50+ appKey = appKey,
51+ token = token,
52+ secret = secret,
53+ navigationType = ExpoFpCrowdConnectedNavigationType .ALL ,
54+ isAllowedInBackground = false ,
55+ isHeadingEnabled = true ,
56+ aliases = aliases,
57+ notificationText = " Indoor navigation is active" ,
58+ serviceIcon = R .drawable.placeholder_icon
59+ )
60+
61+ // ExpoFpCrowdConnectedBackgroundLocationProvider(application, settings)
62+ return ExpoFpCrowdConnectedLocationProvider (application, settings)
9563 }
9664
9765 @ReactProp(name = " settings" )
98- fun setSettings (view : FplanView , settingsMap : ReadableMap ? ) {
99- println (" setSettings: $settingsMap " )
100- settingsMap?.let {
101- var appKey = settingsMap.getString(" appKey" )
102- val token = settingsMap.getString(" token" )
103- val secret = settingsMap.getString(" secret" )
104- if (appKey != null && token != null && secret != null ) {
105- val context = reactContext?.applicationContext ? : return
106- val application = context as ? Application ? : return
107- val aliases = mutableMapOf<String , String >()
108- aliases[" onesignal_user_id" ] = it.getString(" oneSignalUserId" ) ? : " "
109- val lpSettings = com.expofp.crowdconnected.Settings (
110- settingsMap.getString(" appKey" ) ? : " " ,
111- settingsMap.getString(" token" ) ? : " " ,
112- settingsMap.getString(" secret" ) ? : " " ,
113- Mode .IPS_AND_GPS ,
114- true ,
115- aliases
116- )
117- lpSettings.setServiceNotificationInfo(" Background Location is running" , R .drawable.placeholder_icon);
118-
119- val locationProvider = CrowdConnectedProvider (application, lpSettings)
120- // val locationProvider = CrowdConnectedBackgroundProvider(application, lpSettings)
121- GlobalLocationProvider .init (locationProvider)
122- GlobalLocationProvider .start()
123- }
124- if (view.state.equals(FplanViewState .Created )) {
125- val url = it.getString(" url" ) ? : " "
126- val expoKey = getExpoKeyFromUrl(url)
66+ fun setSettings (view : ExpoFpView , settingsMap : ReadableMap ? ) {
67+ if (settingsMap == null ) return
68+ val url = settingsMap.getString(" url" ) ? : return
69+ val context = reactContext?.applicationContext ? : return
70+ val expoKey = getExpoKeyFromUrl(url)
12771
128- openMapForUrl(view, url)
129- triggerOfflinePlanDownload(expoKey)
130- }
131- }
72+ ExpoFpPlan .initialize(context)
73+ val p = ExpoFpPlan .createPlanPresenter(planLink = ExpoFpLinkType .ExpoKey (expoKey))
74+ val ccProvider = createCrowdConnectedProvider(settingsMap)
75+ if (ccProvider != null ) p.setLocationProvider(ccProvider)
76+ view.attachPresenter(p)
13277 }
13378}
0 commit comments