diff GeoQuiz/Controllers/UserController.swift @ 26:425078c01194

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Wed, 09 Nov 2022 10:30:01 +0100
parents GeoQuiz/Logic/UserController.swift@f140bb277c96
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Controllers/UserController.swift	Wed Nov 09 10:30:01 2022 +0100
@@ -0,0 +1,26 @@
+//
+//  UserController.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 7/10/22.
+//
+
+import Foundation
+
+class UserController: ObservableObject {
+    @Published var data = UserDataModel() {
+        didSet {
+            if let userDataEncoded = try? JSONEncoder().encode(data) {
+                UserDefaults.standard.set(userDataEncoded, forKey: "UserData")
+            }
+        }
+    }
+
+    init() {
+        if let userData = UserDefaults.standard.data(forKey: "UserData") {
+            if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) {
+                data = decodedUserData
+            }
+        }
+    }
+}