<lambda>null1 package host.exp.exponent.experience.loading
2 
3 import android.annotation.SuppressLint
4 import android.app.Activity
5 import android.content.Context
6 import android.view.Gravity
7 import android.view.LayoutInflater
8 import android.view.ViewGroup
9 import android.widget.PopupWindow
10 import android.widget.TextView
11 import host.exp.exponent.Constants
12 import host.exp.exponent.ExpoUpdatesAppLoader
13 import host.exp.expoview.R
14 import java.lang.ref.WeakReference
15 import java.util.*
16 
17 /**
18  * Presents loading progress and messages from bundler/fetcher.
19  * Uses PopupWindow to present content above whole application.
20  */
21 class LoadingProgressPopupController(activity: Activity) {
22   private val mWeakActivity = WeakReference(activity)
23   private var mPopupWindow: PopupWindow? = null
24   private var mStatusTextView: TextView? = null
25   private var mPercentageTextView: TextView? = null
26   private var mContainer: ViewGroup? = null
27 
28   fun show() {
29     mWeakActivity.get()?.let { activity ->
30       activity.runOnUiThread {
31         if (mPopupWindow != null) {
32           // already showing
33           return@runOnUiThread
34         }
35         val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
36         @SuppressLint("InflateParams")
37         mContainer = (inflater.inflate(R.layout.loading_progress_popup, null) as ViewGroup).also {
38           mStatusTextView = it.findViewById<TextView>(R.id.status_text_view).also { textView ->
39             textView.text = "Waiting for server ..."
40           }
41           mPercentageTextView = it.findViewById(R.id.percentage_text_view)
42         }
43         mPopupWindow = PopupWindow(mContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT).also {
44           it.isTouchable = false
45           activity.window.decorView.post {
46             it.showAtLocation(activity.window.decorView, Gravity.BOTTOM, 0, 0)
47           }
48         }
49       }
50     }
51   }
52 
53   fun updateProgress(status: String?, done: Int?, total: Int?) {
54     show()
55     mWeakActivity.get()?.runOnUiThread {
56       mStatusTextView!!.text = status ?: "Building JavaScript bundle..."
57       if (done != null && total != null && total > 0) {
58         val percent: Float = done.toFloat() / total * 100
59         mPercentageTextView!!.text = String.format(Locale.getDefault(), "%.2f%%", percent)
60       }
61     }
62   }
63 
64   fun setLoadingProgressStatus(status: ExpoUpdatesAppLoader.AppLoaderStatus) {
65     if (Constants.isStandaloneApp()) {
66       return
67     }
68 
69     val text = when (status) {
70       ExpoUpdatesAppLoader.AppLoaderStatus.CHECKING_FOR_UPDATE -> {
71         "Checking for new update..."
72       }
73       ExpoUpdatesAppLoader.AppLoaderStatus.DOWNLOADING_NEW_UPDATE -> {
74         "New update available, downloading..."
75       }
76     }
77 
78     show()
79     mWeakActivity.get()?.runOnUiThread {
80       mStatusTextView!!.text = text
81     }
82   }
83 
84   fun hide() {
85     mWeakActivity.get()?.runOnUiThread {
86       if (mPopupWindow == null || !mPopupWindow!!.isShowing) {
87         // already hidden
88         return@runOnUiThread
89       }
90 
91       mPopupWindow!!.dismiss()
92       mPopupWindow = null
93       mContainer = null
94       mStatusTextView = null
95       mPercentageTextView = null
96     }
97   }
98 }
99