Skip to content

Commit

Permalink
feat: Support specifying animation duration for camera changes. (#83)
Browse files Browse the repository at this point in the history
* feat: Support specifying animation duration for camera changes.

Change-Id: I18e60574610a5c248627af75369498c9e3e16889

* Use null to indicate default animation duration.

Change-Id: I13fcb192b0e43d40380d07ed2933cd2c29b7d14a

* Use Int.MAX_VALUE for default animation.

Change-Id: Ib77e018ad175e591419191a736d38c1b01bf3c45

* Check for MAX_VALUE for default animation.

Change-Id: I65c007e74dc4d5f2d9b3469d2d4edf4c542fad72
  • Loading branch information
arriolac committed Mar 31, 2022
1 parent a580cc2 commit aa02773
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.suspendCancellableCoroutine
import java.lang.Integer.MAX_VALUE
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

Expand Down Expand Up @@ -173,9 +174,14 @@ class CameraPositionState(
* suspend until a map is bound and animation will begin.
*
* This method should only be called from a dispatcher bound to the map's UI thread.
*
* @param update the change that should be applied to the camera
* @param durationMs The duration of the animation in milliseconds. If [Int.MAX_VALUE] is
* provided, the default animation duration will be used. Otherwise, the value provided must be
* strictly positive, otherwise an [IllegalArgumentException] will be thrown.
*/
@UiThread
suspend fun animate(update: CameraUpdate) {
suspend fun animate(update: CameraUpdate, durationMs: Int = MAX_VALUE) {
val myJob = currentCoroutineContext()[Job]
try {
suspendCancellableCoroutine<Unit> { continuation ->
Expand All @@ -195,7 +201,7 @@ class CameraPositionState(
"internal error; no GoogleMap available to animate position"
)
}
performAnimateCameraLocked(newMap, update, continuation)
performAnimateCameraLocked(newMap, update, durationMs, continuation)
}

override fun onCancelLocked() {
Expand All @@ -216,7 +222,7 @@ class CameraPositionState(
}
}
} else {
performAnimateCameraLocked(map, update, continuation)
performAnimateCameraLocked(map, update, durationMs, continuation)
}
}
}
Expand All @@ -235,17 +241,23 @@ class CameraPositionState(
private fun performAnimateCameraLocked(
map: GoogleMap,
update: CameraUpdate,
durationMs: Int,
continuation: CancellableContinuation<Unit>
) {
map.animateCamera(update, object : GoogleMap.CancelableCallback {
val cancelableCallback = object : GoogleMap.CancelableCallback {
override fun onCancel() {
continuation.resumeWithException(CancellationException("Animation cancelled"))
}

override fun onFinish() {
continuation.resume(Unit)
}
})
}
if (durationMs == MAX_VALUE) {
map.animateCamera(update, cancelableCallback)
} else {
map.animateCamera(update, durationMs, cancelableCallback)
}
doOnMapChangedLocked {
check(it == null) {
"New GoogleMap unexpectedly set while an animation was still running"
Expand Down

0 comments on commit aa02773

Please sign in to comment.