-
-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
635 additions
and
24 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
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
55 changes: 55 additions & 0 deletions
55
...oid/src/main/java/com/rnmapbox/rnmbx/shape_animators/RNMBXMovePointShapeAnimatorModule.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,55 @@ | ||
package com.rnmapbox.rnmbx.shape_animators | ||
|
||
import com.facebook.react.bridge.ReadableArray | ||
import com.rnmapbox.rnmbx.NativeRNMBXMovePointShapeAnimatorModuleSpec | ||
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation | ||
|
||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.google.gson.JsonObject | ||
import com.mapbox.geojson.GeoJson | ||
import com.mapbox.geojson.Point | ||
import com.rnmapbox.rnmbx.NativeRNMBXPointAnnotationModuleSpec | ||
import com.rnmapbox.rnmbx.utils.Logger | ||
import com.rnmapbox.rnmbx.utils.ViewTagResolver | ||
import org.json.JSONObject | ||
import java.util.Date | ||
import java.util.Timer | ||
import java.util.TimerTask | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.DurationUnit | ||
|
||
|
||
/// Simple dummy animator that moves the point lng, lat by 0.01, 0.01 each second. | ||
class MovePointShapeAnimator(tag: Tag, val lng: Double, val lat: Double) : ShapeAnimatorCommon(tag) { | ||
override fun getAnimatedShape(timeSinceStart: Duration): Pair<GeoJson, Boolean> { | ||
return Pair(Point.fromLngLat(lng + timeSinceStart.toDouble(DurationUnit.SECONDS) * 0.01, lat + timeSinceStart.toDouble(DurationUnit.SECONDS) * 0.01), true); | ||
} | ||
} | ||
|
||
|
||
@ReactModule(name = RNMBXMovePointShapeAnimatorModule.NAME) | ||
class RNMBXMovePointShapeAnimatorModule(reactContext: ReactApplicationContext?, val shapeAnimatorManager: ShapeAnimatorManager) : | ||
NativeRNMBXMovePointShapeAnimatorModuleSpec(reactContext) { | ||
|
||
companion object { | ||
const val LOG_TAG = "RNMBXMovePointShapeAnimatorModule" | ||
const val NAME = "RNMBXMovePointShapeAnimatorModule" | ||
} | ||
|
||
@ReactMethod | ||
override fun start(tag: Double, promise: Promise?) { | ||
shapeAnimatorManager?.get(tag.toLong())?.let { | ||
it.start() | ||
} | ||
} | ||
|
||
@ReactMethod | ||
override fun create(tag: Double, from: ReadableArray, promise: Promise) { | ||
shapeAnimatorManager.add(MovePointShapeAnimator(tag.toLong(), from.getDouble(0), from.getDouble(1))) | ||
promise.resolve(tag.toInt()) | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
android/src/main/java/com/rnmapbox/rnmbx/shape_animators/ShapeAnimatorCommon.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,110 @@ | ||
package com.rnmapbox.rnmbx.shape_animators | ||
|
||
import com.facebook.react.bridge.UiThreadUtil.runOnUiThread | ||
import com.mapbox.geojson.GeoJson | ||
import com.rnmapbox.rnmbx.utils.Logger | ||
import org.json.JSONObject | ||
import java.util.Date | ||
import java.util.Timer | ||
import java.util.TimerTask | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
typealias Tag = Long | ||
|
||
interface ShapeAnimationConsumer { | ||
fun shapeUpdated(geoJson: GeoJson) | ||
} | ||
abstract class ShapeAnimator(val tag: Tag) { | ||
|
||
abstract fun getShape(): GeoJson; | ||
abstract fun start() | ||
|
||
abstract fun subscribe(consumer: ShapeAnimationConsumer) | ||
abstract fun unsubscribe(consumer: ShapeAnimationConsumer) | ||
} | ||
|
||
abstract class ShapeAnimatorCommon(tag: Tag): ShapeAnimator(tag) { | ||
var timer: Timer? = null | ||
var progress: Duration? = null | ||
|
||
// region subscribers | ||
var subscribers = mutableListOf<ShapeAnimationConsumer>() | ||
|
||
override fun subscribe(consumer: ShapeAnimationConsumer) { | ||
subscribers.add(consumer) | ||
} | ||
|
||
override fun unsubscribe(consumer: ShapeAnimationConsumer) { | ||
subscribers.remove(consumer) | ||
} | ||
// endregion | ||
|
||
override fun start() { | ||
timer?.let { it.cancel() } | ||
timer = null | ||
|
||
val fps = 30.0 | ||
val period = (1000.0 / fps).toLong() | ||
val start = Date() | ||
val timer = Timer() | ||
this.timer = timer | ||
val animator = this | ||
|
||
timer.schedule(object : TimerTask() { | ||
override fun run() { | ||
|
||
val now = Date() | ||
val diff = now.time - start.time | ||
val progress = diff.milliseconds | ||
animator.progress = progress | ||
|
||
val (shape,doContinue) = getAnimatedShape(progress) | ||
if (!doContinue) { | ||
timer.cancel() | ||
} | ||
runOnUiThread { | ||
subscribers.forEach { it.shapeUpdated(shape) } | ||
} | ||
} | ||
},0, period) | ||
} | ||
|
||
override fun getShape(): GeoJson { | ||
return getAnimatedShape(progress ?: 0.0.milliseconds).first | ||
} | ||
abstract fun getAnimatedShape(timeSinceStart: Duration): Pair<GeoJson,Boolean> | ||
|
||
} | ||
class ShapeAnimatorManager { | ||
private val animators = hashMapOf<Tag, ShapeAnimator>(); | ||
fun add(animator: ShapeAnimator) { | ||
animators.put(animator.tag, animator) | ||
} | ||
|
||
fun isShapeAnimatorTag(shape: String): Boolean { | ||
return shape.startsWith("{\"__nativeTag\":") | ||
} | ||
|
||
fun get(tag: String): ShapeAnimator? { | ||
if (isShapeAnimatorTag(tag)) { | ||
val obj = JSONObject(tag) | ||
val tag = obj.getLong("__nativeTag") | ||
return get(tag); | ||
} | ||
else { | ||
return null | ||
} | ||
} | ||
fun get(tag: Tag): ShapeAnimator? { | ||
val result = animators[tag] | ||
if (result == null) { | ||
Logger.e(LOG_TAG, "Shape animator for tag: $tag was not found") | ||
} | ||
return result | ||
} | ||
|
||
companion object { | ||
const val LOG_TAG = "RNMBXShapeAnimators" | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...oid/src/main/old-arch/com/rnmapbox/rnmbx/NativeRNMBXMovePointShapeAnimatorModuleSpec.java
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,43 @@ | ||
|
||
/** | ||
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). | ||
* | ||
* Do not edit this file as changes may cause incorrect behavior and will be lost | ||
* once the code is regenerated. | ||
* | ||
* @generated by codegen project: GenerateModuleJavaSpec.js | ||
* | ||
* @nolint | ||
*/ | ||
|
||
package com.rnmapbox.rnmbx; | ||
|
||
import com.facebook.proguard.annotations.DoNotStrip; | ||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule; | ||
import javax.annotation.Nonnull; | ||
|
||
public abstract class NativeRNMBXMovePointShapeAnimatorModuleSpec extends ReactContextBaseJavaModule implements TurboModule { | ||
public static final String NAME = "RNMBXMovePointShapeAnimatorModule"; | ||
|
||
public NativeRNMBXMovePointShapeAnimatorModuleSpec(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public @Nonnull String getName() { | ||
return NAME; | ||
} | ||
|
||
@ReactMethod | ||
@DoNotStrip | ||
public abstract void create(double tag, ReadableArray from, Promise promise); | ||
|
||
@ReactMethod | ||
@DoNotStrip | ||
public abstract void start(double tag, Promise promise); | ||
} |
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
Oops, something went wrong.