Skip to content

Instantly share code, notes, and snippets.

@esabook
Last active May 19, 2022 07:01
Show Gist options
  • Save esabook/1faba4e0bb237741ecfbe9ea1c4d5799 to your computer and use it in GitHub Desktop.
Save esabook/1faba4e0bb237741ecfbe9ea1c4d5799 to your computer and use it in GitHub Desktop.
splash screen dialog with event
package ***
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import io.reactivex.functions.Consumer
/**
* example:
* <pre>
* ...
* initSplash()
* ...
*
* override fun onResume() {
* super.onResume()
* RxBus.instance.post(SplashScreenDialog.EVENT_CLOSE_SPLASH)
* }
* fun initSplash(){
* SplashScreenDialog.init(supportFragmentManager)
* RxBus.instance.post(SplashScreenDialog.EVENT_OPEN_SPLASH)
* }
* </pre>
*/
class SplashScreenDialog : DialogFragment() {
companion object {
val EVENT_OPEN_SPLASH = "EVENT_OPEN_SPLASH"
val EVENT_CLOSE_SPLASH = "EVENT_CLOSE_SPLASH"
fun init(fragmentManager: FragmentManager) {
val instance = SplashScreenDialog()
var minimumSplashTime = 3000L
RxBus.instance.getEvent()?.subscribe(Consumer {
when (it) {
EVENT_OPEN_SPLASH -> {
instance.isCancelable = false
instance.showNow(fragmentManager, fragmentManager.hashCode().toString())
// setup default minimum splash live
object : CountDownTimer(minimumSplashTime, minimumSplashTime) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
minimumSplashTime = 0
}
}.start()
}
EVENT_CLOSE_SPLASH -> {
Handler(Looper.getMainLooper()).postDelayed({
instance.dismiss()
}, minimumSplashTime)
}
}
})
}
}
override fun getTheme(): Int {
return R.style.AppTheme_PopupAlertDialog
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return ViewSplashscreenBinding.inflate(inflater, container, false).root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment