Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rg-mobile into issue/372-move-title-to-rich-text

* 'develop' of https://github.com/wordpress-mobile/gutenberg-mobile: (41 commits)
  Update gutenberg ref to master
  Update gutenberg ref
  Update gutenberg ref to Master
  Update gutenberg ref
  Update gutenberg ref
  Make uploads to be successful.
  Making sure we will only send a 32 bits number for the media to avoid conversion issues to JS.
  Implement media actions.
  Update gutenberg ref
  Update gutenberg ref
  Split media pick logic from media upload logic.
  Updated gutenberg submodule
  Updated gutenberg submodule
  Add methods to track ongoing and failed uploads.
  Updated gutenberg sumodule
  Updated gutenberg submodule
  Refactor save to file to separate method.
  Implement iOS new GB bridge methods for media options.
  Updated gutenberg submodule
  Update gutenberg ref
  ...
  • Loading branch information
daniloercoli committed Feb 1, 2019
2 parents 8d1cfb7 + e1e6b9c commit 27b4de6
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 119 deletions.
18 changes: 0 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
addons:
artifacts:
debug: true
s3_region: "us-west-1"
target_paths:
- /${TRAVIS_REPO_SLUG}/${TRAVIS_COMMIT}/

env:
global:
- secure: EUSicS1mBOImgoYHBZJ2ZE0xwNMAkGRYUDetvxcKk0EQrOii1Z/EuYC1IN7mwiI6Ybnm3b2xq51bq/Ro3iQNi+iylA0r20mEjRsSqb8cqy8hXRnl4f7ZI3qUorUvEmsJueve3U2MLu5eYJdSNPbqgdavZDZZxiZIdbHAfA80KMp1RmgeqxkCY9wTfrYEqCtTGEJMJtCHXoDJ0cdYEVDnHQYZrF9SPkkMqmwgfFvHBGcyTPMsRZ9JpggxJltXJFTlHBNn/wLfGPiMsJlNx0oS7SmoZ08W72h2QKBZX5bVZxoNmMeQCZ0FlSHeq8yTGJ5xM8ig6YRzkuK5JiHkTaY9rVQMoU2ozMll9Bb5/A07XDXoutH1/QC1jvvT9eMBhon3uZ7e3qyUiRgCpFWrOW0UXKQqF2hhGWT965mBB5pxWOgGZjxtVIuKpFwMbsPIss44OuzlaFis5YRsKyceBAM+g8gkwxB8r8AT0A6g4jY4fmvrUr0qyaldlcr7LafGvAa73uJHoTUruR2AZQlJmLpO9p5xbuwId8L0alApqoaaR3vPx6LPtl/hTBr+KScs/I8PjdiEaobNi1PT3sNH5KIZEB+WaNZaTkK8gAzwvx7V+V0GaDD/P7L5Qa4mRIsXa1xDxdVM7ne4B5zEK1KCePzbZJuv769IbNgzIqAfELJWmXE=
- secure: Axr+U8LHOxmGi8GyeL0pP0+84ucxeET59BUpHgANgro+2jLwoLzpvssguc0RHxftVsQBiz2CFfFBVIR6ol+Hf6yjI310Yqw9znBvJuxO0EbAGclGkWyx8DdoWlCSwz+RgiIfIoa7E6QZWt+CZwFbi3vTFutl6NX5Reu0wdpHZlfLT8xkWP3PP7awgL2g75WsNKNv4A1CvzkF3j/hi6JU/b3QsBMTOCHA1vcdpKhjF8MiS9d7gzdwsTr6MoDGVab/oGVCljiFmIPYTa4+xp/q1MYkSizxBmq1mqjXkn1RrZP25QGqgubfgtiywk8H9kDE8/POPJuoteO2gnz2u9I1AkVrrsxCfU9XJzXXwVfbFxVlyOIfC9gZj5ZI9niOqTdytPUMPOzJw79xUvYMeoS7ydcrm3O8gCs0/fBnSH0KfMq37um8zE8zah90M/8CcJjS5KC8hFO/tZomkUASoTwnJEZh4OdSG8Ys9YnKMKBDCuI8HUR2zlVPrv/8ACO4eSZ4LCaNeXQVWil5NNFgLPzvkElqpywVmQaGY3UD2W2w76AEs5sF4n/naQsi6L6oy61mDP8j+wSooQczx/1enue/hzjxZUles7p3ek0yha6N6fxkt/MyvEYWmw98wQZmsBzEahTGXy9iWfobFu0jUzUPXhSdoMul3Q9fceUSP9f+XV8=

before_script:
- yarn install

Expand All @@ -30,16 +18,10 @@ matrix:
LANE='node'
CHECK_TESTS='true'
TEST_RN_PLATFORM='android'
ARTIFACTS_BUCKET='gutenberg-mobile-js-bundle'
ARTIFACTS_PATHS="./bundle"
cache:
yarn: true
script:
- set -e # make sure Travis stops if any of the script lines error
- ./.travis/travis-checks-js.sh
- yarn bundle:android # rebuild the Android JS bundle
- rm -Rf bundle/ios # delete the ios bundle since it's not up to date and we don't need to publish to S3
- set +e
- language: node_js
node_js: 8
env:
Expand Down
12 changes: 8 additions & 4 deletions ios/gutenberg.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
F1EE6F7921E7F0A500241744 /* NotoSerif-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = F1EE6F7521E7F0A500241744 /* NotoSerif-Regular.ttf */; };
F1EE6F7A21E7F0A500241744 /* NotoSerif-Italic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = F1EE6F7621E7F0A500241744 /* NotoSerif-Italic.ttf */; };
F1EE6F7B21E7F0A500241744 /* NotoSerif-Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = F1EE6F7721E7F0A500241744 /* NotoSerif-Bold.ttf */; };
FF9A6F4121FA8E2500D36D14 /* MediaPickAndUploadCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF9A6F1621FA8E2500D36D14 /* MediaPickAndUploadCoordinator.swift */; };
FF6836C822035EAB00A0C562 /* MediaUploadCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6836C722035EAB00A0C562 /* MediaUploadCoordinator.swift */; };
FF9A6F4121FA8E2500D36D14 /* MediaPickCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF9A6F1621FA8E2500D36D14 /* MediaPickCoordinator.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -433,7 +434,8 @@
F1EE6F7621E7F0A500241744 /* NotoSerif-Italic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSerif-Italic.ttf"; sourceTree = "<group>"; };
F1EE6F7721E7F0A500241744 /* NotoSerif-Bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSerif-Bold.ttf"; sourceTree = "<group>"; };
F619623252704B46A619C33C /* RNTAztecView.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNTAztecView.xcodeproj; path = "../react-native-aztec/ios/RNTAztecView.xcodeproj"; sourceTree = "<group>"; };
FF9A6F1621FA8E2500D36D14 /* MediaPickAndUploadCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MediaPickAndUploadCoordinator.swift; path = gutenberg/MediaPickAndUploadCoordinator.swift; sourceTree = "<group>"; };
FF6836C722035EAB00A0C562 /* MediaUploadCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MediaUploadCoordinator.swift; path = gutenberg/MediaUploadCoordinator.swift; sourceTree = "<group>"; };
FF9A6F1621FA8E2500D36D14 /* MediaPickCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MediaPickCoordinator.swift; path = gutenberg/MediaPickCoordinator.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -584,7 +586,8 @@
F15198372100DC3C000F6E97 /* gutenberg-Bridging-Header.h */,
F15198392100DC3D000F6E97 /* AppDelegate.swift */,
7EC7328E21907E3F00FED2E6 /* GutenbergViewController.swift */,
FF9A6F1621FA8E2500D36D14 /* MediaPickAndUploadCoordinator.swift */,
FF9A6F1621FA8E2500D36D14 /* MediaPickCoordinator.swift */,
FF6836C722035EAB00A0C562 /* MediaUploadCoordinator.swift */,
F151983A2100DC3D000F6E97 /* MediaProvider.swift */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
Expand Down Expand Up @@ -1368,8 +1371,9 @@
buildActionMask = 2147483647;
files = (
F151983C2100DC3D000F6E97 /* AppDelegate.swift in Sources */,
FF9A6F4121FA8E2500D36D14 /* MediaPickAndUploadCoordinator.swift in Sources */,
FF9A6F4121FA8E2500D36D14 /* MediaPickCoordinator.swift in Sources */,
F151983D2100DC3D000F6E97 /* MediaProvider.swift in Sources */,
FF6836C822035EAB00A0C562 /* MediaUploadCoordinator.swift in Sources */,
7EC7328F21907E3F00FED2E6 /* GutenbergViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
62 changes: 53 additions & 9 deletions ios/gutenberg/GutenbergViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class GutenbergViewController: UIViewController {

fileprivate lazy var gutenberg = Gutenberg(dataSource: self)
fileprivate var htmlMode = false
fileprivate var mediaPickAndUploadCoordinator: MediaPickAndUploadCoordinator?
fileprivate var mediaPickCoordinator: MediaPickCoordinator?
fileprivate lazy var mediaUploadCoordinator: MediaUploadCoordinator = {
let mediaUploadCoordinator = MediaUploadCoordinator(gutenberg: self.gutenberg)
return mediaUploadCoordinator
}()

override func loadView() {
view = gutenberg.rootView
Expand Down Expand Up @@ -49,22 +53,62 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
callback(1, "https://cldup.com/cXyG__fTLN.jpg")
case .deviceLibrary:
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)
pickAndUpload(from: .savedPhotosAlbum, callback: callback)
case .deviceCamera:
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(from: .camera)
pickAndUpload(from: .camera, callback: callback)
}
}

func pickAndUpload(from source: UIImagePickerController.SourceType, callback: @escaping MediaPickerDidPickMediaCallback) {
mediaPickCoordinator = MediaPickCoordinator(presenter: self, callback: { (url) in
guard let url = url, let mediaID = self.mediaUploadCoordinator.upload(url: url) else {
callback(nil, nil)
return
}
callback(mediaID, url.absoluteString)
self.mediaPickCoordinator = nil
} )
mediaPickCoordinator?.pick(from: source)
}

func gutenbergDidRequestMediaUploadSync() {
print("Gutenberg request for media uploads to be resync")
}

func gutenbergDidRequestMediaUploadActionDialog(for mediaID: Int32) {
guard let progress = mediaUploadCoordinator.progressForUpload(mediaID: mediaID) else {
return
}

let title: String = "Media Options"
var message: String? = ""
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
let dismissAction = UIAlertAction(title: "Dismiss", style: .cancel) { (action) in

}
alertController.addAction(dismissAction)

if progress.fractionCompleted < 1 {
let cancelUploadAction = UIAlertAction(title: "Cancel upload", style: .destructive) { (action) in
self.mediaUploadCoordinator.cancelUpload(with: mediaID)
}
alertController.addAction(cancelUploadAction)
} else if let error = progress.userInfo[.mediaError] as? String {
message = error
let retryUploadAction = UIAlertAction(title: "Retry upload", style: .default) { (action) in
self.mediaUploadCoordinator.retryUpload(with: mediaID)
}
alertController.addAction(retryUploadAction)
}

alertController.title = title
alertController.message = message
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
alertController.popoverPresentationController?.permittedArrowDirections = .any
present(alertController, animated: true, completion: nil)
}
}

extension GutenbergViewController: GutenbergBridgeDataSource {
Expand Down
83 changes: 0 additions & 83 deletions ios/gutenberg/MediaPickAndUploadCoordinator.swift

This file was deleted.

60 changes: 60 additions & 0 deletions ios/gutenberg/MediaPickCoordinator.swift
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)
}

}
79 changes: 79 additions & 0 deletions ios/gutenberg/MediaUploadCoordinator.swift
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")
}
1 change: 1 addition & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ before_install:
- yes | $ANDROID_HOME/tools/bin/sdkmanager "platforms;android-27"
- yes | $ANDROID_HOME/tools/bin/sdkmanager "build-tools;27.0.3"
install:
- export TMPDIR=`dirname $(mktemp)`
- echo "Changing into the android folder of the Bridge module"
- pushd react-native-gutenberg-bridge/android && ./gradlew --stacktrace clean -Pgroup=com.github.wordpress-mobile.gutenberg-mobile -Pversion=$VERSION install && popd
- pushd react-native-aztec/android && ./gradlew --stacktrace clean -Pgroup=com.github.wordpress-mobile.gutenberg-mobile -Pversion=$VERSION install && popd
Expand Down
Loading

0 comments on commit 27b4de6

Please sign in to comment.