// // SettingsViewController.swift // Vendoo // // Created by Okechi Onyeje on 6/12/16. // Copyright © 2016 Okechi Onyeje. All rights reserved. // import UIKit import Locksmith import FirebaseAuth class SettingsViewController: UIViewController { //IBOutlet @IBOutlet weak var closeButton: UIBarButtonItem! @IBOutlet weak var table: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.table.dataSource = self self.table.delegate = self } 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. } */ } //MARK: - IBActions extension SettingsViewController { @IBAction func dismissSettings(sender: AnyObject) { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Reveal View Controller") self.presentViewController(vc!, animated: false, completion: nil) } } extension SettingsViewController: 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 2 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: UITableViewCell switch(indexPath.row){ case 0: cell = self.table.dequeueReusableCellWithIdentifier("Change_Pass_Cell")! break default: cell = self.table.dequeueReusableCellWithIdentifier("Delete_Account_Cell")! break } return cell } } extension SettingsViewController: UITableViewDelegate { func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { switch(indexPath.row){ case 0: let alert = UIAlertController(title: "Change Password", message: "WARNING! You are about to change your vendoo account password. Please enter your current password to continue.", preferredStyle: .Alert) alert.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter Current Password" }) alert.addAction(UIAlertAction(title: "Continue", style: .Default, handler:{(action: UIAlertAction!) in let textField1 = alert.textFields![0] as UITextField let dictionary = Locksmith.loadDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") if(textField1.text == dictionary!["pass"] as? String){ //put in password changing code later print("password accepted please enter new password") let alert2 = UIAlertController(title: "Change Password", message: "WARNING! You are about to change your vendoo account password. Please enter your new password to continue.", preferredStyle: .Alert) alert2.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter New Password" }) alert2.addAction(UIAlertAction(title: "Change Password", style: .Default, handler:{(action: UIAlertAction!) in let textField2 = alert2.textFields![0] as UITextField FIRAuth.auth()?.currentUser?.updatePassword(textField2.text!, completion: { (error) -> Void in if (error != nil) { print(error) }else { print("Current user's new password is now \(textField2.text!).") do{ try Locksmith.updateData(["pass": textField2.text!], forUserAccount: (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") print("account credentials saved") } catch{ //could not save data to keychain print("account credentials could not be saved") } } }) })) alert.dismissViewControllerAnimated(false, completion: {}) self.presentViewController(alert2, animated: true, completion: nil) }else{ //put in password changing code later print("incorrect password") let alert2 = UIAlertController(title: "Incorrect Password", message: "Incorrect password. Please try again.", preferredStyle: .Alert) alert2.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert2.dismissViewControllerAnimated(false, completion: {}) alert.textFields![0].text = "" self.presentViewController(alert, animated: true, completion: nil) })) alert.dismissViewControllerAnimated(false, completion: { Void in}) self.presentViewController(alert2, animated: true, completion: nil) } })) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction!) in})) self.presentViewController(alert, animated: true, completion: nil) break default: let alert = UIAlertController(title: "Delete Account", message: "WARNING! You are about to delete your vendoo account. This action will not only delete your account information, but delete any listing history and network authorizations you may currently have. To proceed please enter your account password.", preferredStyle: .Alert) alert.addTextFieldWithConfigurationHandler({ (textField : UITextField!) -> Void in textField.placeholder = "Enter Password" }) alert.addAction(UIAlertAction(title: "Delete", style: .Default, handler:{(action: UIAlertAction!) in let textField = alert.textFields![0] as UITextField let dictionary = Locksmith.loadDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo") if(textField.text == dictionary!["pass"] as? String){ //put in deletion code later print("account deleted, add in deletion code as well as deauthorization code for any network currently authorized") alert.dismissViewControllerAnimated(true, completion: nil) }else{ //put in password changing code later print("incorrect password") let alert2 = UIAlertController(title: "Incorrect Password", message: "Incorrect password. Please try again.", preferredStyle: .Alert) alert2.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert2.dismissViewControllerAnimated(false, completion: {}) alert.textFields![0].text = "" self.presentViewController(alert, animated: true, completion: nil) })) alert.dismissViewControllerAnimated(false, completion: { Void in}) self.presentViewController(alert2, animated: true, completion: nil) } })) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction!) in})) self.presentViewController(alert, animated: true, completion: nil) break } } }