Skip to content
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

Upload captured photo ios #494

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions ios/gutenberg/GutenbergViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GutenbergViewController: UIViewController {
}

extension GutenbergViewController: GutenbergBridgeDelegate {

func gutenbergDidLoad() {

}
Expand All @@ -44,12 +44,20 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
callback(1, "https://cldup.com/cXyG__fTLN.jpg")
}

func gutenbergDidRequestMediaFromDevicePicker(with callback: @escaping MediaPickerDidPickMediaToUploadCallback) {
print("Gutenberg did request a device media picker, passing a sample url in callback and a fake ID")
func gutenbergDidRequestMediaFromDevicePicker(with callback: @escaping MediaPickerDidPickMediaCallback) {
print("Gutenberg did request a device media picker, opening the device picker")
mediaPickAndUploadCoordinator = MediaPickAndUploadCoordinator(presenter: self, gutenberg: gutenberg, mediaCallback: callback, finishCallback: {
self.mediaPickAndUploadCoordinator = nil
} )
mediaPickAndUploadCoordinator?.pickAndUpload(from: .savedPhotosAlbum)
}

func gutenbergDidRequestMediaFromCameraPicker(with callback: @escaping MediaPickerDidPickMediaCallback) {
print("Gutenberg did request a device media picker, opening the camera picker")
mediaPickAndUploadCoordinator = MediaPickAndUploadCoordinator(presenter: self, gutenberg: gutenberg, mediaCallback: callback, finishCallback: {
self.mediaPickAndUploadCoordinator = nil
} )
mediaPickAndUploadCoordinator?.pickAndUpload()
mediaPickAndUploadCoordinator?.pickAndUpload(from: .camera)
}
}

Expand Down
13 changes: 11 additions & 2 deletions ios/gutenberg/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<string>The app would like to add your location to posts on sites where you have enabled geotagging.</string>
<key>NSCameraUsageDescription</key>
<string>To take photos or videos to use in your posts.</string>
<key>NSLocationUsageDescription</key>
<string>The app would like to add your location to posts on sites where you have enabled geotagging.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Enable microphone access to record sound in your videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To add photos or videos to your posts.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>To add photos or videos to your posts.</string>
<key>NSAppTransportSecurity</key>
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
<dict>
<key>NSExceptionDomains</key>
<dict>
Expand Down
12 changes: 9 additions & 3 deletions ios/gutenberg/MediaPickAndUploadCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import RNReactNativeGutenbergBridge
class MediaPickAndUploadCoordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

private let presenter: UIViewController
private let mediaCallback: MediaPickerDidPickMediaToUploadCallback
private let mediaCallback: MediaPickerDidPickMediaCallback
private let gutenberg: Gutenberg

init(presenter: UIViewController,
gutenberg: Gutenberg,
mediaCallback: @escaping MediaPickerDidPickMediaToUploadCallback,
mediaCallback: @escaping MediaPickerDidPickMediaCallback,
finishCallback: @escaping () -> Void) {
self.presenter = presenter
self.gutenberg = gutenberg
self.mediaCallback = mediaCallback
}

func pickAndUpload() {
func pickAndUpload(from source: UIImagePickerController.SourceType) {
guard UIImagePickerController.isSourceTypeAvailable(source) else {
// Camera not available, bound to happen in the simulator
mediaCallback(nil, nil)
return
}
let pickerController = UIImagePickerController()
pickerController.sourceType = source
pickerController.delegate = self
presenter.show(pickerController, sender: nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
public typealias MediaPickerDidPickMediaCallback = (_ id: Int?, _ url: String?) -> Void
public typealias MediaPickerDidPickMediaToUploadCallback = (_ id: Int?, _ url: String?) -> Void

public protocol GutenbergBridgeDelegate: class {
/// Tells the delegate that Gutenberg had returned the requested HTML content.
Expand All @@ -18,9 +17,15 @@ public protocol GutenbergBridgeDelegate: class {

/// Tells the delegate that an image block requested an image from the device media.
///
/// - Parameter callback: A callbak block to be called with an upload mediaIdentifier and a placaholder image file url,
/// use nil on both parameters to signal that the action was canceled.
func gutenbergDidRequestMediaFromDevicePicker(with callback: @escaping MediaPickerDidPickMediaCallback)

/// Tells the delegate that an image block requested an image from the device cameras.
///
/// - Parameter callback: A callbak block to be called with and temporary
/// image file url and an mediaIdentifier or nil to signal that the action was canceled.
func gutenbergDidRequestMediaFromDevicePicker(with callback: @escaping MediaPickerDidPickMediaToUploadCallback)
func gutenbergDidRequestMediaFromCameraPicker(with callback: @escaping MediaPickerDidPickMediaCallback)

/// Tells the delegate that the Gutenberg module has finished loading.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(RNReactNativeGutenbergBridge, NSObject)
RCT_EXTERN_METHOD(provideToNative_Html:(NSString *)html changed:(BOOL)changed)
RCT_EXTERN_METHOD(onMediaLibraryPressed:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(onUploadMediaPressed:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(onCapturePhotoPressed:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(editorDidLayout)

@end
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter {
}
}

@objc
func onCapturePhotoPressed(_ callback: @escaping RCTResponseSenderBlock) {
DispatchQueue.main.async {
self.delegate?.gutenbergDidRequestMediaFromCameraPicker(with: { (mediaID, url) in
guard let url = url, let mediaID = mediaID else {
callback(nil)
return
}
callback([mediaID, url])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea:
All the media events in the bridge look super similar. (onMediaLibraryPressed. onUploadMediaPressed, onCapturePhotoPressed)
Maybe we could merge them and send a flag to identify them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just proposed that on the Android PR that has the bridge method for JS. I can also change the delegate here to make it a single method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh nice! 👍

})
}
}

@objc
func editorDidLayout() {
DispatchQueue.main.async {
Expand Down