diff --git a/.DS_Store b/.DS_Store index 8a7a61a..59fbdbd 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Vendoo.xcodeproj/project.pbxproj b/Vendoo.xcodeproj/project.pbxproj index c4ff4cf..a00d28c 100644 --- a/Vendoo.xcodeproj/project.pbxproj +++ b/Vendoo.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 3E1B1F0B1D2493EE00DBCF08 /* EtsySettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E1B1F0A1D2493EE00DBCF08 /* EtsySettingsViewController.swift */; }; 3E1DC3CD1D4232490091BC60 /* CategoriesTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E1DC3CC1D4232490091BC60 /* CategoriesTableViewController.swift */; }; 3E1DC3CF1D42328C0091BC60 /* CategoryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E1DC3CE1D42328C0091BC60 /* CategoryCell.swift */; }; + 3E3A004A1D76680E00A3A5B6 /* ServiceNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E3A00491D76680E00A3A5B6 /* ServiceNotificationManager.swift */; }; 3E4897D11D0067030069F742 /* FirebaseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E4897D01D0067030069F742 /* FirebaseManager.swift */; }; 3E6CC3571CF2A94B00E00A70 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6CC3561CF2A94B00E00A70 /* AppDelegate.swift */; }; 3E6CC3591CF2A94B00E00A70 /* SignInViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6CC3581CF2A94B00E00A70 /* SignInViewController.swift */; }; @@ -74,6 +75,7 @@ 3E1B1F0A1D2493EE00DBCF08 /* EtsySettingsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EtsySettingsViewController.swift; sourceTree = ""; }; 3E1DC3CC1D4232490091BC60 /* CategoriesTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoriesTableViewController.swift; sourceTree = ""; }; 3E1DC3CE1D42328C0091BC60 /* CategoryCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoryCell.swift; sourceTree = ""; }; + 3E3A00491D76680E00A3A5B6 /* ServiceNotificationManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceNotificationManager.swift; sourceTree = ""; }; 3E4897D01D0067030069F742 /* FirebaseManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FirebaseManager.swift; sourceTree = ""; }; 3E6CC3531CF2A94B00E00A70 /* Vendoo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Vendoo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 3E6CC3561CF2A94B00E00A70 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -175,6 +177,7 @@ 3E4897CF1D00629B0069F742 /* FirebaseServices */, 3E7CF3FF1CF6363200F486B2 /* EtsyServices */, 3EC325241CF7E6B300626C48 /* FacebookServices */, + 3E3A00491D76680E00A3A5B6 /* ServiceNotificationManager.swift */, ); name = Services; sourceTree = ""; @@ -753,6 +756,7 @@ 3E7CF3FC1CF5FE9400F486B2 /* RecoverPasswordViewController.swift in Sources */, 3E1DC3CF1D42328C0091BC60 /* CategoryCell.swift in Sources */, 3EC325111CF703A600626C48 /* ItemCell.swift in Sources */, + 3E3A004A1D76680E00A3A5B6 /* ServiceNotificationManager.swift in Sources */, 3EA668A11D02836C00EE57A8 /* EbayWebServiceManager.swift in Sources */, 3EF422B21D73262000B9A3EB /* UserCell.swift in Sources */, 3ECEDB871CF9C722003566B3 /* ListingPreviewViewController.swift in Sources */, diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate index fe23b11..8169c3e 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 f33afc7..533f5c9 100644 --- a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -9,61 +9,13 @@ shouldBeEnabled = "Yes" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "Vendoo/NetworksTableViewController.swift" - timestampString = "496411234.903757" + filePath = "Vendoo/HomeViewController.swift" + timestampString = "497362780.635053" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "865" - endingLineNumber = "865" - landmarkName = "toggleNetworkSelection(_:)" - landmarkType = "5"> - - - - - - - - - - - - @@ -73,13 +25,13 @@ shouldBeEnabled = "Yes" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "Vendoo/SignInViewController.swift" - timestampString = "496597298.054868" + filePath = "Vendoo/HomeViewController.swift" + timestampString = "497362780.635053" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "36" - endingLineNumber = "36" - landmarkName = "viewDidLoad()" + startingLineNumber = "238" + endingLineNumber = "238" + landmarkName = "listingHasEnded(_:ebayInfo:amazonInfo:etsyInfo:)" landmarkType = "5"> @@ -89,13 +41,29 @@ shouldBeEnabled = "No" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "Vendoo/SignInViewController.swift" - timestampString = "496597377.488932" + filePath = "Vendoo/EtsyRESTAPIManager.swift" + timestampString = "498402567.760806" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "70" - endingLineNumber = "70" - landmarkName = "signInUser(_:)" + startingLineNumber = "625" + endingLineNumber = "625" + landmarkName = "getListingInfo(_:onCompletion:)" + landmarkType = "5"> + + + + @@ -105,13 +73,13 @@ shouldBeEnabled = "Yes" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "Vendoo/SignInViewController.swift" - timestampString = "496597380.466944" + filePath = "Vendoo/EtsyRESTAPIManager.swift" + timestampString = "498402567.760806" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "71" - endingLineNumber = "71" - landmarkName = "signInUser(_:)" + startingLineNumber = "623" + endingLineNumber = "623" + landmarkName = "getListingInfo(_:onCompletion:)" landmarkType = "5"> @@ -121,46 +89,14 @@ shouldBeEnabled = "Yes" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "Vendoo/SignInViewController.swift" - timestampString = "496597417.616973" + filePath = "Vendoo/ServiceNotificationManager.swift" + timestampString = "498402552.569625" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "57" - endingLineNumber = "57" - landmarkName = "signInUser(_:)" + startingLineNumber = "124" + endingLineNumber = "124" + landmarkName = "pollServices()" landmarkType = "5"> - - - - - - + + + + + + + + diff --git a/Vendoo/.DS_Store b/Vendoo/.DS_Store index 851e8ae..0929a6a 100644 Binary files a/Vendoo/.DS_Store and b/Vendoo/.DS_Store differ diff --git a/Vendoo/EbayWebServiceManager.swift b/Vendoo/EbayWebServiceManager.swift index 81d10dd..1458763 100644 --- a/Vendoo/EbayWebServiceManager.swift +++ b/Vendoo/EbayWebServiceManager.swift @@ -41,6 +41,9 @@ class EbayWebServiceManager: NSObject { private var isGettingSubCategories: Bool = false private var isRevisingListing: Bool = false private var requestGroup: dispatch_group_t = dispatch_group_create() + var isGettingNotification: Bool = false + private var qtySold: Int! + private var currItemQuantity: Int! //---------------------------------------------// @@ -346,6 +349,27 @@ extension EbayWebServiceManager { self.catDetailLevel = detailLevel self.requestMaker(soapMessage, theRequest: theRequest) } + + func getListingInfo(listingID: String, onComplete: ServiceResponse?){ + var soapMessage = "" + + "" + + "" + + "\(self.xmlResponseDictionary["eBayAuthToken"] as! String)" + + "" + + soapMessage = soapMessage + + "\(listingID)" + + "" + + let url = NSURL(string: self.baseURL) + let theRequest = NSMutableURLRequest(URL: url!) + theRequest.addValue("GetItem", forHTTPHeaderField: "X-EBAY-API-CALL-NAME") + + self.completion = onComplete + self.isGettingNotification = true + self.requestMaker(soapMessage, theRequest: theRequest) + + } } extension EbayWebServiceManager: NSURLConnectionDelegate { @@ -471,21 +495,23 @@ extension EbayWebServiceManager: NSXMLParserDelegate { self.completion(nil, nil) break case "UserID" : - print(string) - EbayWebServiceManager.settingsDictionary["userID"] = string - NSUserDefaults.standardUserDefaults().setObject(EbayWebServiceManager.settingsDictionary, forKey: "ebaySettings") - print(self.xmlResponseDictionary["eBayAuthToken"]) - let soapmessage = "" + - "" + - "\(self.xmlResponseDictionary["eBayAuthToken"] as! String)" + - "0ReturnAll1" + - "" - - let url = NSURL(string: self.baseURL) - let theRequest = NSMutableURLRequest(URL: url!) - theRequest.addValue("GetCategories", forHTTPHeaderField: "X-EBAY-API-CALL-NAME") - self.completion(nil, nil) - self.requestMaker(soapmessage, theRequest: theRequest) + if(!self.isGettingNotification) { + print(string) + EbayWebServiceManager.settingsDictionary["userID"] = string + NSUserDefaults.standardUserDefaults().setObject(EbayWebServiceManager.settingsDictionary, forKey: "ebaySettings") + print(self.xmlResponseDictionary["eBayAuthToken"]) + let soapmessage = "" + + "" + + "\(self.xmlResponseDictionary["eBayAuthToken"] as! String)" + + "0ReturnAll1" + + "" + + let url = NSURL(string: self.baseURL) + let theRequest = NSMutableURLRequest(URL: url!) + theRequest.addValue("GetCategories", forHTTPHeaderField: "X-EBAY-API-CALL-NAME") + self.completion(nil, nil) + self.requestMaker(soapmessage, theRequest: theRequest) + } break /* Used to build both the top level categories, and subcategories on request during item listing workflow @@ -497,9 +523,16 @@ extension EbayWebServiceManager: NSXMLParserDelegate { self.catLevel = string break case "CategoryName" : - if(!self.isGettingSubCategories) { - if((EbayWebServiceManager.settingsDictionary["categories"]) == nil) { - EbayWebServiceManager.settingsDictionary["categories"] = Dictionary>() + if (!self.isGettingNotification) { + if(!self.isGettingSubCategories) { + if((EbayWebServiceManager.settingsDictionary["categories"]) == nil) { + EbayWebServiceManager.settingsDictionary["categories"] = Dictionary>() + } + var dict: Dictionary> = (EbayWebServiceManager.settingsDictionary["categories"]!) as! Dictionary> + + dict[string] = ["cat_id": self.catID, "isLeaf": false, "level": self.catLevel] + EbayWebServiceManager.settingsDictionary["categories"] = dict + NSUserDefaults.standardUserDefaults().setObject(EbayWebServiceManager.settingsDictionary, forKey: "ebaySettings") } var dict: Dictionary> = (EbayWebServiceManager.settingsDictionary["categories"]!) as! Dictionary> @@ -509,16 +542,17 @@ extension EbayWebServiceManager: NSXMLParserDelegate { } else { - if "\(self.catDetailLevel)" == self.catLevel { + /*if "\(self.catDetailLevel)" == self.catLevel { if((self.subCategories[string] == nil )){ self.subCategories[string] = ["cat_id": self.catID, "isLeaf": false, "level": self.catLevel] } + + } - - + self.currentCategory = string*/ } - self.currentCategory = string + break case "LeafCategory": @@ -547,13 +581,38 @@ extension EbayWebServiceManager: NSXMLParserDelegate { Used during the listing creation process to save the new listing id, as well as adding pictures to the new listing */ case "ItemID": - self.completion(string, nil) + if (!self.isGettingNotification) { + self.completion(string, nil) + } break case "ReviseItemResponse": self.isRevisingListing = true break + /* + Used for retrieving listing info for notifications + */ + case "Quantity": + if self.isGettingNotification { + print("Quantity: \(string)") + self.currItemQuantity = Int(string) + } + break + case "QuantitySold": + if self.isGettingNotification { + print("QuantitySold: \(string)") + self.qtySold = Int(string) + } + break + case "ListingStatus": + if self.isGettingNotification { + print("ListingStatus: \(string)") + self.completion(["status":string, "quantitySold": self.qtySold, "itemQuantity": self.currItemQuantity], nil) + self.qtySold = nil + self.currItemQuantity = nil + } + break default: //print(string) print("\(self.currentElementName):\(string)") diff --git a/Vendoo/EtsyRESTAPIManager.swift b/Vendoo/EtsyRESTAPIManager.swift index d9e1204..e36346e 100644 --- a/Vendoo/EtsyRESTAPIManager.swift +++ b/Vendoo/EtsyRESTAPIManager.swift @@ -608,6 +608,28 @@ extension EtsyRESTAPIManager { ) } + //gets listing info + func getListingInfo (request: String, onCompletion: ServiceResponse?) { + self.oauthswift.client.get(self.baseURL + request, + success: { + (data, response) -> Void in + let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) + let dict = self.convertStringToDictionary(dataString! as String) + print(dict) + print(response) + var dict2 = (dict!["results"] as! [[String : AnyObject]]) + + let listingState = dict2[0]["state"] as! String + + onCompletion!(["status":listingState], nil) + + + }, failure: { + (error) -> Void in + print(error) + } + )} + //sends the pregenerated url as a request to api service func sendPOSTRequest(request: String, var body: [String: AnyObject]?, onCompletion: ServiceResponse?) { diff --git a/Vendoo/HomeViewController.swift b/Vendoo/HomeViewController.swift index f82bba5..4d3c1a3 100644 --- a/Vendoo/HomeViewController.swift +++ b/Vendoo/HomeViewController.swift @@ -16,11 +16,16 @@ class HomeViewController: UITabBarController { let firebaseManager = FirebaseManager() let ebayGraphManager = EbayWebServiceManager() var userListings: [Listing] = [] + var endedListings: [Listing] = [] + var soldListings: [Listing] = [] + var notificationsManager = ServiceNotificationManager() + var notifications: [Dictionary] = (NSUserDefaults.standardUserDefaults().objectForKey("notifications") == nil ? [] : (NSUserDefaults.standardUserDefaults().objectForKey("notifications") as! [Dictionary])) override func viewDidLoad() { super.viewDidLoad() - + ServiceNotificationManager.delegate = self + self.notificationsManager.setManagers(fbGraphManager, fireManager: firebaseManager, ebayManager: ebayGraphManager, etsyManager: etsyManager) // Do any additional setup after loading the view. dispatch_async(dispatch_get_main_queue(), { @@ -35,11 +40,12 @@ class HomeViewController: UITabBarController { NSUserDefaults.standardUserDefaults().setObject(userDict!["name"] as? String, forKey: "name") if listingDict != nil { + var notificationStartTracker = 0 dispatch_async(dispatch_get_main_queue(), { - - + //let notificationStartGroup = dispatch_group_create() for (key, values) in listingDict! { let serviceGroup: dispatch_group_t = dispatch_group_create() + //dispatch_group_enter(notificationStartGroup) self.firebaseManager.ref.observeSingleEventOfType(.Value, withBlock: { snapshot in let databaseDict = snapshot.value as! [String : AnyObject] @@ -54,6 +60,7 @@ class HomeViewController: UITabBarController { if(error != nil){ print("problem retrieving items") + notificationStartTracker += 1 }else{ let mainImage = UIImage(data: dataMain!) @@ -74,6 +81,9 @@ class HomeViewController: UITabBarController { count+=1 if(count == 4){ dispatch_group_leave(serviceGroup) + notificationStartTracker += 1 + //dispatch_group_enter(notificationStartGroup) + //dispatch_group_leave(notificationStartGroup) } }) @@ -83,20 +93,58 @@ class HomeViewController: UITabBarController { dispatch_group_notify(serviceGroup, dispatch_get_main_queue(), { print(listingImages.count) let listingInfo = values as? [String : AnyObject] - self.userListings.append( - Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!, - itemCategory: listingInfo!["listingCategory"] as? String, - itemQuantity: (listingInfo!["listingQuantity"] as? String)!, - itemPrice: listingInfo!["listingPrice"] as? String, - itemDescription: listingInfo!["listingDescription"] as? String, - itemImages: listingImages, - isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!, - itemKey: key, - networksSellingOn: (listingInfo!["networks"] as? Dictionary)! - )) - NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil) + if listingInfo!["listingStatus"] == nil { + self.userListings.append( + Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!, + itemCategory: listingInfo!["listingCategory"] as? String, + itemQuantity: (listingInfo!["listingQuantity"] as? String)!, + itemPrice: listingInfo!["listingPrice"] as? String, + itemDescription: listingInfo!["listingDescription"] as? String, + itemImages: listingImages, + isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!, + itemKey: key, + networksSellingOn: (listingInfo!["networks"] as? Dictionary)! + )) + } + else if listingInfo!["listingStatus"] as? String == "Ended" { + self.endedListings.append( + Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!, + itemCategory: listingInfo!["listingCategory"] as? String, + itemQuantity: (listingInfo!["listingQuantity"] as? String)!, + itemPrice: listingInfo!["listingPrice"] as? String, + itemDescription: listingInfo!["listingDescription"] as? String, + itemImages: listingImages, + isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!, + itemKey: key, + networksSellingOn: (listingInfo!["networks"] as? Dictionary)! + )) + } + else if listingInfo!["listingStatus"] as? String == "Sold" { + self.soldListings.append( + Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!, + itemCategory: listingInfo!["listingCategory"] as? String, + itemQuantity: (listingInfo!["listingQuantity"] as? String)!, + itemPrice: listingInfo!["listingPrice"] as? String, + itemDescription: listingInfo!["listingDescription"] as? String, + itemImages: listingImages, + isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!, + itemKey: key, + networksSellingOn: (listingInfo!["networks"] as? Dictionary)! + )) + } + + print(notificationStartTracker) + if(notificationStartTracker == listingDict?.count){ + NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil) + self.notificationsManager.setListings(self.userListings) + self.notificationsManager.startServicePolling() + } + + + }) + @@ -108,6 +156,11 @@ class HomeViewController: UITabBarController { } + /* + dispatch_group_notify(notificationStartGroup, dispatch_get_main_queue(), { + + + })*/ @@ -139,3 +192,146 @@ class HomeViewController: UITabBarController { } + + +extension HomeViewController: ServiceNotificationManagerDelegate { + func listingHasFinishedRetrievingNotifications() { + NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications") + } + + func listingHasStartedRetrievingNotifications() { + + } + + func listingHasEnded(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) { + if (fbInfo != nil) { + self.notifications.append( + [ + "network": "facebook", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": "ended", + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + if (ebayInfo != nil) { + self.notifications.append( + [ + "network": "ebay", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": (ebayInfo as! [String : AnyObject])["type"] as! String, + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + self.endedListings.appendContentsOf( self.userListings.filter({$0.key == ((ebayInfo as! [String : AnyObject])["listingKey"] as! String)})) + self.userListings = self.userListings.filter({$0.key != ((ebayInfo as! [String : AnyObject])["listingKey"] as! String)}) + let itemsView = self.viewControllers![0] as! ItemTableViewController + + self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((ebayInfo as! [String : AnyObject])["listingKey"] as! String))").observeSingleEventOfType( .Value, withBlock: { + (snapshot) -> Void in + + var listingDict = snapshot.value as? [String : AnyObject] + listingDict!["listingStatus"] = "Ended" + self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((ebayInfo as! [String : AnyObject])["listingKey"] as! String))").setValue(listingDict) + + itemsView.itemTable.reloadData() + }) + } + + if (amazonInfo != nil) { + self.notifications.append( + [ + "network": "amazon", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": (ebayInfo as! [String : AnyObject])["type"] as! String, + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + if (etsyInfo != nil) { + self.notifications.append( + [ + "network": "etsy", + "message": (etsyInfo as! [String : AnyObject])["notification"] as! String, + "type": "ended", + "key": (etsyInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + self.endedListings.appendContentsOf( self.userListings.filter({$0.key == ((etsyInfo as! [String : AnyObject])["listingKey"] as! String)})) + self.userListings = self.userListings.filter({$0.key != ((etsyInfo as! [String : AnyObject])["listingKey"] as! String)}) + let itemsView = self.viewControllers![0] as! ItemTableViewController + + self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((etsyInfo as! [String : AnyObject])["listingKey"] as! String))").observeSingleEventOfType( .Value, withBlock: { + (snapshot) -> Void in + + var listingDict = snapshot.value as? [String : AnyObject] + listingDict!["listingStatus"] = "Ended" + self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((etsyInfo as! [String : AnyObject])["listingKey"] as! String))").setValue(listingDict) + + itemsView.itemTable.reloadData() + }) + } + + } + + func listingHasNewNotifications(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) { + + + if (fbInfo != nil) { + self.notifications.append( + [ + "network": "facebook", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": (ebayInfo as! [String : AnyObject])["type"] as! String, + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + if (ebayInfo != nil) { + self.notifications.append( + [ + "network": "ebay", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": (ebayInfo as! [String : AnyObject])["type"] as! String, + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + if (amazonInfo != nil) { + self.notifications.append( + [ + "network": "amazon", + "message": (ebayInfo as! [String : AnyObject])["notification"] as! String, + "type": "", + "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + if (etsyInfo != nil) { + self.notifications.append( + [ + "network": "etsy", + "message": (etsyInfo as! [String : AnyObject])["notification"] as! String, + "type": (etsyInfo as! [String : AnyObject])["type"] as! String, + "key": (etsyInfo as! [String : AnyObject])["listingKey"] as! String, + "UUID": NSUUID().UUIDString + ] + ) + } + + //@TODO: add firbase logic to add notifications and delete already viewed notifications + + } +} diff --git a/Vendoo/ItemImagePickerViewController.swift b/Vendoo/ItemImagePickerViewController.swift index c7977fc..bee776f 100644 --- a/Vendoo/ItemImagePickerViewController.swift +++ b/Vendoo/ItemImagePickerViewController.swift @@ -45,6 +45,8 @@ class ItemImagePickerViewController: UIViewController { private var imageIndex: Int! private var isEditing: Bool = false private var currentInformation: Dictionary! + + override func viewDidLoad() { super.viewDidLoad() diff --git a/Vendoo/ItemTableViewController.swift b/Vendoo/ItemTableViewController.swift index 2da8be4..ff66e52 100644 --- a/Vendoo/ItemTableViewController.swift +++ b/Vendoo/ItemTableViewController.swift @@ -80,6 +80,12 @@ class ItemTableViewController: UIViewController { } +// MARK: - IBAction +extension ItemTableViewController { + @IBAction func segSwitch(sender: UISegmentedControl) { + self.itemTable.reloadData() + } +} // MARK: - Navigation extension ItemTableViewController { @@ -129,63 +135,143 @@ extension ItemTableViewController: UITableViewDataSource{ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows - return ((self.tabBarController as? HomeViewController)?.userListings.count)! + if self.tableSegmentController.selectedSegmentIndex == 0 { + return ((self.tabBarController as? HomeViewController)?.userListings.count)! + } + else if tableSegmentController.selectedSegmentIndex == 1 { + return ((self.tabBarController as? HomeViewController)?.endedListings.count)! + } + else{ + return ((self.tabBarController as? HomeViewController)?.soldListings.count)! + } + + } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: ItemCell! = (tableView.dequeueReusableCellWithIdentifier("Item Cell", forIndexPath: indexPath) as! ItemCell) - - - // Configure the cell... //for each listing cell generated need to find the marketplaces it belongs to, the listing price, the name of the item, the status of the item, and the item image. //Once these objects are retrieved, access the ItemCell properties and manipulate as needed - cell.itemImage.image = (self.tabBarController as? HomeViewController)?.userListings[indexPath.row].images[0] //come back - cell.itemName.text = (self.tabBarController as? HomeViewController)!.userListings[indexPath.row].title - cell.itemPrice.text = (self.tabBarController as? HomeViewController)!.userListings[indexPath.row].price - //check if item is published - if((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].isDraft!){ - cell.itemStatus.text = "Status: Unpublished" - dispatch_async(dispatch_get_main_queue(), { - - cell.alpha = 0.5 - - - }) + if self.tableSegmentController.selectedSegmentIndex == 0 { + cell.itemImage.image = (self.tabBarController as? HomeViewController)?.userListings[indexPath.row].images[0] //come back + cell.itemName.text = (self.tabBarController as? HomeViewController)!.userListings[indexPath.row].title + cell.itemPrice.text = (self.tabBarController as? HomeViewController)!.userListings[indexPath.row].price - }else { - cell.itemStatus.text = "Status: Active" - } - - if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["areNetworksChosen"])!){ - cell.networks.hidden = true - }else{ - dispatch_async(dispatch_get_main_queue(), { - if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["ebay"])!){ - cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true - } - if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["amazon"])!){ - cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true - } - if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["etsy"])!){ - cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true + //check if item is published + if((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].isDraft!){ + cell.itemStatus.text = "Status: Unpublished" + dispatch_async(dispatch_get_main_queue(), { - } - if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["facebook"])!){ - cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true - } - - }) + cell.alpha = 0.5 + + + }) + + }else { + cell.itemStatus.text = "Status: Active" + } + if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["areNetworksChosen"])!){ + cell.networks.hidden = true + }else{ + dispatch_async(dispatch_get_main_queue(), { + if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["ebay"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["amazon"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["etsy"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true + + } + if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["facebook"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true + } + + }) + + } + } + else if tableSegmentController.selectedSegmentIndex == 1 { + cell.itemImage.image = (self.tabBarController as? HomeViewController)?.endedListings[indexPath.row].images[0] //come back + cell.itemName.text = (self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].title + cell.itemPrice.text = (self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].price + + cell.itemStatus.text = "Status: Unsold" + + if(!((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["areNetworksChosen"])!){ + cell.networks.hidden = true + }else{ + dispatch_async(dispatch_get_main_queue(), { + if(!((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["ebay"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["amazon"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["etsy"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true + + } + if(!((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["facebook"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true + } + + }) + + } + + } + else{ + cell.itemImage.image = (self.tabBarController as? HomeViewController)?.soldListings[indexPath.row].images[0] //come back + cell.itemName.text = (self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].title + cell.itemPrice.text = (self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].price + + cell.itemStatus.text = "Status: Sold" + + if(!((self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].networks["areNetworksChosen"])!){ + cell.networks.hidden = true + }else{ + dispatch_async(dispatch_get_main_queue(), { + if(!((self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].networks["ebay"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].networks["amazon"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true + } + if(!((self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].networks["etsy"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true + + } + if(!((self.tabBarController as? HomeViewController)!.soldListings[indexPath.row].networks["facebook"])!){ + cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true + } + + }) + + } + } + + return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - self.selectedListing = (self.tabBarController as? HomeViewController)?.userListings[indexPath.row] + if self.tableSegmentController.selectedSegmentIndex == 0 { + self.selectedListing = (self.tabBarController as? HomeViewController)?.userListings[indexPath.row] + } + else if tableSegmentController.selectedSegmentIndex == 1 { + self.selectedListing = (self.tabBarController as? HomeViewController)?.endedListings[indexPath.row] + } + else{ + self.selectedListing = (self.tabBarController as? HomeViewController)?.soldListings[indexPath.row] + } self.performSegueWithIdentifier("ItemDetailSegue", sender: self) } } diff --git a/Vendoo/Listing.swift b/Vendoo/Listing.swift index a0373b0..76ec5cc 100644 --- a/Vendoo/Listing.swift +++ b/Vendoo/Listing.swift @@ -20,6 +20,7 @@ class Listing : NSObject { var images: [UIImage]! var key: String! var networks: Dictionary! + var listingStatus: String! init(itemTitle:String, itemCategory: String!, itemQuantity: String!, itemPrice: String!, itemDescription: String!, itemImages: [UIImage], isDraftListing: Bool, itemKey: String, networksSellingOn: Dictionary){ diff --git a/Vendoo/Main.storyboard b/Vendoo/Main.storyboard index cb927f7..a2f6b1f 100755 --- a/Vendoo/Main.storyboard +++ b/Vendoo/Main.storyboard @@ -1242,14 +1242,18 @@ - + + + + + @@ -2501,11 +2505,11 @@ + + - - - + diff --git a/Vendoo/NotificationTableViewCell.swift b/Vendoo/NotificationTableViewCell.swift new file mode 100644 index 0000000..05e6389 --- /dev/null +++ b/Vendoo/NotificationTableViewCell.swift @@ -0,0 +1,24 @@ +// +// NotificationTableViewCell.swift +// Vendoo +// +// Created by Okechi Onyeje on 11/2/16. +// Copyright © 2016 Okechi Onyeje. All rights reserved. +// + +import UIKit + +class NotificationTableViewCell: UITableViewCell { + + override func awakeFromNib() { + super.awakeFromNib() + // Initialization code + } + + override func setSelected(selected: Bool, animated: Bool) { + super.setSelected(selected, animated: animated) + + // Configure the view for the selected state + } + +} diff --git a/Vendoo/NotificationsViewController.swift b/Vendoo/NotificationsViewController.swift new file mode 100644 index 0000000..3dbecdb --- /dev/null +++ b/Vendoo/NotificationsViewController.swift @@ -0,0 +1,35 @@ +// +// NotificationsViewController.swift +// Vendoo +// +// Created by Okechi Onyeje on 11/2/16. +// Copyright © 2016 Okechi Onyeje. All rights reserved. +// + +import UIKit + +class NotificationsViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/Vendoo/ServiceNotificationManager.swift b/Vendoo/ServiceNotificationManager.swift new file mode 100644 index 0000000..a59fee3 --- /dev/null +++ b/Vendoo/ServiceNotificationManager.swift @@ -0,0 +1,181 @@ +// +// ServiceNotificationManager.swift +// Vendoo +// +// Created by Okechi Onyeje on 8/30/16. +// Copyright © 2016 Okechi Onyeje. All rights reserved. +// + +import Foundation + +protocol ServiceNotificationManagerDelegate { + func listingHasEnded(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) + func listingHasNewNotifications(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) + func listingHasFinishedRetrievingNotifications() + func listingHasStartedRetrievingNotifications() + +} + +class ServiceNotificationManager: NSObject { + + var etsyManager: EtsyRESTAPIManager! + var fbGraphManager: FacebookGraphAPIManager! + var firebaseManager: FirebaseManager! + var ebayGraphManager: EbayWebServiceManager! + var userListings: [Listing] = [] + var timer: NSTimer! + static var delegate: ServiceNotificationManagerDelegate? + + override init() { + super.init() + + + } + + func startServicePolling() { + //timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(ServiceNotificationManager.pollServices), userInfo: nil, repeats: true) + pollServices() + } + + func stopServicePolling() { + timer.invalidate() + } + + func setManagers(fbManager: FacebookGraphAPIManager, fireManager: FirebaseManager, ebayManager: EbayWebServiceManager, etsyManager: EtsyRESTAPIManager){ + self.fbGraphManager = fbManager + self.firebaseManager = fireManager + self.ebayGraphManager = ebayManager + self.etsyManager = etsyManager + } + + func setListings(listings: [Listing]) { + self.userListings = listings + } + + func pollServices(){ + let notificationGroup = dispatch_group_create() + for listing in userListings { + + self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(listing.key)").observeSingleEventOfType( .Value, withBlock: { + (snapshot) -> Void in + //dispatch_group_enter(notificationGroup) + + let listingDict = snapshot.value as? [String : AnyObject] + + if !(listingDict!["isListingDraft"] as! Bool) { + if let networksDict = listingDict!["networkIDs"] as? [String : AnyObject] { + let ebayID = (networksDict["ebay"] as? String) + let facebookID = (networksDict["facebook"] as? String) + let etsyID = (networksDict["etsy"] as? Int) + let amazonID = (networksDict["amazon"] as? String) + + //@TODO: Skeleton for Notfications Manager is set up. Need to implement listing request for each listing to retrieve relevant notifications for user such as if listing has ended + if(ebayID != nil){ + if self.ebayGraphManager.isAuthorized { + self.ebayGraphManager.isGettingNotification = true + self.ebayGraphManager.getListingInfo(ebayID!, onComplete: { + (listingNotificationInfo, error) -> Void in + //dispatch_group_leave(notificationGroup) + let info = (listingNotificationInfo as! [String : AnyObject]) + if(info["status"] as! String == "Ended") { + if((info["quantitySold"] as! Int) > 0) { + //@TODO: Add both firbase logic and code logic to do notifications for sold listings + 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": "Ended", + "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has ended on ebay.", + "listingKey": listing.key], + amazonInfo: nil, + etsyInfo: nil + ) + } + + + } + + }) + } + } + if(facebookID != nil){ + + } + if(etsyID != nil){ + if (self.etsyManager.isAuthorized) { + self.etsyManager.getListingInfo ("/listings/\(etsyID!)", onCompletion: { + (listingNotificationInfo, error) -> Void in + + let info = (listingNotificationInfo as! [String : AnyObject]) + + //@TODO: Test this code with the draft condition for both expired and sold_out + if ((info["status"] as! String == "expired") || (info["status"] as! String == "removed")) { + + ServiceNotificationManager.delegate?.listingHasEnded( + nil, + ebayInfo: nil, + amazonInfo: nil, + etsyInfo: [ + "type": "Ended", + "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has ended on etsy.", + "listingKey": listing.key] + ) + + + }else if info["status"] as! String == "sold_out" { + + ServiceNotificationManager.delegate?.listingHasNewNotifications( + nil, + ebayInfo: nil, + amazonInfo: nil, + etsyInfo: [ + "type": "Sold", + "notification": "Your listing \"\(listingDict!["listingTitle"] as! String)\" has sold out on etsy.", + "listingKey": listing.key] + ) + + } else if info["status"] as! String == "draft" { + + } + + }) + + } + } + if(amazonID != nil){ + + } + //dispatch_group_leave(notificationGroup) + }else { + // dispatch_group_leave(notificationGroup) + } + + + }else { + //dispatch_group_leave(notificationGroup) + } + }) + } + self.ebayGraphManager.isGettingNotification = false + + + //dispatch_group_notify(notificationGroup, dispatch_get_main_queue(), { + //self.delegate?.listingHasNewNotifications(nil, ebayInfo: nil, amazonInfo: nil, etsyInfo: nil) + //}) + + } + +} +