vendoo_v1.0/Vendoo/NetworksTableViewController.swift
Okechi Onyeje 26affd96e4 User interaction disabled when side menu open
almost fully implemented but when opening side menu at items table, closing menu, switch tabs to network views, open menu, close menu, and switch back to items table, userInteraction mysteriously reenabled
2016-11-29 00:47:09 -05:00

998 lines
42 KiB
Swift

//
// NetworksTableViewController.swift
// Vendoo
//
// Created by Okechi Onyeje on 5/26/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
/*
NOTES:
//Make it so u are accessing REST api service managers from TabController
//to promote more centralized code.
//need to make the class control the navigation bar by changing the buttons on it to show a back button in the case where class is controlling network selection, and hide back button for the case where class is controlling network authorization
*/
import UIKit
import Foundation
import SWRevealViewController
class NetworksTableViewController: UIViewController {
//IBOutlets
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var menuButton: UIBarButtonItem!
//class variables
private var networkToggleOrSelect: Bool = false
private var networksDictionary: Dictionary<String, Bool> = ["ebay":false, "amazon":false,"etsy":false,"facebook":false]
private var itemListingDictionary: Dictionary<String, AnyObject>! = Dictionary<String, AnyObject>()
private var graphManager: FacebookGraphAPIManager! = nil
private var firManager: FirebaseManager! = nil
private var etsyManager: EtsyRESTAPIManager! = nil
private var ebayManager: EbayWebServiceManager! = EbayWebServiceManager()
private var shouldShowDictionary: Dictionary<String, Bool> = ["ebay":false, "amazon":false,"etsy":false,"facebook":false]
private var networkOrderSelection: [String] = []
private var networkCount = 0
override func viewDidLoad() {
super.viewDidLoad()
// 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.tableView.delegate = self
self.tableView.dataSource = self
if(!networkToggleOrSelect){
((self.tabBarController as? HomeViewController)?.ebayGraphManager)!.delegate = self
}else{
self.ebayManager.delegate = self
}
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
(menuButton.target as! SWRevealViewController).delegate = self
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//set the class boolean in order to choose what toggling each network does
func setNetworkSelectFunctionality(bool: Bool){
self.networkToggleOrSelect = bool
}
func toggleMarketPlaceVisibitlity (market: String) {
self.shouldShowDictionary[market] = !self.shouldShowDictionary[market]!
}
private func checkVisibility () -> String? {
var string = ""
for (market, visibility) in self.shouldShowDictionary {
if visibility {
self.networkCount += 1
self.shouldShowDictionary[market] = !self.shouldShowDictionary[market]!
string = market
self.networkOrderSelection.append(market)
break
}
}
return string
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if(!networkToggleOrSelect) {
self.tabBarController?.tabBar.hidden = false
}
}
}
//MARK: - class methods Network Selection when creating a new listing
extension NetworksTableViewController {
func setListingDictionary(listingDictionary: Dictionary<String, AnyObject>){
self.itemListingDictionary = listingDictionary
}
func setManagers(fbManager: FacebookGraphAPIManager, fireManager: FirebaseManager, ebayManager: EbayWebServiceManager, etsyManager: EtsyRESTAPIManager){
self.graphManager = fbManager
self.firManager = fireManager
self.ebayManager = ebayManager
self.etsyManager = etsyManager
}
}
// MARK: - Side Menu View
extension NetworksTableViewController: 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 NetworksTableViewController {
// 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.
if(segue.identifier == "ItemPreviewSegue"){
if(networkCount > 0){
var count = 0;
for (_,v) in self.networksDictionary{
if v {
count += 1
}
}
if(count > 0){
(segue.destinationViewController as! ListingPreviewViewController).setDictionary(self.networksDictionary, itemdictionary: self.itemListingDictionary)
(segue.destinationViewController as! ListingPreviewViewController).setManagers(self.graphManager, fireManager: self.firManager, ebayManager: self.ebayManager, etsyManager: self.etsyManager)
}else{
let alert = UIAlertController(title: "No Networks Selected", message: "You have not selected any networks to publish on. At least 1 network must be selected to continue.", 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{
let alert = UIAlertController(title: "No Networks Authorized", message: "You currently have no authorized networks to post on. You will not be able to publish this listing until at least 1 network has been authorized and selected for publishing.", 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)
}
}
if(segue.identifier == "EbaySettingsSegue") {
(segue.destinationViewController as! EbaySettingsViewController).loadManager(self.ebayManager)
}
if(segue.identifier == "EtsySettingsSegue") {
(segue.destinationViewController as! EtsySettingsViewController).loadManager(self.etsyManager)
}
}
}
// MARK: - TableView Datasource methods
extension NetworksTableViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.networkToggleOrSelect) {
var count = 0
// #warning Incomplete implementation, return the number of rows
for (_, visibility) in self.shouldShowDictionary {
if visibility {
count += 1
}
}
return count
}
else {
return 4
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell!
//case where class is being used in new item posting workflow to select which network to sell on
//may hide future networks that don't apply to a listing
if(self.networkToggleOrSelect){
let market = self.checkVisibility()!
switch (market) {
case "facebook":
cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell)
break
case "ebay":
cell = (self.tableView.dequeueReusableCellWithIdentifier("ebay", forIndexPath: indexPath) as! EbayTableViewCell)
break
case "etsy":
cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell)
break
case "amazon":
cell = (self.tableView.dequeueReusableCellWithIdentifier("amazon", forIndexPath: indexPath) as! AmazonTableViewCell)
break
default:
cell = UITableViewCell()
break
}
/*switch (indexPath.row){
case 0:
//loads network cell for ebay
cell = (self.tableView.dequeueReusableCellWithIdentifier("ebay", forIndexPath: indexPath) as! EbayTableViewCell)
break
case 1:
//loads network cell for amazon
cell = (self.tableView.dequeueReusableCellWithIdentifier("amazon", forIndexPath: indexPath) as! AmazonTableViewCell)
break
case 2:
//loads network cell for etsy
cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell)
break
default:
//loads network cell for facebook
cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell)
break
}*/
}
else{
//case where class is being used to authorize a network for the app to post on
switch (indexPath.row){
case 0:
//loads network cell for ebay
cell = (self.tableView.dequeueReusableCellWithIdentifier("ebay", forIndexPath: indexPath) as! EbayTableViewCell)
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).networkToggle.setOn(true, animated: false)
}else{
(cell as! EbayTableViewCell).settingsBtn.enabled = false
(cell as! EbayTableViewCell).settingsBtn.hidden = true
}
break
case 1:
//loads network cell for amazon
cell = (self.tableView.dequeueReusableCellWithIdentifier("amazon", forIndexPath: indexPath) as! AmazonTableViewCell)
break
case 2:
//loads network cell for etsy
cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell)
let tabBar = self.tabBarController
if(((tabBar as? HomeViewController)?.etsyManager.isAuthorized)!){
dispatch_async(dispatch_get_main_queue(), {
(cell as! EtsyTableViewCell).authorizeBtn.setTitle("Logout", forState: UIControlState.Normal)
//(cell as! EtsyTableViewCell).networkToggle.setOn(true, animated: false)
})
}else{
(cell as! EtsyTableViewCell).settingsBtn.enabled = false
(cell as! EtsyTableViewCell).settingsBtn.hidden = true
}
break
default:
//loads network cell for facebook
cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell)
let tabBar = self.tabBarController
if(((tabBar as? HomeViewController)?.fbGraphManager.isAuthorized)!){
dispatch_async(dispatch_get_main_queue(), {
(cell as! FBTableViewCell).authorizeBtn.setTitle("Logout", forState: UIControlState.Normal)
})
//(cell as! FBTableViewCell).networkToggle.setOn(true, animated: false)
}else{
dispatch_async(dispatch_get_main_queue(), {
(cell as! FBTableViewCell).settingsBtn.enabled = false
(cell as! FBTableViewCell).settingsBtn.hidden = true
})
}
break
}
}
// Configure the cell (in this case show or hide the cell depending on which network the item is being listed on)
return cell
}
}
//MARK: - IBActions
extension NetworksTableViewController {
@IBAction func showListingPreview(sender: AnyObject) {
self.performSegueWithIdentifier("ItemPreviewSegue", sender: self)
}
@IBAction func draftItem(sender: AnyObject) {
if((self.itemListingDictionary["pictures"]! as? [UIImageView])?[0].image == nil){
let alert = UIAlertController(title: "Image Needed", message: "To save the listing as a draft, 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 = ["listingID": newListingRef.key,
"seller email": (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!,
"listingTitle": self.itemListingDictionary["title"],
"listingPrice": self.itemListingDictionary["price"],
"listingCategory": self.itemListingDictionary["category"],
"listingDescription": self.itemListingDictionary["description"],
"numberOfSupportingImages" : ((self.itemListingDictionary["pictures"] as? [UIImageView])?.count)! - 1,
"isListingDraft": true,
"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)!)
//check each imageview to see if there is an image to upload
if((self.itemListingDictionary["pictures"] as? [UIImageView])!.count >= 2){
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+"/2.jpg")
listingImageRef.putData(UIImageJPEGRepresentation((self.itemListingDictionary["pictures"] as? [UIImageView])![1].image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
print(metadata)
})
}
if((self.itemListingDictionary["pictures"] as? [UIImageView])!.count >= 3){
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+"/3.jpg")
listingImageRef.putData(UIImageJPEGRepresentation((self.itemListingDictionary["pictures"] as? [UIImageView])![2].image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
print(metadata)
})
}
if((self.itemListingDictionary["pictures"] as? [UIImageView])!.count >= 4){
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+"/4.jpg")
listingImageRef.putData(UIImageJPEGRepresentation((self.itemListingDictionary["pictures"] as? [UIImageView])![3].image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
})
}
if((self.itemListingDictionary["pictures"] as? [UIImageView])!.count == 5){
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+"/5.jpg")
listingImageRef.putData(UIImageJPEGRepresentation((self.itemListingDictionary["pictures"] as? [UIImageView])![4].image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
})
}
if((self.itemListingDictionary["pictures"] as? [UIImageView])!.count >= 1){
let listingImageRef = storageRef.child("images/\(newListingRef.key)"+"/main.jpg")
listingImageRef.putData(UIImageJPEGRepresentation((self.itemListingDictionary["pictures"] as? [UIImageView])![0].image!, 0.8)!, metadata: nil,completion:
{(metadata, error) -> Void in
newListingRef.setValue(listing as? Dictionary<String,AnyObject>)
let alert = UIAlertController(title: "Item Saved", message: "Your listing has been saved by a draft", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: {
self.dismissViewControllerAnimated(true, completion: nil)
})}))
self.presentViewController(alert, animated: true, completion: nil)
})
}
})
}
}
}
//SET UP TO ADD EACH INTEGRATION AS DEVELOPMENT GETS TO IT
//MARK: - Tableview Delegate Methods
extension NetworksTableViewController: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//case where class is being used in new item posting workflow to select which network to sell on
if(self.networkToggleOrSelect){
switch (self.networkOrderSelection[indexPath.row]){
case "ebay":
let cell: EbayTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("ebay", forIndexPath: indexPath) as! EbayTableViewCell)
cell.setSelected(false, animated: false)
//selection code for ebay
//print(cell.networkToggle.on)
if(cell.networkToggle.on){
cell.networkToggle.setOn(false, animated: true)
cell.networkToggle.on = false
//code to deselect network
self.networksDictionary["ebay"] = false
}
else{
cell.networkToggle.setOn(true, animated: true)
cell.networkToggle.on = true
//code to select network
self.networksDictionary["ebay"] = true
}
break
case "amazon":
let cell: AmazonTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("amazon", forIndexPath: indexPath) as! AmazonTableViewCell)
cell.setSelected(false, animated: false)
//selection code for amazon
if(cell.networkToggle.on == true){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
})
//code to deselect network
self.networksDictionary["amazon"] = false
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
//code to select network
self.networksDictionary["amazon"] = true
}
break
case "etsy":
let cell: EtsyTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell)
cell.setSelected(false, animated: false)
//selection code for etsy
if(cell.networkToggle.on == true){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
cell.networkToggle.on = false
})
//code to deselect network
self.networksDictionary["etsy"] = false
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
cell.networkToggle.on = true
})
//code to select network
self.networksDictionary["etsy"] = true
//this is the type of code desired to access the rest management classes
/*
let tabBar = self.tabBarController
(tabBar as? HomeViewController)?.etsyManager.authorizeApp(self)
*/
}
break
case "facebook":
let cell: FBTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell)
cell.setSelected(false, animated: false)
//selection code for facebook
if(cell.networkToggle.on == true){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
})
//code to deselect network
self.networksDictionary["facebook"] = false
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
//code to select network
self.networksDictionary["facebook"] = true
}
break
default:
break
}
}
else{
/*
//case where class is being used to authorize a network for the app to post on
switch (indexPath.row){
case 0:
let cell: EbayTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("ebay", forIndexPath: indexPath) as! EbayTableViewCell)
cell.setSelected(false, animated: false)
/*OAuthorization code for ebay*/
print(cell.networkToggle.on)
if(((self.tabBarController as? HomeViewController)?.ebayGraphManager.isAuthorized)!){
//code to deauthorize network
cell.networkToggle.setOn(false, animated: true)
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.ebayGraphManager.deauthorizeApp())!
cell.networkToggle.on = ((tabBar as? HomeViewController)?.ebayGraphManager.isAuthorized)!
}
else{
//code to authorize network
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.ebayGraphManager.authorizeApp())!
cell.networkToggle.on = ((tabBar as? HomeViewController)?.ebayGraphManager.isAuthorized)!
}
break
case 1:
let cell: AmazonTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("amazon", forIndexPath: indexPath) as! AmazonTableViewCell)
cell.setSelected(false, animated: false)
//OAuthorization code for amazon
if(cell.networkToggle.on == true){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
})
//code to deauthorize network
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
//code to authorize network
}
break
case 2:
let cell: EtsyTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell)
cell.setSelected(false, animated: false)
//OAuthorization code for etsy
if(((self.tabBarController as? HomeViewController)?.etsyManager.isAuthorized)!){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
})
//code to deauthorize network
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.etsyManager.deAuthorizeApp(self))!
cell.networkToggle.on = ((tabBar as? HomeViewController)?.etsyManager.isAuthorized)!
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
//code to authorize network
//this is the type of code desired to access the rest management classes
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.etsyManager.authorizeApp(self))!
cell.networkToggle.on = ((tabBar as? HomeViewController)?.etsyManager.isAuthorized)!
//self.etsyManager.authorizeApp(self)
}
break
default:
let cell: FBTableViewCell!
cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell)
cell.setSelected(false, animated: false)
//OAuthorization code for facebook
if((self.tabBarController as? HomeViewController)?.fbGraphManager.isAuthorized)!{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(false, animated: true)
})
//code to deauthorize network
let tabBar = self.tabBarController
let didSucceed:Bool = ((tabBar as? HomeViewController)?.fbGraphManager.deAuthorizeApp(self))!
cell.networkToggle.on = !didSucceed
}
else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.networkToggle.setOn(true, animated: true)
})
//code to authorize network
//this is the type of code desired to access the rest management classes
let tabBar = self.tabBarController
let didSucceed: Bool = ((tabBar as? HomeViewController)?.fbGraphManager.authorizeApp(self))!
cell.networkToggle.on = didSucceed
}
break
}*/
}
}
/*
// 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
}
*/
}
extension NetworksTableViewController: EbayWebServiceManagerDelegate {
func signInUser(signInString : String!) {
let mywebViewController = UIViewController()
//let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let myWebView = UIWebView(frame: mywebViewController.view.bounds)
myWebView.delegate = self
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: signInString)!))
mywebViewController.view = myWebView
let navController = UINavigationController(rootViewController: mywebViewController)
mywebViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: Selector("dismiss"))
self.presentViewController(navController, animated: true, completion: nil)
//self.view.addSubview(myWebView)
}
}
extension NetworksTableViewController: UIWebViewDelegate {
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print("Webview fail with error \(error)");
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return true;
}
func webViewDidStartLoad(webView: UIWebView) {
print("Webview started Loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
print("Webview did finish load")
if (webView.request != nil)
{
//print(webView.request?.allHTTPHeaderFields)
var responseString : NSString? = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")!
if((responseString!.containsString("Authorization successfully completed"))){
NSLog("Oauth successful")
((self.tabBarController as? HomeViewController)?.ebayGraphManager)!.fetchToken()
}
}
}
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
extension NetworksTableViewController {
@IBAction func toggleAuth(sender: UIButton){
print(sender.superview!.superview!.classForCoder)
//Authorize app
if(sender.titleLabel?.text == "Login") {
switch (sender.superview?.superview?.classForCoder){
case is EbayTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.ebayGraphManager.authorizeApp({
something,_ -> Void in
// // //
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Logout", forState: UIControlState.Normal)
(sender.superview?.superview as! EbayTableViewCell).settingsBtn.enabled = true
(sender.superview?.superview as! EbayTableViewCell).settingsBtn.hidden = false
})
}))!
break
case is AmazonTableViewCell.Type:
break
case is EtsyTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.etsyManager.authorizeApp(self, onComplete: {
something , _ -> Void in
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Logout", forState: UIControlState.Normal)
(sender.superview?.superview as! EtsyTableViewCell).settingsBtn.enabled = true
(sender.superview?.superview as! EtsyTableViewCell).settingsBtn.hidden = false
})
}))!
break
case is FBTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.fbGraphManager.authorizeApp(self, onComplete: {
something , _ -> Void in
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Logout", forState: UIControlState.Normal)
(sender.superview?.superview as! FBTableViewCell).settingsBtn.enabled = true
(sender.superview?.superview as! FBTableViewCell).settingsBtn.hidden = false
})
}))!
break
default:
break
}
}
//DeAuthorize app
else {
switch (sender.superview?.superview?.classForCoder){
case is EbayTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.ebayGraphManager.deauthorizeApp())!
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Login", forState: UIControlState.Normal)
(sender.superview?.superview as! EbayTableViewCell).settingsBtn.enabled = false
(sender.superview?.superview as! EbayTableViewCell).settingsBtn.hidden = true
})
break
case is AmazonTableViewCell.Type:
break
case is EtsyTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.etsyManager.deAuthorizeApp(self))!
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Login", forState: UIControlState.Normal)
(sender.superview?.superview as! EtsyTableViewCell).settingsBtn.enabled = false
(sender.superview?.superview as! EtsyTableViewCell).settingsBtn.hidden = true
})
break
case is FBTableViewCell.Type:
let tabBar = self.tabBarController
((tabBar as? HomeViewController)?.fbGraphManager.deAuthorizeApp(self))!
dispatch_async(dispatch_get_main_queue(), {
sender.setTitle("Login", forState: UIControlState.Normal)
(sender.superview?.superview as! FBTableViewCell).settingsBtn.enabled = false
(sender.superview?.superview as! FBTableViewCell).settingsBtn.hidden = true
})
break
default:
break
}
}
}
@IBAction func toggleNetworkSelection(sender: UISwitch) {
switch (sender.superview?.superview?.classForCoder){
case is EbayTableViewCell.Type:
//selection code for ebay
if((sender.superview?.superview as! EbayTableViewCell).networkToggle.on){
//code to select network
self.networksDictionary["ebay"] = true
}
else{
//code to deselect network
self.networksDictionary["ebay"] = false
}
break
case is AmazonTableViewCell.Type:
//selection code for amazon
if((sender.superview?.superview as! AmazonTableViewCell).networkToggle.on){
//code to select network
self.networksDictionary["amazon"] = true
}
else{
//code to deselect network
self.networksDictionary["amazon"] = false
}
break
case is EtsyTableViewCell.Type:
//selection code for etsy
if((sender.superview?.superview as! EtsyTableViewCell).networkToggle.on){
//code to select network
self.networksDictionary["etsy"] = true
}
else{
//code to deselect network
self.networksDictionary["etsy"] = false
}
break
case is FBTableViewCell.Type:
//selection code for facebook
if((sender.superview?.superview as! FBTableViewCell).networkToggle.on){
//code to select network
self.networksDictionary["facebook"] = true
}
else{
//code to deselect network
self.networksDictionary["facebook"] = false
}
break
default:
break
}
}
}