|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:developer' as developer; |
| 7 | + |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | + |
| 10 | +import '../shared_preferences.dart'; |
| 11 | + |
| 12 | +const String _eventPrefix = 'shared_preferences.'; |
| 13 | + |
| 14 | +/// A typedef for the post event function. |
| 15 | +@visibleForTesting |
| 16 | +typedef PostEvent = void Function( |
| 17 | + String eventKind, |
| 18 | + Map<String, Object?> eventData, |
| 19 | +); |
| 20 | + |
| 21 | +/// A helper class that provides data to the DevTools extension. |
| 22 | +/// |
| 23 | +/// It is only visible for testing and eval. |
| 24 | +@visibleForTesting |
| 25 | +class SharedPreferencesDevToolsExtensionData { |
| 26 | + /// The default constructor for [SharedPreferencesDevToolsExtensionData]. |
| 27 | + /// |
| 28 | + /// Accepts an optional [PostEvent] that should only be overwritten when testing. |
| 29 | + const SharedPreferencesDevToolsExtensionData([ |
| 30 | + this._postEvent = developer.postEvent, |
| 31 | + ]); |
| 32 | + |
| 33 | + final PostEvent _postEvent; |
| 34 | + |
| 35 | + /// Requests all legacy and async keys and post an event with the result. |
| 36 | + Future<void> requestAllKeys() async { |
| 37 | + final SharedPreferences legacyPrefs = await SharedPreferences.getInstance(); |
| 38 | + final Set<String> legacyKeys = legacyPrefs.getKeys(); |
| 39 | + final Set<String> asyncKeys = await SharedPreferencesAsync().getKeys(); |
| 40 | + |
| 41 | + _postEvent('${_eventPrefix}all_keys', <String, List<String>>{ |
| 42 | + 'asyncKeys': asyncKeys.toList(), |
| 43 | + 'legacyKeys': legacyKeys.toList(), |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /// Requests the value for a given key and posts an event with the result. |
| 48 | + Future<void> requestValue(String key, bool legacy) async { |
| 49 | + final Object? value; |
| 50 | + if (legacy) { |
| 51 | + final SharedPreferences legacyPrefs = |
| 52 | + await SharedPreferences.getInstance(); |
| 53 | + value = legacyPrefs.get(key); |
| 54 | + } else { |
| 55 | + value = await SharedPreferencesAsync().getAll(allowList: <String>{ |
| 56 | + key |
| 57 | + }).then((Map<String, Object?> map) => map.values.firstOrNull); |
| 58 | + } |
| 59 | + |
| 60 | + _postEvent('${_eventPrefix}value', <String, Object?>{ |
| 61 | + 'value': value, |
| 62 | + // It is safe to use `runtimeType` here. This code |
| 63 | + // will only ever run in debug mode. |
| 64 | + 'kind': value.runtimeType.toString(), |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /// Requests the value change for the given key and posts an empty event when finished. |
| 69 | + Future<void> requestValueChange( |
| 70 | + String key, |
| 71 | + String serializedValue, |
| 72 | + String kind, |
| 73 | + bool legacy, |
| 74 | + ) async { |
| 75 | + final Object? value = jsonDecode(serializedValue); |
| 76 | + if (legacy) { |
| 77 | + final SharedPreferences legacyPrefs = |
| 78 | + await SharedPreferences.getInstance(); |
| 79 | + // we need to check the kind because sometimes a double |
| 80 | + // gets interpreted as an int. If this was not an issue |
| 81 | + // we'd only need to do a simple pattern matching on value. |
| 82 | + switch (kind) { |
| 83 | + case 'int': |
| 84 | + await legacyPrefs.setInt(key, value! as int); |
| 85 | + case 'bool': |
| 86 | + await legacyPrefs.setBool(key, value! as bool); |
| 87 | + case 'double': |
| 88 | + await legacyPrefs.setDouble(key, value! as double); |
| 89 | + case 'String': |
| 90 | + await legacyPrefs.setString(key, value! as String); |
| 91 | + case 'List<String>': |
| 92 | + await legacyPrefs.setStringList( |
| 93 | + key, |
| 94 | + (value! as List<Object?>).cast(), |
| 95 | + ); |
| 96 | + } |
| 97 | + } else { |
| 98 | + final SharedPreferencesAsync prefs = SharedPreferencesAsync(); |
| 99 | + // we need to check the kind because sometimes a double |
| 100 | + // gets interpreted as an int. If this was not an issue |
| 101 | + // we'd only need to do a simple pattern matching on value. |
| 102 | + switch (kind) { |
| 103 | + case 'int': |
| 104 | + await prefs.setInt(key, value! as int); |
| 105 | + case 'bool': |
| 106 | + await prefs.setBool(key, value! as bool); |
| 107 | + case 'double': |
| 108 | + await prefs.setDouble(key, value! as double); |
| 109 | + case 'String': |
| 110 | + await prefs.setString(key, value! as String); |
| 111 | + case 'List<String>': |
| 112 | + await prefs.setStringList( |
| 113 | + key, |
| 114 | + (value! as List<Object?>).cast(), |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + _postEvent('${_eventPrefix}change_value', <String, Object?>{}); |
| 119 | + } |
| 120 | + |
| 121 | + /// Requests a key removal and posts an empty event when removed. |
| 122 | + Future<void> requestRemoveKey(String key, bool legacy) async { |
| 123 | + if (legacy) { |
| 124 | + final SharedPreferences legacyPrefs = |
| 125 | + await SharedPreferences.getInstance(); |
| 126 | + await legacyPrefs.remove(key); |
| 127 | + } else { |
| 128 | + await SharedPreferencesAsync().remove(key); |
| 129 | + } |
| 130 | + _postEvent('${_eventPrefix}remove', <String, Object?>{}); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/// Include a variable to keep the library alive in web builds. |
| 135 | +/// It must be a `final` variable. |
| 136 | +/// Check this discussion for more info: https://github.com/flutter/packages/pull/6749/files/6eb1b4fdce1eba107294770d581713658ff971e9#discussion_r1755375409 |
| 137 | +// ignore: prefer_const_declarations |
| 138 | +final bool fieldToKeepDevtoolsExtensionLibraryAlive = false; |
0 commit comments