diff --git a/.DS_Store b/.DS_Store index d1356d2..e10655d 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate index 7a36e29..2d9d20b 100644 Binary files a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate and b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 01efc02..9193f2d 100644 --- a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -280,7 +280,7 @@ moduleName = "Vendoo" usesParentBreakpointCondition = "Yes" urlString = "file:///Users/okechi/Documents/iOs%20Practice/Kroleo/Vendoo/Vendoo_bb/Vendoo/Vendoo/ExternalWebViewController.swift" - timestampString = "506444138.580552" + timestampString = "507078870.598666" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "32" @@ -295,7 +295,7 @@ moduleName = "Vendoo" usesParentBreakpointCondition = "Yes" urlString = "file:///Users/okechi/Documents/iOs%20Practice/Kroleo/Vendoo/Vendoo_bb/Vendoo/Vendoo/ExternalWebViewController.swift" - timestampString = "506444138.580651" + timestampString = "507078870.59876" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "32" @@ -310,7 +310,7 @@ moduleName = "Vendoo" usesParentBreakpointCondition = "Yes" urlString = "file:///Users/okechi/Documents/iOs%20Practice/Kroleo/Vendoo/Vendoo_bb/Vendoo/Vendoo/ExternalWebViewController.swift" - timestampString = "506444138.580732" + timestampString = "507078870.598847" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "32" @@ -325,7 +325,7 @@ moduleName = "Vendoo" usesParentBreakpointCondition = "Yes" urlString = "file:///Users/okechi/Documents/iOs%20Practice/Kroleo/Vendoo/Vendoo_bb/Vendoo/Vendoo/ExternalWebViewController.swift" - timestampString = "506444138.580811" + timestampString = "507078870.598953" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "32" @@ -610,15 +610,15 @@ @@ -630,11 +630,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EbayWebServiceManager.swift" - timestampString = "506415773.009495" + timestampString = "507078869.180288" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "245" - endingLineNumber = "245" + startingLineNumber = "246" + endingLineNumber = "246" landmarkName = "listItem(_:imageUrls:completion:)" landmarkType = "5"> @@ -646,11 +646,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EbayWebServiceManager.swift" - timestampString = "506416123.471754" + timestampString = "507078869.180288" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "296" - endingLineNumber = "296" + startingLineNumber = "297" + endingLineNumber = "297" landmarkName = "listItem(_:imageUrls:completion:)" landmarkType = "5"> @@ -658,15 +658,15 @@ @@ -914,7 +914,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Vendoo/.DS_Store b/Vendoo/.DS_Store index c2e7423..1640ede 100644 Binary files a/Vendoo/.DS_Store and b/Vendoo/.DS_Store differ diff --git a/Vendoo/EbayWebServiceManager.swift b/Vendoo/EbayWebServiceManager.swift index 5ea221c..117053d 100644 --- a/Vendoo/EbayWebServiceManager.swift +++ b/Vendoo/EbayWebServiceManager.swift @@ -45,10 +45,16 @@ class EbayWebServiceManager: NSObject { private var completion: ServiceResponse! private var isGettingSubCategories: Bool = false private var isRevisingListing: Bool = false + private var isErrorHandling: Bool = false + private var currErrorCodeKey: String = "" + private var currErrorMessage: String = "" + private var errorCount = 0 + var errorInfo: [[String:String]] = [] private var requestGroup: dispatch_group_t = dispatch_group_create() var isGettingNotification: Bool = false private var qtySold: Int! private var currItemQuantity: Int! + private var currViewController: UIViewController! //---------------------------------------------// @@ -307,7 +313,7 @@ extension EbayWebServiceManager { } //after getting a sessionID get a unique oauth token - func fetchToken() { + func fetchToken(onComplete: ServiceResponse?) { let soapMessage = "" + "\(self.xmlResponseDictionary["SessionID"] as! String!)" @@ -315,6 +321,7 @@ extension EbayWebServiceManager { let theRequest = NSMutableURLRequest(URL: url!) theRequest.addValue("FetchToken", forHTTPHeaderField: "X-EBAY-API-CALL-NAME") //dispatch_group_enter(self.requestGroup) + self.completion = onComplete self.requestMaker(soapMessage, theRequest: theRequest) } @@ -382,6 +389,7 @@ extension EbayWebServiceManager: NSURLConnectionDelegate { print(response) var response_fixed = response!.stringByReplacingOccurrencesOfString("&", withString: "and") + response_fixed = response_fixed.stringByReplacingOccurrencesOfString("'", withString: "'") let data_fixed = response_fixed.dataUsingEncoding(NSUTF8StringEncoding) print(response_fixed) let xmlParser = NSXMLParser(data: data_fixed!) @@ -417,6 +425,45 @@ extension EbayWebServiceManager: NSXMLParserDelegate { self.isRevisingListing = false } } + else if(string == "Failure") { + self.isErrorHandling = true + } + break + case "Errors": + if(self.isErrorHandling) { + + } + case "ShortMessage": + if(self.isErrorHandling) { + self.errorInfo.append(Dictionary()) + self.errorInfo[self.errorCount]["short_message"] = string + } + break + case "LongMessage": + if(self.isErrorHandling) { + self.errorInfo[self.errorCount]["long_message"] = string + } + break + case "ErrorCode": + if(self.isErrorHandling) { + self.errorInfo[self.errorCount]["error_code"] = string + } + break + case "SeverityCode": + if(self.isErrorHandling) { + self.errorInfo[self.errorCount]["severity_code"] = string + } + break + case "ErrorClassification": + if(self.isErrorHandling) { + self.errorInfo[self.errorCount]["error_classification"] = string + self.errorCount+=1 + } + break + case "Build": + if(self.isErrorHandling){ + self.completion(self.errorInfo, true) + } break /* Used to authorize user to allow posting new listings to ebay sites and fetches all necessary information when posting to Ebay @@ -439,7 +486,7 @@ extension EbayWebServiceManager: NSXMLParserDelegate { print("account credentials saved") NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ebayAuthorized") self.isAuthorized = true - //self.completion(nil, nil) + self.completion(nil, nil) //Test authentication let soapmessage = "" + @@ -595,3 +642,4 @@ extension EbayWebServiceManager: NSXMLParserDelegate { } } + diff --git a/Vendoo/EtsyRESTAPIManager.swift b/Vendoo/EtsyRESTAPIManager.swift index fcf0221..385e903 100644 --- a/Vendoo/EtsyRESTAPIManager.swift +++ b/Vendoo/EtsyRESTAPIManager.swift @@ -14,7 +14,7 @@ import OAuthSwift import Locksmith -typealias ServiceResponse = (AnyObject?, NSError?) -> Void +typealias ServiceResponse = (AnyObject?, AnyObject?) -> Void class EtsyRESTAPIManager: NSObject { diff --git a/Vendoo/HomeViewController.swift b/Vendoo/HomeViewController.swift index 9c4974e..8810557 100644 --- a/Vendoo/HomeViewController.swift +++ b/Vendoo/HomeViewController.swift @@ -216,6 +216,7 @@ class HomeViewController: UITabBarController { self.soldListings.sortInPlace({$0.title.lowercaseString < $1.title.lowercaseString}) NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil) self.notificationsManager.setListings(self.userListings) + self.notificationsManager.setVC(self.selectedViewController!) self.notificationsManager.startServicePolling() } diff --git a/Vendoo/ListingPreviewViewController.swift b/Vendoo/ListingPreviewViewController.swift index 82e8a7b..6123700 100644 --- a/Vendoo/ListingPreviewViewController.swift +++ b/Vendoo/ListingPreviewViewController.swift @@ -652,14 +652,35 @@ extension ListingPreviewViewController { "category_id":self.itemListingDictionary["ebaySubCategoryID"] as! String ] let listingCompletion: ServiceResponse = { - (listingID, error) -> Void in - if((self.newInProgressListing["networkIDs"] == nil)){ - self.newInProgressListing["networkIDs"] = Dictionary() + (listingData, error) -> Void in + + //checks if there is an error from the xml response + if((error as? Bool) != nil && (error as! Bool) && listingData as? [[String : String]] != nil){ + var listOfErrors = "" + for errorData in listingData as! [[String:String]] { + if(errorData["severity_code"] == "Error") { + listOfErrors += "Error Code: " + errorData["error_code"]! + "\n" + listOfErrors += "Message: " + errorData["long_message"]! + "\n\n" + } + } + + let alert = UIAlertController(title: "A Problem Occurred With Ebay", message: "The following is the list of errors when trying to post to ebay.\n\n\(listOfErrors)", preferredStyle: .Alert) + + alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in + alert.dismissViewControllerAnimated(true, completion: { + dispatch_group_leave(postingGroup) + }) + })) + }else{ + if((self.newInProgressListing["networkIDs"] == nil)){ + self.newInProgressListing["networkIDs"] = Dictionary() + } + var networkIDs = (self.newInProgressListing["networkIDs"] as! Dictionary) + networkIDs["ebay"] = listingData as! String + self.newInProgressListing["networkIDs"] = networkIDs + dispatch_group_leave(postingGroup) } - var networkIDs = (self.newInProgressListing["networkIDs"] as! Dictionary) - networkIDs["ebay"] = listingID as! String - self.newInProgressListing["networkIDs"] = networkIDs - dispatch_group_leave(postingGroup) + } self.ebayManager.listItem(body, imageUrls: imageURLs, completion: listingCompletion) diff --git a/Vendoo/NetworksTableViewController.swift b/Vendoo/NetworksTableViewController.swift index 3836d1f..cab8348 100644 --- a/Vendoo/NetworksTableViewController.swift +++ b/Vendoo/NetworksTableViewController.swift @@ -64,6 +64,7 @@ class NetworksTableViewController: UIViewController { override func viewDidAppear(animated: Bool) { if (!self.networkToggleOrSelect) { (menuButton.target as! SWRevealViewController).delegate = self + self.tableView.reloadData() } } @@ -269,11 +270,14 @@ extension NetworksTableViewController: UITableViewDataSource { if(((self.tabBarController as? HomeViewController)?.ebayGraphManager.isAuthorized)!){ dispatch_async(dispatch_get_main_queue(), { (cell as! EbayTableViewCell).authorizeBtn.setTitle("Logout", forState: UIControlState.Normal) + (cell as! EbayTableViewCell).settingsBtn.enabled = true + (cell as! EbayTableViewCell).settingsBtn.hidden = false }) }else{ (cell as! EbayTableViewCell).settingsBtn.enabled = false (cell as! EbayTableViewCell).settingsBtn.hidden = true + (cell as! EbayTableViewCell).authorizeBtn.setTitle("Login", forState: UIControlState.Normal) } break @@ -289,12 +293,14 @@ extension NetworksTableViewController: UITableViewDataSource { if(((tabBar as? HomeViewController)?.etsyManager.isAuthorized)!){ dispatch_async(dispatch_get_main_queue(), { (cell as! EtsyTableViewCell).authorizeBtn.setTitle("Logout", forState: UIControlState.Normal) - + (cell as! EtsyTableViewCell).settingsBtn.enabled = true + (cell as! EtsyTableViewCell).settingsBtn.hidden = false }) }else{ (cell as! EtsyTableViewCell).settingsBtn.enabled = false (cell as! EtsyTableViewCell).settingsBtn.hidden = true + (cell as! EtsyTableViewCell).authorizeBtn.setTitle("Login", forState: UIControlState.Normal) } break default: @@ -304,12 +310,15 @@ extension NetworksTableViewController: UITableViewDataSource { let tabBar = self.tabBarController if(((tabBar as? HomeViewController)?.fbGraphManager.isAuthorized)!){ dispatch_async(dispatch_get_main_queue(), { + (cell as! FBTableViewCell).settingsBtn.enabled = true + (cell as! FBTableViewCell).settingsBtn.hidden = false (cell as! FBTableViewCell).authorizeBtn.setTitle("Logout", forState: UIControlState.Normal) }) }else{ dispatch_async(dispatch_get_main_queue(), { (cell as! FBTableViewCell).settingsBtn.enabled = false (cell as! FBTableViewCell).settingsBtn.hidden = true + (cell as! FBTableViewCell).authorizeBtn.setTitle("Login", forState: UIControlState.Normal) }) } @@ -624,7 +633,32 @@ extension NetworksTableViewController: UIWebViewDelegate { var responseString : NSString? = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")! if((responseString!.containsString("Authorization successfully completed"))){ NSLog("Oauth successful") - ((self.tabBarController as? HomeViewController)?.ebayGraphManager)!.fetchToken() + ((self.tabBarController as? HomeViewController)?.ebayGraphManager)!.fetchToken({ + (loginResponse, error) in + + if((error as? Bool) != nil && (error as! Bool) && loginResponse as? [[String : String]] != nil){ + var listOfErrors = "" + for errorData in loginResponse as! [[String:String]] { + if(errorData["severity_code"] == "Error") { + listOfErrors += "Error Code: " + errorData["error_code"]! + "\n" + if errorData["error_code"] == "931" { + listOfErrors += "Message: Must relogin into your account to reauthorize your Ebay account for Vendoo." + }else{ + listOfErrors += "Message: " + errorData["long_message"]! + "\n\n" + } + } + } + + let alert = UIAlertController(title: "A Problem With Ebay Login", message: "The following is the list of errors when logging in to ebay.\n\n\(listOfErrors).\n\nAn easy fix to this would be to deauthorize vendoo from your account manually on the Ebay official site and relogin with the Vendoo application.", preferredStyle: .Alert) + + alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in + alert.dismissViewControllerAnimated(true, completion: { + + }) + })) + self.presentViewController(alert, animated: true, completion: nil) + } + }) } } diff --git a/Vendoo/ServiceNotificationManager.swift b/Vendoo/ServiceNotificationManager.swift index 0788150..72412de 100644 --- a/Vendoo/ServiceNotificationManager.swift +++ b/Vendoo/ServiceNotificationManager.swift @@ -7,6 +7,8 @@ // import Foundation +import UIKit +import Locksmith protocol ServiceNotificationManagerDelegate { func listingHasEnded(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) @@ -24,6 +26,7 @@ class ServiceNotificationManager: NSObject { var ebayGraphManager: EbayWebServiceManager! var userListings: [Listing] = [] var timer: NSTimer! + var vc: UIViewController! static var delegate: ServiceNotificationManagerDelegate? override init() { @@ -43,6 +46,10 @@ class ServiceNotificationManager: NSObject { timer.invalidate() } + func setVC(vc: UIViewController){ + self.vc = vc + } + func setManagers(fbManager: FacebookGraphAPIManager, fireManager: FirebaseManager, ebayManager: EbayWebServiceManager, etsyManager: EtsyRESTAPIManager){ self.fbGraphManager = fbManager self.firebaseManager = fireManager @@ -76,37 +83,75 @@ class ServiceNotificationManager: NSObject { self.ebayGraphManager.getListingInfo(ebayID!, onComplete: { (listingNotificationInfo, error) -> Void in - let info = (listingNotificationInfo as! [String : AnyObject]) - if(info["status"] as! String == "Ended") { - if((info["quantitySold"] as! Int) > 0) { - - //checks if listing is sold out - if (((info["itemQuantity"] as! Int) - (info["quantitySold"] as! Int)) == 0) { + + //checks if there is an error from the xml response + if((error as? Bool) != nil && (error as! Bool) && listingNotificationInfo as? [[String : String]] != nil){ + var listOfErrors = "" + for errorData in listingNotificationInfo as! [[String:String]] { + if(errorData["severity_code"] == "Error") { + listOfErrors += "Error Code: " + errorData["error_code"]! + "\n" + if errorData["error_code"] == "931" { + listOfErrors += "Message: Must relogin into your account to reauthorize your Ebay account for Vendoo." + }else{ + listOfErrors += "Message: " + errorData["long_message"]! + "\n\n" + } + } + } + + let alert = UIAlertController(title: "A Problem Occurred With Ebay", message: "The following is the list of errors when trying to to retrieve notifications from ebay.\n\n\(listOfErrors)", preferredStyle: .Alert) + + alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in + alert.dismissViewControllerAnimated(true, completion: { - ServiceNotificationManager.delegate?.listingHasNewNotifications( + }) + })) + self.vc.presentViewController(alert, animated: true, completion: { + do { + try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_ebay") + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "ebayAuthorized") + self.ebayGraphManager.isAuthorized = false + }catch { + (error) + print(error) + } + }) + }else{ + let info = (listingNotificationInfo as! [String : AnyObject]) + if(info["status"] as! String == "Ended") { + if((info["quantitySold"] as! Int) > 0) { + + //checks if listing is sold out + if (((info["itemQuantity"] as! Int) - (info["quantitySold"] as! Int)) == 0) { + + ServiceNotificationManager.delegate?.listingHasNewNotifications( + nil, + ebayInfo: [ + "type": "Sold", + "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has sold out on ebay.", + "listingKey": listing.key], + amazonInfo: nil, + etsyInfo: nil + ) + } + }else{ + ServiceNotificationManager.delegate?.listingHasEnded( nil, ebayInfo: [ - "type": "Sold", - "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has sold out on ebay.", + "type": "Ended", + "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has ended on ebay.", "listingKey": listing.key], amazonInfo: nil, etsyInfo: nil ) } - }else{ - ServiceNotificationManager.delegate?.listingHasEnded( - nil, - ebayInfo: [ - "type": "Ended", - "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has ended on ebay.", - "listingKey": listing.key], - amazonInfo: nil, - etsyInfo: nil - ) + + } - - } + + + + }) }