Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shared_preferences] Add shared preferences devtools #6749

Merged
merged 86 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 80 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
9a98dce
Add shared preferences devtools
adsonpleal May 21, 2024
f6a1ce9
Add docs
adsonpleal May 21, 2024
b9c0b6b
change version constraint
adsonpleal May 21, 2024
e2f1478
update readme
adsonpleal May 21, 2024
b798091
pr requested changes
adsonpleal May 21, 2024
0115bea
fix broken test
adsonpleal May 21, 2024
421d981
remove icons from manifest
adsonpleal May 22, 2024
bf23f70
Use InheritedModel to avoid unnecessary rebuilds
adsonpleal May 23, 2024
2402e78
change search bar
adsonpleal May 23, 2024
215d551
PR requested changes
adsonpleal May 24, 2024
18ac82b
Fix wrong change
adsonpleal May 24, 2024
16f30fc
add TestOn annotations
adsonpleal May 24, 2024
c14409e
lint fix and update dependencies
adsonpleal May 24, 2024
382f612
revert vm_service version change
adsonpleal May 24, 2024
aba50c5
fix licenses
adsonpleal May 24, 2024
a118924
solve some CI checks
adsonpleal May 27, 2024
0604550
Add web unavailable message
adsonpleal May 28, 2024
eb6dc64
Add codeowners and allowed unpinned deps
adsonpleal May 28, 2024
c9dab29
PR requested changes
adsonpleal May 28, 2024
1627ec8
update codeowners
adsonpleal May 28, 2024
c7c3eb5
fix typo
adsonpleal May 28, 2024
bb58727
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal May 28, 2024
ee1d2f4
Remove chared preferences changes and change codeowners
adsonpleal Jun 18, 2024
01d7f5c
Remove asyncEval call
adsonpleal Jun 18, 2024
39b3990
remove commented code
adsonpleal Jun 18, 2024
00e568c
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Jun 18, 2024
21202e6
pr requested changes
adsonpleal Jun 18, 2024
e489086
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Jun 20, 2024
c32457f
Merge branch 'main' into add-shared-preferences-devtools
stuartmorgan Jun 21, 2024
c6c8068
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Jun 24, 2024
c4be218
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Jul 29, 2024
862938b
Add pre_publish.dart and extension config.yaml to shared_preferences
adsonpleal Jul 29, 2024
84c6103
fix changelog and license validations
adsonpleal Jul 29, 2024
866ef50
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Jul 31, 2024
7b1a151
Support new API and update changelog
adsonpleal Aug 1, 2024
bffbe95
also update the version in pubspec
adsonpleal Aug 1, 2024
d8af525
Merge remote-tracking branch 'origin/main' into add-shared-preference…
adsonpleal Aug 1, 2024
26f186b
revert changes to pubspec
adsonpleal Aug 1, 2024
8b005d9
add empty line back to pubspec
adsonpleal Aug 1, 2024
2dc2a33
update minor version
adsonpleal Aug 1, 2024
5b7cbbf
Fix android support
adsonpleal Aug 2, 2024
c497026
Add api switcher
adsonpleal Aug 2, 2024
bc887de
add missing license
adsonpleal Aug 2, 2024
ffd64a9
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 5, 2024
e95d285
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 5, 2024
ed56a14
resolve conflicts
adsonpleal Aug 8, 2024
8936da6
Merge branch 'add-shared-preferences-devtools' of github.com:adsonple…
adsonpleal Aug 8, 2024
966d0fc
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 9, 2024
3267c1b
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 12, 2024
1acebd4
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 12, 2024
3846f73
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Aug 15, 2024
6eb1b4f
Merge remote-tracking branch 'origin/main' into add-shared-preference…
adsonpleal Aug 15, 2024
d3fea9e
Merge branch 'flutter:main' into add-shared-preferences-devtools
adsonpleal Sep 2, 2024
af0dfca
Add devtool_eval_test to shared_preferences
adsonpleal Sep 2, 2024
107fc82
apply requested changes to devtools_eval_test
adsonpleal Sep 5, 2024
4b2f619
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Sep 5, 2024
6abec8a
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Sep 30, 2024
a45bc1c
Add license header to devtools_eval_test
adsonpleal Sep 30, 2024
c1a9c85
Update libraries
adsonpleal Sep 30, 2024
612e118
Merge branch 'add-shared-preferences-devtools' of github.com:adsonple…
adsonpleal Sep 30, 2024
ea80fce
remove error holder
adsonpleal Sep 30, 2024
1ef04f6
remove error holder
adsonpleal Sep 30, 2024
4df0393
replace text with circular progress indicator
adsonpleal Oct 1, 2024
2f8e7ea
fix broken test
adsonpleal Oct 1, 2024
b77893c
Apply suggested changes
adsonpleal Oct 7, 2024
7a3ec34
Merge remote-tracking branch 'origin/main' into add-shared-preference…
adsonpleal Nov 6, 2024
e9b913c
add devtools_extension.dart file
adsonpleal Nov 7, 2024
8362269
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 7, 2024
119df62
update pre_publish.dart
adsonpleal Nov 7, 2024
38954ba
pin vm service version
adsonpleal Nov 8, 2024
e3c39f2
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 8, 2024
9f7cdd4
Fix mockito generated code
adsonpleal Nov 8, 2024
026b225
Merge branch 'add-shared-preferences-devtools' of github.com:adsonple…
adsonpleal Nov 8, 2024
659b9cd
Change vm_service version
adsonpleal Nov 8, 2024
7bd20c1
Merge remote-tracking branch 'origin/main' into add-shared-preference…
adsonpleal Nov 11, 2024
d672329
add missing license
adsonpleal Nov 11, 2024
e6da5b8
add path as a dev_dependency
adsonpleal Nov 11, 2024
3c5cecd
change path version
adsonpleal Nov 11, 2024
c761404
requested changes
adsonpleal Nov 12, 2024
2b7d31f
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 12, 2024
c80795a
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 22, 2024
bb0b9be
rename lib/src/shared_preferences_dev_tools_extension_data.dart to li…
adsonpleal Nov 22, 2024
4f6e93b
Merge branch 'add-shared-preferences-devtools' of github.com:adsonple…
adsonpleal Nov 22, 2024
4f2d2fd
rename test file
adsonpleal Nov 22, 2024
9838731
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 27, 2024
a783959
Merge branch 'main' into add-shared-preferences-devtools
adsonpleal Nov 28, 2024
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
4 changes: 4 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ packages/local_auth/local_auth_windows/** @cbracken
packages/path_provider/path_provider_windows/** @cbracken
packages/shared_preferences/shared_preferences_windows/** @cbracken
packages/url_launcher/url_launcher_windows/** @cbracken

# - DevTools extensions
# @adsonpleal is the actual maintainer of shared_preferences_tool but is not yet a committer, so can't be listed as the owner.
packages/shared_preferences/shared_preferences_tool/** @tarrinneal
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.0

* Adds shared preferences devtools extension

## 2.3.3

* Clarifies scope of prefix handling in README.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
adsonpleal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!build
adsonpleal marked this conversation as resolved.
Show resolved Hide resolved
adsonpleal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: shared_preferences
issueTracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
kenzieschmoll marked this conversation as resolved.
Show resolved Hide resolved
version: 1.0.0
materialIconCodePoint: '0xe683'
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:flutter/foundation.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:shared_preferences_platform_interface/types.dart';

import 'shared_preferences_dev_tools_extension_data.dart';
kenzieschmoll marked this conversation as resolved.
Show resolved Hide resolved

/// Provides a persistent store for simple data.
///
/// Data is persisted to and fetched from the disk asynchronously.
Expand Down Expand Up @@ -401,3 +403,10 @@ class SharedPreferencesWithCache {
return _cacheOptions.allowList?.contains(key) ?? true;
}
}

// Include an unused import to ensure this library is included
// when running `flutter run -d chrome`.
/// Check this discussion for more info: https://github.com/flutter/packages/pull/6749/files/6eb1b4fdce1eba107294770d581713658ff971e9#discussion_r1755375409
// ignore: unused_element
final bool _fieldToKeepDevtoolsExtensionReachable =
fieldToKeepDevtoolsExtensionLibraryAlive;
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:developer' as developer;

import 'package:flutter/foundation.dart';

import '../shared_preferences.dart';

const String _eventPrefix = 'shared_preferences.';

/// A typedef for the post event function.
@visibleForTesting
typedef PostEvent = void Function(
String eventKind,
Map<String, Object?> eventData,
);

/// A helper class that provides data to the DevTools extension.
///
/// It is only visible for testing and eval.
@visibleForTesting
class SharedPreferencesDevToolsExtensionData {
/// The default constructor for [SharedPreferencesDevToolsExtensionData].
///
/// Accepts an optional [PostEvent] that should only be overwritten when testing.
const SharedPreferencesDevToolsExtensionData([
this._postEvent = developer.postEvent,
]);

final PostEvent _postEvent;

/// Requests all legacy and async keys and post an event with the result.
Future<void> requestAllKeys() async {
final SharedPreferences legacyPrefs = await SharedPreferences.getInstance();
final Set<String> legacyKeys = legacyPrefs.getKeys();
final Set<String> asyncKeys = await SharedPreferencesAsync().getKeys();

_postEvent('${_eventPrefix}all_keys', <String, List<String>>{
'asyncKeys': asyncKeys.toList(),
'legacyKeys': legacyKeys.toList(),
});
}

/// Requests the value for a given key and posts an event with the result.
Future<void> requestValue(String key, bool legacy) async {
final Object? value;
if (legacy) {
final SharedPreferences legacyPrefs =
await SharedPreferences.getInstance();
value = legacyPrefs.get(key);
} else {
value = await SharedPreferencesAsync().getAll(allowList: <String>{
key
}).then((Map<String, Object?> map) => map.values.firstOrNull);
}

_postEvent('${_eventPrefix}value', <String, Object?>{
'value': value,
// It is safe to use `runtimeType` here. This code
// will only ever run in debug mode.
'kind': value.runtimeType.toString(),
});
}

/// Requests the value change for the given key and posts an empty event when finished.
Future<void> requestValueChange(
String key,
String serializedValue,
String kind,
bool legacy,
) async {
final Object? value = jsonDecode(serializedValue);
if (legacy) {
final SharedPreferences legacyPrefs =
await SharedPreferences.getInstance();
// we need to check the kind because sometimes a double
// gets interpreted as an int. If this was not an issue
// we'd only need to do a simple pattern matching on value.
switch (kind) {
case 'int':
await legacyPrefs.setInt(key, value! as int);
case 'bool':
await legacyPrefs.setBool(key, value! as bool);
case 'double':
await legacyPrefs.setDouble(key, value! as double);
case 'String':
await legacyPrefs.setString(key, value! as String);
case 'List<String>':
await legacyPrefs.setStringList(
key,
(value! as List<Object?>).cast(),
);
}
} else {
final SharedPreferencesAsync prefs = SharedPreferencesAsync();
// we need to check the kind because sometimes a double
// gets interpreted as an int. If this was not an issue
// we'd only need to do a simple pattern matching on value.
switch (kind) {
case 'int':
await prefs.setInt(key, value! as int);
case 'bool':
await prefs.setBool(key, value! as bool);
case 'double':
await prefs.setDouble(key, value! as double);
case 'String':
await prefs.setString(key, value! as String);
case 'List<String>':
await prefs.setStringList(
key,
(value! as List<Object?>).cast(),
);
}
}
_postEvent('${_eventPrefix}change_value', <String, Object?>{});
}

/// Requests a key removal and posts an empty event when removed.
Future<void> requestRemoveKey(String key, bool legacy) async {
if (legacy) {
final SharedPreferences legacyPrefs =
await SharedPreferences.getInstance();
await legacyPrefs.remove(key);
} else {
await SharedPreferencesAsync().remove(key);
}
_postEvent('${_eventPrefix}remove', <String, Object?>{});
}
}

/// Include a variable to keep the library alive in web builds.
/// It must be a `final` variable.
/// Check this discussion for more info: https://github.com/flutter/packages/pull/6749/files/6eb1b4fdce1eba107294770d581713658ff971e9#discussion_r1755375409
// ignore: prefer_const_declarations
final bool fieldToKeepDevtoolsExtensionLibraryAlive = false;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_platform_interface/types.dart';

import 'shared_preferences_dev_tools_extension_data.dart';

/// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
/// a persistent store for simple data.
///
Expand Down Expand Up @@ -285,3 +287,10 @@ Either update the implementation to support setPrefix, or do not call setPrefix.
_completer = null;
}
}

// Include an unused import to ensure this library is included
// when running `flutter run -d chrome`.
/// Check this discussion for more info: https://github.com/flutter/packages/pull/6749/files/6eb1b4fdce1eba107294770d581713658ff971e9#discussion_r1755375409
// ignore: unused_element
final bool _fieldToKeepDevtoolsExtensionReachable =
fieldToKeepDevtoolsExtensionLibraryAlive;
6 changes: 3 additions & 3 deletions packages/shared_preferences/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name: shared_preferences
description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
description: Flutter plugin for reading and writing simple key-value pairs. Wraps NSUserDefaults on iOS and SharedPreferences on Android.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.3.3
version: 2.4.0

environment:
sdk: ^3.4.0
Expand Down Expand Up @@ -40,6 +39,7 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
path: ^1.9.0

topics:
- persistence
Expand Down
Loading