diff --git a/packages/core/lib/client.dart b/packages/core/lib/client.dart index a6d40a7..231d5b0 100644 --- a/packages/core/lib/client.dart +++ b/packages/core/lib/client.dart @@ -19,7 +19,10 @@ Analytics createClient(Configuration configuration) { configuration = setFlushPolicies(configuration, defaultFlushPolicies); } - final analytics = Analytics(configuration, storeFactory()); + final analytics = Analytics( + configuration, + storeFactory(storageJson: configuration.storageJson ?? true), + ); if (configuration.debug) { analytics.addPlugin(EventLogger()); diff --git a/packages/core/lib/utils/store/io.dart b/packages/core/lib/utils/store/io.dart index 9159415..f93b53d 100644 --- a/packages/core/lib/utils/store/io.dart +++ b/packages/core/lib/utils/store/io.dart @@ -9,8 +9,11 @@ import 'package:segment_analytics/utils/store/store.dart'; import 'package:path_provider/path_provider.dart'; class StoreImpl with Store { + final bool storageJson; + StoreImpl({this.storageJson = true}); @override Future?> getPersisted(String key) { + if (!storageJson) return Future.value(null); return _readFile(key); } @@ -19,8 +22,18 @@ class StoreImpl with Store { @override Future setPersisted(String key, Map value) { + if (!storageJson) return Future.value(); return _writeFile(key, value); } + + @override + Future deletePersisted(String key) async { + if (!storageJson) return; + final file = File(await _fileName(key)); + if (await file.exists()) { + await file.delete(); + } + } Future _writeFile(String fileKey, Map data) async { RandomAccessFile file = diff --git a/packages/core/lib/utils/store/store.dart b/packages/core/lib/utils/store/store.dart index 084e37f..73f1d72 100644 --- a/packages/core/lib/utils/store/store.dart +++ b/packages/core/lib/utils/store/store.dart @@ -4,12 +4,14 @@ mixin Store { Future?> getPersisted(String key); Future setPersisted(String key, Map value); + + Future deletePersisted(String key); Future get ready; void dispose(); } -StoreImpl storeFactory() { - return StoreImpl(); +StoreImpl storeFactory({bool storageJson = true}) { + return StoreImpl(storageJson: storageJson); } diff --git a/packages/core/lib/utils/store/unsupported.dart b/packages/core/lib/utils/store/unsupported.dart index f82d429..3f8b441 100644 --- a/packages/core/lib/utils/store/unsupported.dart +++ b/packages/core/lib/utils/store/unsupported.dart @@ -4,6 +4,8 @@ import 'package:segment_analytics/errors.dart'; import 'store.dart'; class StoreImpl with Store { + final bool storageJson; + StoreImpl({this.storageJson = true}); @override Future?> getPersisted(String key) { throw PlatformNotSupportedError(); @@ -16,9 +18,14 @@ class StoreImpl with Store { Future setPersisted(String key, Map value) { throw PlatformNotSupportedError(); } + + @override + Future deletePersisted(String key) { + throw PlatformNotSupportedError(); + } @override void dispose() { throw PlatformNotSupportedError(); } -} +} \ No newline at end of file diff --git a/packages/core/lib/utils/store/web.dart b/packages/core/lib/utils/store/web.dart index 714564a..a90b27e 100644 --- a/packages/core/lib/utils/store/web.dart +++ b/packages/core/lib/utils/store/web.dart @@ -5,10 +5,13 @@ import 'package:segment_analytics/utils/store/store.dart'; import 'package:web/web.dart' as web; class StoreImpl implements Store { + final bool storageJson; + StoreImpl({this.storageJson = true}); web.Storage get localStorage => web.window.localStorage; @override Future?> getPersisted(String key) { + if (!storageJson) return Future.value(null); return _readFromStorage(key); } @@ -17,9 +20,17 @@ class StoreImpl implements Store { @override Future setPersisted(String key, Map value) { + if (!storageJson) return Future.value(); _writeToStorage(key, value); return Future.value(); } + + @override + Future deletePersisted(String key) { + if (!storageJson) return Future.value(); + localStorage.removeItem(_getFileName(key)); + return Future.value(); + } String _getFileName(String fileKey) { return "analytics-flutter-$fileKey.json"; diff --git a/packages/core/pubspec.yaml b/packages/core/pubspec.yaml index 5e85070..a302d9b 100644 --- a/packages/core/pubspec.yaml +++ b/packages/core/pubspec.yaml @@ -1,6 +1,6 @@ name: segment_analytics description: The hassle-free way to add Segment analytics to your Flutter app. -version: 1.1.8 +version: 1.1.9 homepage: https://github.com/segmentio/analytics_flutter#readme repository: https://github.com/segmentio/analytics_flutter/tree/main/packages/core#readme issue_tracker: https://github.com/segmentio/analytics_flutter/issues