checkAppVersion method

  1. @override
Future<void> checkAppVersion(
  1. String appVersion
)
override

Checks the app version and clears cache if it's outdated.

Implementation

@override
Future<void> checkAppVersion(String appVersion) async {
  final cacheKey = getCacheKey();
  debugPrint('WebCacheClear: Using session storage key: "$cacheKey"');
  debugPrint('WebCacheClear: Current app version from code: "$appVersion"');

  try {
    final String? storedVersion = window.sessionStorage.getItem(cacheKey);
    debugPrint(
      'WebCacheClear: Version found in session storage: "$storedVersion"',
    );

    if (storedVersion != appVersion) {
      debugPrint(
        'WebCacheClear: Versions do not match. Clearing cache and updating session storage.',
      );
      await clearCache();

      window.sessionStorage.setItem(cacheKey, appVersion);
      debugPrint(
        'WebCacheClear: Set session storage with new version: "$appVersion"',
      );

      // Only reload if there was a different version before, not on first load.
      if (storedVersion != null) {
        debugPrint('WebCacheClear: Reloading page to apply new version.');
        window.location.reload();
      }
    } else {
      debugPrint(
        'WebCacheClear: App version is up to date. No action needed.',
      );
    }
  } catch (e) {
    debugPrint('WebCacheClear: An error occurred during checkAppVersion: $e');
  }
}