Skip to content

Commit

Permalink
[expo-updates][ios] Use swift enum for AppLoaderTask delegate (expo#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman authored Jun 27, 2023
1 parent fccfbf3 commit a7d701c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
5 changes: 0 additions & 5 deletions ios/Exponent/Kernel/AppLoader/EXAppLoaderExpoUpdates.m
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ - (void)requestFromCache

#pragma mark - EXUpdatesAppLoaderTaskDelegate

// Implement empty stubs
- (void)didStartCheckingForRemoteUpdate {}
- (void)didFinishCheckingForRemoteUpdate:(NSDictionary<NSString *,id> *)body {}
- (void)appLoaderTask:(EXUpdatesAppLoaderTask *)_ didLoadAsset:(EXUpdatesAsset *)asset successfulAssetCount:(NSInteger)successfulAssetCount failedAssetCount:(NSInteger)failedAssetCount totalAssetCount:(NSInteger)totalAssetCount {}

- (BOOL)appLoaderTask:(EXUpdatesAppLoaderTask *)appLoaderTask didLoadCachedUpdate:(EXUpdatesUpdate *)update
{
[self _setShouldShowRemoteUpdateStatus:update.manifest];
Expand Down
2 changes: 2 additions & 0 deletions packages/expo-updates/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- [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))

- [iOS] Use swift enum for AppLoaderTask delegate. ([#23064](https://github.com/expo/expo/pull/23064) by [@wschurman](https://github.com/wschurman))

## 0.18.3 — 2023-06-24

### 🐛 Bug fixes
Expand Down
18 changes: 10 additions & 8 deletions packages/expo-updates/ios/EXUpdates/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public protocol AppControllerDelegate: AnyObject {
*/
@objc(EXUpdatesAppController)
@objcMembers
public class AppController: NSObject, AppLoaderTaskDelegate, ErrorRecoveryDelegate, UpdatesStateChangeDelegate {
public class AppController: NSObject, AppLoaderTaskDelegate, AppLoaderTaskSwiftDelegate, ErrorRecoveryDelegate, UpdatesStateChangeDelegate {
private static let ErrorDomain = "EXUpdatesAppController"
private static let EXUpdatesEventName = "Expo.nativeUpdatesEvent"
private static let EXUpdatesStateChangeEventName = "Expo.nativeUpdatesStateChangeEvent"
Expand Down Expand Up @@ -267,6 +267,7 @@ public class AppController: NSObject, AppLoaderTaskDelegate, ErrorRecoveryDelega
delegateQueue: controllerQueue
)
loaderTask!.delegate = self
loaderTask!.swiftDelegate = self
loaderTask!.start()
}

Expand Down Expand Up @@ -345,17 +346,18 @@ public class AppController: NSObject, AppLoaderTaskDelegate, ErrorRecoveryDelega
return true
}

public func didStartCheckingForRemoteUpdate() {
public func appLoaderTaskDidStartCheckingForRemoteUpdate(_: AppLoaderTask) {
stateMachine?.processEvent(UpdatesStateEventCheck())
}

public func didFinishCheckingForRemoteUpdate(_ body: [String: Any]) {
let manifest: [String: Any]? = body["manifest"] as? [String: Any]
let rollback: Bool = body["isRollBackToEmbedded"] as? Bool ?? false
var event: UpdatesStateEvent = UpdatesStateEventCheckComplete()
if manifest != nil {
public func appLoaderTask(_: AppLoaderTask, didFinishCheckingForRemoteUpdateWithRemoteCheckResult remoteCheckResult: RemoteCheckResult) {
let event: UpdatesStateEvent
switch remoteCheckResult {
case .noUpdateAvailable:
event = UpdatesStateEventCheckComplete()
case .updateAvailable(let manifest):
event = UpdatesStateEventCheckCompleteWithUpdate(manifest: manifest)
} else if rollback {
case .rollBackToEmbedded:
event = UpdatesStateEventCheckCompleteWithRollback()
}
stateMachine?.processEvent(event)
Expand Down
56 changes: 41 additions & 15 deletions packages/expo-updates/ios/EXUpdates/AppLoader/AppLoaderTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public protocol AppLoaderTaskDelegate: AnyObject {
* AppLoaderTask proceed as usual.
*/
func appLoaderTask(_: AppLoaderTask, didLoadCachedUpdate update: Update) -> Bool
func didStartCheckingForRemoteUpdate()
func didFinishCheckingForRemoteUpdate(_ body: [String: Any])
func appLoaderTask(_: AppLoaderTask, didStartLoadingUpdate update: Update?)
func appLoaderTask(_: AppLoaderTask, didLoadAsset asset: UpdateAsset, successfulAssetCount: Int, failedAssetCount: Int, totalAssetCount: Int)
func appLoaderTask(_: AppLoaderTask, didFinishWithLauncher launcher: AppLauncher, isUpToDate: Bool)
func appLoaderTask(_: AppLoaderTask, didFinishWithError error: Error)
func appLoaderTask(
Expand All @@ -32,6 +29,18 @@ public protocol AppLoaderTaskDelegate: AnyObject {
)
}

public enum RemoteCheckResult {
case noUpdateAvailable
case updateAvailable(manifest: [String: Any])
case rollBackToEmbedded
}

public protocol AppLoaderTaskSwiftDelegate: AnyObject {
func appLoaderTaskDidStartCheckingForRemoteUpdate(_: AppLoaderTask)
func appLoaderTask(_: AppLoaderTask, didFinishCheckingForRemoteUpdateWithRemoteCheckResult remoteCheckResult: RemoteCheckResult)
func appLoaderTask(_: AppLoaderTask, didLoadAsset asset: UpdateAsset, successfulAssetCount: Int, failedAssetCount: Int, totalAssetCount: Int)
}

@objc(EXUpdatesBackgroundUpdateStatus)
public enum BackgroundUpdateStatus: Int {
case error = 0
Expand Down Expand Up @@ -64,6 +73,7 @@ public final class AppLoaderTask: NSObject {
private static let ErrorDomain = "EXUpdatesAppLoaderTask"

public weak var delegate: AppLoaderTaskDelegate?
public weak var swiftDelegate: AppLoaderTaskSwiftDelegate?

private let config: UpdatesConfig
private let database: UpdatesDatabase
Expand Down Expand Up @@ -328,9 +338,9 @@ public final class AppLoaderTask: NSObject {
completionQueue: loaderTaskQueue
)

if let delegate = self.delegate {
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
delegate.didStartCheckingForRemoteUpdate()
swiftDelegate.appLoaderTaskDidStartCheckingForRemoteUpdate(self)
}
}
remoteAppLoader!.loadUpdate(
Expand All @@ -340,17 +350,23 @@ public final class AppLoaderTask: NSObject {
switch updateDirective {
case is NoUpdateAvailableUpdateDirective:
self.isUpToDate = true
if let delegate = self.delegate {
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
delegate.didFinishCheckingForRemoteUpdate([:])
swiftDelegate.appLoaderTask(self, didFinishCheckingForRemoteUpdateWithRemoteCheckResult: RemoteCheckResult.noUpdateAvailable)
}
}
return false
case is RollBackToEmbeddedUpdateDirective:
self.isUpToDate = false

if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
swiftDelegate.appLoaderTask(self, didFinishCheckingForRemoteUpdateWithRemoteCheckResult: RemoteCheckResult.rollBackToEmbedded)
}
}

if let delegate = self.delegate {
self.delegateQueue.async {
delegate.didFinishCheckingForRemoteUpdate(["isRollBackToEmbedded": true])
delegate.appLoaderTask(self, didStartLoadingUpdate: nil)
}
}
Expand All @@ -364,9 +380,9 @@ public final class AppLoaderTask: NSObject {
guard let update = updateResponse.manifestUpdateResponsePart?.updateManifest else {
// No response, so no update available
self.isUpToDate = true
if let delegate = self.delegate {
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
delegate.didFinishCheckingForRemoteUpdate([:])
swiftDelegate.appLoaderTask(self, didFinishCheckingForRemoteUpdateWithRemoteCheckResult: RemoteCheckResult.noUpdateAvailable)
}
}
return false
Expand All @@ -379,27 +395,37 @@ public final class AppLoaderTask: NSObject {
) {
// got a response, and it is new so should be downloaded
self.isUpToDate = false
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
swiftDelegate.appLoaderTask(
self,
didFinishCheckingForRemoteUpdateWithRemoteCheckResult: RemoteCheckResult.updateAvailable(
manifest: update.manifest.rawManifestJSON()
)
)
}
}

if let delegate = self.delegate {
self.delegateQueue.async {
delegate.didFinishCheckingForRemoteUpdate(["manifest": update.manifest.rawManifestJSON()])
delegate.appLoaderTask(self, didStartLoadingUpdate: update)
}
}
return true
} else {
// got a response, but we already have it
self.isUpToDate = true
if let delegate = self.delegate {
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
delegate.didFinishCheckingForRemoteUpdate([:])
swiftDelegate.appLoaderTask(self, didFinishCheckingForRemoteUpdateWithRemoteCheckResult: RemoteCheckResult.noUpdateAvailable)
}
}
return false
}
} asset: { asset, successfulAssetCount, failedAssetCount, totalAssetCount in
if let delegate = self.delegate {
if let swiftDelegate = self.swiftDelegate {
self.delegateQueue.async {
delegate.appLoaderTask(
swiftDelegate.appLoaderTask(
self,
didLoadAsset: asset,
successfulAssetCount: successfulAssetCount,
Expand Down

0 comments on commit a7d701c

Please sign in to comment.