Skip to content

Commit

Permalink
Implement iOS side of Bridge for Media Collection blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
etoledom committed Nov 5, 2020
1 parent a2f7e95 commit da98214
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 33 deletions.
46 changes: 42 additions & 4 deletions packages/react-native-bridge/ios/Gutenberg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,41 @@ 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<State: MediaState>(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
}
if let serverID = serverID {
data["mediaServerId"] = serverID
}
sendEvent(.mediaUpload, body: data)
sendEvent(event, body: data)
}

public func appendMedia(id: Int32, url: URL, type: MediaType) {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *> *)mediaFiles blockId:(NSString *)blockId)
RCT_EXTERN_METHOD(requestMediaFilesFailedRetryDialog:(NSArray<NSString *> *)mediaFiles)
RCT_EXTERN_METHOD(requestMediaFilesUploadCancelDialog:(NSArray<NSString *> *)mediaFiles)
RCT_EXTERN_METHOD(requestMediaFilesSaveCancelDialog:(NSArray<NSString *> *)mediaFiles)
RCT_EXTERN_METHOD(onCancelUploadForMediaCollection:(NSArray<NSString *> *)mediaFiles)
RCT_EXTERN_METHOD(actionButtonPressed:(NSString *)buttonType)
RCT_EXTERN_METHOD(mediaSaveSync)

Expand Down
46 changes: 21 additions & 25 deletions packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class GutenbergViewController: UIViewController {
}

extension GutenbergViewController: GutenbergBridgeDelegate {

func gutenbergDidRequestFetch(path: String, completion: @escaping (Result<Any, NSError>) -> Void) {
completion(Result.success([:]))
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit da98214

Please sign in to comment.