Skip to content

Commit

Permalink
feat(android): adds fade out preference. ref #136
Browse files Browse the repository at this point in the history
refactor(android): when fullscreen is set to true, then the dialog hides the status bar, too
refactor(android): background color is set on Dialog, too
  • Loading branch information
timbru31 committed Feb 4, 2020
1 parent c8ebdc2 commit 9cc1abb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ await lottie.splashscreen.show(location?: string, remote?: boolean, width?: numb
<preference name="LottieCancelOnTap" value="true" />
```

- `LottieHideTimeout` (Double, default `0`). Duration in seconds after which the Lottie animation should be hidden.
- `LottieHideTimeout` (Integer, default `0`). Duration in seconds after which the Lottie animation should be hidden.

```xml
<preference name="LottieHideTimeout" value="10" />
Expand Down Expand Up @@ -173,6 +173,12 @@ await lottie.splashscreen.show(location?: string, remote?: boolean, width?: numb
<preference name="LottieCacheDisabled" value="true" />
```

- `LottieFadeOutDuration` (Integer, default `0`). Duration in milliseconds for the fade out animation. Set to `0` disable the fade out animation.

```xml
<preference name="LottieFadeOutDuration" value="500" />
```

---

Built by (c) Tim Brust and contributors. Released under the MIT license.
53 changes: 44 additions & 9 deletions src/android/LottieSplashScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.dustplanet.cordova.lottie

import android.R.style
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Handler
import android.util.Log
import android.widget.ImageView
Expand All @@ -14,9 +16,11 @@ import java.lang.Exception
import org.apache.cordova.CallbackContext
import org.apache.cordova.CordovaArgs
import org.apache.cordova.CordovaPlugin
import android.view.animation.Animation
import android.view.animation.AlphaAnimation
import java.util.concurrent.TimeUnit

class LottieSplashScreen : CordovaPlugin() {

private lateinit var splashDialog: Dialog
private lateinit var animationView: LottieAnimationView

Expand All @@ -43,8 +47,7 @@ class LottieSplashScreen : CordovaPlugin() {
override fun execute(action: String, args: CordovaArgs, callbackContext: CallbackContext): Boolean {
when (action) {
"hide" -> return try {
destroyView()
callbackContext.success()
destroyView(callbackContext)
true
} catch (e: Exception) {
callbackContext.error(e.message)
Expand All @@ -67,11 +70,11 @@ class LottieSplashScreen : CordovaPlugin() {
}
}

private fun destroyView() {
private fun destroyView(callbackContext: CallbackContext? = null) {
cordova.activity.runOnUiThread {
animationView.cancelAnimation()
if (::splashDialog.isInitialized) {
splashDialog.dismiss()
dismissDialog(callbackContext)
}
}
}
Expand Down Expand Up @@ -139,9 +142,15 @@ class LottieSplashScreen : CordovaPlugin() {
}

animationView.scaleType = ImageView.ScaleType.valueOf(preferences.getString("LottieScaleType", "FIT_CENTER").toUpperCase())
animationView.setBackgroundColor(ColorHelper.parseColor(preferences.getString("LottieBackgroundColor", "#ffffff")))
val color = ColorHelper.parseColor(preferences.getString("LottieBackgroundColor", "#ffffff"))
animationView.setBackgroundColor(color)


splashDialog = Dialog(context, android.R.style.Theme_Translucent_NoTitleBar)
val fullScreen = preferences.getBoolean("LottieFullScreen", false)
splashDialog = Dialog(context, when { fullScreen -> style.Theme_NoTitleBar_Fullscreen
else -> style.Theme_Translucent_NoTitleBar
})
splashDialog.window?.setBackgroundDrawable(ColorDrawable(color))
splashDialog.setContentView(animationView)
splashDialog.setCancelable(false)

Expand All @@ -153,14 +162,14 @@ class LottieSplashScreen : CordovaPlugin() {
val cancelOnTap = preferences.getBoolean("LottieCancelOnTap", false)
if (cancelOnTap) {
animationView.cancelAnimation()
splashDialog.dismiss()
dismissDialog()
}
}

val delay = preferences.getInteger("LottieHideTimeout", 0)
if (delay > 0) {
val handler = Handler()
handler.postDelayed(splashDialog::dismiss, delay.toLong() * 1000)
handler.postDelayed({ dismissDialog() }, TimeUnit.SECONDS.toMillis(delay.toLong()))
}
}
}
Expand Down Expand Up @@ -192,6 +201,32 @@ class LottieSplashScreen : CordovaPlugin() {
return (px * webView.context.resources.displayMetrics.density).toInt()
}

private fun dismissDialog(callbackContext: CallbackContext? = null) {
val fadeDuration = preferences.getInteger("LottieFadeOutDuration", 0)
when {
fadeDuration > 0 -> {
val fadeOut = AlphaAnimation(1f, 0f)
fadeOut.duration = fadeDuration.toLong()
animationView.animation = fadeOut
animationView.startAnimation(fadeOut)
fadeOut.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {}

override fun onAnimationEnd(animation: Animation?) {
splashDialog.dismiss()
callbackContext?.success()
}

override fun onAnimationRepeat(animation: Animation?) {}
})
}
else -> {
splashDialog.dismiss()
callbackContext?.success()
}
}
}

companion object {
private const val LOG_TAG = "LottieSplashScreen"
}
Expand Down

0 comments on commit 9cc1abb

Please sign in to comment.