11package com.expofp
22
3- import android.Manifest
4- import android.app.Activity
5- import android.app.AlertDialog
63import android.app.Application
7- import android.os.Build
84import android.util.Log
95import android.view.View
10- import android.widget.Toast
11- import androidx.activity.result.contract.ActivityResultContracts
12- import androidx.core.app.ActivityCompat
136import com.expofp.common.GlobalLocationProvider
147import com.expofp.crowdconnected.CrowdConnectedProvider
158import com.expofp.crowdconnected.Mode
16- import com.expofp.crowdconnected.Settings
17- // import com.expofp.crowdconnectedbackground.CrowdConnectedBackgroundProvider
189import com.expofp.fplan.FplanView
1910import com.expofp.fplan.models.FplanViewState
2011import com.facebook.react.bridge.ReadableMap
2112import com.facebook.react.uimanager.SimpleViewManager
2213import com.facebook.react.uimanager.ThemedReactContext
2314import com.facebook.react.uimanager.annotations.ReactProp
2415import com.expofp.R
16+ import com.expofp.fplan.contracts.DownloadOfflinePlanCallback
17+ import com.expofp.fplan.models.OfflinePlanInfo
2518
2619class ExpofpViewManager : SimpleViewManager <View >() {
2720 private var reactContext: ThemedReactContext ? = null
@@ -40,6 +33,67 @@ class ExpofpViewManager : SimpleViewManager<View>() {
4033 super .onDropViewInstance(view)
4134 }
4235
36+ private fun getExpoKeyFromUrl (url : String ): String {
37+ return url.substringAfter(" https://" ).substringBefore(" .expofp.com" )
38+ }
39+
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+ })
95+ }
96+
4397 @ReactProp(name = " settings" )
4498 fun setSettings (view : FplanView , settingsMap : ReadableMap ? ) {
4599 println (" setSettings: $settingsMap " )
@@ -68,7 +122,11 @@ class ExpofpViewManager : SimpleViewManager<View>() {
68122 GlobalLocationProvider .start()
69123 }
70124 if (view.state.equals(FplanViewState .Created )) {
71- view.load(it.getString(" url" ) ? : " " , com.expofp.fplan.models.Settings ().withGlobalLocationProvider());
125+ val url = it.getString(" url" ) ? : " "
126+ val expoKey = getExpoKeyFromUrl(url)
127+
128+ openMapForUrl(view, url)
129+ triggerOfflinePlanDownload(expoKey)
72130 }
73131 }
74132 }
0 commit comments