Skip to content

Commit

Permalink
Merge pull request #198 from Hirobreak/syncfix
Browse files Browse the repository at this point in the history
Sync Improvement, Header View for Empty Trash
  • Loading branch information
danieltigse authored Sep 20, 2018
2 parents 3922e83 + ed3d7b9 commit 3944005
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 59 deletions.
4 changes: 4 additions & 0 deletions iOS-Email-Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
7C154FD3204DEBAF00D72F6E /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C154FD2204DEBAF00D72F6E /* Account.swift */; };
7C15669E20A232E8000CA8D2 /* TopbarUIView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7C15669D20A232E8000CA8D2 /* TopbarUIView.xib */; };
7C1566A020A23AEB000CA8D2 /* TopbarUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C15669F20A23AEB000CA8D2 /* TopbarUIView.swift */; };
7C1DA3892152F17000C4C38E /* MailboxHeaderUITableCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7C1DA3882152F17000C4C38E /* MailboxHeaderUITableCell.xib */; };
7C1E82BE2116A122007CA853 /* Instructions.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C1E82BD2116A122007CA853 /* Instructions.framework */; };
7C1E82C22116BD43007CA853 /* HintUIView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7C1E82C12116BD43007CA853 /* HintUIView.xib */; };
7C1E82C42116BDF2007CA853 /* HintUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C1E82C32116BDF2007CA853 /* HintUIView.swift */; };
Expand Down Expand Up @@ -342,6 +343,7 @@
7C154FD2204DEBAF00D72F6E /* Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Account.swift; sourceTree = "<group>"; };
7C15669D20A232E8000CA8D2 /* TopbarUIView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TopbarUIView.xib; sourceTree = "<group>"; };
7C15669F20A23AEB000CA8D2 /* TopbarUIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopbarUIView.swift; sourceTree = "<group>"; };
7C1DA3882152F17000C4C38E /* MailboxHeaderUITableCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MailboxHeaderUITableCell.xib; sourceTree = "<group>"; };
7C1E82BD2116A122007CA853 /* Instructions.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Instructions.framework; path = Carthage/Build/iOS/Instructions.framework; sourceTree = "<group>"; };
7C1E82BF2116A12B007CA853 /* InstructionsAppExtensions.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = InstructionsAppExtensions.framework; path = Carthage/Build/iOS/InstructionsAppExtensions.framework; sourceTree = "<group>"; };
7C1E82C12116BD43007CA853 /* HintUIView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HintUIView.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -867,6 +869,7 @@
7CF93F4E2146D05200361837 /* WelcomeTourView.xib */,
7CF93F5B214963A500361837 /* GeneralVersionTableViewCell.swift */,
7C2B7D1F214ACA6400604059 /* GeneralHeaderTableViewCell.swift */,
7C1DA3882152F17000C4C38E /* MailboxHeaderUITableCell.xib */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -1101,6 +1104,7 @@
412CB0362037402D005D1B3F /* NunitoSans-LightItalic.ttf in Resources */,
7C1E82C22116BD43007CA853 /* HintUIView.xib in Resources */,
41AAC553201AA06F00809C4D /* MCOMessageViewScript.js in Resources */,
7C1DA3892152F17000C4C38E /* MailboxHeaderUITableCell.xib in Resources */,
7C5322122111394000C78981 /* GoogleService-Info.plist in Resources */,
7C5FD822205C2FBB000E5F14 /* AttachmentTableViewCell.xib in Resources */,
7CB3B942208A5ADB00EDD427 /* DetailMoreOptionsUIView.xib in Resources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Support"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
1 change: 1 addition & 0 deletions iOS-Email-Client/AsyncTasks/SendMailAsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class SendMailAsyncTask {
return
}
guard case let .SuccessArray(keysArray) = responseData else {
self.setEmailAsFailed()
DispatchQueue.main.async {
completion(responseData)
}
Expand Down
34 changes: 0 additions & 34 deletions iOS-Email-Client/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -788,40 +788,6 @@
<outlet property="subjectWidthConstraint" destination="1vh-Ub-Ltk" id="wSY-pB-XfU"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" shouldIndentWhileEditing="NO" reuseIdentifier="InboxHeaderTableViewCell" rowHeight="95" id="eat-5Z-KvI" customClass="InboxHeaderUITableCell" customModule="iOS_Email_Client" customModuleProvider="target">
<rect key="frame" x="0.0" y="134.5" width="375" height="95"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="eat-5Z-KvI" id="ahv-8O-jyw">
<rect key="frame" x="0.0" y="0.0" width="375" height="95"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Messages that have been in Trash more than 30 days will be automatically deleted. " textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ju6-Ht-8Y3">
<rect key="frame" x="31" y="25" width="302" height="38.5"/>
<fontDescription key="fontDescription" name="NunitoSans-Regular" family="Nunito Sans" pointSize="14"/>
<color key="textColor" red="0.25882352941176467" green="0.29803921568627451" blue="0.32156862745098036" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7L4-wr-sIJ">
<rect key="frame" x="274" y="59.5" width="81" height="32"/>
<fontDescription key="fontDescription" name="NunitoSans-Bold" family="Nunito Sans" pointSize="14"/>
<state key="normal" title="Empty Trash">
<color key="titleColor" red="0.0" green="0.56862745098039214" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</state>
<connections>
<action selector="onEmptyTrashPress:" destination="eat-5Z-KvI" eventType="touchUpInside" id="oP9-9C-gZ9"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="Ju6-Ht-8Y3" firstAttribute="top" secondItem="ahv-8O-jyw" secondAttribute="top" constant="25" id="0eO-1D-vIX"/>
<constraint firstAttribute="trailing" secondItem="Ju6-Ht-8Y3" secondAttribute="trailing" constant="42" id="RPs-5k-uRR"/>
<constraint firstAttribute="trailing" secondItem="7L4-wr-sIJ" secondAttribute="trailing" constant="20" id="icn-eP-tP3"/>
<constraint firstItem="7L4-wr-sIJ" firstAttribute="top" secondItem="Ju6-Ht-8Y3" secondAttribute="bottom" constant="-4" id="kJI-HW-Xyi"/>
<constraint firstItem="Ju6-Ht-8Y3" firstAttribute="leading" secondItem="ahv-8O-jyw" secondAttribute="leading" constant="31" id="yuK-Bc-YPS"/>
</constraints>
</tableViewCellContentView>
<color key="backgroundColor" red="0.94901960784313721" green="0.97254901960784312" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="gA0-rr-zOr" id="zIv-5u-lHz"/>
Expand Down
25 changes: 16 additions & 9 deletions iOS-Email-Client/Controllers/EmailDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,20 @@ extension EmailDetailViewController: NavigationToolbarDelegate {
self.setLabels(added: [SystemLabel.trash.id], removed: [], forceRemove: true)
return
}
let archiveAction = UIAlertAction(title: "Ok", style: .destructive){ (alert : UIAlertAction!) -> Void in
DBManager.delete(self.emailData.emails)
self.mailboxData.removeSelectedRow = true
self.navigationController?.popViewController(animated: true)
let deleteAction = UIAlertAction(title: "Ok", style: .destructive){ (alert : UIAlertAction!) -> Void in
let eventData = EventData.Peer.ThreadDeleted(threadIds: [self.emailData.threadId])
self.postPeerEvent(["cmd": Event.Peer.threadsDeleted.rawValue, "params": eventData.asDictionary()]) { (responseData) in
guard case .Success = responseData else {
self.showAlert("Something went wrong", message: "Unable to delete thread. Please try again", style: .alert)
return
}
DBManager.delete(self.emailData.emails)
self.mailboxData.removeSelectedRow = true
self.navigationController?.popViewController(animated: true)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
showAlert("Delete Threads", message: "The selected threads will be PERMANENTLY deleted", style: .alert, actions: [archiveAction, cancelAction])
showAlert("Delete Threads", message: "The selected threads will be PERMANENTLY deleted", style: .alert, actions: [deleteAction, cancelAction])
}

func onMarkThreads() {
Expand Down Expand Up @@ -520,7 +527,7 @@ extension EmailDetailViewController: DetailMoreOptionsViewDelegate {
let emailKey = email.key
postPeerEvent(["cmd": Event.Peer.emailsDeleted.rawValue, "params": eventData.asDictionary()]) { (responseData) in
guard case .Success = responseData else {
self.showAlert("Something went wrong", message: "Unable to set labels. Please try again", style: .alert)
self.showAlert("Something went wrong", message: "Unable to delete email. Please try again", style: .alert)
return
}
DBManager.delete(email)
Expand All @@ -543,7 +550,7 @@ extension EmailDetailViewController: DetailMoreOptionsViewDelegate {
}

func removeEmail(key: Int){
guard let index = emailData.emails.index(where: {$0.key == key}) else {
guard let index = emailData.emails.index(where: {$0.isInvalidated || $0.key == key}) else {
return
}
emailData.emails.remove(at: index)
Expand Down Expand Up @@ -808,8 +815,8 @@ extension EmailDetailViewController: EventHandlerDelegate {

func didReceiveEvents(result: EventData.Result) {
guard result.modifiedThreadIds.contains(emailData.threadId) || result.modifiedEmailKeys.contains(where: { (key) -> Bool in
return emailData.emails.contains(where: {$0.key == key})
}) || result.emails.contains(where: {$0.threadId == emailData.threadId}) else {
return emailData.emails.contains(where: {$0.isInvalidated || $0.key == key})
}) || result.emails.contains(where: {$0.isInvalidated || $0.threadId == emailData.threadId}) else {
return
}
reloadContent()
Expand Down
46 changes: 40 additions & 6 deletions iOS-Email-Client/Controllers/InboxViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class InboxViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let headerNib = UINib(nibName: "MailboxHeaderUITableCell", bundle: nil)
self.tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: "InboxHeaderTableViewCell")

self.navigationController?.navigationBar.addSubview(self.topToolbar)
let margins = self.navigationController!.navigationBar.layoutMarginsGuide
self.topToolbar.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: -8.0).isActive = true
Expand Down Expand Up @@ -609,7 +612,8 @@ extension InboxViewController: UITableViewDataSource{
guard mailboxData.selectedLabel == SystemLabel.trash.id && !mailboxData.threads.isEmpty else {
return nil
}
let cell = tableView.dequeueReusableCell(withIdentifier: "InboxHeaderTableViewCell") as! InboxHeaderUITableCell
let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "InboxHeaderTableViewCell") as! InboxHeaderUITableCell
cell.contentView.backgroundColor = UIColor(red: 242/255, green: 248/255, blue: 1, alpha: 1)
cell.onEmptyPress = {
self.showEmptyTrashWarning()
}
Expand Down Expand Up @@ -808,13 +812,17 @@ extension InboxViewController: InboxTableViewCellDelegate, UITableViewDelegate {
let emailDetailData = EmailDetailData(threadId: selectedThread.threadId, label: mailboxData.searchMode ? SystemLabel.all.id : selectedLabel)
var labelsSet = Set<Label>()
var openKeys = [Int]()
var peerKeys = [Int]()
for email in emails {
email.isExpanded = email.unread
labelsSet.formUnion(email.labels)
if(email.unread && email.status == .none){
openKeys.append(email.key)
if(email.unread){
if(email.status == .none) {
openKeys.append(email.key)
} else {
peerKeys.append(email.key)
}
}
DBManager.updateEmail(email, unread: false)
}
emailDetailData.emails = emails
emailDetailData.selectedLabel = selectedLabel
Expand All @@ -829,8 +837,34 @@ extension InboxViewController: InboxTableViewCellDelegate, UITableViewDelegate {
vc.mailboxData = self.mailboxData
vc.myAccount = self.myAccount
self.navigationController?.pushViewController(vc, animated: true)
if(!openKeys.isEmpty) {
APIManager.notifyOpen(keys: openKeys, token: myAccount.jwt)
openEmails(openKeys: openKeys, peerKeys: peerKeys)
}

func openOwnEmails(_ peerKeys: [Int]){
guard !peerKeys.isEmpty else {
return
}
let params = ["cmd": Event.Peer.emailsUnread.rawValue,
"params": [
"unread": 0,
"metadataKeys": peerKeys
]] as [String : Any]
self.postPeerEvent(params, failSilently: true) {
DBManager.updateEmails(peerKeys, unread: false)
}
}

func openEmails(openKeys: [Int], peerKeys: [Int]) {
guard !openKeys.isEmpty else {
openOwnEmails(peerKeys)
return
}
APIManager.notifyOpen(keys: openKeys, token: myAccount.jwt) { responseData in
guard case .Success = responseData else {
return
}
DBManager.updateEmails(openKeys, unread: false)
self.openOwnEmails(peerKeys)
}
}

Expand Down
1 change: 0 additions & 1 deletion iOS-Email-Client/Libs/EventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class EventHandler {
func handleEvent(_ event: Dictionary<String, Any>, finishCallback: @escaping (_ successfulEventId : Int32?, _ data: Any?) -> Void){
let cmd = event["cmd"] as! Int32
let rowId = event["rowid"] as? Int32 ?? -1

guard let params = event["params"] as? [String : Any] ?? Utils.convertToDictionary(text: (event["params"] as! String)) else {
finishCallback(nil, nil)
return
Expand Down
7 changes: 5 additions & 2 deletions iOS-Email-Client/Managers/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ class APIManager {
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
}

class func notifyOpen(keys: [Int], token: String){
class func notifyOpen(keys: [Int], token: String, completion: @escaping ((ResponseData) -> Void)){
let url = "\(self.baseUrl)/event/open"
let headers = ["Authorization": "Bearer \(token)",
"API-Version": apiVersion]
let params = [
"metadataKeys": keys
] as [String: Any]
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseString { response in
let responseData = handleResponse(response, satisfy: .success)
completion(responseData)
}
}

class func unsendEmail(key: Int, recipients: [String], token: String, completion: @escaping ((ResponseData) -> Void)){
Expand Down
19 changes: 17 additions & 2 deletions iOS-Email-Client/Managers/DBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ extension DBManager {
let emailContacts = getEmailContacts(emailKey: email.key)
for emailContact in emailContacts {
emailContact.email = newEmail
if(emailContact.type != ContactType.from.rawValue && email.fromContact == emailContact.contact) {
newEmail.status = .delivered
newEmail.unread = true
}
}

realm.delete(email)
Expand All @@ -365,6 +369,17 @@ extension DBManager {
}
}

class func updateEmails(_ emailKeys: [Int], unread: Bool) {
let realm = try! Realm()

try! realm.write() {
for key in emailKeys {
let email = realm.object(ofType: Email.self, forPrimaryKey: key)
email?.unread = unread
}
}
}

class func updateEmail(_ email: Email, muted: Bool){
let realm = try! Realm()

Expand All @@ -389,15 +404,15 @@ extension DBManager {
class func delete(_ email:Email){
let realm = try! Realm()

try! realm.write {
try? realm.write {
self.deleteEmail(realm: realm, emails: [email])
}
}

class func delete(_ emails:[Email]){
let realm = try! Realm()

try! realm.write {
try? realm.write {
self.deleteEmail(realm: realm, emails: emails)
}
}
Expand Down
2 changes: 1 addition & 1 deletion iOS-Email-Client/Views/InboxHeaderUITableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

class InboxHeaderUITableCell: UITableViewCell {
class InboxHeaderUITableCell: UITableViewHeaderFooterView {
var onEmptyPress: (() -> Void)?

@IBAction func onEmptyTrashPress(_ sender: Any) {
Expand Down
4 changes: 1 addition & 3 deletions iOS-Email-Client/Views/InboxTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ class InboxTableViewCell: UITableViewCell {
}

func setFields(thread: Thread, label: Int, myEmail: String){
let isSentFolder = label == SystemLabel.sent.id

secureAttachmentImageView.isHidden = true
secureAttachmentImageView.tintColor = UIColor(red:0.84, green:0.84, blue:0.84, alpha:1.0)

if !thread.unread || isSentFolder {
if !thread.unread {
backgroundColor = UIColor(red:244/255, green:244/255, blue:244/255, alpha:1.0)
senderLabel.font = Font.regular.size(15)
}else{
Expand Down
Loading

0 comments on commit 3944005

Please sign in to comment.