Advertisement
Guest User

Untitled

a guest
Jan 30th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.33 KB | None | 0 0
  1. import UIKit
  2. import CoreData
  3.  
  4. class ConfirmationRegistrationViewController: UIViewController {
  5. //This ViewController is used twice
  6. //- As the Confirmation at the end of the registration
  7. //- As an Overview of the user's own data
  8.    
  9.     var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  10.    
  11.     var PersonToRegister = [
  12.         "FirstName":"",
  13.         "LastName":"",
  14.         "Password":"",
  15.         "Email":"",
  16.         "Street":"",
  17.         "AdditionalAdressInfo":"",
  18.         "ZipCode":"",
  19.         "City":"",
  20.         "PaymentMethod":""
  21.     ]
  22.  
  23.    
  24.     var user: User?
  25.     var UserEmail: String? = ""
  26.    
  27.     @IBOutlet weak var NameConfirmationRegistration: UILabel!
  28.     @IBOutlet weak var MailConfirmationRegistration: UILabel!
  29.     @IBOutlet weak var StreetConfirmationRegistration: UILabel!
  30.     @IBOutlet weak var ZipcityConfirmationRegistration: UILabel!
  31.     @IBOutlet weak var AdditionalInfoConfirmationRegistration: UILabel!
  32.     @IBOutlet weak var PaymentConfirmationRegistration: UILabel!
  33.     @IBOutlet weak var PasswordConfirmationRegistration: UILabel!
  34.    
  35.     @IBOutlet weak var DataView: UIView!
  36.    
  37.     @IBOutlet weak var SaveButton: UIButton!
  38.    
  39.     override func viewDidLoad() {
  40.         super.viewDidLoad()
  41.        
  42.         self.navigationItem.largeTitleDisplayMode = .never
  43.         if let uem = UserDefaults.standard.string(forKey: "UserEmail"){
  44.             UserEmail = uem
  45.         }
  46.        
  47.         if !UserDefaults.standard.bool(forKey: "loggedIn"){
  48.             //If no user is logged in: diplays the entered data to confirm
  49.             DisplayDataToConfirm()
  50.         }else{
  51.             //If an user is logged in: display the fetch and display the data of the current user
  52.             fetchUserData()
  53.             DisplayCoreDataEntry()
  54.             SaveButton.isHidden = true
  55.         }
  56.     }
  57.    
  58.     @IBAction func CloseRegistration(_ sender: Any) {
  59.             self.saveUser()
  60.        
  61.             let alertController = UIAlertController(title: "Erfolgreich registriert", message: "Bitte melden Sie sich nun an um Ihre Eingaben zu bestätigen", preferredStyle: .alert)
  62.        
  63.             let action = UIAlertAction(title: "Ok!", style: .default) { (action:UIAlertAction) in
  64.                 self.dismiss(animated: true, completion: nil)
  65.             }
  66.        
  67.             alertController.addAction(action)
  68.             self.present(alertController, animated: true, completion: nil)
  69.     }
  70.    
  71.     func DisplayDataToConfirm(){
  72.         NameConfirmationRegistration.text = PersonToRegister["FirstName"]! + " " + PersonToRegister["LastName"]!
  73.         MailConfirmationRegistration.text = PersonToRegister["Email"]
  74.         StreetConfirmationRegistration.text = PersonToRegister["Street"]
  75.         ZipcityConfirmationRegistration.text = PersonToRegister["ZipCode"]! + " " + PersonToRegister["City"]!
  76.         AdditionalInfoConfirmationRegistration.text = PersonToRegister["AdditionalAdressInfo"]
  77.         PaymentConfirmationRegistration.text = PersonToRegister["PaymentMethod"]
  78.         PasswordConfirmationRegistration.text = PersonToRegister["Password"]
  79.     }
  80.    
  81.    
  82.     func DisplayCoreDataEntry(){
  83.         NameConfirmationRegistration.text = (user?.firstname!)! + " " + (user?.lastname!)!
  84.         MailConfirmationRegistration.text = user?.email
  85.         StreetConfirmationRegistration.text = user?.street
  86.         ZipcityConfirmationRegistration.text = (user?.zipcode!)! + " " + (user?.city!)!
  87.         AdditionalInfoConfirmationRegistration.text = user?.additionaladressinfo
  88.         PaymentConfirmationRegistration.text = user?.paymentmethod
  89.         PasswordConfirmationRegistration.text = user?.password
  90.     }
  91.    
  92.     func saveUser(){
  93.         let userEntity: NSEntityDescription? = NSEntityDescription.entity(forEntityName: "User", in: self.appDelegate.coreDataStack.managedObjectContext)
  94.        
  95.         if userEntity != nil {
  96.             let user1: User = User(entity: userEntity!, insertInto: self.appDelegate.coreDataStack.managedObjectContext)
  97.             user1.firstname = PersonToRegister["FirstName"]
  98.             user1.lastname = PersonToRegister["LastName"]
  99.             user1.email = PersonToRegister["Email"]
  100.             user1.street = PersonToRegister["Street"]
  101.             user1.additionaladressinfo = PersonToRegister["AdditionalAdressInfo"]
  102.             user1.zipcode = PersonToRegister["ZipCode"]
  103.             user1.city = PersonToRegister["City"]
  104.             user1.paymentmethod = PersonToRegister["PaymentMethod"]
  105.             user1.password = PersonToRegister["Password"]
  106.            
  107.             self.appDelegate.coreDataStack.saveContext()
  108.         }
  109.     }
  110.    
  111.     func fetchUserData(){
  112.        
  113.         let fetchRequest: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
  114.         fetchRequest.predicate = NSPredicate(format: "email == %@", UserEmail!)
  115.         fetchRequest.fetchLimit = 1
  116.         do {
  117.             if let results = try self.appDelegate.coreDataStack.managedObjectContext.fetch(fetchRequest) as? [NSManagedObject] {
  118.                 let fetchedUser: [User]? = results as? [User]
  119.                 if fetchedUser != nil {
  120.                     self.user = fetchedUser![0]
  121.                 }
  122.             }
  123.            
  124.         }
  125.         catch {
  126.             fatalError("There was an error fetching the items")
  127.         }
  128.        
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement