From da98214dd9d89da42fa3621d1e0be4b773840080 Mon Sep 17 00:00:00 2001 From: eToledo Date: Wed, 4 Nov 2020 19:06:05 +0100 Subject: [PATCH] Implement iOS side of Bridge for Media Collection blocks --- .../react-native-bridge/ios/Gutenberg.swift | 46 +++++++++++++++++-- .../ios/GutenbergBridgeDelegate.swift | 22 +++++++++ .../ios/RNReactNativeGutenbergBridge.m | 8 ++-- .../ios/RNReactNativeGutenbergBridge.swift | 46 +++++++++---------- .../GutenbergViewController.swift | 21 ++++++++- 5 files changed, 110 insertions(+), 33 deletions(-) diff --git a/packages/react-native-bridge/ios/Gutenberg.swift b/packages/react-native-bridge/ios/Gutenberg.swift index c8bf028e88705..7a194e53e31a5 100644 --- a/packages/react-native-bridge/ios/Gutenberg.swift +++ b/packages/react-native-bridge/ios/Gutenberg.swift @@ -141,8 +141,33 @@ public class Gutenberg: NSObject { private func sendEvent(_ event: RNReactNativeGutenbergBridge.EventName, body: [String: Any]? = nil) { bridgeModule.sendEvent(withName: event.rawValue, body: body) } - + public func mediaUploadUpdate(id: Int32, state: MediaUploadState, progress: Float, url: URL?, serverID: Int32?) { + mediaUpdate(event: .mediaUpload, id: id, state: state, progress: progress, url: url, serverID: serverID) + } + + public func updateMediaSaveStatus(id: Int32, state: MediaSaveState, progress: Float, url: URL?, serverID: Int32?) { + mediaUpdate(event: .mediaSave, id: id, state: state, progress: progress, url: url, serverID: serverID) + } + + public func onMediaCollectionSaveResult(firstMediaIdInCollection: String, success: Bool) { + sendEvent(.mediaSave, body: [ + "state": MediaSaveEvent.result.rawValue, + "firstMediaIdInCollection": firstMediaIdInCollection, + "success": success, + ]) + } + + public func onMediaIdChanged(oldId: String, newId: String, oldUrl: URL) { + sendEvent(.mediaSave, body: [ + "state": MediaSaveEvent.idChange.rawValue, + "oldId": oldId, + "newId": newId, + "oldUrl": oldUrl, + ]) + } + + private func mediaUpdate(event: RNReactNativeGutenbergBridge.EventName, id: Int32, state: State, progress: Float, url: URL?, serverID: Int32?) { var data: [String: Any] = ["mediaId": id, "state": state.rawValue, "progress": progress]; if let url = url { data["mediaUrl"] = url.absoluteString @@ -150,7 +175,7 @@ public class Gutenberg: NSObject { if let serverID = serverID { data["mediaServerId"] = serverID } - sendEvent(.mediaUpload, body: data) + sendEvent(event, body: data) } public func appendMedia(id: Int32, url: URL, type: MediaType) { @@ -204,14 +229,27 @@ extension Gutenberg: RCTBridgeDelegate { } } +protocol MediaState: RawRepresentable {} + extension Gutenberg { - public enum MediaUploadState: Int { + public enum MediaUploadState: Int, MediaState { case uploading = 1 case succeeded = 2 case failed = 3 case reset = 4 } - + + public enum MediaSaveState: Int, MediaState { + case saving = 5 + case succeeded = 6 + case failed = 7 + case reset = 8 + } + + enum MediaSaveEvent: Int { + case result = 9 + case idChange = 10 + } } extension Gutenberg { diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift index 117502f6ad74f..ce250c9c21518 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift @@ -231,6 +231,20 @@ public protocol GutenbergBridgeDelegate: class { func gutenbergDidRequestSetStarterPageTemplatesTooltipShown(_ tooltipShown: Bool) func gutenbergDidSendButtonPressedAction(_ buttonType: Gutenberg.ActionButtonType) + + // Media Collection + + /// Tells the delegate that a media collection block requested to reconnect with media save coordinator. + /// + func gutenbergDidRequestMediaSaveSync() + + func gutenbergDidRequestMediaFilesEditorLoad(_ mediaFiles: [String], blockId: String) + + func gutenbergDidRequestMediaFilesFailedRetryDialog(_ mediaFiles: [String]) + + func gutenbergDidRequestMediaFilesUploadCancelDialog(_ mediaFiles: [String]) + + func gutenbergDidRequestMediaFilesSaveCancelDialog(_ mediaFiles: [String]) } // MARK: - Optional GutenbergBridgeDelegate methods @@ -240,4 +254,12 @@ public extension GutenbergBridgeDelegate { func gutenbergDidLayout() { } func gutenbergDidRequestUnsupportedBlockFallback(for block: Block) { } func gutenbergDidSendButtonPressedAction(_ buttonType: Gutenberg.ActionButtonType) { } + + // Media Collection + + func gutenbergDidRequestMediaSaveSync() {} + func gutenbergDidRequestMediaFilesEditorLoad(_ mediaFiles: [String], blockId: String) { } + func gutenbergDidRequestMediaFilesFailedRetryDialog(_ mediaFiles: [String]) { } + func gutenbergDidRequestMediaFilesUploadCancelDialog(_ mediaFiles: [String]) { } + func gutenbergDidRequestMediaFilesSaveCancelDialog(_ mediaFiles: [String]) { } } diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m index a3bbf91ecbc4f..aafdce5c869aa 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.m @@ -23,9 +23,11 @@ @interface RCT_EXTERN_MODULE(RNReactNativeGutenbergBridge, NSObject) RCT_EXTERN_METHOD(addMention:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)rejecter) RCT_EXTERN_METHOD(requestStarterPageTemplatesTooltipShown:(RCTResponseSenderBlock)callback) RCT_EXTERN_METHOD(setStarterPageTemplatesTooltipShown:(BOOL)tooltipShown) -RCT_EXTERN_METHOD(requestMediaFilesEditorLoad:(NSArray *)mediaFiles blockId:(NSString *)blockId) -RCT_EXTERN_METHOD(requestMediaFilesFailedRetryDialog:(NSArray *)mediaFiles) -RCT_EXTERN_METHOD(onCancelUploadForMediaCollection:(NSArray *)mediaFiles) +RCT_EXTERN_METHOD(requestMediaFilesEditorLoad:(NSArray *)mediaFiles blockId:(NSString *)blockId) +RCT_EXTERN_METHOD(requestMediaFilesFailedRetryDialog:(NSArray *)mediaFiles) +RCT_EXTERN_METHOD(requestMediaFilesUploadCancelDialog:(NSArray *)mediaFiles) +RCT_EXTERN_METHOD(requestMediaFilesSaveCancelDialog:(NSArray *)mediaFiles) +RCT_EXTERN_METHOD(onCancelUploadForMediaCollection:(NSArray *)mediaFiles) RCT_EXTERN_METHOD(actionButtonPressed:(NSString *)buttonType) RCT_EXTERN_METHOD(mediaSaveSync) diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift index 590102b19a782..603c53879a046 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift @@ -289,44 +289,40 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { } @objc - func requestMediaFilesEditorLoad(_ mediaFilesArray: [AnyObject], blockId: String) { - // TODO actually implement the delegate call on iOS - let mediaFiles = mediaFilesArray.compactMap { $0 as? String } - // DispatchQueue.main.async { - // self.delegate?.gutenbergDidRequestMediaFilesEditorLoad(mediaFiles: mediaFiles, blockId: blockId) - // } + func requestMediaFilesEditorLoad(_ mediaFiles: [String], blockId: String) { + DispatchQueue.main.async { + self.delegate?.gutenbergDidRequestMediaFilesEditorLoad(mediaFiles, blockId: blockId) + } } @objc - func requestMediaFilesFailedRetryDialog(_ mediaFilesArray: [AnyObject]) { - // TODO actually implement the delegate call on iOS - let mediaFiles = mediaFilesArray.compactMap { $0 as? String } - // DispatchQueue.main.async { - // self.delegate?.gutenbergDidRequestMediaFilesFailedRetryDialog(mediaFiles: mediaFiles, blockId: blockId) - // } + func requestMediaFilesFailedRetryDialog(_ mediaFiles: [String]) { + DispatchQueue.main.async { + self.delegate?.gutenbergDidRequestMediaFilesFailedRetryDialog(mediaFiles) + } } @objc - func requestMediaFilesUploadCancelDialog(_ mediaFilesArray: [AnyObject]) { - // TODO actually implement the delegate call on iOS - let mediaFiles = mediaFilesArray.compactMap { $0 as? String } - // DispatchQueue.main.async { - // self.delegate?.gutenbergDidRequestMediaFilesUploadCancelDialog(mediaFiles: mediaFiles, blockId: blockId) - // } + func requestMediaFilesUploadCancelDialog(_ mediaFiles: [String]) { + DispatchQueue.main.async { + self.delegate?.gutenbergDidRequestMediaFilesUploadCancelDialog(mediaFiles) + } } @objc - func requestMediaFilesSaveCancelDialog(_ mediaFilesArray: [AnyObject]) { - // TODO actually implement the delegate call on iOS - let mediaFiles = mediaFilesArray.compactMap { $0 as? String } - // DispatchQueue.main.async { - // self.delegate?.gutenbergDidRequestMediaFilesSaveCancelDialog(mediaFiles: mediaFiles, blockId: blockId) - // } + func rrequestMediaFilesSaveCancelDialog(_ mediaFiles: [String]) { + DispatchQueue.main.async { + self.delegate?.gutenbergDidRequestMediaFilesSaveCancelDialog(mediaFiles) + } } @objc func mediaSaveSync() { - // TODO: To be implemented + DispatchQueue.main.async { + if self.hasObservers { + self.delegate?.gutenbergDidRequestMediaSaveSync() + } + } } @objc diff --git a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift index 2be5d1219c259..d23256220c216 100644 --- a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift +++ b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift @@ -50,7 +50,6 @@ class GutenbergViewController: UIViewController { } extension GutenbergViewController: GutenbergBridgeDelegate { - func gutenbergDidRequestFetch(path: String, completion: @escaping (Result) -> Void) { completion(Result.success([:])) } @@ -222,6 +221,26 @@ extension GutenbergViewController: GutenbergBridgeDelegate { func gutenbergDidRequestSetStarterPageTemplatesTooltipShown(_ tooltipShown: Bool) { print("Gutenberg requested setting tooltip flag") } + + func gutenbergDidRequestMediaSaveSync() { + print(#function) + } + + func gutenbergDidRequestMediaFilesEditorLoad(_ mediaFiles: [String], blockId: String) { + print(#function) + } + + func gutenbergDidRequestMediaFilesFailedRetryDialog(_ mediaFiles: [String]) { + print(#function) + } + + func gutenbergDidRequestMediaFilesUploadCancelDialog(_ mediaFiles: [String]) { + print(#function) + } + + func gutenbergDidRequestMediaFilesSaveCancelDialog(_ mediaFiles: [String]) { + print(#function) + } } extension GutenbergViewController: GutenbergWebDelegate {