vendoo_v1.0/Vendoo/ListingPreviewViewController.swift

303 lines
12 KiB
Swift

//
// ListingPreviewViewController.swift
// Vendoo
//
// Created by Okechi Onyeje on 5/28/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
/*
NOTES:
need to display navigation bar so user can navigate back to previous screens if changes need to be made
*/
import UIKit
class ListingPreviewViewController: UIViewController {
//IBOutlets
@IBOutlet weak var containerScrollView: UIScrollView!
@IBOutlet weak var itemPicture: UIImageView!
@IBOutlet weak var itemTitle: UITextView!
@IBOutlet weak var itemDescription: UITextView!
@IBOutlet weak var itemPrice: UITextView!
@IBOutlet weak var itemCategory: UITextView!
@IBOutlet weak var networks: UICollectionView!
//class variables
private var networksDictionary: Dictionary<String, Bool> = Dictionary<String, Bool>()
private var graphManager: FacebookGraphAPIManager! = nil
private var firManager: FirebaseManager! = nil
private var itemListingDictionary: Dictionary<String, AnyObject>! = Dictionary<String, AnyObject>()
private var alert = UIAlertController(title: "Listing Published", message: "Your listing has been published", preferredStyle: .Alert)
private var lastListingKey: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Initialization code
self.networks.delegate = self
self.networks.dataSource = self
self.networks.backgroundView?.backgroundColor = UIColor.whiteColor()
self.graphManager.delegate = self
}
override func viewDidAppear(animated: Bool) {
self.setListing()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setListing(){
self.itemPicture.image = (self.itemListingDictionary["picture"] as? UIImageView)!.image
self.itemTitle.text = self.itemListingDictionary["title"] as! String
self.itemDescription.text = self.itemListingDictionary["description"] as! String
self.itemPrice.text = self.itemListingDictionary["price"] as! String
self.itemCategory.text = self.itemListingDictionary["category"] as! String
}
func setDictionary(netdictionary:Dictionary<String, Bool>, itemdictionary: Dictionary<String, AnyObject!>){
self.networksDictionary = netdictionary
self.itemListingDictionary = itemdictionary
}
func setManagers(fbManager: FacebookGraphAPIManager, fireManager: FirebaseManager){
self.graphManager = fbManager
self.firManager = fireManager
}
/*
// 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.
}
*/
}
//MARK: - FacebookGraphAPIManagerDelegate methods
extension ListingPreviewViewController: FacebookGraphAPIManagerDelegate {
func listingUploadedFB(fbID: String) {
self.firManager.ref.child("Users").child("\(self.firManager.user_email)").child("user_Listings").child(self.lastListingKey).updateChildValues(["network_listing_IDs":["fbID":fbID]])
self.alert.title = "Listing Published"
self.alert.message = "Your listing has been published to facebook"
self.presentViewController(self.alert, animated: true, completion: nil)
}
}
//MARK: - IBActions
extension ListingPreviewViewController {
@IBAction func publishItem(sender: AnyObject) {
if(self.itemPicture.image == nil){
let alert = UIAlertController(title: "Image Needed", message: "To proceed to choose networks, you must supply at least one picture for your listing", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)}))
self.presentViewController(alert, animated: true, completion: nil)
}else{
//save listing to private user path in firebase
let newListingRef = self.firManager.ref.child("Users").child("\(self.firManager.user_email)").child("user_Listings").childByAutoId()
self.networksDictionary["areNetworksChosen"] = true
let listing: Dictionary<String,AnyObject> = ["listingID": newListingRef.key,
"seller email": (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!,
"listingTitle": self.itemTitle.text,
"listingPrice": self.itemPrice.text,
"listingCategory": self.itemCategory.text,
"listingDescription": self.itemDescription.text,
"isListingDraft": false,
"networks": self.networksDictionary]
self.firManager.ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
let databaseDict = snapshot.value as! [String : AnyObject]
let storageRef = self.firManager.storage.referenceForURL((databaseDict["image_storage"] as? String)!)
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+".jpg")
listingImageRef.putData(UIImageJPEGRepresentation(self.itemPicture.image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
newListingRef.setValue(listing)
self.alert.title = "Listing saved with error"
self.alert.message = "Your listing has been saved but something went wrong when trying to publish to facebook"
//let alert = UIAlertController(title: "Listing Published", message: "Your listing has been published", preferredStyle: .Alert)
self.alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in self.alert.dismissViewControllerAnimated(true, completion: nil)}))
let parameters: Dictionary<String, AnyObject> = ["message":("**"+self.itemTitle.text!+"**\n\n"+"Price: "+self.itemPrice.text! + "\n\n"+self.itemDescription.text!)]
//post to ebay
if(self.networksDictionary["ebay"]!){
}
//post to amazon
if(self.networksDictionary["amazon"]!){
}
//post to etsy
if(self.networksDictionary["etsy"]!){
}
//post to facebook
if(self.networksDictionary["facebook"]!){
//need to reverse process and upload to firebase last so that all network listing id's can be saved to firebase for notification access.
self.lastListingKey = newListingRef.key
self.graphManager.makePOSTResquest("me/feed", params: parameters)
}
//register new listing id in global path of firebase root'
self.firManager.ref.child("Global_listings").child(newListingRef.key).setValue(newListingRef.key)
})
})
}
}
}
extension ListingPreviewViewController: UICollectionViewDelegate {
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
}
*/
}
//Mark: - UICollectionViewDataSource methods
//need to dynamically show, hide, and rearrange the network cells based on users choice of networks
extension ListingPreviewViewController: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 4
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell!
switch (indexPath.row){
case 0:
cell = collectionView.dequeueReusableCellWithReuseIdentifier("ebay", forIndexPath: indexPath)
if(self.networksDictionary["ebay"] == false){
cell.hidden = true
}
else{
cell.hidden = false
}
break
case 1:
cell = collectionView.dequeueReusableCellWithReuseIdentifier("amazon", forIndexPath: indexPath)
if(self.networksDictionary["amazon"] == false){
cell.hidden = true
}
else{
cell.hidden = false
}
break
case 2:
cell = collectionView.dequeueReusableCellWithReuseIdentifier("etsy", forIndexPath: indexPath)
if(self.networksDictionary["etsy"] == false){
cell.hidden = true
}
else{
cell.hidden = false
}
break
default:
cell = collectionView.dequeueReusableCellWithReuseIdentifier("facebook", forIndexPath: indexPath)
if(self.networksDictionary["facebook"] == false){
cell.hidden = true
}
else{
cell.hidden = false
}
break
}
return cell
}
}