-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-land [shared_preferences] Add shared preferences devtool (#8531)
Re-re-lands #8494 with a change to remove `--ignored` from the `git status` check in the repo tooling `publish` command, which is what caused the `release` failure in the last landing. `pub publish` has ignored files in `.gitignore` for years, so there's no longer any value in checking that they don't exist. I've verified that `publish --dry-run` passes locally with this change. Server-only check failures could still potentially cause another revert, but those can't be found other than the hard way.
- Loading branch information
1 parent
91da100
commit b66b6d6
Showing
40 changed files
with
4,582 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/shared_preferences/shared_preferences/extension/devtools/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
1 change: 1 addition & 0 deletions
1
packages/shared_preferences/shared_preferences/extension/devtools/.pubignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!build |
4 changes: 4 additions & 0 deletions
4
packages/shared_preferences/shared_preferences/extension/devtools/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
version: 1.0.0 | ||
materialIconCodePoint: '0xe683' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ed_preferences/shared_preferences/lib/src/shared_preferences_devtools_extension_data.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.