Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Remove shared preferences as a hard dependency #30

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 0 additions & 125 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -74,11 +60,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: transitive
description:
Expand All @@ -93,13 +74,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -128,97 +102,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
shared_preferences:
dependency: transitive
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
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.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -280,20 +170,5 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"
51 changes: 42 additions & 9 deletions lib/src/mixpanel_analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
// import 'package:shared_preferences/shared_preferences.dart';

enum MixpanelUpdateOperations {
$set,
Expand All @@ -16,6 +16,40 @@ enum MixpanelUpdateOperations {
$delete,
}

abstract class StringCache {
Future<String?> getString(String key);
Future<bool> setString(String key, String value);
}

class InMemoryStringCache implements StringCache {
final _map = <String, String>{};

@override
Future<String?> getString(String key) => Future.value(_map[key]);

@override
Future<bool> setString(String key, String value) {
_map[key] = value;
return Future.value(true);
}
}

// class SharedPreferencesStringCache implements StringCache {
// SharedPreferences? _prefs;
//
// @override
// Future<String?> getString(String key) async {
// _prefs ??= await SharedPreferences.getInstance();
// return _prefs!.getString(key);
// }
//
// @override
// Future<bool> setString(String key, String value) async {
// _prefs ??= await SharedPreferences.getInstance();
// return _prefs!.setString(key, value);
// }
// }

typedef ShaFn = String Function(String value);

class MixpanelAnalytics {
Expand All @@ -33,10 +67,7 @@ class MixpanelAnalytics {
/// Will be zero by default
Duration _uploadInterval = Duration.zero;

/// In case we use [MixpanelAnalytics.batch()] we need to provide a storage provider
/// This will be used to save the events not sent
@visibleForTesting
SharedPreferences? prefs;
final StringCache _cache;

/// If exists, will be sent in the event, otherwise anonymousId will be used.
final Stream<String?>? _userId$;
Expand Down Expand Up @@ -129,6 +160,7 @@ class MixpanelAnalytics {
/// [baseApiUrl] Ingestion API URL. If you don't inform it, the US-based url will be used (api.mixpanel.com). https://developer.mixpanel.com/docs/privacy-security#storing-your-data-in-the-european-union
MixpanelAnalytics({
required String token,
StringCache? cache,
Stream<String?>? userId$,
bool shouldAnonymize = false,
ShaFn shaFn = _defaultShaFn,
Expand All @@ -140,6 +172,7 @@ class MixpanelAnalytics {
String? prefsKey,
String? baseApiUrl,
}) : _token = token,
_cache = cache ?? InMemoryStringCache(),
_userId$ = userId$,
_verbose = verbose,
_useIp = useIp,
Expand Down Expand Up @@ -169,6 +202,7 @@ class MixpanelAnalytics {
/// [baseApiUrl] Ingestion API URL. If you don't inform it, the US-based url will be used (api.mixpanel.com). https://developer.mixpanel.com/docs/privacy-security#storing-your-data-in-the-european-union
MixpanelAnalytics.batch({
required String token,
StringCache? cache,
required Duration uploadInterval,
Stream<String?>? userId$,
bool shouldAnonymize = false,
Expand All @@ -181,6 +215,7 @@ class MixpanelAnalytics {
String? prefsKey,
String? baseApiUrl,
}) : _token = token,
_cache = cache ?? InMemoryStringCache(),
_userId$ = userId$,
_verbose = verbose,
_useIp = useIp,
Expand Down Expand Up @@ -269,8 +304,7 @@ class MixpanelAnalytics {
/// Reads queued events from the storage when we are in batch mode.
/// We do this in case the app was closed with events pending to be sent.
Future<void> _restoreQueuedEventsFromStorage() async {
prefs ??= await SharedPreferences.getInstance();
final encoded = prefs!.getString(_prefsKey);
final encoded = await _cache.getString(_prefsKey);
if (encoded != null) {
Map<String, dynamic> events = json.decode(encoded);
_queuedEvents.addAll(events);
Expand All @@ -279,10 +313,9 @@ class MixpanelAnalytics {

/// If we are in batch mode we save all events in storage in case the app is closed.
Future<bool> _saveQueuedEventsToLocalStorage() async {
prefs ??= await SharedPreferences.getInstance();
final encoded = json.encode(_queuedEvents);
final result =
await prefs!.setString(_prefsKey, encoded).catchError((error) {
await _cache.setString(_prefsKey, encoded).catchError((error) {
_onErrorHandler(error, 'Error saving events in storage');
return false;
});
Expand Down
Loading