Skip to content

Commit

Permalink
feat: use hive taken replace the shared_preferences for AsyncStorage …
Browse files Browse the repository at this point in the history
…module.
  • Loading branch information
andycall committed Aug 14, 2024
1 parent db71cd4 commit 8b006c1
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 59 deletions.
2 changes: 1 addition & 1 deletion webf/example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion webf/example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
14 changes: 7 additions & 7 deletions webf/example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -342,7 +342,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -358,7 +358,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = ANRD47DNBX;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -429,7 +429,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -478,7 +478,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -496,7 +496,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = ANRD47DNBX;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -529,7 +529,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = ANRD47DNBX;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,11 +36,11 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -61,8 +59,6 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Profile"
Expand Down
2 changes: 1 addition & 1 deletion webf/example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
2 changes: 0 additions & 2 deletions webf/example/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import FlutterMacOS
import Foundation

import shared_preferences_foundation
import webf

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
WebFPlugin.register(with: registry.registrar(forPlugin: "WebFPlugin"))
}
2 changes: 1 addition & 1 deletion webf/example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1510;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "The Flutter Authors";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1510"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
87 changes: 52 additions & 35 deletions webf/lib/src/module/async_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,73 @@
*/

import 'dart:async';

import 'package:archive/archive.dart';
import 'package:path/path.dart' as path;
import 'package:hive/hive.dart';
import 'package:webf/foundation.dart';
import 'package:webf/src/module/module_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AsyncStorageModule extends BaseModule {
@override
String get name => 'AsyncStorage';

static Future<SharedPreferences>? _prefs;
static String getBoxKey(ModuleManager moduleManager) {
String origin = moduleManager.controller.origin + '_async';
int fileCheckSum = getCrc32(origin.codeUnits);
return '_webf_$fileCheckSum';
}

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

/// Loads and parses the [SharedPreferences] for this app from disk.
///
/// Because this is reading from disk, it shouldn't be awaited in
/// performance-sensitive blocks.
static Future<SharedPreferences> _getPrefs() {
_prefs ??= SharedPreferences.getInstance();
return _prefs!;
late LazyBox<String> _lazyBox;

@override
Future<void> initialize() async {
final key = getBoxKey(moduleManager!);
final tmpPath = await getWebFTemporaryPath();
final storagePath = path.join(tmpPath, 'AsyncStorage');
try {
_lazyBox = await Hive.openLazyBox(key, path: storagePath);
} catch (e) {
// Try again to avoid resources are temporarily unavailable.
_lazyBox = await Hive.openLazyBox(key, path: storagePath);
}
}

static Future<bool> setItem(String key, String value) async {
final SharedPreferences prefs = await _getPrefs();
return prefs.setString(key, value);
Future<bool> setItem(String key, String value) async {
try {
await _lazyBox.put(key, value);
return true;
} catch (e, stack) {
return false;
}
}

static Future<String?> getItem(String key) async {
final SharedPreferences prefs = await _getPrefs();
return prefs.getString(key);
Future<String?> getItem(String key) async {
return _lazyBox.get(key);
}

static Future<bool> removeItem(String key) async {
final SharedPreferences prefs = await _getPrefs();
return prefs.remove(key);
Future<bool> removeItem(String key) async {
try {
_lazyBox.delete(key);
return true;
} catch (e, stack) {
return false;
}
}

static Future<Set<String>> getAllKeys() async {
final SharedPreferences prefs = await _getPrefs();
return prefs.getKeys();
Future<Set<dynamic>> getAllKeys() async {
Set<dynamic> keys = _lazyBox.keys.toSet();
return keys;
}

static Future<bool> clear() async {
final SharedPreferences prefs = await _getPrefs();
return prefs.clear();
Future<bool> clear() async {
await _lazyBox.clear();
return true;
}

static Future<int> length() async {
final SharedPreferences prefs = await _getPrefs();
final Set<String> keys = prefs.getKeys();
return keys.length;
Future<int> length() async {
return _lazyBox.length;
}

@override
Expand All @@ -63,7 +80,7 @@ class AsyncStorageModule extends BaseModule {
String invoke(String method, params, InvokeModuleCallback callback) {
switch (method) {
case 'getItem':
AsyncStorageModule.getItem(params).then((String? value) {
getItem(params).then((String? value) {
callback(data: value ?? '');
}).catchError((e, stack) {
callback(error: '$e\n$stack');
Expand All @@ -72,36 +89,36 @@ class AsyncStorageModule extends BaseModule {
case 'setItem':
String key = params[0];
String value = params[1];
AsyncStorageModule.setItem(key, value).then((bool isSuccess) {
setItem(key, value).then((bool isSuccess) {
callback(data: isSuccess.toString());
}).catchError((e, stack) {
callback(error: 'Error: $e\n$stack');
});
break;
case 'removeItem':
AsyncStorageModule.removeItem(params).then((bool isSuccess) {
removeItem(params).then((bool isSuccess) {
callback(data: isSuccess.toString());
}).catchError((e, stack) {
callback(error: 'Error: $e\n$stack');
});
break;
case 'getAllKeys':
AsyncStorageModule.getAllKeys().then((Set<String> set) {
getAllKeys().then((Set<dynamic> set) {
List<String> list = List.from(set);
callback(data: list);
}).catchError((e, stack) {
callback(error: 'Error: $e\n$stack');
});
break;
case 'clear':
AsyncStorageModule.clear().then((bool isSuccess) {
clear().then((bool isSuccess) {
callback(data: isSuccess.toString());
}).catchError((e, stack) {
callback(error: 'Error: $e\n$stack');
});
break;
case 'length':
AsyncStorageModule.length().then((int length) {
length().then((int length) {
callback(data: length);
}).catchError((e, stack) {
callback(error: 'Error: $e\n$stack');
Expand Down
2 changes: 0 additions & 2 deletions webf/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ dependencies:
meta: ^1.7.0 # Pure dart module.
ffi: ^2.0.1 # Pure dart module.
hive: ^2.2.3
characters: ^1.2.0
collection: ^1.16.0
async: ^2.8.2 # Pure dart module.
quiver: ^3.2.1 # Pure dart module.
vector_math: ^2.1.2 # Pure dart module.
source_span: ^1.9.0 # Pure dart module.
shared_preferences: 2.2.0 # No AndroidX used.
archive: ^3.3.7 # Pure dart module.
web_socket_channel: ^2.2.0

Expand Down

0 comments on commit 8b006c1

Please sign in to comment.