-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[iOS] Implement client-defined media options. #1539
Changes from 4 commits
2403382
ed17f05
feb7aa3
b987dec
38cabc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,23 @@ public struct MediaInfo { | |
} | ||
|
||
public typealias MediaPickerDidPickMediaCallback = (_ media: [MediaInfo]?) -> Void | ||
|
||
public typealias MediaImportCallback = (_ media: MediaInfo?) -> Void | ||
|
||
public enum MediaPickerSource: String { | ||
case mediaLibrary = "SITE_MEDIA_LIBRARY" | ||
case deviceLibrary = "DEVICE_MEDIA_LIBRARY" | ||
case deviceCamera = "DEVICE_CAMERA" | ||
} | ||
|
||
public enum MediaFilter: String { | ||
case image | ||
case video | ||
case audio | ||
case other | ||
/// Declare internal Media Sources. | ||
/// Label and Type are not relevant since they are delcared on the JS side. | ||
/// Hopefully soon, this will need to be declared on the client side. | ||
extension Gutenberg.MediaSource { | ||
public static let mediaLibrary = Gutenberg.MediaSource(id: "SITE_MEDIA_LIBRARY", label: "", types: [.image, .video]) | ||
public static let deviceLibrary = Gutenberg.MediaSource(id: "DEVICE_MEDIA_LIBRARY", label: "", types: [.image, .video]) | ||
public static let deviceCamera = Gutenberg.MediaSource(id: "DEVICE_CAMERA", label: "", types: [.image, .video]) | ||
|
||
static var registeredInternalSources: [Gutenberg.MediaSource] { | ||
return [ | ||
.deviceCamera, | ||
.deviceLibrary, | ||
.mediaLibrary, | ||
] | ||
} | ||
} | ||
|
||
/// Ref. https://github.com/facebook/react-native/blob/master/Libraries/polyfills/console.js#L376 | ||
|
@@ -77,7 +80,7 @@ public protocol GutenbergBridgeDelegate: class { | |
/// - source: the source from where the picker will get the media | ||
/// - 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 was canceled. | ||
/// | ||
func gutenbergDidRequestMedia(from source: MediaPickerSource, filter: [MediaFilter]?, allowMultipleSelection: Bool, with callback: @escaping MediaPickerDidPickMediaCallback) | ||
func gutenbergDidRequestMedia(from source: Gutenberg.MediaSource, filter: [Gutenberg.MediaType], allowMultipleSelection: Bool, with callback: @escaping MediaPickerDidPickMediaCallback) | ||
Comment on lines
-80
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made |
||
|
||
/// Tells the delegate that gutenberg JS requested the import of media item based on the provided URL | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
@objc (RNReactNativeGutenbergBridge) | ||
public class RNReactNativeGutenbergBridge: RCTEventEmitter { | ||
weak var delegate: GutenbergBridgeDelegate? | ||
weak var dataSource: GutenbergBridgeDataSource? | ||
private var isJSLoading = true | ||
private var hasObservers = false | ||
|
||
// MARK: - Messaging methods | ||
|
||
@objc | ||
|
@@ -14,13 +16,9 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { | |
|
||
@objc | ||
func requestMediaPickFrom(_ source: String, filter: [String]?, allowMultipleSelection: Bool, callback: @escaping RCTResponseSenderBlock) { | ||
let mediaSource: MediaPickerSource = MediaPickerSource(rawValue: source) ?? .deviceLibrary | ||
let mediaFilter: [MediaFilter]? = filter?.map({ | ||
if let type = MediaFilter(rawValue: $0) { | ||
return type | ||
} | ||
return MediaFilter.other | ||
}) | ||
let mediaSource = getMediaSource(withId: source) | ||
let mediaFilter = getMediaTypes(from: filter) | ||
|
||
DispatchQueue.main.async { | ||
self.delegate?.gutenbergDidRequestMedia(from: mediaSource, filter: mediaFilter, allowMultipleSelection: allowMultipleSelection, with: { media in | ||
guard let media = media else { | ||
|
@@ -45,15 +43,25 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { | |
}) | ||
} | ||
} | ||
|
||
@objc | ||
func requestOtherMediaPickFrom(_ source: String, allowMultipleSelection: Bool, callback: @escaping RCTResponseSenderBlock) { | ||
//TODO implement me | ||
requestMediaPickFrom(source, filter: nil, allowMultipleSelection: allowMultipleSelection, callback: callback) | ||
} | ||
Comment on lines
48
to
50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This event ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but better check with @marecar3 if this can be changed on Android side or is there any limitation there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mean to do it on this PR, but it's good to keep it in mind. There might also be a reason for it on Android? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this mentioned change here: WordPress/gutenberg#18303 |
||
|
||
@objc | ||
func getOtherMediaOptions(_ filter: [String]?, callback: @escaping RCTResponseSenderBlock) { | ||
//TODO implement me | ||
guard let dataSource = dataSource else { | ||
return callback([]) | ||
} | ||
|
||
let mediaSources = dataSource.gutenbergMediaSources() | ||
let allowedTypes = getMediaTypes(from: filter) | ||
let filteredSources = mediaSources.filter { | ||
return $0.types.intersection(allowedTypes).isEmpty == false | ||
} | ||
let jsMediaSources = filteredSources.map { $0.jsRepresentation } | ||
callback([jsMediaSources]) | ||
} | ||
|
||
@objc | ||
|
@@ -194,13 +202,21 @@ extension RNReactNativeGutenbergBridge { | |
// MARK: - Helpers | ||
|
||
extension RNReactNativeGutenbergBridge { | ||
|
||
func optionalArray(from optionalString: String?) -> [String]? { | ||
guard let string = optionalString else { | ||
return nil | ||
} | ||
return [string] | ||
} | ||
|
||
private func getMediaSource(withId mediaSourceID: String) -> Gutenberg.MediaSource { | ||
let allMediaSources = Gutenberg.MediaSource.registeredInternalSources + (dataSource?.gutenbergMediaSources() ?? []) | ||
return allMediaSources.first{ $0.id == mediaSourceID } ?? .deviceLibrary | ||
} | ||
|
||
private func getMediaTypes(from jsMediaTypes: [String]?) -> [Gutenberg.MediaType] { | ||
return (jsMediaTypes ?? []).map { Gutenberg.MediaType(fromJSString: $0) } | ||
} | ||
} | ||
|
||
extension RNReactNativeGutenbergBridge { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged
MediaType
withMediaFilter
since we filters byTypes
of media. It seemed like redundant.