added helper file

image picker, call, message, email, alertProtocol
This commit is contained in:
Jungpyo Hong 2021-04-22 05:02:55 -05:00 committed by GitHub
parent 4255156615
commit 4adb7bac8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,45 @@
//
// AlertProtocol.swift
// VariousManagerDemoApp
//
// Created by JungpyoHong on 4/21/21.
//
import UIKit
enum AlertButton: String {
case ok
case cancel
case delete
case settings
}
protocol AlertProtocol: UIViewController {
func showAlert(title: String, message: String, buttons: [AlertButton], completion: @escaping (UIAlertController, AlertButton) -> Void)
}
extension AlertProtocol {
func showAlert(title: String, message: String, buttons: [AlertButton] = [.ok], completion: @escaping (UIAlertController, AlertButton) -> Void) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
buttons.forEach { button in
let action = UIAlertAction(title: button.rawValue.capitalized, style: button == .delete ? .destructive : .default) { [alert, button] _ in
completion(alert, button)
}
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
protocol CellReusable {
static var identifier: String { get }
}
extension CellReusable {
static var identifier: String {
String(describing: self)
}
}

View File

@ -0,0 +1,79 @@
//
// ConversationManager.swift
// VariousManagerDemoApp
//
// Created by JungpyoHong on 4/20/21.
//
import Foundation
import MessageUI
import UIKit
struct MailFeedback {
let recipients: [String]
let subject: String
let body: String
}
struct MessageFeedBack {
let recipients: [String]
let body: String
}
class ConversationManager: NSObject {
private weak var mailDelegate: MFMailComposeViewControllerDelegate?
private let mailController: MFMailComposeViewController
private weak var messageDelegate: MFMessageComposeViewControllerDelegate?
private let messageController: MFMessageComposeViewController
init(presentingController controller: NSObject, mailDelegate: MFMailComposeViewControllerDelegate, messageDelegate: MFMessageComposeViewControllerDelegate) {
self.mailDelegate = mailDelegate
self.mailController = MFMailComposeViewController()
self.messageDelegate = messageDelegate
self.messageController = MFMessageComposeViewController()
super.init()
messageController.messageComposeDelegate = self
mailController.mailComposeDelegate = self
}
func sendEmail(feedback: MailFeedback) -> MFMailComposeViewController {
if MFMailComposeViewController.canSendMail() {
self.mailDelegate = self
mailController.setToRecipients(feedback.recipients)
mailController.setSubject(feedback.subject)
mailController.setMessageBody(feedback.body, isHTML: false)
}
return mailController
}
func sendMessage(feedback: MessageFeedBack) -> MFMessageComposeViewController {
if MFMessageComposeViewController.canSendText() {
self.mailDelegate = self
messageController.recipients = feedback.recipients
messageController.body = feedback.body
}
return messageController
}
func makeCall(number: String) {
let number = number
guard let url = URL(string: "tel://\(number)") else { return }
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
extension ConversationManager: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
extension ConversationManager: MFMessageComposeViewControllerDelegate {
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
}

92
Helper/ImagePicker.swift Normal file
View File

@ -0,0 +1,92 @@
//
// ImagePicker.swift
// VariousManagerDemoApp
//
// Created by JungpyoHong on 4/21/21.
//
import UIKit
public protocol ImagePickerDelegate: AnyObject {
func didSelect(image: UIImage?)
}
open class ImagePicker: NSObject {
private let imagePicker: UIImagePickerController
private weak var presentationController: UIViewController?
private weak var delegate: ImagePickerDelegate?
public init(presentationController: UIViewController, delegate: ImagePickerDelegate) {
self.imagePicker = UIImagePickerController()
super.init()
self.presentationController = presentationController
self.delegate = delegate
self.imagePicker.delegate = self
self.imagePicker.allowsEditing = true
self.imagePicker.mediaTypes = ["public.image"]
}
private func action(for type: UIImagePickerController.SourceType, title: String) -> UIAlertAction? {
guard UIImagePickerController.isSourceTypeAvailable(type) else {
return nil
}
return UIAlertAction(title: title, style: .default) { [weak self] _ in
self?.imagePicker.sourceType = type
self?.presentationController?.present(self?.imagePicker ?? UIImagePickerController(), animated: true)
}
}
public func present(from sourceView: UIView) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if let action = self.action(for: .camera, title: "Take photo") {
alertController.addAction(action)
}
if let action = self.action(for: .savedPhotosAlbum, title: "Camera roll") {
alertController.addAction(action)
}
if let action = self.action(for: .photoLibrary, title: "Photo library") {
alertController.addAction(action)
}
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// if UIDevice.current.userInterfaceIdiom == .pad {
// alertController.popoverPresentationController?.sourceView = sourceView
// alertController.popoverPresentationController?.sourceRect = sourceView.bounds
// alertController.popoverPresentationController?.permittedArrowDirections = [.down, .up]
// }
self.presentationController?.present(alertController, animated: true)
}
private func pickerController(_ controller: UIImagePickerController, didSelect image: UIImage?) {
controller.dismiss(animated: true, completion: nil)
self.delegate?.didSelect(image: image)
}
}
extension ImagePicker: UIImagePickerControllerDelegate {
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.pickerController(picker, didSelect: nil)
}
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.editedImage] as? UIImage else {
return self.pickerController(picker, didSelect: nil)
}
self.pickerController(picker, didSelect: image)
}
}
extension ImagePicker: UINavigationControllerDelegate {
}