-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into issue/alt-image-setting
- Loading branch information
Showing
16 changed files
with
353 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
import Foundation | ||
import UIKit | ||
import RNReactNativeGutenbergBridge | ||
|
||
class MediaPickCoordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | ||
|
||
private let presenter: UIViewController | ||
private let callback: (URL?) -> Void | ||
|
||
init(presenter: UIViewController, | ||
callback: @escaping (URL?) -> Void) { | ||
self.presenter = presenter | ||
self.callback = callback | ||
} | ||
|
||
func pick(from source: UIImagePickerController.SourceType) { | ||
guard UIImagePickerController.isSourceTypeAvailable(source) else { | ||
// Camera not available, bound to happen in the simulator | ||
callback(nil) | ||
return | ||
} | ||
let pickerController = UIImagePickerController() | ||
pickerController.sourceType = source | ||
pickerController.delegate = self | ||
presenter.show(pickerController, sender: nil) | ||
} | ||
|
||
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | ||
presenter.dismiss(animated: true, completion: nil) | ||
callback(nil) | ||
} | ||
|
||
func save(image: UIImage, toTemporaryDirectoryUsingName name: String) -> URL? { | ||
let url = URL(fileURLWithPath: NSTemporaryDirectory() + name + ".jpg") | ||
guard let data = UIImageJPEGRepresentation(image, 1.0) else { | ||
return nil | ||
} | ||
do { | ||
try data.write(to: url) | ||
return url | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | ||
presenter.dismiss(animated: true, completion: nil) | ||
let mediaID = UUID().uuidString | ||
guard | ||
let image = info[UIImagePickerControllerOriginalImage] as? UIImage, | ||
let url = save(image: image, toTemporaryDirectoryUsingName: mediaID) | ||
else { | ||
callback(nil) | ||
return | ||
} | ||
callback(url) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
import Foundation | ||
import UIKit | ||
import RNReactNativeGutenbergBridge | ||
|
||
class MediaUploadCoordinator: NSObject { | ||
|
||
private let gutenberg: Gutenberg | ||
|
||
private var activeUploads: [Int32: Progress] = [:] | ||
|
||
init(gutenberg: Gutenberg) { | ||
self.gutenberg = gutenberg | ||
} | ||
|
||
func upload(url: URL) -> Int32? { | ||
//Make sure the media is not larger than a 32 bits to number to avoid problems when bridging to JS | ||
let mediaID = Int32(truncatingIfNeeded:UUID().uuidString.hash) | ||
let progress = Progress(parent: nil, userInfo: [ProgressUserInfoKey.mediaID: mediaID, ProgressUserInfoKey.mediaURL: url]) | ||
progress.totalUnitCount = 100 | ||
activeUploads[mediaID] = progress | ||
let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerFireMethod(_:)), userInfo: progress, repeats: true) | ||
progress.cancellationHandler = { () in | ||
timer.invalidate() | ||
self.gutenberg.mediaUploadUpdate(id: mediaID, state: .reset, progress: 0, url: nil, serverID: nil) | ||
} | ||
return mediaID | ||
} | ||
|
||
func progressForUpload(mediaID: Int32) -> Progress? { | ||
return activeUploads[mediaID] | ||
} | ||
|
||
func retryUpload(with mediaID: Int32) { | ||
guard let progress = activeUploads[mediaID] else { | ||
return | ||
} | ||
progress.completedUnitCount = 0 | ||
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerFireMethod(_:)), userInfo: progress, repeats: true) | ||
} | ||
|
||
func cancelUpload(with mediaID: Int32) { | ||
guard let progress = activeUploads[mediaID] else { | ||
return | ||
} | ||
progress.cancel() | ||
} | ||
|
||
@objc func timerFireMethod(_ timer: Timer) { | ||
guard let progress = timer.userInfo as? Progress, | ||
let mediaID = progress.userInfo[.mediaID] as? Int32, | ||
let mediaURL = progress.userInfo[.mediaURL] as? URL | ||
else { | ||
timer.invalidate() | ||
return | ||
} | ||
progress.completedUnitCount += 1 | ||
//Variable to switch upload final state from success to failure. | ||
let successfull = true | ||
if progress.fractionCompleted < 1 { | ||
gutenberg.mediaUploadUpdate(id: mediaID, state: .uploading, progress: Float(progress.fractionCompleted), url: nil, serverID: nil) | ||
} else if progress.fractionCompleted >= 1 { | ||
timer.invalidate() | ||
if successfull { | ||
gutenberg.mediaUploadUpdate(id: mediaID, state: .failed, progress: 1, url: mediaURL, serverID: 123) | ||
activeUploads[mediaID] = nil | ||
} else { | ||
progress.setUserInfoObject("Network upload failed", forKey: .mediaError) | ||
gutenberg.mediaUploadUpdate(id: mediaID, state: .failed, progress: 1, url: nil, serverID: nil) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension ProgressUserInfoKey { | ||
static let mediaID = ProgressUserInfoKey("mediaID") | ||
static let mediaURL = ProgressUserInfoKey("mediaURL") | ||
static let mediaError = ProgressUserInfoKey("mediaError") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.