Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import CoreData
- class ConfirmationRegistrationViewController: UIViewController {
- //This ViewController is used twice
- //- As the Confirmation at the end of the registration
- //- As an Overview of the user's own data
- var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
- var PersonToRegister = [
- "FirstName":"",
- "LastName":"",
- "Password":"",
- "Email":"",
- "Street":"",
- "AdditionalAdressInfo":"",
- "ZipCode":"",
- "City":"",
- "PaymentMethod":""
- ]
- var user: User?
- var UserEmail: String? = ""
- @IBOutlet weak var NameConfirmationRegistration: UILabel!
- @IBOutlet weak var MailConfirmationRegistration: UILabel!
- @IBOutlet weak var StreetConfirmationRegistration: UILabel!
- @IBOutlet weak var ZipcityConfirmationRegistration: UILabel!
- @IBOutlet weak var AdditionalInfoConfirmationRegistration: UILabel!
- @IBOutlet weak var PaymentConfirmationRegistration: UILabel!
- @IBOutlet weak var PasswordConfirmationRegistration: UILabel!
- @IBOutlet weak var DataView: UIView!
- @IBOutlet weak var SaveButton: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.navigationItem.largeTitleDisplayMode = .never
- if let uem = UserDefaults.standard.string(forKey: "UserEmail"){
- UserEmail = uem
- }
- if !UserDefaults.standard.bool(forKey: "loggedIn"){
- //If no user is logged in: diplays the entered data to confirm
- DisplayDataToConfirm()
- }else{
- //If an user is logged in: display the fetch and display the data of the current user
- fetchUserData()
- DisplayCoreDataEntry()
- SaveButton.isHidden = true
- }
- }
- @IBAction func CloseRegistration(_ sender: Any) {
- self.saveUser()
- let alertController = UIAlertController(title: "Erfolgreich registriert", message: "Bitte melden Sie sich nun an um Ihre Eingaben zu bestätigen", preferredStyle: .alert)
- let action = UIAlertAction(title: "Ok!", style: .default) { (action:UIAlertAction) in
- self.dismiss(animated: true, completion: nil)
- }
- alertController.addAction(action)
- self.present(alertController, animated: true, completion: nil)
- }
- func DisplayDataToConfirm(){
- NameConfirmationRegistration.text = PersonToRegister["FirstName"]! + " " + PersonToRegister["LastName"]!
- MailConfirmationRegistration.text = PersonToRegister["Email"]
- StreetConfirmationRegistration.text = PersonToRegister["Street"]
- ZipcityConfirmationRegistration.text = PersonToRegister["ZipCode"]! + " " + PersonToRegister["City"]!
- AdditionalInfoConfirmationRegistration.text = PersonToRegister["AdditionalAdressInfo"]
- PaymentConfirmationRegistration.text = PersonToRegister["PaymentMethod"]
- PasswordConfirmationRegistration.text = PersonToRegister["Password"]
- }
- func DisplayCoreDataEntry(){
- NameConfirmationRegistration.text = (user?.firstname!)! + " " + (user?.lastname!)!
- MailConfirmationRegistration.text = user?.email
- StreetConfirmationRegistration.text = user?.street
- ZipcityConfirmationRegistration.text = (user?.zipcode!)! + " " + (user?.city!)!
- AdditionalInfoConfirmationRegistration.text = user?.additionaladressinfo
- PaymentConfirmationRegistration.text = user?.paymentmethod
- PasswordConfirmationRegistration.text = user?.password
- }
- func saveUser(){
- let userEntity: NSEntityDescription? = NSEntityDescription.entity(forEntityName: "User", in: self.appDelegate.coreDataStack.managedObjectContext)
- if userEntity != nil {
- let user1: User = User(entity: userEntity!, insertInto: self.appDelegate.coreDataStack.managedObjectContext)
- user1.firstname = PersonToRegister["FirstName"]
- user1.lastname = PersonToRegister["LastName"]
- user1.email = PersonToRegister["Email"]
- user1.street = PersonToRegister["Street"]
- user1.additionaladressinfo = PersonToRegister["AdditionalAdressInfo"]
- user1.zipcode = PersonToRegister["ZipCode"]
- user1.city = PersonToRegister["City"]
- user1.paymentmethod = PersonToRegister["PaymentMethod"]
- user1.password = PersonToRegister["Password"]
- self.appDelegate.coreDataStack.saveContext()
- }
- }
- func fetchUserData(){
- let fetchRequest: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
- fetchRequest.predicate = NSPredicate(format: "email == %@", UserEmail!)
- fetchRequest.fetchLimit = 1
- do {
- if let results = try self.appDelegate.coreDataStack.managedObjectContext.fetch(fetchRequest) as? [NSManagedObject] {
- let fetchedUser: [User]? = results as? [User]
- if fetchedUser != nil {
- self.user = fetchedUser![0]
- }
- }
- }
- catch {
- fatalError("There was an error fetching the items")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement