Files
GitHawk/Classes/Views/TextActionsController.swift
Ryan Nystrom 45335eb4a0 [WIP] Refactor networking to shared lib (#1602)
* [WIP] Refactor networking to shared lib

* notification models as codable and tests

* default request params

* break out into files

* fix tests

* check notification values

* add milestones

* add milestone response

* repo notifications

* refactor v3 response

* refactor to support passing response to response initializer

* add release fetch and model

* add examples from githawk

* notifications working

* almost all notifications requests migrated

* finish notification migration, add graphql request/response

* replace issue requests

* replace milestones request

* fetch assignees migrated

* readme fetch migrated

* migrate badge request

* delete unused request from client

* remove paging

* nearly done w/ migration

* local build green

* refactor gql fetches, replace mutations

* build green, slim down client

* strip session manager from client

* everything working
2018-03-04 21:35:36 -05:00

81 lines
2.6 KiB
Swift

//
// TextActionsController.swift
// Freetime
//
// Created by Ryan Nystrom on 10/8/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
final class TextActionsController: NSObject,
IssueTextActionsViewDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
ImageUploadDelegate {
private weak var textView: UITextView?
weak var viewController: UIViewController?
private var client: GithubClient?
// MARK: Public API
func configure(client: GithubClient, textView: UITextView, actions: IssueTextActionsView) {
self.client = client
self.textView = textView
actions.delegate = self
}
// MARK: IssueTextActionsViewDelegate
func didSelect(actionsView: IssueTextActionsView, operation: IssueTextActionOperation) {
func handle(_ operation: IssueTextActionOperation.Operation) {
switch operation {
case .wrap(let left, let right):
textView?.replace(left: left, right: right, atLineStart: false)
case .line(let left):
textView?.replace(left: left, right: nil, atLineStart: true)
case .execute(let block):
block()
case .uploadImage:
displayUploadImage()
case .multi(let operations):
operations.forEach { handle($0) }
}
}
handle(operation.operation)
}
// MARK: Image Upload
func displayUploadImage() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.modalPresentationStyle = .formSheet
viewController?.present(imagePicker, animated: trueUnlessReduceMotionEnabled)
}
// MARK: UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
let username = client?.userSession?.username ?? Constants.Strings.unknown
guard let uploadController = ImageUploadTableViewController.create(image, username: username, delegate: self) else { return }
picker.pushViewController(uploadController, animated: trueUnlessReduceMotionEnabled)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: trueUnlessReduceMotionEnabled)
}
// MARK: ImageUploadDelegate
func imageUploaded(link: String, altText: String) {
textView?.replace(left: "![\(altText)](\(link))", right: nil, atLineStart: false)
}
}