Skip to content

Commit

Permalink
ChatPrivateController: adding ability to send video with image picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvor committed Jan 18, 2017
1 parent dde0729 commit d4977f1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
83 changes: 54 additions & 29 deletions Antidote/ChatPrivateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,24 +551,21 @@ extension ChatPrivateController: ChatInputViewDelegate {
alert.popoverPresentationController?.sourceView = cameraView
alert.popoverPresentationController?.sourceRect = CGRect(x: cameraView.frame.size.width / 2, y: cameraView.frame.size.height / 2, width: 1.0, height: 1.0)

if UIImagePickerController.isSourceTypeAvailable(.camera) {
alert.addAction(UIAlertAction(title: String(localized: "photo_from_camera"), style: .default) { [unowned self] _ -> Void in
let controller = UIImagePickerController()
controller.sourceType = .camera
controller.delegate = self
self.present(controller, animated: true, completion: nil)
})
}

if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
alert.addAction(UIAlertAction(title: String(localized: "photo_from_photo_library"), style: .default) { [unowned self] _ -> Void in
let controller = UIImagePickerController()
controller.sourceType = .photoLibrary
controller.delegate = self
self.present(controller, animated: true, completion: nil)
})
func addAction(title: String, sourceType: UIImagePickerControllerSourceType) {
if UIImagePickerController.isSourceTypeAvailable(sourceType) {
alert.addAction(UIAlertAction(title: title, style: .default) { [unowned self] _ -> Void in
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = sourceType
controller.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
controller.videoQuality = .typeHigh
self.present(controller, animated: true, completion: nil)
})
}
}

addAction(title: String(localized: "photo_from_camera"), sourceType: .camera)
addAction(title: String(localized: "photo_from_photo_library"), sourceType: .photoLibrary)
alert.addAction(UIAlertAction(title: String(localized: "alert_cancel"), style: .cancel, handler: nil))

present(alert, animated: true, completion: nil)
Expand Down Expand Up @@ -602,22 +599,20 @@ extension ChatPrivateController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
dismiss(animated: true, completion: nil)

guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
guard let data = UIImageJPEGRepresentation(image, 0.9) else {
guard let type = info[UIImagePickerControllerMediaType] as? String else {
return
}

var fileName: String? = fileNameFromImageInfo(info as [String : AnyObject])
let typeImage = kUTTypeImage as String
let typeMovie = kUTTypeMovie as String

if fileName == nil {
let dateString = DateFormatter(type: .dateAndTime).string(from: Date())
fileName = "Photo \(dateString).jpg".replacingOccurrences(of: "/", with: "-")
}

submanagerFiles.send(data, withFileName: fileName!, to: chat) { (error: Error) in
handleErrorWithType(.sendFileToFriend, error: error as NSError)
switch type {
case typeImage:
sendImage(imagePickerInfo: info)
case typeMovie:
sendMovie(imagePickerInfo: info)
default:
return
}
}

Expand Down Expand Up @@ -1060,7 +1055,7 @@ private extension ChatPrivateController {
}
}

func fileNameFromImageInfo(_ info: [String: AnyObject]) -> String? {
func fileNameFromImageInfo(_ info: [String: Any]) -> String? {
guard let url = info[UIImagePickerControllerReferenceURL] as? URL else {
return nil
}
Expand Down Expand Up @@ -1138,4 +1133,34 @@ private extension ChatPrivateController {

present(alert, animated: true, completion: nil)
}

func sendImage(imagePickerInfo: [String : Any]) {
guard let image = imagePickerInfo[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
guard let data = UIImageJPEGRepresentation(image, 0.9) else {
return
}

var fileName: String? = fileNameFromImageInfo(imagePickerInfo)

if fileName == nil {
let dateString = DateFormatter(type: .dateAndTime).string(from: Date())
fileName = "Photo \(dateString).jpg".replacingOccurrences(of: "/", with: "-")
}

submanagerFiles.send(data, withFileName: fileName!, to: chat) { (error: Error) in
handleErrorWithType(.sendFileToFriend, error: error as NSError)
}
}

func sendMovie(imagePickerInfo: [String : Any]) {
guard let url = imagePickerInfo[UIImagePickerControllerMediaURL] as? URL else {
return
}

submanagerFiles.sendFile(atPath: url.path, moveToUploads: true, to: chat) { (error: Error) in
handleErrorWithType(.sendFileToFriend, error: error as NSError)
}
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]
### Added
- ChatPrivateController: adding ability to send video with image picker. [#252](https://github.com/Antidote-for-Tox/Antidote/issues/252).

## [1.3.2] - 2017-01-15
### Added
Expand Down

0 comments on commit d4977f1

Please sign in to comment.