Skip to content

Commit

Permalink
[expo-updates][android] Convert LoaderTask.RemoteCheckResult to seale…
Browse files Browse the repository at this point in the history
…d class (expo#23061)
  • Loading branch information
wschurman authored Jun 26, 2023
1 parent 77c81e2 commit 3fa54f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/expo-updates/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐛 Bug fixes

- [iOS] Use weak delegate for state machine. ([#23060](https://github.com/expo/expo/pull/23060) by [@wschurman](https://github.com/wschurman))
- [Android] Convert LoaderTask.RemoteCheckResult to sealed class. ([#23061](https://github.com/expo/expo/pull/23061) by [@wschurman](https://github.com/wschurman))

### 💡 Others

Expand All @@ -18,6 +19,7 @@

- [Android] fix instrumentation tests. ([#23037](https://github.com/expo/expo/pull/23037) by [@douglowder](https://github.com/douglowder))
- [iOS] Fix crash when dev-client and updates used together. ([#23070](https://github.com/expo/expo/pull/23070) by [@douglowder](https://github.com/douglowder))
- [Android] Use sealed class for UpdatesStateEvent. ([#23038](https://github.com/expo/expo/pull/23038) by [@wschurman](https://github.com/wschurman))

## 0.18.2 — 2023-06-23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,10 @@ class UpdatesController private constructor(
}

override fun onRemoteCheckForUpdateFinished(result: LoaderTask.RemoteCheckResult) {
var event: UpdatesStateEvent = UpdatesStateEvent.CheckCompleteUnavailable()
if (result.manifest != null) {
event = UpdatesStateEvent.CheckCompleteWithUpdate(
result.manifest
)
} else if (result.isRollBackToEmbedded == true) {
event = UpdatesStateEvent.CheckCompleteWithRollback()
val event = when (result) {
is LoaderTask.RemoteCheckResult.NoUpdateAvailable -> UpdatesStateEvent.CheckCompleteUnavailable()
is LoaderTask.RemoteCheckResult.UpdateAvailable -> UpdatesStateEvent.CheckCompleteWithUpdate(result.manifest)
is LoaderTask.RemoteCheckResult.RollBackToEmbedded -> UpdatesStateEvent.CheckCompleteWithRollback()
}
stateMachine.processEvent(event)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ class LoaderTask(
enum class RemoteUpdateStatus {
ERROR, NO_UPDATE_AVAILABLE, UPDATE_AVAILABLE
}
data class RemoteCheckResult(
val isRollBackToEmbedded: Boolean? = null,
val manifest: JSONObject? = null
)

sealed class RemoteCheckResult(private val status: Status) {
private enum class Status {
NO_UPDATE_AVAILABLE,
UPDATE_AVAILABLE,
ROLL_BACK_TO_EMBEDDED
}

class NoUpdateAvailable : RemoteCheckResult(Status.NO_UPDATE_AVAILABLE)
class UpdateAvailable(val manifest: JSONObject) : RemoteCheckResult(Status.UPDATE_AVAILABLE)
class RollBackToEmbedded : RemoteCheckResult(Status.ROLL_BACK_TO_EMBEDDED)
}

interface LoaderTaskCallback {
fun onFailure(e: Exception)
Expand Down Expand Up @@ -327,12 +335,12 @@ class LoaderTask(
return when (updateDirective) {
is UpdateDirective.RollBackToEmbeddedUpdateDirective -> {
isUpToDate = true
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult(true, null))
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult.RollBackToEmbedded())
Loader.OnUpdateResponseLoadedResult(shouldDownloadManifestIfPresentInResponse = false)
}
is UpdateDirective.NoUpdateAvailableUpdateDirective -> {
isUpToDate = true
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult())
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult.NoUpdateAvailable())
Loader.OnUpdateResponseLoadedResult(shouldDownloadManifestIfPresentInResponse = false)
}
}
Expand All @@ -341,7 +349,7 @@ class LoaderTask(
val updateManifest = updateResponse.manifestUpdateResponsePart?.updateManifest
if (updateManifest == null) {
isUpToDate = true
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult())
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult.NoUpdateAvailable())
return Loader.OnUpdateResponseLoadedResult(shouldDownloadManifestIfPresentInResponse = false)
}

Expand All @@ -353,12 +361,12 @@ class LoaderTask(
) {
isUpToDate = false
callback.onRemoteUpdateManifestResponseManifestLoaded(updateManifest)
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult(null, updateManifest.manifest.getRawJson()))
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult.UpdateAvailable(updateManifest.manifest.getRawJson()))
callback.onRemoteUpdateLoadStarted()
Loader.OnUpdateResponseLoadedResult(shouldDownloadManifestIfPresentInResponse = true)
} else {
isUpToDate = true
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult())
callback.onRemoteCheckForUpdateFinished(RemoteCheckResult.NoUpdateAvailable())
Loader.OnUpdateResponseLoadedResult(shouldDownloadManifestIfPresentInResponse = false)
}
}
Expand Down

0 comments on commit 3fa54f5

Please sign in to comment.