From a09525c553da23a121a15cb564cee70b34f23278 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 21 Aug 2022 01:32:41 +0200 Subject: [PATCH] improved caching --- README.md | 13 ++----- ios/Podfile.lock | 6 +++ lib/managers/cache_manager.dart | 59 ------------------------------ lib/managers/file_manager.dart | 11 +++--- lib/screens/settings_screen.dart | 4 ++ lib/screens/welcome_screen.dart | 1 - pubspec.lock | 63 ++++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 8 files changed, 83 insertions(+), 75 deletions(-) delete mode 100644 lib/managers/cache_manager.dart diff --git a/README.md b/README.md index 8346794..24c7ab7 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,9 @@ # Quid faciam Hodie -A new Flutter project. +## What did I do today? -## Getting Started +Find out what you did all the days and unlock moments you completely forgot! -This project is a starting point for a Flutter application. +## Showcase -A few resources to get you started if this is your first Flutter project: -- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) -- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) - -For help getting started with Flutter development, view the -[online documentation](https://docs.flutter.dev/), which offers tutorials, -samples, guidance on mobile development, and a full API reference. diff --git a/ios/Podfile.lock b/ios/Podfile.lock index d7fed63..8c25194 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -24,6 +24,8 @@ PODS: - permission_handler_apple (9.0.4): - Flutter - Polyline (5.0.2) + - shared_preferences_ios (0.0.1): + - Flutter - Tangram-es (0.17.1) - Toast (4.0.0) - uni_links (0.0.1): @@ -44,6 +46,7 @@ DEPENDENCIES: - location (from `.symlinks/plugins/location/ios`) - path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`) - permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`) + - shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`) - uni_links (from `.symlinks/plugins/uni_links/ios`) - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) - video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/ios`) @@ -76,6 +79,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/path_provider_ios/ios" permission_handler_apple: :path: ".symlinks/plugins/permission_handler_apple/ios" + shared_preferences_ios: + :path: ".symlinks/plugins/shared_preferences_ios/ios" uni_links: :path: ".symlinks/plugins/uni_links/ios" url_launcher_ios: @@ -96,6 +101,7 @@ SPEC CHECKSUMS: path_provider_ios: 14f3d2fd28c4fdb42f44e0f751d12861c43cee02 permission_handler_apple: 44366e37eaf29454a1e7b1b7d736c2cceaeb17ce Polyline: fce41d72e1146c41c6d081f7656827226f643dff + shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad Tangram-es: 628b634f7fc09d2217469b9914de00d9de49ff9d Toast: 91b396c56ee72a5790816f40d3a94dd357abc196 uni_links: d97da20c7701486ba192624d99bffaaffcfc298a diff --git a/lib/managers/cache_manager.dart b/lib/managers/cache_manager.dart deleted file mode 100644 index 6b8a004..0000000 --- a/lib/managers/cache_manager.dart +++ /dev/null @@ -1,59 +0,0 @@ -import 'dart:convert'; - -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; -import 'package:quid_faciam_hodie/constants/storage_keys.dart'; -import 'package:quid_faciam_hodie/constants/values.dart'; - -const storage = FlutterSecureStorage(); - -class CacheManager { - static _createKey(final String key) => '$CACHE_KEY/$key'; - - static Future isCacheValid(final String key) async { - final cacheKey = _createKey(key); - final existingEntry = await storage.read(key: cacheKey); - - if (existingEntry != null) { - try { - final entry = jsonDecode(existingEntry); - final DateTime creationDate = DateTime.parse(entry['creationDate']); - - // Check if the entry is still valid using CACHE_INVALIDATION_DURATION as the validity duration. - return DateTime.now().difference(creationDate) < - CACHE_INVALIDATION_DURATION; - } catch (_) { - return false; - } - } - - return false; - } - - static Future set(final String key, final String data) async { - final cacheKey = _createKey(key); - final cacheEntry = { - 'creationDate': DateTime.now().toIso8601String(), - 'data': data, - }; - - return storage.write( - key: cacheKey, - value: json.encode({ - key: cacheEntry, - }), - ); - } - - static Future get(final String key) async { - final cacheKey = _createKey(key); - final existingEntry = await storage.read(key: cacheKey); - - if (existingEntry == null) { - return null; - } - - final entry = jsonDecode(existingEntry); - - return entry['data']; - } -} diff --git a/lib/managers/file_manager.dart b/lib/managers/file_manager.dart index 3968a64..2578910 100644 --- a/lib/managers/file_manager.dart +++ b/lib/managers/file_manager.dart @@ -1,11 +1,12 @@ import 'dart:io'; import 'dart:typed_data'; +import 'package:flutter_cache/flutter_cache.dart' as cache; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:location/location.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:quid_faciam_hodie/constants/values.dart'; import 'package:quid_faciam_hodie/foreign_types/memory.dart'; -import 'package:quid_faciam_hodie/managers/cache_manager.dart'; import 'package:quid_faciam_hodie/managers/global_values_manager.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:uuid/uuid.dart'; @@ -99,15 +100,15 @@ class FileManager { final key = '$table:$path'; // Check cache - if (!disableCache && await CacheManager.isCacheValid(key)) { - final data = (await CacheManager.get(key))!; - return Uint8List.fromList(data.codeUnits); + if (!disableCache) { + return cache.load(key) as Uint8List; } final data = await _downloadFileData(table, path); final cacheData = String.fromCharCodes(data); - await CacheManager.set(key, cacheData); + + await cache.write(key, cacheData, CACHE_INVALIDATION_DURATION.inMinutes); return data; } diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 257a666..4b9f327 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -1,6 +1,7 @@ import 'package:camera/camera.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_cache/flutter_cache.dart' as cache; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; @@ -179,6 +180,9 @@ class _SettingsScreenState extends AuthRequiredState title: Text(localizations .settingsScreenAccountSectionLogoutLabel), onPressed: (_) async { + cache.clear(); + storage.deleteAll(); + await callWithLoading(supabase.auth.signOut); if (!mounted) { diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index 2e43b0c..3c5aec8 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -31,7 +31,6 @@ class _WelcomeScreenState extends State { void initState() { super.initState(); - storage.deleteAll(); getInitialImageForPhotoSwitching(); } diff --git a/pubspec.lock b/pubspec.lock index 8ebcb8a..a5bb70e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -167,6 +167,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_cache: + dependency: "direct main" + description: + name: flutter_cache + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" flutter_calendar_widget: dependency: "direct main" description: @@ -630,6 +637,62 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.2" + shared_preferences: + dependency: transitive + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.15" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.12" + shared_preferences_ios: + dependency: transitive + description: + name: shared_preferences_ios + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index a937c61..2519240 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -64,6 +64,7 @@ dependencies: url_launcher: ^6.1.5 apple_maps_flutter: ^1.2.0 morphing_text: ^1.0.1 + flutter_cache: ^0.1.0 dev_dependencies: flutter_test: