1 package host.exp.exponent.experience.splashscreen
2 
3 import android.content.Context
4 import android.graphics.Color
5 import android.os.Handler
6 import android.util.AttributeSet
7 import android.view.LayoutInflater
8 import android.view.View
9 import android.view.animation.AccelerateDecelerateInterpolator
10 import android.view.animation.AlphaAnimation
11 import android.view.animation.Animation
12 import android.widget.ProgressBar
13 import android.widget.RelativeLayout
14 import host.exp.expoview.R
15 
16 /**
17  * The only purpose of this view is to present Android progressBar (spinner) before manifest with experience-related info is available
18  */
19 class LoadingView @JvmOverloads constructor(
20   context: Context,
21   attrs: AttributeSet? = null,
22   defStyleAttr: Int = 0
23 ) : RelativeLayout(context, attrs, defStyleAttr) {
24   private val progressBar: ProgressBar
25   private val progressBarHandler = Handler()
26   private var progressBarShown = false
27 
28   init {
29     LayoutInflater.from(context).inflate(R.layout.loading_view, this, true)
30     progressBar = findViewById(R.id.progressBar)
31     setBackgroundColor(Color.WHITE)
32     show()
33   }
34 
shownull35   fun show() {
36     if (progressBarShown) {
37       return
38     }
39     progressBarHandler.postDelayed(
40       {
41         progressBar.visibility = View.VISIBLE
42         progressBar.startAnimation(
43           AlphaAnimation(0.0f, 1.0f).also {
44             it.duration = 250
45             it.interpolator = AccelerateDecelerateInterpolator()
46             it.fillAfter = true
47           }
48         )
49         progressBarShown = true
50       },
51       PROGRESS_BAR_DELAY_MS
52     )
53   }
54 
hidenull55   fun hide() {
56     if (!progressBarShown) {
57       return
58     }
59     progressBarHandler.removeCallbacksAndMessages(null)
60     progressBar.clearAnimation()
61     if (progressBar.visibility == View.VISIBLE) {
62       progressBar.startAnimation(
63         AlphaAnimation(1.0f, 0.0f).also {
64           it.duration = 250
65           it.interpolator = AccelerateDecelerateInterpolator()
66           it.fillAfter = true
67           it.setAnimationListener(object : Animation.AnimationListener {
68             override fun onAnimationStart(animation: Animation) {}
69             override fun onAnimationRepeat(animation: Animation) {}
70             override fun onAnimationEnd(animation: Animation) {
71               progressBar.visibility = View.GONE
72               progressBarShown = false
73             }
74           })
75         }
76       )
77     }
78   }
79 
80   companion object {
81     private const val PROGRESS_BAR_DELAY_MS = 2500L
82   }
83 }
84