1 package expo.modules.updates.loader 2 3 import expo.modules.jsonutils.getNullable 4 import expo.modules.jsonutils.require 5 import expo.modules.updates.UpdatesUtils.parseDateString 6 import expo.modules.updates.manifest.ResponseHeaderData 7 import expo.modules.updates.manifest.UpdateManifest 8 import org.json.JSONObject 9 import java.util.* 10 11 data class SigningInfo(val easProjectId: String, val scopeKey: String) 12 13 sealed class UpdateDirective(val signingInfo: SigningInfo?) { 14 class NoUpdateAvailableUpdateDirective(signingInfo: SigningInfo?) : UpdateDirective(signingInfo) 15 class RollBackToEmbeddedUpdateDirective(val commitTime: Date, signingInfo: SigningInfo?) : UpdateDirective(signingInfo) 16 17 companion object { fromJSONStringnull18 fun fromJSONString(jsonString: String): UpdateDirective { 19 val json = JSONObject(jsonString) 20 val signingInfo = json.getNullable<JSONObject>("extra")?.getNullable<JSONObject>("signingInfo")?.let { 21 SigningInfo(it.require("projectId"), it.require("scopeKey")) 22 } 23 return when (val messageType = json.require<String>("type")) { 24 "noUpdateAvailable" -> NoUpdateAvailableUpdateDirective(signingInfo) 25 "rollBackToEmbedded" -> RollBackToEmbeddedUpdateDirective( 26 parseDateString(json.require<JSONObject>("parameters").require("commitTime")), 27 signingInfo 28 ) 29 else -> throw Error("Invalid message messageType: $messageType") 30 } 31 } 32 } 33 } 34 35 data class UpdateResponse( 36 val responseHeaderData: ResponseHeaderData?, 37 val manifestUpdateResponsePart: UpdateResponsePart.ManifestUpdateResponsePart?, 38 val directiveUpdateResponsePart: UpdateResponsePart.DirectiveUpdateResponsePart? 39 ) 40 41 sealed class UpdateResponsePart { 42 data class ManifestUpdateResponsePart(val updateManifest: UpdateManifest) : UpdateResponsePart() 43 data class DirectiveUpdateResponsePart(val updateDirective: UpdateDirective) : UpdateResponsePart() 44 } 45