Skip to content

Commit 1f39f6d

Browse files
authored
Merge pull request #46 from gitamego/feat/move-android-v5
feat(android): update to 5
2 parents befef73 + 8eb47f9 commit 1f39f6d

File tree

3 files changed

+56
-123
lines changed

3 files changed

+56
-123
lines changed

android/build.gradle

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ dependencies {
9191
implementation "com.facebook.react:react-native:+"
9292
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
9393

94-
implementation 'com.expofp:common:4.11.0'
95-
implementation 'com.expofp:fplan:4.11.0'
94+
implementation 'com.expofp:fplan:5.1.0'
9695

97-
implementation 'com.expofp:crowdconnected:4.11.0'
98-
// implementation 'com.expofp:crowdconnectedbackground:4.11.0'
96+
implementation 'com.expofp:crowdconnected:5.1.0'
97+
// implementation 'com.expofp:crowdconnectedbackground:5.1.0'
9998
implementation 'net.crowdconnected.android.core:android-core:2.1.0'
10099
implementation 'net.crowdconnected.android.ips:android-ips:2.1.0'
101100
implementation 'net.crowdconnected.android.geo:android-geo:2.1.0'
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
package com.expofp
22

3-
import com.facebook.react.bridge.NativeModule
3+
import android.util.Log
44
import com.facebook.react.bridge.ReactApplicationContext
5-
import com.facebook.react.bridge.ReactContext
65
import com.facebook.react.bridge.ReactContextBaseJavaModule
76
import com.facebook.react.bridge.ReactMethod
87
import com.facebook.react.bridge.Promise
9-
import com.expofp.fplan.SharedFplanView
10-
import com.expofp.fplan.models.Settings
11-
import com.facebook.react.bridge.UiThreadUtil
128

139
class ExpofpModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
1410
override fun getName() = "ExpofpModule"
1511

1612
@ReactMethod
1713
fun preload(url: String, promise: Promise) {
18-
try {
19-
val context = this.reactApplicationContext.applicationContext
20-
UiThreadUtil.runOnUiThread {
21-
SharedFplanView.preload(url, Settings(), context)
22-
}
23-
promise.resolve(null)
24-
} catch (e: Exception) {
25-
promise.reject("PRELOAD_ERROR", e.message)
26-
}
14+
Log.d("ExpofpModule", "preload: not implemented")
15+
promise.reject("PRELOAD_UNAVAILABLE", "ExpoFP Android v5 preload is not implemented yet")
2716
}
2817
}
Lines changed: 50 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,78 @@
11
package com.expofp
22

33
import 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
118
import com.facebook.react.bridge.ReadableMap
129
import com.facebook.react.uimanager.SimpleViewManager
1310
import com.facebook.react.uimanager.ThemedReactContext
1411
import 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

Comments
 (0)