Skip to content

Commit

Permalink
Merge pull request #1494 from wordpress-mobile/issue/fix_request_impo…
Browse files Browse the repository at this point in the history
…rt_on_bridge

Fix requestImport call to send the correct arguments.
  • Loading branch information
etoledom authored Oct 28, 2019
2 parents f190247 + a4bab40 commit e243459
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1.15.2
------
* Fix issue when copy/paste photos from other apps, was not inserting an image on the post.
* Fix issue where the block inserter layout wasn't correct after device rotation.

1.15.0
Expand Down
18 changes: 10 additions & 8 deletions ios/gutenberg/GutenbergViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
switch currentFilter {
case .image:
if(allowMultipleSelection) {
callback([(1, "https://cldup.com/cXyG__fTLN.jpg", "image"), (3, "https://cldup.com/cXyG__fTLN.jpg", "image")])
callback([MediaInfo(id: 1, url: "https://cldup.com/cXyG__fTLN.jpg", type: "image"),
MediaInfo(id: 3, url: "https://cldup.com/cXyG__fTLN.jpg", type: "image")])
} else {
callback([(1, "https://cldup.com/cXyG__fTLN.jpg", "image")])
callback([MediaInfo(id: 1, url: "https://cldup.com/cXyG__fTLN.jpg", type: "image")])
}
case .video:
if(allowMultipleSelection) {
callback([(2, "https://i.cloudup.com/YtZFJbuQCE.mov", "video"), (4, "https://i.cloudup.com/YtZFJbuQCE.mov", "video")])
callback([MediaInfo(id: 2, url: "https://i.cloudup.com/YtZFJbuQCE.mov", type: "video"),
MediaInfo(id: 4, url: "https://i.cloudup.com/YtZFJbuQCE.mov", type: "video")])
} else {
callback([(2, "https://i.cloudup.com/YtZFJbuQCE.mov", "video")])
callback([MediaInfo(id: 2, url: "https://i.cloudup.com/YtZFJbuQCE.mov", type: "video")])
}
default:
break
Expand All @@ -96,18 +98,18 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
}
}

func gutenbergDidRequestImport(from url: URL, with callback: @escaping MediaPickerDidPickMediaCallback) {
func gutenbergDidRequestImport(from url: URL, with callback: @escaping MediaImportCallback) {
let id = mediaUploadCoordinator.upload(url: url)
callback([(id, url.absoluteString, "image")])
callback(MediaInfo(id: id, url: url.absoluteString, type: "image"))
}

func pickAndUpload(from source: UIImagePickerController.SourceType, filter: MediaFilter, callback: @escaping MediaPickerDidPickMediaCallback) {
mediaPickCoordinator = MediaPickCoordinator(presenter: self, filter: filter, callback: { (url) in
guard let url = url, let mediaID = self.mediaUploadCoordinator.upload(url: url) else {
callback([(nil, nil, nil)])
callback([MediaInfo(id: nil, url: nil, type: nil)])
return
}
callback([(mediaID, url.absoluteString, "image")])
callback([MediaInfo(id: mediaID, url: url.absoluteString, type: "image")])
self.mediaPickCoordinator = nil
} )
mediaPickCoordinator?.pick(from: source)
Expand Down
18 changes: 16 additions & 2 deletions react-native-gutenberg-bridge/ios/GutenbergBridgeDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
public typealias MediaPickerDidPickMediaCallback = (_ media: [(Int32?,String?,String?)]?) -> Void
public struct MediaInfo {
public let id: Int32?
public let url: String?
public let type: String?

public init(id: Int32?, url: String?, type: String?) {
self.id = id
self.url = url
self.type = type
}
}

public typealias MediaPickerDidPickMediaCallback = (_ media: [MediaInfo]?) -> Void

public typealias MediaImportCallback = (_ media: MediaInfo?) -> Void

public enum MediaPickerSource: String {
case mediaLibrary = "SITE_MEDIA_LIBRARY"
Expand Down Expand Up @@ -71,7 +85,7 @@ public protocol GutenbergBridgeDelegate: class {
/// - url: the url to import
/// - callback: A callback block to be called with an array of upload mediaIdentifiers and a placeholder images file url, use nil on both parameters to signal that the action has failed.
//
func gutenbergDidRequestImport(from url: URL, with callback: @escaping MediaPickerDidPickMediaCallback)
func gutenbergDidRequestImport(from url: URL, with callback: @escaping MediaImportCallback)

/// Tells the delegate that an image block requested to reconnect with media uploads coordinator.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter {
callback(nil)
return
}
if (allowMultipleSelection) {
let formattedMedia = media.map { (id, url, type) in
return [mediaDictKeys.IDKey: id, mediaDictKeys.URLKey: url, mediaDictKeys.TypeKey: type]
}
callback([formattedMedia])
let mediaToReturn: [MediaInfo]
if allowMultipleSelection {
mediaToReturn = media
} else {
guard let (mediaID, mediaURL, mediaType) = media.first else {
callback(nil)
return
}
callback([[mediaDictKeys.IDKey: mediaID, mediaDictKeys.URLKey: mediaURL, mediaDictKeys.TypeKey: mediaType]])
mediaToReturn = Array(media.prefix(1))
}

let jsFormattedMedia = mediaToReturn.map { mediaInfo in
return mediaInfo.encodeForJS()
}
if allowMultipleSelection {
callback([jsFormattedMedia])
} else {
callback(jsFormattedMedia)
}
})
}
Expand All @@ -50,12 +53,12 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter {
return
}
DispatchQueue.main.async {
self.delegate?.gutenbergDidRequestImport(from: url, with: { mediaList in
guard let mediaList = mediaList else {
self.delegate?.gutenbergDidRequestImport(from: url, with: { mediaInfo in
guard let mediaInfo = mediaInfo else {
callback(nil)
return
}
callback(mediaList)
}
callback([mediaInfo.id as Any, mediaInfo.url as Any])
})
}
}
Expand Down Expand Up @@ -197,3 +200,14 @@ extension RNReactNativeGutenbergBridge {
static let TypeKey = "type"
}
}

extension MediaInfo {

func encodeForJS() -> [String: Any] {
return [
RNReactNativeGutenbergBridge.mediaDictKeys.IDKey: id as Any,
RNReactNativeGutenbergBridge.mediaDictKeys.URLKey: url as Any,
RNReactNativeGutenbergBridge.mediaDictKeys.TypeKey: type as Any
]
}
}

0 comments on commit e243459

Please sign in to comment.