Skip to content

Commit

Permalink
Added EmailUtil implementation. Improvement in MochaTask class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Sholl e Santos committed Jul 19, 2017
1 parent 6aa0183 commit 9fa54bb
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 32 deletions.
Binary file not shown.
28 changes: 28 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MochaUtilities.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MochaUtilities'
s.version = '0.2.1'
s.version = '0.3.0'
s.summary = 'A framework designed to deal with some common iOS needs.'

# This description is used to generate tags and improve search results.
Expand Down
53 changes: 53 additions & 0 deletions MochaUtilities/Classes/Network/Domain/MochaEmailAttachment.swift
@@ -0,0 +1,53 @@
//
// MochaEmailAttachment.swift
// Pods
//
// Created by Gregory Sholl e Santos on 18/07/17.
//
//

import UIKit

//MARK: -

public class MochaEmailAttachment {

//MARK: Variables

private var _data : Data!
private var _type : String!
private var _filename: String!

public var data : Data {
return _data
}

public var type : String {
return _type
}

public var filename : String {
return _filename
}

//MARK: Inits

///compressionQuality is a number between 0.0 and 1.0
public init(jpegImage: UIImage, compressionQuality: CGFloat = 1.0, filename: String) {
_data = UIImageJPEGRepresentation(jpegImage, compressionQuality)
_type = "image/jpeg"
_filename = filename
}

public init(pngImage: UIImage, filename: String) {
_data = UIImagePNGRepresentation(pngImage)
_type = "image/png"
_filename = filename
}

public init(data: Data, type: String, filename: String) {
_data = data
_type = type
_filename = filename
}
}
15 changes: 15 additions & 0 deletions MochaUtilities/Classes/Network/Protocols/EmailUtilDelegate.swift
@@ -0,0 +1,15 @@
//
// EmailUtilDelegate.swift
// Pods
//
// Created by Gregory Sholl e Santos on 18/07/17.
//
//

import UIKit

public protocol EmailUtilDelegate {
func onEmailSuccessful()
func onEmailCancelled()
func onEmailFailed()
}
120 changes: 120 additions & 0 deletions MochaUtilities/Classes/Network/Utils/EmailUtil.swift
@@ -0,0 +1,120 @@
//
// EmailUtil.swift
// Pods
//
// Created by Gregory Sholl e Santos on 18/07/17.
//
//

import UIKit

import MobileCoreServices
import MessageUI

//MARK: - Main

public class EmailUtil: NSObject {

//MARK: Variables

static fileprivate var instance : EmailUtil?

fileprivate var delegate : EmailUtilDelegate?

//MARK: Inits

fileprivate override init() {
super.init()
}
}

//MARK: - Valid

public extension EmailUtil {

static public func isValid(_ string: String?, strictRules strict: Bool = false) -> Bool {
let emailRegex = strict ? "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" : "^.+@.+\\.[A-Za-z]{2}[A-Za-z]*$"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
return emailTest.evaluate(with: string)
}
}

//MARK: - Send

public extension EmailUtil {

static public func canSend() -> Bool {
return MFMailComposeViewController.canSendMail()
}

static public func send(to destinationEmail: String, withSubject subject: String = "", andBody body: String = "") {

if !canSend() {
return
}

var email = "mailto:\(destinationEmail)"

if subject.isNotEmpty {
email = "\(email)?subject=\(subject)"
}

if body.isNotEmpty {
email = "\(email)&body=\(body)"
}

if let addedPercent = email.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) {
if let url = URL(string: addedPercent) {
UIApplication.shared.openURL(url)
}
}
}

static public func open(with delegate: EmailUtilDelegate, on viewController: UIViewController, withRecipients recipients: [String] = [], subject: String = "", body: String = "", isBodyHtml: Bool = false, andAttachments attachments: [MochaEmailAttachment] = []) -> EmailUtil? {

if !canSend() {
return nil
}

let emailUtils = EmailUtil()
emailUtils.delegate = delegate

let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = emailUtils
mailCompose.setToRecipients(recipients)
mailCompose.setSubject(subject)
mailCompose.setMessageBody(body, isHTML: isBodyHtml)

for attachment in attachments {
mailCompose.addAttachmentData(attachment.data, mimeType: attachment.type, fileName: attachment.filename)
}

viewController.present(mailCompose, animated: true, completion: nil)

EmailUtil.instance = emailUtils

return emailUtils
}
}

//MARK: - Mail Compose View Controller Delegate

extension EmailUtil: MFMailComposeViewControllerDelegate {

public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
EmailUtil.instance = nil

controller.dismiss(animated: true, completion: {
DispatchQueue.main.async {
switch result {
case .cancelled:
self.delegate?.onEmailCancelled()
case .sent:
self.delegate?.onEmailSuccessful()
default:
break
}
}
})
}
}
37 changes: 6 additions & 31 deletions MochaUtilities/Classes/Tasks/Domain/MochaTask.swift
Expand Up @@ -12,9 +12,11 @@ public typealias TaskBlock = () -> Void
public typealias ThrowableTaskBlock = () throws -> Void
public typealias ErrorTaskBlock = (_ error: Error) -> Void

//MARK: -

public class MochaTask: NSObject {

//MARK: - Variables
//MARK: Variables

var preExecute : TaskBlock?
var execute : ThrowableTaskBlock!
Expand All @@ -24,45 +26,18 @@ public class MochaTask: NSObject {

var operation : BlockOperation!

//MARK: - Inits
//MARK: Inits

override public init() {
super.init()
}

public convenience init(execute: @escaping ThrowableTaskBlock, updateView: @escaping TaskBlock) {
self.init()

preExecute = nil
self.execute = execute
self.updateView = updateView
error = nil
}

public convenience init(execute: @escaping ThrowableTaskBlock, updateView: @escaping TaskBlock, onError: @escaping ErrorTaskBlock) {
self.init()

preExecute = nil
self.execute = execute
self.updateView = updateView
error = onError
}

public convenience init(preExecute: @escaping TaskBlock, execute: @escaping ThrowableTaskBlock, updateView: @escaping TaskBlock) {
self.init()

self.preExecute = preExecute
self.execute = execute
self.updateView = updateView
error = nil
}

public convenience init(preExecute: @escaping TaskBlock, execute: @escaping ThrowableTaskBlock, updateView: @escaping TaskBlock, onError: @escaping ErrorTaskBlock) {
public convenience init(preExecute: TaskBlock? = nil, execute: @escaping ThrowableTaskBlock, updateView: @escaping TaskBlock, error: ErrorTaskBlock? = nil) {
self.init()

self.preExecute = preExecute
self.execute = execute
self.updateView = updateView
error = onError
self.error = error
}
}
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -251,6 +251,10 @@ func doHttpRequest(needsBasicAuth: Bool, addDefaultHeader: Bool) {

More examples will be included as the documentation grows.

#### EmailUtilDelegate

The protocol used to communicate the caller class and [EmailUtil](#emailutil). Its methods are `onEmailSuccess()`, `onEmailCancelled()` and `onEmailFailed()`, all of which are of mandatory implementation.

#### BrowserUtil

To open the default browser of the device, use the `openUrl(_: String?)`. If the given String is nil or is not valid, no action is taken.
Expand Down

0 comments on commit 9fa54bb

Please sign in to comment.