Skip to content

Commit

Permalink
Merge pull request #2 from snipsco/fix/demo
Browse files Browse the repository at this point in the history
Fix demo
  • Loading branch information
jeremiegirault authored Aug 3, 2016
2 parents e62ee8d + 578c766 commit 4e6ce6c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Postal.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Postal'
s.version = '0.0.2'
s.version = '0.1.1'
s.summary = 'A swift framework for working with emails.'
s.description = 'A Swift framework for working with emails. Simple and quick to use. Built on top of libetpan.'
s.homepage = 'https://github.com/snipsco/Postal'
Expand Down
3 changes: 1 addition & 2 deletions Postal/IMAPSession+Fetch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ extension IMAPSession {
func fetchLast(folder: String, last: UInt, flags: FetchFlag, extraHeaders: Set<String> = [], handler: FetchResult -> Void) throws {
let info = try select(folder)

let location = info.messagesCount > last ? info.messagesCount - last + 1 : 0
let location = info.messagesCount > last ? info.messagesCount - last + 1 : 1
let length = info.messagesCount > last ? last : info.messagesCount
let range = NSRange(location: Int(location), length: Int(length))
let indexSet = NSIndexSet(indexesInRange: range)
Expand All @@ -174,7 +174,6 @@ extension IMAPSession {
let fetchType = flags.unreleasedFetchAttributeList(extraHeaders)
defer { mailimap_fetch_type_free(fetchType) }


let givenIndexSet: NSIndexSet
let fetchFunc: (session: UnsafeMutablePointer<mailimap>, set: UnsafeMutablePointer<mailimap_set>, fetch_type: UnsafeMutablePointer<mailimap_fetch_type>, result: UnsafeMutablePointer<UnsafeMutablePointer<clist>>) -> Int32

Expand Down
2 changes: 1 addition & 1 deletion Postal/IMAPSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ final class IMAPSession {
case .accessToken(let accessToken):
result = mailimap_oauth2_authenticate(imap, configuration.login, accessToken)
case .plain(let password):
result = mailimap_authenticate(imap, "", configuration.hostname, nil, nil, configuration.login, configuration.login, password, nil)
result = mailimap_login(imap, configuration.login, password)
}

try result.toIMAPError?.enrich { return .loginError(String.fromCString(imap.memory.imap_response) ?? "") }.check()
Expand Down
4 changes: 3 additions & 1 deletion Postal/Postal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ private extension Postal {
}
}

onComplete(nil)
dispatch_async(dispatch_get_main_queue()) {
onComplete(nil)
}
} catch let error as E {
dispatch_async(dispatch_get_main_queue()) {
onComplete(error)
Expand Down
4 changes: 4 additions & 0 deletions PostalDemo/PostalDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
F7DE80131CF363110031E26A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F7DE80121CF363110031E26A /* Assets.xcassets */; };
F7DE80161CF363110031E26A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7DE80141CF363110031E26A /* LaunchScreen.storyboard */; };
F7DE80271CF36C870031E26A /* AddAccountTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7DE80261CF36C870031E26A /* AddAccountTableViewController.swift */; };
F7EC542C1D52253C0088983E /* UIViewController+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7EC542B1D52253C0088983E /* UIViewController+Helpers.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -53,6 +54,7 @@
F7DE80151CF363110031E26A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
F7DE80171CF363110031E26A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
F7DE80261CF36C870031E26A /* AddAccountTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddAccountTableViewController.swift; sourceTree = "<group>"; };
F7EC542B1D52253C0088983E /* UIViewController+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Helpers.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -91,6 +93,7 @@
F7DE80261CF36C870031E26A /* AddAccountTableViewController.swift */,
F7A1E12C1CF464D00046C03E /* LoginTableViewController.swift */,
F77FEF191D058E310036A9A7 /* MailsTableViewController.swift */,
F7EC542B1D52253C0088983E /* UIViewController+Helpers.swift */,
F7DE800F1CF363110031E26A /* Main.storyboard */,
F7DE80121CF363110031E26A /* Assets.xcassets */,
F7DE80141CF363110031E26A /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -181,6 +184,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F7EC542C1D52253C0088983E /* UIViewController+Helpers.swift in Sources */,
F77FEF1A1D058E310036A9A7 /* MailsTableViewController.swift in Sources */,
F7DE800C1CF363110031E26A /* AppDelegate.swift in Sources */,
F7DE80271CF36C870031E26A /* AddAccountTableViewController.swift in Sources */,
Expand Down
12 changes: 10 additions & 2 deletions PostalDemo/PostalDemo/AddAccountTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum MailProvider: Int {
case yahoo
case outlook
case aol
case other

var hostname: String {
switch self {
Expand All @@ -24,7 +23,16 @@ enum MailProvider: Int {
case .yahoo: return "yahoo.com"
case .outlook: return "outlook.com"
case .aol: return "aol.com"
case .other: return "example.com"
}
}

var preConfiguration: Configuration? {
switch self {
case .icloud: return .icloud(login: "", password: "")
case .google: return .gmail(login: "", password: .plain(""))
case .yahoo: return .yahoo(login: "", password: .plain(""))
case .outlook: return .outlook(login: "", password: "")
case .aol: return .aol(login: "", password: "")
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions PostalDemo/PostalDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" allowsSelection="NO" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="hZQ-Oc-KOa">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
<sections>
<tableViewSection id="aeB-0x-gCB">
<cells>
Expand All @@ -179,7 +179,7 @@
<constraint firstAttribute="width" constant="90" id="Vry-Uy-TDJ"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -207,7 +207,7 @@
<constraint firstAttribute="width" constant="90" id="KCo-s2-MdN"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Required" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="9qE-0C-rrH">
Expand Down Expand Up @@ -249,7 +249,7 @@
<constraint firstAttribute="width" constant="90" id="nJN-LJ-Yni"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -277,13 +277,13 @@
<constraint firstAttribute="width" constant="90" id="HGR-an-YM0"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Required" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="dmQ-Sy-4JN">
<rect key="frame" x="106" y="8" width="486" height="27.5"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" keyboardType="numberPad" secureTextEntry="YES"/>
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
</textField>
</subviews>
<constraints>
Expand Down Expand Up @@ -343,14 +343,14 @@
<rect key="frame" x="15" y="6" width="31.5" height="19.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Detail" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Slb-l8-5qu">
<rect key="frame" x="15" y="25.5" width="30.5" height="13.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down Expand Up @@ -385,7 +385,7 @@
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="hello" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WOl-EH-kXD">
<rect key="frame" x="281.5" y="289.5" width="37" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down
33 changes: 14 additions & 19 deletions PostalDemo/PostalDemo/LoginTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class LoginTableViewController: UITableViewController {
@IBOutlet weak var hostnameTextField: UITextField!
@IBOutlet weak var portTextField: UITextField!

var provider: MailProvider!
var provider: MailProvider?
}

// MARK: - View lifecycle
Expand All @@ -45,7 +45,13 @@ extension LoginTableViewController {
override func viewDidLoad() {
super.viewDidLoad()

emailTextField.placeholder = "example@\(provider.hostname)"
if let provider = provider, configuration = provider.preConfiguration {
emailTextField.placeholder = "exemple@\(provider.hostname)"
hostnameTextField.userInteractionEnabled = false
hostnameTextField.text = configuration.hostname
portTextField.userInteractionEnabled = false
portTextField.text = "\(configuration.port)"
}
}
}

Expand All @@ -59,9 +65,7 @@ extension LoginTableViewController {
do {
vc.configuration = try createConfiguration()
} catch let error as LoginError {
let alert = UIAlertController(title: "Error", message: error.description, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
showAlertError("Error login", message: (error as NSError).localizedDescription)
} catch {
fatalError()
}
Expand All @@ -79,23 +83,14 @@ private extension LoginTableViewController {
guard let email = emailTextField.text where !email.isEmpty else { throw LoginError.badEmail }
guard let password = passwordTextField.text where !password.isEmpty else { throw LoginError.badPassword }

switch provider! {
case .icloud:
return .icloud(login: email, password: password)
case .google:
return .gmail(login: email, password: .plain(password))
case .yahoo:
return .yahoo(login: email, password: .plain(password))
case .outlook:
return .outlook(login: email, password: password)
case .aol:
return .aol(login: email, password: password)
case .other:
if let configuration = provider?.preConfiguration {
return Configuration(hostname: configuration.hostname, port: configuration.port, login: email, password: .plain(password), connectionType: configuration.connectionType, checkCertificateEnabled: configuration.checkCertificateEnabled)
} else {
guard let hostname = hostnameTextField.text where !hostname.isEmpty else { throw LoginError.badHostname }
guard let portText = portTextField.text where !portText.isEmpty else { throw LoginError.badPort }
guard let port = UInt16(portText) else { throw LoginError.badPort }

return Configuration(hostname: hostname, port: port, login: email, password: .plain(password), connectionType: .TLS, checkCertificateEnabled: true)
return Configuration(hostname: hostname, port: port, login: email, password: .plain(""), connectionType: .tls, checkCertificateEnabled: true)
}
}
}
35 changes: 20 additions & 15 deletions PostalDemo/PostalDemo/MailsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ extension MailsTableViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Do connection
postal.connect(timeout: Postal.defaultTimeout, completion: { [weak self] result in
switch result {
case .Success:
print("success")
case .Success: // Fetch 50 last mails of the INBOX
self?.postal.fetchLast("INBOX", last: 50, flags: [ .fullHeaders ], onMessage: { message in
self?.messages.insert(message, atIndex: 0)

}, onComplete: { error in
if let error = error {
self?.showAlertError("Fetch error", message: (error as NSError).localizedDescription)
} else {
self?.tableView.reloadData()
}
})

case .Failure(let error):
print("error: \(error)")
self?.showAlertError("Connection error", message: (error as NSError).localizedDescription)
}

self?.postal.fetchLast("INBOX", last: 50, flags: [ .headers ], onMessage: { message in
self?.messages.append(message)

}, onComplete: { error in
if let error = error {
print("fetch error: \(error)")
return
}

self?.tableView.reloadData()
})
})
}
}
Expand All @@ -66,3 +65,9 @@ extension MailsTableViewController {
return cell
}
}

// MARK: - Helper

private extension MailsTableViewController {

}
17 changes: 17 additions & 0 deletions PostalDemo/PostalDemo/UIViewController+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIViewController+Helpers.swift
// PostalDemo
//
// Created by Kevin Lefevre on 03/08/2016.
// Copyright © 2016 Snips. All rights reserved.
//

import UIKit

extension UIViewController {
func showAlertError(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
}

0 comments on commit 4e6ce6c

Please sign in to comment.