1 package expo.modules.updates.statemachine
2 
3 import android.os.Bundle
4 import com.facebook.react.bridge.Arguments
5 import com.facebook.react.bridge.WritableMap
6 import org.json.JSONObject
7 import java.text.SimpleDateFormat
8 import java.util.Date
9 import java.util.Locale
10 
11 /**
12 The state machine context, with information intended to be consumed by application JS code.
13  */
14 data class UpdatesStateContext(
15   val isUpdateAvailable: Boolean = false,
16   val isUpdatePending: Boolean = false,
17   val isChecking: Boolean = false,
18   val isDownloading: Boolean = false,
19   val isRestarting: Boolean = false,
20   val latestManifest: JSONObject? = null,
21   val downloadedManifest: JSONObject? = null,
22   val rollback: UpdatesStateContextRollback? = null,
23   val checkError: UpdatesStateError? = null,
24   val downloadError: UpdatesStateError? = null,
25   val lastCheckForUpdateTime: Date? = null,
26 ) {
27 
28   val json: Map<String, Any>
29     get() {
30       val map: MutableMap<String, Any> = mutableMapOf(
31         "isUpdateAvailable" to isUpdateAvailable,
32         "isUpdatePending" to isUpdatePending,
33         "isChecking" to isChecking,
34         "isDownloading" to isDownloading,
35         "isRestarting" to isRestarting
36       )
37       if (latestManifest != null) {
38         map["latestManifest"] = latestManifest
39       }
40       if (downloadedManifest != null) {
41         map["downloadedManifest"] = downloadedManifest
42       }
43       if (rollback != null) {
44         map["rollback"] = rollback.json
45       }
46       if (checkError != null) {
47         map["checkError"] = checkError.json
48       }
49       if (downloadError != null) {
50         map["downloadError"] = downloadError.json
51       }
52       if (lastCheckForUpdateTime != null) {
53         map["lastCheckForUpdateTime"] = lastCheckForUpdateTime
54       }
55       return map
56     }
57 
58   /**
59    * Creates a WritableMap to be sent to JS on a state change.
60    */
61   val writableMap: WritableMap
62     get() {
63       val result = Arguments.createMap()
64       result.putMap("context", Arguments.fromBundle(bundle))
65       return result
66     }
67 
68   /**
69    * Creates a Bundle to be returned to JS on a call to nativeStateMachineContext()
70    */
71   val bundle: Bundle
72     get() {
<lambda>null73       return Bundle().apply {
74         putBoolean("isUpdateAvailable", isUpdateAvailable)
75         putBoolean("isUpdatePending", isUpdatePending)
76         putBoolean("isChecking", isChecking)
77         putBoolean("isDownloading", isDownloading)
78         putBoolean("isRestarting", isRestarting)
79         if (latestManifest != null) {
80           putString("latestManifestString", latestManifest.toString())
81         }
82         if (downloadedManifest != null) {
83           putString("downloadedManifestString", downloadedManifest.toString())
84         }
85         if (rollback != null) {
86           putBundle(
87             "rollback",
88             Bundle().apply {
89               putString("commitTime", rollback.commitTimeString)
90             }
91           )
92         }
93         if (checkError != null) {
94           val errorMap = Bundle().apply {
95             putString("message", checkError.message)
96           }
97           this.putBundle("checkError", errorMap)
98         }
99         if (downloadError != null) {
100           val errorMap = Bundle().apply {
101             putString("message", downloadError.message)
102           }
103           putBundle("downloadError", errorMap)
104         }
105         if (lastCheckForUpdateTime != null) {
106           putString("lastCheckForUpdateTimeString", DATE_FORMATTER.format(lastCheckForUpdateTime))
107         }
108       }
109     }
110 
111   companion object {
<lambda>null112     val DATE_FORMATTER: SimpleDateFormat by lazy {
113       SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).apply {
114         timeZone = java.util.TimeZone.getTimeZone("GMT")
115       }
116     }
117   }
118 }
119