Skip to content

Commit 4e6ce6c

Browse files
Merge pull request #2 from snipsco/fix/demo
Fix demo
2 parents e62ee8d + 578c766 commit 4e6ce6c

10 files changed

Lines changed: 80 additions & 50 deletions

Postal.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Postal'
3-
s.version = '0.0.2'
3+
s.version = '0.1.1'
44
s.summary = 'A swift framework for working with emails.'
55
s.description = 'A Swift framework for working with emails. Simple and quick to use. Built on top of libetpan.'
66
s.homepage = 'https://github.com/snipsco/Postal'

Postal/IMAPSession+Fetch.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension IMAPSession {
147147
func fetchLast(folder: String, last: UInt, flags: FetchFlag, extraHeaders: Set<String> = [], handler: FetchResult -> Void) throws {
148148
let info = try select(folder)
149149

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

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

Postal/IMAPSession.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ final class IMAPSession {
153153
case .accessToken(let accessToken):
154154
result = mailimap_oauth2_authenticate(imap, configuration.login, accessToken)
155155
case .plain(let password):
156-
result = mailimap_authenticate(imap, "", configuration.hostname, nil, nil, configuration.login, configuration.login, password, nil)
156+
result = mailimap_login(imap, configuration.login, password)
157157
}
158158

159159
try result.toIMAPError?.enrich { return .loginError(String.fromCString(imap.memory.imap_response) ?? "") }.check()

Postal/Postal.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ private extension Postal {
210210
}
211211
}
212212

213-
onComplete(nil)
213+
dispatch_async(dispatch_get_main_queue()) {
214+
onComplete(nil)
215+
}
214216
} catch let error as E {
215217
dispatch_async(dispatch_get_main_queue()) {
216218
onComplete(error)

PostalDemo/PostalDemo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
F7DE80131CF363110031E26A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F7DE80121CF363110031E26A /* Assets.xcassets */; };
1717
F7DE80161CF363110031E26A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7DE80141CF363110031E26A /* LaunchScreen.storyboard */; };
1818
F7DE80271CF36C870031E26A /* AddAccountTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7DE80261CF36C870031E26A /* AddAccountTableViewController.swift */; };
19+
F7EC542C1D52253C0088983E /* UIViewController+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7EC542B1D52253C0088983E /* UIViewController+Helpers.swift */; };
1920
/* End PBXBuildFile section */
2021

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

5860
/* Begin PBXFrameworksBuildPhase section */
@@ -91,6 +93,7 @@
9193
F7DE80261CF36C870031E26A /* AddAccountTableViewController.swift */,
9294
F7A1E12C1CF464D00046C03E /* LoginTableViewController.swift */,
9395
F77FEF191D058E310036A9A7 /* MailsTableViewController.swift */,
96+
F7EC542B1D52253C0088983E /* UIViewController+Helpers.swift */,
9497
F7DE800F1CF363110031E26A /* Main.storyboard */,
9598
F7DE80121CF363110031E26A /* Assets.xcassets */,
9699
F7DE80141CF363110031E26A /* LaunchScreen.storyboard */,
@@ -181,6 +184,7 @@
181184
isa = PBXSourcesBuildPhase;
182185
buildActionMask = 2147483647;
183186
files = (
187+
F7EC542C1D52253C0088983E /* UIViewController+Helpers.swift in Sources */,
184188
F77FEF1A1D058E310036A9A7 /* MailsTableViewController.swift in Sources */,
185189
F7DE800C1CF363110031E26A /* AppDelegate.swift in Sources */,
186190
F7DE80271CF36C870031E26A /* AddAccountTableViewController.swift in Sources */,

PostalDemo/PostalDemo/AddAccountTableViewController.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ enum MailProvider: Int {
1515
case yahoo
1616
case outlook
1717
case aol
18-
case other
1918

2019
var hostname: String {
2120
switch self {
@@ -24,7 +23,16 @@ enum MailProvider: Int {
2423
case .yahoo: return "yahoo.com"
2524
case .outlook: return "outlook.com"
2625
case .aol: return "aol.com"
27-
case .other: return "example.com"
26+
}
27+
}
28+
29+
var preConfiguration: Configuration? {
30+
switch self {
31+
case .icloud: return .icloud(login: "", password: "")
32+
case .google: return .gmail(login: "", password: .plain(""))
33+
case .yahoo: return .yahoo(login: "", password: .plain(""))
34+
case .outlook: return .outlook(login: "", password: "")
35+
case .aol: return .aol(login: "", password: "")
2836
}
2937
}
3038
}

PostalDemo/PostalDemo/Base.lproj/Main.storyboard

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
<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">
158158
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
159159
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
160-
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
160+
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
161161
<sections>
162162
<tableViewSection id="aeB-0x-gCB">
163163
<cells>
@@ -179,7 +179,7 @@
179179
<constraint firstAttribute="width" constant="90" id="Vry-Uy-TDJ"/>
180180
</constraints>
181181
<fontDescription key="fontDescription" type="system" pointSize="17"/>
182-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
182+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
183183
<nil key="highlightedColor"/>
184184
</label>
185185
</subviews>
@@ -207,7 +207,7 @@
207207
<constraint firstAttribute="width" constant="90" id="KCo-s2-MdN"/>
208208
</constraints>
209209
<fontDescription key="fontDescription" type="system" pointSize="17"/>
210-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
210+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
211211
<nil key="highlightedColor"/>
212212
</label>
213213
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Required" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="9qE-0C-rrH">
@@ -249,7 +249,7 @@
249249
<constraint firstAttribute="width" constant="90" id="nJN-LJ-Yni"/>
250250
</constraints>
251251
<fontDescription key="fontDescription" type="system" pointSize="17"/>
252-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
252+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
253253
<nil key="highlightedColor"/>
254254
</label>
255255
</subviews>
@@ -277,13 +277,13 @@
277277
<constraint firstAttribute="width" constant="90" id="HGR-an-YM0"/>
278278
</constraints>
279279
<fontDescription key="fontDescription" type="system" pointSize="17"/>
280-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
280+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
281281
<nil key="highlightedColor"/>
282282
</label>
283283
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Required" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="dmQ-Sy-4JN">
284284
<rect key="frame" x="106" y="8" width="486" height="27.5"/>
285285
<fontDescription key="fontDescription" type="system" pointSize="14"/>
286-
<textInputTraits key="textInputTraits" keyboardType="numberPad" secureTextEntry="YES"/>
286+
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
287287
</textField>
288288
</subviews>
289289
<constraints>
@@ -343,14 +343,14 @@
343343
<rect key="frame" x="15" y="6" width="31.5" height="19.5"/>
344344
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
345345
<fontDescription key="fontDescription" type="system" pointSize="16"/>
346-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
346+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
347347
<nil key="highlightedColor"/>
348348
</label>
349349
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Detail" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Slb-l8-5qu">
350350
<rect key="frame" x="15" y="25.5" width="30.5" height="13.5"/>
351351
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
352352
<fontDescription key="fontDescription" type="system" pointSize="11"/>
353-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
353+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
354354
<nil key="highlightedColor"/>
355355
</label>
356356
</subviews>
@@ -385,7 +385,7 @@
385385
<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">
386386
<rect key="frame" x="281.5" y="289.5" width="37" height="21"/>
387387
<fontDescription key="fontDescription" type="system" pointSize="17"/>
388-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
388+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
389389
<nil key="highlightedColor"/>
390390
</label>
391391
</subviews>

PostalDemo/PostalDemo/LoginTableViewController.swift

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class LoginTableViewController: UITableViewController {
3535
@IBOutlet weak var hostnameTextField: UITextField!
3636
@IBOutlet weak var portTextField: UITextField!
3737

38-
var provider: MailProvider!
38+
var provider: MailProvider?
3939
}
4040

4141
// MARK: - View lifecycle
@@ -45,7 +45,13 @@ extension LoginTableViewController {
4545
override func viewDidLoad() {
4646
super.viewDidLoad()
4747

48-
emailTextField.placeholder = "example@\(provider.hostname)"
48+
if let provider = provider, configuration = provider.preConfiguration {
49+
emailTextField.placeholder = "exemple@\(provider.hostname)"
50+
hostnameTextField.userInteractionEnabled = false
51+
hostnameTextField.text = configuration.hostname
52+
portTextField.userInteractionEnabled = false
53+
portTextField.text = "\(configuration.port)"
54+
}
4955
}
5056
}
5157

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

82-
switch provider! {
83-
case .icloud:
84-
return .icloud(login: email, password: password)
85-
case .google:
86-
return .gmail(login: email, password: .plain(password))
87-
case .yahoo:
88-
return .yahoo(login: email, password: .plain(password))
89-
case .outlook:
90-
return .outlook(login: email, password: password)
91-
case .aol:
92-
return .aol(login: email, password: password)
93-
case .other:
86+
if let configuration = provider?.preConfiguration {
87+
return Configuration(hostname: configuration.hostname, port: configuration.port, login: email, password: .plain(password), connectionType: configuration.connectionType, checkCertificateEnabled: configuration.checkCertificateEnabled)
88+
} else {
9489
guard let hostname = hostnameTextField.text where !hostname.isEmpty else { throw LoginError.badHostname }
9590
guard let portText = portTextField.text where !portText.isEmpty else { throw LoginError.badPort }
9691
guard let port = UInt16(portText) else { throw LoginError.badPort }
97-
98-
return Configuration(hostname: hostname, port: port, login: email, password: .plain(password), connectionType: .TLS, checkCertificateEnabled: true)
92+
93+
return Configuration(hostname: hostname, port: port, login: email, password: .plain(""), connectionType: .tls, checkCertificateEnabled: true)
9994
}
10095
}
10196
}

PostalDemo/PostalDemo/MailsTableViewController.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,24 @@ extension MailsTableViewController {
2424
override func viewDidLoad() {
2525
super.viewDidLoad()
2626

27+
// Do connection
2728
postal.connect(timeout: Postal.defaultTimeout, completion: { [weak self] result in
2829
switch result {
29-
case .Success:
30-
print("success")
30+
case .Success: // Fetch 50 last mails of the INBOX
31+
self?.postal.fetchLast("INBOX", last: 50, flags: [ .fullHeaders ], onMessage: { message in
32+
self?.messages.insert(message, atIndex: 0)
33+
34+
}, onComplete: { error in
35+
if let error = error {
36+
self?.showAlertError("Fetch error", message: (error as NSError).localizedDescription)
37+
} else {
38+
self?.tableView.reloadData()
39+
}
40+
})
41+
3142
case .Failure(let error):
32-
print("error: \(error)")
43+
self?.showAlertError("Connection error", message: (error as NSError).localizedDescription)
3344
}
34-
35-
self?.postal.fetchLast("INBOX", last: 50, flags: [ .headers ], onMessage: { message in
36-
self?.messages.append(message)
37-
38-
}, onComplete: { error in
39-
if let error = error {
40-
print("fetch error: \(error)")
41-
return
42-
}
43-
44-
self?.tableView.reloadData()
45-
})
4645
})
4746
}
4847
}
@@ -66,3 +65,9 @@ extension MailsTableViewController {
6665
return cell
6766
}
6867
}
68+
69+
// MARK: - Helper
70+
71+
private extension MailsTableViewController {
72+
73+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// UIViewController+Helpers.swift
3+
// PostalDemo
4+
//
5+
// Created by Kevin Lefevre on 03/08/2016.
6+
// Copyright © 2016 Snips. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIViewController {
12+
func showAlertError(title: String, message: String) {
13+
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
14+
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
15+
presentViewController(alert, animated: true, completion: nil)
16+
}
17+
}

0 commit comments

Comments
 (0)