Skip to content

Commit 8b006c1

Browse files
committed
feat: use hive taken replace the shared_preferences for AsyncStorage module.
1 parent db71cd4 commit 8b006c1

File tree

10 files changed

+68
-59
lines changed

10 files changed

+68
-59
lines changed

webf/example/ios/Flutter/AppFrameworkInfo.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<key>CFBundleVersion</key>
2222
<string>1.0</string>
2323
<key>MinimumOSVersion</key>
24-
<string>11.0</string>
24+
<string>12.0</string>
2525
</dict>
2626
</plist>

webf/example/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Uncomment this line to define a global platform for your project
2-
# platform :ios, '11.0'
2+
# platform :ios, '12.0'
33

44
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
55
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

webf/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
97C146E61CF9000F007C117D /* Project object */ = {
156156
isa = PBXProject;
157157
attributes = {
158-
LastUpgradeCheck = 1300;
158+
LastUpgradeCheck = 1510;
159159
ORGANIZATIONNAME = "";
160160
TargetAttributes = {
161161
97C146ED1CF9000F007C117D = {
@@ -342,7 +342,7 @@
342342
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
343343
GCC_WARN_UNUSED_FUNCTION = YES;
344344
GCC_WARN_UNUSED_VARIABLE = YES;
345-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
345+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
346346
MTL_ENABLE_DEBUG_INFO = NO;
347347
SDKROOT = iphoneos;
348348
SUPPORTED_PLATFORMS = iphoneos;
@@ -358,7 +358,7 @@
358358
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
359359
CLANG_ENABLE_MODULES = YES;
360360
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
361-
DEVELOPMENT_TEAM = "";
361+
DEVELOPMENT_TEAM = ANRD47DNBX;
362362
ENABLE_BITCODE = NO;
363363
FRAMEWORK_SEARCH_PATHS = (
364364
"$(inherited)",
@@ -429,7 +429,7 @@
429429
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
430430
GCC_WARN_UNUSED_FUNCTION = YES;
431431
GCC_WARN_UNUSED_VARIABLE = YES;
432-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
432+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
433433
MTL_ENABLE_DEBUG_INFO = YES;
434434
ONLY_ACTIVE_ARCH = YES;
435435
SDKROOT = iphoneos;
@@ -478,7 +478,7 @@
478478
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
479479
GCC_WARN_UNUSED_FUNCTION = YES;
480480
GCC_WARN_UNUSED_VARIABLE = YES;
481-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
481+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
482482
MTL_ENABLE_DEBUG_INFO = NO;
483483
SDKROOT = iphoneos;
484484
SUPPORTED_PLATFORMS = iphoneos;
@@ -496,7 +496,7 @@
496496
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
497497
CLANG_ENABLE_MODULES = YES;
498498
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
499-
DEVELOPMENT_TEAM = "";
499+
DEVELOPMENT_TEAM = ANRD47DNBX;
500500
ENABLE_BITCODE = NO;
501501
FRAMEWORK_SEARCH_PATHS = (
502502
"$(inherited)",
@@ -529,7 +529,7 @@
529529
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
530530
CLANG_ENABLE_MODULES = YES;
531531
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
532-
DEVELOPMENT_TEAM = "";
532+
DEVELOPMENT_TEAM = ANRD47DNBX;
533533
ENABLE_BITCODE = NO;
534534
FRAMEWORK_SEARCH_PATHS = (
535535
"$(inherited)",

webf/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1510"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -27,8 +27,6 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30-
<Testables>
31-
</Testables>
3230
<MacroExpansion>
3331
<BuildableReference
3432
BuildableIdentifier = "primary"
@@ -38,11 +36,11 @@
3836
ReferencedContainer = "container:Runner.xcodeproj">
3937
</BuildableReference>
4038
</MacroExpansion>
41-
<AdditionalOptions>
42-
</AdditionalOptions>
39+
<Testables>
40+
</Testables>
4341
</TestAction>
4442
<LaunchAction
45-
buildConfiguration = "Debug"
43+
buildConfiguration = "Release"
4644
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4745
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4846
launchStyle = "0"
@@ -61,8 +59,6 @@
6159
ReferencedContainer = "container:Runner.xcodeproj">
6260
</BuildableReference>
6361
</BuildableProductRunnable>
64-
<AdditionalOptions>
65-
</AdditionalOptions>
6662
</LaunchAction>
6763
<ProfileAction
6864
buildConfiguration = "Profile"

webf/example/ios/Runner/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UIKit
22
import Flutter
33

4-
@UIApplicationMain
4+
@main
55
@objc class AppDelegate: FlutterAppDelegate {
66
override func application(
77
_ application: UIApplication,

webf/example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import FlutterMacOS
66
import Foundation
77

8-
import shared_preferences_foundation
98
import webf
109

1110
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
12-
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
1311
WebFPlugin.register(with: registry.registrar(forPlugin: "WebFPlugin"))
1412
}

webf/example/macos/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
isa = PBXProject;
203203
attributes = {
204204
LastSwiftUpdateCheck = 0920;
205-
LastUpgradeCheck = 1510;
205+
LastUpgradeCheck = 1430;
206206
ORGANIZATIONNAME = "The Flutter Authors";
207207
TargetAttributes = {
208208
33CC10EC2044A3C60003C045 = {

webf/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1510"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

webf/lib/src/module/async_storage.dart

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,73 @@
44
*/
55

66
import 'dart:async';
7-
7+
import 'package:archive/archive.dart';
8+
import 'package:path/path.dart' as path;
9+
import 'package:hive/hive.dart';
10+
import 'package:webf/foundation.dart';
811
import 'package:webf/src/module/module_manager.dart';
9-
import 'package:shared_preferences/shared_preferences.dart';
1012

1113
class AsyncStorageModule extends BaseModule {
1214
@override
1315
String get name => 'AsyncStorage';
1416

15-
static Future<SharedPreferences>? _prefs;
17+
static String getBoxKey(ModuleManager moduleManager) {
18+
String origin = moduleManager.controller.origin + '_async';
19+
int fileCheckSum = getCrc32(origin.codeUnits);
20+
return '_webf_$fileCheckSum';
21+
}
1622

1723
AsyncStorageModule(ModuleManager? moduleManager) : super(moduleManager);
1824

19-
/// Loads and parses the [SharedPreferences] for this app from disk.
20-
///
21-
/// Because this is reading from disk, it shouldn't be awaited in
22-
/// performance-sensitive blocks.
23-
static Future<SharedPreferences> _getPrefs() {
24-
_prefs ??= SharedPreferences.getInstance();
25-
return _prefs!;
25+
late LazyBox<String> _lazyBox;
26+
27+
@override
28+
Future<void> initialize() async {
29+
final key = getBoxKey(moduleManager!);
30+
final tmpPath = await getWebFTemporaryPath();
31+
final storagePath = path.join(tmpPath, 'AsyncStorage');
32+
try {
33+
_lazyBox = await Hive.openLazyBox(key, path: storagePath);
34+
} catch (e) {
35+
// Try again to avoid resources are temporarily unavailable.
36+
_lazyBox = await Hive.openLazyBox(key, path: storagePath);
37+
}
2638
}
2739

28-
static Future<bool> setItem(String key, String value) async {
29-
final SharedPreferences prefs = await _getPrefs();
30-
return prefs.setString(key, value);
40+
Future<bool> setItem(String key, String value) async {
41+
try {
42+
await _lazyBox.put(key, value);
43+
return true;
44+
} catch (e, stack) {
45+
return false;
46+
}
3147
}
3248

33-
static Future<String?> getItem(String key) async {
34-
final SharedPreferences prefs = await _getPrefs();
35-
return prefs.getString(key);
49+
Future<String?> getItem(String key) async {
50+
return _lazyBox.get(key);
3651
}
3752

38-
static Future<bool> removeItem(String key) async {
39-
final SharedPreferences prefs = await _getPrefs();
40-
return prefs.remove(key);
53+
Future<bool> removeItem(String key) async {
54+
try {
55+
_lazyBox.delete(key);
56+
return true;
57+
} catch (e, stack) {
58+
return false;
59+
}
4160
}
4261

43-
static Future<Set<String>> getAllKeys() async {
44-
final SharedPreferences prefs = await _getPrefs();
45-
return prefs.getKeys();
62+
Future<Set<dynamic>> getAllKeys() async {
63+
Set<dynamic> keys = _lazyBox.keys.toSet();
64+
return keys;
4665
}
4766

48-
static Future<bool> clear() async {
49-
final SharedPreferences prefs = await _getPrefs();
50-
return prefs.clear();
67+
Future<bool> clear() async {
68+
await _lazyBox.clear();
69+
return true;
5170
}
5271

53-
static Future<int> length() async {
54-
final SharedPreferences prefs = await _getPrefs();
55-
final Set<String> keys = prefs.getKeys();
56-
return keys.length;
72+
Future<int> length() async {
73+
return _lazyBox.length;
5774
}
5875

5976
@override
@@ -63,7 +80,7 @@ class AsyncStorageModule extends BaseModule {
6380
String invoke(String method, params, InvokeModuleCallback callback) {
6481
switch (method) {
6582
case 'getItem':
66-
AsyncStorageModule.getItem(params).then((String? value) {
83+
getItem(params).then((String? value) {
6784
callback(data: value ?? '');
6885
}).catchError((e, stack) {
6986
callback(error: '$e\n$stack');
@@ -72,36 +89,36 @@ class AsyncStorageModule extends BaseModule {
7289
case 'setItem':
7390
String key = params[0];
7491
String value = params[1];
75-
AsyncStorageModule.setItem(key, value).then((bool isSuccess) {
92+
setItem(key, value).then((bool isSuccess) {
7693
callback(data: isSuccess.toString());
7794
}).catchError((e, stack) {
7895
callback(error: 'Error: $e\n$stack');
7996
});
8097
break;
8198
case 'removeItem':
82-
AsyncStorageModule.removeItem(params).then((bool isSuccess) {
99+
removeItem(params).then((bool isSuccess) {
83100
callback(data: isSuccess.toString());
84101
}).catchError((e, stack) {
85102
callback(error: 'Error: $e\n$stack');
86103
});
87104
break;
88105
case 'getAllKeys':
89-
AsyncStorageModule.getAllKeys().then((Set<String> set) {
106+
getAllKeys().then((Set<dynamic> set) {
90107
List<String> list = List.from(set);
91108
callback(data: list);
92109
}).catchError((e, stack) {
93110
callback(error: 'Error: $e\n$stack');
94111
});
95112
break;
96113
case 'clear':
97-
AsyncStorageModule.clear().then((bool isSuccess) {
114+
clear().then((bool isSuccess) {
98115
callback(data: isSuccess.toString());
99116
}).catchError((e, stack) {
100117
callback(error: 'Error: $e\n$stack');
101118
});
102119
break;
103120
case 'length':
104-
AsyncStorageModule.length().then((int length) {
121+
length().then((int length) {
105122
callback(data: length);
106123
}).catchError((e, stack) {
107124
callback(error: 'Error: $e\n$stack');

webf/pubspec.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ dependencies:
1616
meta: ^1.7.0 # Pure dart module.
1717
ffi: ^2.0.1 # Pure dart module.
1818
hive: ^2.2.3
19-
characters: ^1.2.0
2019
collection: ^1.16.0
2120
async: ^2.8.2 # Pure dart module.
2221
quiver: ^3.2.1 # Pure dart module.
2322
vector_math: ^2.1.2 # Pure dart module.
2423
source_span: ^1.9.0 # Pure dart module.
25-
shared_preferences: 2.2.0 # No AndroidX used.
2624
archive: ^3.3.7 # Pure dart module.
2725
web_socket_channel: ^2.2.0
2826

0 commit comments

Comments
 (0)