Mercurial > public > geoquiz
diff GeoQuiz/Logic/UserClass.swift @ 10:a793f33f05fb
refactor code and fix layout
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 08 Oct 2022 21:36:40 +0200 |
parents | GeoQuiz/Logic/User.swift@3540c7efc216 |
children | 136928bae534 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GeoQuiz/Logic/UserClass.swift Sat Oct 08 21:36:40 2022 +0200 @@ -0,0 +1,27 @@ +// +// UserClass.swift +// GeoQuiz +// +// Created by Dennis Concepción Martín on 7/10/22. +// + +import Foundation + +class User: ObservableObject { + @Published var settings = UserSettings() { + didSet { + if let userSettingsEncoded = try? JSONEncoder().encode(settings) { + UserDefaults.standard.set(userSettingsEncoded, forKey: "UserSettings") + } + } + } + + + init() { + if let userSettings = UserDefaults.standard.data(forKey: "UserSettings") { + if let decodedUserSettings = try? JSONDecoder().decode(UserSettings.self, from: userSettings) { + settings = decodedUserSettings + } + } + } +}