-
-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rnmbxcodegen): add codegen to generate rn boilerplate
- Loading branch information
Showing
42 changed files
with
1,535 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationProviderManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.rnmapbox.rnmbx.components.location | ||
|
||
import com.rnmapbox.rnmbx.v11compat.Cancelable | ||
import com.mapbox.maps.MapView | ||
import com.mapbox.maps.plugin.locationcomponent.DefaultLocationProvider | ||
import com.mapbox.maps.plugin.locationcomponent.LocationProvider | ||
import com.mapbox.maps.plugin.locationcomponent.location | ||
import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView | ||
import java.lang.ref.WeakReference | ||
|
||
typealias CallbackType = (old: LocationProvider, new: LocationProvider) -> Unit; | ||
|
||
|
||
class CallbackList<T> { | ||
class Entry<T>(val callback: T, val list: WeakReference<CallbackList<T>>): Cancelable { | ||
|
||
override fun cancel() { | ||
list.get()?.remove(this) | ||
} | ||
} | ||
private val callbacks = mutableListOf<Entry<T>>() | ||
|
||
fun add(callback: T): Cancelable { | ||
val result = Entry<T>(callback, WeakReference(this)); | ||
callbacks.add(result) | ||
return result; | ||
} | ||
|
||
internal fun remove(entry: Entry<T>) { | ||
callbacks.remove(entry) | ||
} | ||
|
||
fun forEach(callback: (it: T) -> Unit) { | ||
callbacks.map { it.callback }.forEach(callback) | ||
} | ||
} | ||
|
||
|
||
class LocationProviderManager(val mapView: RNMBXMapView) { | ||
var callbacks = CallbackList<CallbackType>() | ||
|
||
fun getLocationProvider(mapView: MapView): LocationProvider { | ||
val location = mapView.location | ||
val result = location.getLocationProvider() | ||
|
||
if (result != null) { | ||
return result | ||
} else { | ||
val result = DefaultLocationProvider(mapView.context) | ||
location.setLocationProvider(result) | ||
return result | ||
} | ||
} | ||
|
||
fun update(provider: LocationProvider) { | ||
mapView.withMapView { mapView: MapView -> | ||
val location = mapView.location | ||
val oldProvider = location.getLocationProvider() | ||
location.setLocationProvider(provider) | ||
|
||
if (oldProvider != null) { | ||
apply(oldProvider, provider) | ||
} | ||
} | ||
} | ||
|
||
fun onChange(callback: CallbackType): Cancelable = | ||
callbacks.add(callback) | ||
|
||
fun apply(old: LocationProvider, new: LocationProvider) { | ||
callbacks.forEach { it(old, new) } | ||
} | ||
} |
Oops, something went wrong.