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
// | |
// PaginationNetworkLogic.swift | |
// | |
// Created by Daniel Tartaglia on 4/9/17. | |
// Copyright © 2019 Daniel Tartaglia. MIT License | |
// | |
import RxSwift | |
struct PaginationUISource { |
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
// ObservableBuffer.swift | |
// | |
// Created by Daniel Tartaglia | |
// Copyright © 2019 Daniel Tartaglia. MIT License. | |
import RxSwift | |
extension ObservableType { | |
/** |
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
class ViewController: UIViewController | |
{ | |
@IBOutlet weak var avatarView: UIImageView! | |
var api: API! | |
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) { | |
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") } | |
getImage(focusView: senderView).then { image -> Promise<UIImage> in |
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
extension UIViewController | |
{ | |
func getImage(focusView: UIView) -> Promise<UIImage> { | |
var delegate: ImagePickerDelegate! = ImagePickerDelegate() | |
let controller = UIImagePickerController() | |
controller.delegate = delegate | |
let result = choiceIndexUsingActionSheet(title: "", message: "", choices: sourceOptions.map { $0.title }, onSourceView: focusView).then { (index) -> Promise<UIImage> in | |
sourceOptions[index].action(controller) | |
self.present(controller, animated: true, completion: nil) | |
return delegate.promise |
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
class ViewController: UIViewController | |
{ | |
@IBOutlet weak var avatarView: UIImageView! | |
var api: API! | |
var imagePickerDelegate: ImagePickerDelegate! | |
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) { | |
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") } | |
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
class ImagePickerDelegate: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate | |
{ | |
let promise = Promise<UIImage>() | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage) { | |
promise.fulfill(image) | |
} | |
else { | |
promise.reject(UIImagePickerControllerError.missingImage) |
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
class ViewController: UIViewController | |
{ | |
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) { | |
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") } | |
let controller = UIImagePickerController() | |
controller.delegate = self | |
choiceIndexUsingActionSheet(title: "", message: "", choices: sourceOptions.map { $0.title }, onSourceView: senderView).then { index in | |
sourceOptions[index].action(controller) | |
self.present(controller, animated: true, completion: nil) | |
} |
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
enum UserInteractionError: Error { | |
case userCanceled | |
} | |
extension UIViewController | |
{ | |
func choiceIndexUsingActionSheet(title: String, message: String, choices: [String], onSourceView view: UIView) -> Promise<Int> { | |
return Promise(queue: DispatchQueue.main) { fulfill, reject in | |
let alert = UIAlertController(title: title.isEmpty ? nil : title, message: message.isEmpty ? nil : message, preferredStyle: .actionSheet) |
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
extension UIViewController | |
{ | |
@discardableResult | |
func displayInformationAlert(title: String, message: String) -> Promise<Void> { | |
return Promise(queue: DispatchQueue.main) { fulfill, _ in | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in fulfill() })) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} |
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
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage) | |
if let data = image.flatMap({ UIImageJPEGRepresentation($0, 0.8)} ) { | |
api.upload(avatar: data).then { [weak self] in | |
self?.avatarView.image = image | |
self?.dismiss(animated: true, completion: nil) | |
} | |
.catch { [weak self] error in |