From 4adb7bac8f9e752a8d1b153756f243898c27ec58 Mon Sep 17 00:00:00 2001 From: Jungpyo Hong <54448459+jphong1111@users.noreply.github.com> Date: Thu, 22 Apr 2021 05:02:55 -0500 Subject: [PATCH] added helper file image picker, call, message, email, alertProtocol --- Helper/AlertProtocol.swift | 45 ++++++++++++++++ Helper/ConversationManager.swift | 79 +++++++++++++++++++++++++++ Helper/ImagePicker.swift | 92 ++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+) create mode 100644 Helper/AlertProtocol.swift create mode 100644 Helper/ConversationManager.swift create mode 100644 Helper/ImagePicker.swift diff --git a/Helper/AlertProtocol.swift b/Helper/AlertProtocol.swift new file mode 100644 index 0000000..dea2103 --- /dev/null +++ b/Helper/AlertProtocol.swift @@ -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) + } +} diff --git a/Helper/ConversationManager.swift b/Helper/ConversationManager.swift new file mode 100644 index 0000000..4bc6fd9 --- /dev/null +++ b/Helper/ConversationManager.swift @@ -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) + } +} diff --git a/Helper/ImagePicker.swift b/Helper/ImagePicker.swift new file mode 100644 index 0000000..cb5834b --- /dev/null +++ b/Helper/ImagePicker.swift @@ -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 { +}