vendoo_v1.0/Vendoo/MenuPanelViewController.swift
2016-11-29 19:02:06 -05:00

157 lines
5.1 KiB
Swift

//
// MenuPanelViewController.swift
// Vendoo
//
// Created by Okechi Onyeje on 6/12/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
import UIKit
import FirebaseAuth
import Locksmith
/// Used to display options for user, help and legal page, the number of notifications for the user has, and logout capability
class MenuPanelViewController: UIViewController{
var count = (NSUserDefaults.standardUserDefaults().objectForKey("notifications") == nil ? 0 : (NSUserDefaults.standardUserDefaults().objectForKey("notifications") as! [Dictionary<String, AnyObject>]).count)
//Outlets
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.table.dataSource = self
self.table.delegate = self
// 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.
}
*/
}
extension MenuPanelViewController: UITableViewDelegate
{
}
extension MenuPanelViewController: UITableViewDataSource
{
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 6
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
switch(indexPath.row){
//display user name and email
case 0:
cell = (self.table.dequeueReusableCellWithIdentifier("User_Cell", forIndexPath: indexPath) as! UserCell)
(cell as! UserCell).name.text = "Hi " + (NSUserDefaults.standardUserDefaults().objectForKey("name") as? String)!
(cell as! UserCell).email.text = (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!
break
//display notification count
case 1:
let cellN: Not_TableViewCell!
cellN = self.table.dequeueReusableCellWithIdentifier("Notifications_Cell")! as! Not_TableViewCell
cellN.count.text = "\(count)"
return cellN
//display settings cell
case 2:
cell = self.table.dequeueReusableCellWithIdentifier("Settings_Cell")!
break
//display analytics (next iteration)
case 3:
cell = self.table.dequeueReusableCellWithIdentifier("Sales_Cell")!
break
//display FAQ cell
case 4:
cell = self.table.dequeueReusableCellWithIdentifier("FAQ_Cell")!
break
//display how to cell
default:
cell = self.table.dequeueReusableCellWithIdentifier("Getting_Started_Cell")!
break
}
return cell
}
}
extension MenuPanelViewController{
//logout user and deauthorize all accounts
@IBAction func logoutUser(sender: AnyObject) {
do{
try FIRAuth.auth()?.signOut()
try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo")
do {
try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_ebay")
}catch {
(error)
print(error)
}
do {
try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_etsy")
}catch {
(error)
print(error)
}
do {
try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_amazon")
}catch {
(error)
print(error)
}
//remove saved information from local cache
for key in Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys) {
NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
}
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SignInViewController")
self.presentViewController(vc!, animated: true, completion: nil)
}
catch{
(error)
print(error)
}
}
}