vendoo_v1.0/Vendoo/ListingPreviewViewController.swift
2016-06-03 07:14:54 -04:00

243 lines
8.7 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>()
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()
}
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: - 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()
let listing = ["listingID": newListingRef.key,
"seller email": (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!,
"listingTitle": self.itemTitle,
"listingPrice": self.itemPrice,
"listingCategory": self.itemCategory,
"listingDescription": self.itemDescription,
"isListingDraft": false,
"listingPicture": self.itemPicture]
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(UIImagePNGRepresentation(self.itemPicture.image!)!, metadata: nil,completion:
{(metadata, error) -> Void in
newListingRef.setValue(listing)
})
//register new listing id in global path of firebase root'
self.firManager.ref.child("Global_listings").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
}
}