From eeefcf19b737be7c64f4487c2195eb9103a7fb48 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 6 Feb 2025 15:33:40 -0600 Subject: [PATCH] added live logic for checking/saving app version to clear cache Signed-off-by: Matt Bruce --- EmployeeDirectory/SceneDelegate.swift | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/EmployeeDirectory/SceneDelegate.swift b/EmployeeDirectory/SceneDelegate.swift index 177b19b..f4c0abe 100644 --- a/EmployeeDirectory/SceneDelegate.swift +++ b/EmployeeDirectory/SceneDelegate.swift @@ -11,6 +11,25 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? + let keyAppVersion = "savedAppVersion" + + func getAppVersion() -> String { + if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String, + let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String { + return "\(version) (\(build))" + } + return "Unknown Version" + } + + // Retrieve saved version + func getSavedAppVersion() -> String? { + return UserDefaults.standard.string(forKey: keyAppVersion) + } + + func saveAppVersion(_ version: String) { + UserDefaults.standard.set(version, forKey: keyAppVersion) + UserDefaults.standard.synchronize() + } func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { @@ -25,13 +44,19 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { let navigationController = UINavigationController(rootViewController: employeeListVC) window.rootViewController = navigationController - let appVersion = 1.5// get app version - let currentVersion = 1.0 //cached version + // get app version + let currentAppVersion = getAppVersion() - if appVersion != currentVersion { + // get app cached version + let savedAppVersion = getSavedAppVersion() ?? "none" + + // check to see if there is a diff and if so clear cache + if savedAppVersion != currentAppVersion { EmployeeCacheService.shared.clear() + // save the current app version for next run + saveAppVersion(currentAppVersion) } - + // Set the window to the scene self.window = window window.makeKeyAndVisible()