// // ItemTableViewController.swift // Vendoo // // Created by Okechi Onyeje on 5/26/16. // Copyright © 2016 Okechi Onyeje. All rights reserved. // /* This class will be used to retrieve and manipulate data seen by the end user for he/she's item listings */ import UIKit import TYMActivityIndicatorView import SWRevealViewController class ItemTableViewController: UIViewController { //outlet @IBOutlet weak var itemTable: UITableView! @IBOutlet weak var tableSegmentController: UISegmentedControl! @IBOutlet weak var menuButton: UIBarButtonItem! @IBOutlet weak var historyCurrentlyFilter: UISegmentedControl! @IBOutlet weak var cancelBtn: UIBarButtonItem! var loadingView: UIView! var selectedListing: Listing! override func viewDidLoad() { super.viewDidLoad() //Check if this segment was loaded through the analytics button if(NSUserDefaults.standardUserDefaults().boolForKey("isAnalytics")){ dispatch_async(dispatch_get_main_queue(), { self.tabBarController?.tabBar.hidden = true self.tableSegmentController.selectedSegmentIndex = 1 self.tableSegmentController.userInteractionEnabled = false self.tableSegmentController.hidden = true self.tableSegmentController.enabled = false self.tabBarController?.tabBar.userInteractionEnabled = false self.cancelBtn.enabled = true self.cancelBtn.tintColor = UIColor(red: 0.35, green: 0.83, blue: 0.74, alpha: 1.0) self.menuButton.enabled = false self.menuButton.tintColor = UIColor.clearColor() self.historyCurrentlyFilter.setTitle("Sold", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Unsold", forSegmentAtIndex: 1) }) }else{ dispatch_async(dispatch_get_main_queue(), { self.tabBarController?.tabBar.hidden = false self.tableSegmentController.selectedSegmentIndex = 0 self.tableSegmentController.userInteractionEnabled = true self.tableSegmentController.hidden = false self.tableSegmentController.enabled = true self.historyCurrentlyFilter.setTitle("Active", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Draft", forSegmentAtIndex: 1) self.cancelBtn.enabled = false self.cancelBtn.tintColor = UIColor.clearColor() self.menuButton.enabled = true self.menuButton.tintColor = UIColor(red: 0.35, green: 0.83, blue: 0.74, alpha: 1.0) self.tabBarController?.tabBar.userInteractionEnabled = true }) } // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() //self.tabBarController?.tabBar.hidden = false //set delegates self.itemTable.dataSource = self self.itemTable.delegate = self //History Segement if(self.tableSegmentController.selectedSegmentIndex == 1){ self.historyCurrentlyFilter.selectedSegmentIndex = UISegmentedControlNoSegment self.historyCurrentlyFilter.setTitle("Sold", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Unsold", forSegmentAtIndex: 1) //Active segment }else{ self.historyCurrentlyFilter.selectedSegmentIndex = UISegmentedControlNoSegment self.historyCurrentlyFilter.setTitle("Active", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Draft", forSegmentAtIndex: 1) } NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(stopIndicator), name: "finished_fetching_listings", object: nil) if self.revealViewController() != nil { menuButton.target = self.revealViewController() (menuButton.target as! SWRevealViewController).delegate = self menuButton.action = "revealToggle:" self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) } self.loadingView = UIView(frame: self.view.frame) self.loadingView.backgroundColor = UIColor.grayColor() self.loadingView.alpha = 0.4 ((self.tabBarController as? HomeViewController)?.firebaseManager.indicator)!.center = self.view.center self.loadingView.addSubview(((self.tabBarController as? HomeViewController)?.firebaseManager.indicator)!) self.view.addSubview(loadingView) (self.tabBarController as? HomeViewController)?.firebaseManager.indicator.startAnimating() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) (menuButton.target as! SWRevealViewController).delegate = self self.tabBarController?.tabBar.hidden = false } override func viewWillDisappear(animated: Bool) { NSUserDefaults.standardUserDefaults().removeObjectForKey("isAnalytics") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func stopIndicator(){ (self.tabBarController as? HomeViewController)?.firebaseManager.indicator.stopAnimating() self.loadingView.removeFromSuperview() self.itemTable.reloadData() } /* */ } // MARK: - IBAction extension ItemTableViewController { @IBAction func activeHistorSegSwitch(sender: UISegmentedControl) { self.historyCurrentlyFilter.selectedSegmentIndex = UISegmentedControlNoSegment self.itemTable.reloadData() //History Segement if(self.tableSegmentController.selectedSegmentIndex == 1){ self.historyCurrentlyFilter.setTitle("Sold", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Unsold", forSegmentAtIndex: 1) //Active segment }else{ self.historyCurrentlyFilter.setTitle("Active", forSegmentAtIndex: 0) self.historyCurrentlyFilter.setTitle("Draft", forSegmentAtIndex: 1) } } @IBAction func historyCurrentlyFilterSegSwitch(sender: UISegmentedControl) { self.itemTable.reloadData() } @IBAction func cancelPressed(sender:AnyObject){ let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Reveal View Controller") NSUserDefaults.standardUserDefaults().removeObjectForKey("isAnalytics") self.navigationItem.rightBarButtonItem = nil self.presentViewController(vc!, animated: false, completion: nil) } } // MARK: - Side Menu View extension ItemTableViewController: SWRevealViewControllerDelegate { func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) { if(position == FrontViewPosition.Left) { for view in self.view.subviews { view.userInteractionEnabled = true } self.tabBarController?.tabBar.userInteractionEnabled = true } else { for view in self.view.subviews { if !view.isKindOfClass(UINavigationBar){ view.userInteractionEnabled = false } } self.tabBarController?.tabBar.userInteractionEnabled = false } } func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) { if(position == FrontViewPosition.Left) { for view in self.view.subviews { view.userInteractionEnabled = true } self.tabBarController?.tabBar.userInteractionEnabled = true } else { for view in self.view.subviews { if !view.isKindOfClass(UINavigationBar){ view.userInteractionEnabled = false } } self.tabBarController?.tabBar.userInteractionEnabled = false } } } // MARK: - Navigation extension ItemTableViewController { // 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. let des = segue.destinationViewController as! ListingPreviewViewController var tempImages: [UIImageView] = [] for uiImage in self.selectedListing.images { tempImages.append(UIImageView(image: uiImage)) } let dict:[String:AnyObject] = [ "pictures": tempImages, "title": self.selectedListing.title != nil ? self.selectedListing.title : "", "description": self.selectedListing.desc != nil ? self.selectedListing.desc: "", "price": self.selectedListing.price != nil ? self.selectedListing.price : "", "category": self.selectedListing.category != nil ? self.selectedListing.category : "", "quantity": self.selectedListing.quantity != nil ? self.selectedListing.quantity: nil ] des.setDictionary(self.selectedListing.networks, itemdictionary: dict) des.setManagers( ((self.tabBarController as? HomeViewController)?.fbGraphManager)!, fireManager: ((self.tabBarController as? HomeViewController)?.firebaseManager)!, ebayManager: ((self.tabBarController as? HomeViewController)?.ebayGraphManager)!, etsyManager: ((self.tabBarController as? HomeViewController)?.etsyManager)! ) if self.selectedListing.isDraft! { des.setViewState("Editible") }else { des.setViewState("NonEditable") } } } // MARK: - TableView Datasource methods extension ItemTableViewController: UITableViewDataSource{ func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { //if in the history segment if(self.tableSegmentController.selectedSegmentIndex == 1){ //hides title of unsold section if Sold filter selected if(self.historyCurrentlyFilter.selectedSegmentIndex == 0){ if(section == 1){ return 0.0 }else{ return self.itemTable.sectionHeaderHeight } //hides title of sold section if Unsold filter selected }else if(self.historyCurrentlyFilter.selectedSegmentIndex == 1){ if(section == 0){ return 0.0 }else{ return self.itemTable.sectionHeaderHeight } //Shows both section titles }else{ return self.itemTable.sectionHeaderHeight } }else{ //hides title of draft section if active filter selected if(self.historyCurrentlyFilter.selectedSegmentIndex == 0){ if(section == 1){ return 0.0 }else{ return self.itemTable.sectionHeaderHeight } //hides title of active section if draft filter selected }else if(self.historyCurrentlyFilter.selectedSegmentIndex == 1){ if(section == 0){ return 0.0 }else{ return self.itemTable.sectionHeaderHeight } //Shows both section titles }else{ return self.itemTable.sectionHeaderHeight } } } func numberOfSectionsInTableView(tableView: UITableView) -> Int { //number of sections for active segment if self.tableSegmentController.selectedSegmentIndex == 0 { return 2 } else { return 2 } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //if in the active segment if self.tableSegmentController.selectedSegmentIndex == 0 { //hides draft section cells if active filter selected if(self.historyCurrentlyFilter.selectedSegmentIndex == 0){ if(section == 1){ return 0 }else{ return ((self.tabBarController as? HomeViewController)?.userListings.count)! } //hides active section cells if draft filter selected }else if(self.historyCurrentlyFilter.selectedSegmentIndex == 1){ if(section == 0){ return 0 }else{ return ((self.tabBarController as? HomeViewController)?.draftListings.count)! } }else{ //shows both respective sections cells if(section == 1){ return ((self.tabBarController as? HomeViewController)?.draftListings.count)! }else{ return ((self.tabBarController as? HomeViewController)?.userListings.count)! } } } //if in the history segment else { //hides unsold section cells if Sold filter selected if(self.historyCurrentlyFilter.selectedSegmentIndex == 0){ if(section == 1){ return 0 }else{ return ((self.tabBarController as? HomeViewController)?.soldListings.count)! } //hides sold section cells if Unsold filter selected }else if(self.historyCurrentlyFilter.selectedSegmentIndex == 1){ if(section == 0){ return 0 }else{ return ((self.tabBarController as? HomeViewController)?.endedListings.count)! } }else{ //shows both respective sections cells if(section == 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 //Active and unpublished listings if self.tableSegmentController.selectedSegmentIndex == 0 { //active lstings if indexPath.section == 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 cell.itemStatus.text = "Status: Active" if(!((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["areNetworksChosen"])!){ cell.networks.hidden = true }else{ cell.selectedNetworks = [] dispatch_async(dispatch_get_main_queue(), { if(((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["ebay"])!){ cell.addNetwork("ebay_icon") } if(((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["amazon"])!){ cell.addNetwork("amazon_icon") } if(((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["etsy"])!){ cell.addNetwork("etsy_icon") } if(((self.tabBarController as? HomeViewController)!.userListings[indexPath.row].networks["facebook"])!){ cell.addNetwork("facebook_icon") } cell.networks.reloadData() }) } }else{ cell.itemImage.image = (self.tabBarController as? HomeViewController)?.draftListings[indexPath.row].images[0] //come back cell.itemName.text = (self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].title cell.itemPrice.text = (self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].price cell.itemStatus.text = "Status: Unpublished" dispatch_async(dispatch_get_main_queue(), { cell.alpha = 0.5 }) if(!((self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].networks["areNetworksChosen"])!){ cell.networks.hidden = true }else{ cell.selectedNetworks = [] dispatch_async(dispatch_get_main_queue(), { if(((self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].networks["ebay"])!){ cell.addNetwork("ebay_icon") } if(((self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].networks["amazon"])!){ cell.addNetwork("amazon_icon") } if(((self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].networks["etsy"])!){ cell.addNetwork("etsy_icon") } if(((self.tabBarController as? HomeViewController)!.draftListings[indexPath.row].networks["facebook"])!){ cell.addNetwork("facebook_icon") } cell.networks.reloadData() }) } } } //Sold and Unsold Listings else { //unsold listings section if indexPath.section == 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{ cell.selectedNetworks = [] dispatch_async(dispatch_get_main_queue(), { if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["ebay"])!){ cell.addNetwork("ebay_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["amazon"])!){ cell.addNetwork("amazon_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["etsy"])!){ cell.addNetwork("etsy_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["facebook"])!){ cell.addNetwork("facebook_icon") } cell.networks.reloadData() }) } //section for sold listings }else{ cell.itemImage.image = (self.tabBarController as? HomeViewController)?.soldListings[indexPath.row].images[0] 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{ cell.selectedNetworks = [] dispatch_async(dispatch_get_main_queue(), { if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["ebay"])!){ cell.addNetwork("ebay_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["amazon"])!){ cell.addNetwork("amazon_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["etsy"])!){ cell.addNetwork("etsy_icon") } if(((self.tabBarController as? HomeViewController)!.endedListings[indexPath.row].networks["facebook"])!){ cell.addNetwork("facebook_icon") } cell.networks.reloadData() }) } } } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //view listing that is active or draft if self.tableSegmentController.selectedSegmentIndex == 0 { //draft if(indexPath.section == 1){ self.selectedListing = (self.tabBarController as? HomeViewController)?.draftListings[indexPath.row] //active }else{ self.selectedListing = (self.tabBarController as? HomeViewController)?.userListings[indexPath.row] } } //view listing that is sold or unsold else{ //unsold if(indexPath.section == 1){ self.selectedListing = (self.tabBarController as? HomeViewController)?.endedListings[indexPath.row] //sold }else{ self.selectedListing = (self.tabBarController as? HomeViewController)?.soldListings[indexPath.row] } } self.performSegueWithIdentifier("ItemDetailSegue", sender: self) } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if(self.tableSegmentController.selectedSegmentIndex == 1){ if(section == 1){ return (self.tabBarController as? HomeViewController)!.userListings.count > 0 ? "Unsold" : "" }else{ return (self.tabBarController as? HomeViewController)!.userListings.count > 0 ? "Sold" : "" } }else{ if (self.tabBarController as? HomeViewController)!.userListings.count > 0 { if(section == 1){ return "Draft" }else{ return "Active" } }else{ return "" } } } } //MARK: - Tableview Delegate Methods extension ItemTableViewController: UITableViewDelegate { /* // Override to support conditional editing of the table view. override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } */ /* // Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return false if you do not want the item to be re-orderable. return true } */ }