Skip to content

Commit

Permalink
Merge pull request #494 from wordpress-mobile/feature/upload_captured…
Browse files Browse the repository at this point in the history
…_photo_ios

Upload captured photo ios
  • Loading branch information
SergioEstevao authored Jan 24, 2019
2 parents 73cf794 + 833ccf0 commit b2b7e4b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
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])
})
}
}

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

0 comments on commit b2b7e4b

Please sign in to comment.