Skip to content

Commit

Permalink
add version() for flutter & fix reKey() failtrue after fullWriteBack()
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Nov 27, 2020
1 parent a7bc544 commit 5071368
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Core/MMKV_IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ bool MMKV::reKey(const string &cryptKey) {
// change encryption key
MMKVInfo("reKey with new aes key");
auto newCrypt = new AESCrypt(cryptKey.data(), cryptKey.length());
m_hasFullWriteback = false;
ret = fullWriteback(newCrypt);
if (ret) {
delete m_crypter;
Expand All @@ -965,6 +966,7 @@ bool MMKV::reKey(const string &cryptKey) {
} else {
// decryption to plain text
MMKVInfo("reKey to no aes key");
m_hasFullWriteback = false;
ret = fullWriteback(InvalidCryptPtr);
if (ret) {
delete m_crypter;
Expand All @@ -978,6 +980,7 @@ bool MMKV::reKey(const string &cryptKey) {
if (cryptKey.length() > 0) {
// transform plain text to encrypted text
MMKVInfo("reKey to a aes key");
m_hasFullWriteback = false;
auto newCrypt = new AESCrypt(cryptKey.data(), cryptKey.length());
ret = fullWriteback(newCrypt);
if (ret) {
Expand Down
4 changes: 2 additions & 2 deletions flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you already include MMKV native lib in your App, you need to upgrade to versi
#### iOS
To avoid conflict of the native lib name 'libMMKV.so' on iOS, we need to **change the plugin name 'mmkv' to 'mmkvflutter'**.

* For a **purely flutter** App, add this function `fix_mmkv_plugin_name()` to `ios/Podfile`. Then call it **before** calling any `flutter_xxx()` functions:
* For a **purely flutter** App, add this function `fix_mmkv_plugin_name()` to `ios/Podfile`, invoke it **before** calling any `flutter_xxx()` functions. Run `pod install` and we are all set.

```ruby
def fix_mmkv_plugin_name(flutter_application_path)
Expand All @@ -60,7 +60,7 @@ end
fix_mmkv_plugin_name(File.dirname(File.realpath(__FILE__)))
```

* For using **[flutter as a module](https://flutter.dev/docs/development/add-to-app/ios/project-setup#embed-the-flutter-module-in-your-existing-application)** to your existing iOS App, add the function `fix_mmkv_plugin_name()` above to your iOS App's `Podfile`. Then call it **before** calling any `flutter_xxx()` functions:
* For using **[flutter as a module](https://flutter.dev/docs/development/add-to-app/ios/project-setup#embed-the-flutter-module-in-your-existing-application)** to your existing iOS App, add the function `fix_mmkv_plugin_name()` above to your iOS App's `Podfile`, invoke it **before** calling any `flutter_xxx()` functions. Run `pod install` and we are all set.

```ruby
def fix_mmkv_plugin_name(flutter_application_path)
Expand Down
2 changes: 1 addition & 1 deletion flutter/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ SPEC CHECKSUMS:
mmkvflutter: d4a166f8ab6b4586cca40948feec6c9f65a34ad8
path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c

PODFILE CHECKSUM: ef36e106ef9d8d594b1116eba84859922c477694
PODFILE CHECKSUM: 56a04502099a98f6f9fd2c6f8055159cd69b1375

COCOAPODS: 1.10.0
119 changes: 105 additions & 14 deletions flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,30 @@ class _MyAppState extends State<MyApp> {
if (!mounted) return;

setState(() {
funtionalTest();
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('MMKV Plugin example app'),
),
body: Center(
child: Column(children: <Widget>[
Text('MMKV Version: ${MMKV.version}\n'),
TextButton(onPressed:() {
funtionalTest();
}, child: Text('functional test', style: TextStyle(fontSize: 18))),
TextButton(onPressed:() {
testReKey();
}, child: Text('encryption test', style: TextStyle(fontSize: 18))),
])),
),
);
}

void funtionalTest() {
var mmkv = MMKV("test");
mmkv.encodeBool('bool', true);
Expand Down Expand Up @@ -105,18 +125,89 @@ class _MyAppState extends State<MyApp> {
// mmkv.close();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(children: <Widget>[
// Text('Running on: $_platformVersion\n'),
])),
),
);
MMKV testMMKV(String mmapID, String cryptKey, bool decodeOnly, String rootPath) {
final mmkv = MMKV(mmapID, cryptKey: cryptKey, rootDir: rootPath);
if (mmkv == null) {
return null;
}

if (!decodeOnly) {
mmkv.encodeBool('bool', true);
}
print('bool = ${mmkv.decodeBool('bool')}');

if (!decodeOnly) {
mmkv.encodeInt32('int32', (1 << 31) - 1);
}
print('max int32 = ${mmkv.decodeInt32('int32')}');

if (!decodeOnly) {
mmkv.encodeInt32('int32', 0 - (1 << 31));
}
print('min int32 = ${mmkv.decodeInt32('int32')}');

if (!decodeOnly) {
mmkv.encodeInt('int', (1 << 63) - 1);
}
print('max int = ${mmkv.decodeInt('int')}');

if (!decodeOnly) {
mmkv.encodeInt('int', 0 - (1 << 63));
}
print('min int = ${mmkv.decodeInt('int')}');

if (!decodeOnly) {
mmkv.encodeDouble('double', double.maxFinite);
}
print('max double = ${mmkv.decodeDouble('double')}');

if (!decodeOnly) {
mmkv.encodeDouble('double', double.minPositive);
}
print('min positive double = ${mmkv.decodeDouble('double')}');

String str = 'Hello dart from MMKV';
if (!decodeOnly) {
mmkv.encodeString('string', str);
}
print('string = ${mmkv.decodeString('string')}');

str += ' with bytes';
var bytes = MMBuffer.fromList(Utf8Encoder().convert(str));
if (!decodeOnly) {
mmkv.encodeBytes('bytes', bytes);
}
bytes.destroy();
bytes = mmkv.decodeBytes('bytes');
print('bytes = ${Utf8Decoder().convert(bytes.asList())}');
bytes.destroy();

print('contains "bool": ${mmkv.containsKey('bool')}');
mmkv.removeValue('bool');
print('after remove, contains "bool": ${mmkv.containsKey('bool')}');
mmkv.removeValues(['int32', 'int']);
print('all keys: ${mmkv.allKeys}');

return mmkv;
}

void testReKey() {
final mmapID = 'testAES_reKey1';
MMKV kv = testMMKV(mmapID, null, false, null);
if (kv == null) {
return;
}

kv.reKey("Key_seq_1");
kv.clearMemoryCache();
testMMKV(mmapID, 'Key_seq_1', true, null);

kv.reKey('Key_seq_2');
kv.clearMemoryCache();
testMMKV(mmapID, 'Key_seq_2', true, null);

kv.reKey(null);
kv.clearMemoryCache();
testMMKV(mmapID, null, true, null);
}
}
4 changes: 4 additions & 0 deletions flutter/ios/Classes/flutter-bridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ MMKV_EXPORT int32_t pageSize() {
return static_cast<int32_t>([MMKV pageSize]);
}

MMKV_EXPORT const char *version() {
return [MMKV version].UTF8String;
}

MMKV_EXPORT void trim(const void *handle) {
MMKV *kv = (__bridge MMKV *) handle;
if (kv) {
Expand Down
8 changes: 8 additions & 0 deletions flutter/lib/mmkv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ class MMKV {
return _pageSize();
}

static String get version {
return _pointer2String(_version());
}

/// Trim the file size to minimal.
///
/// * MMKV's size won't reduce after deleting key-values.
Expand Down Expand Up @@ -810,6 +814,10 @@ final int Function() _pageSize = _nativeLib
.lookup<NativeFunction<Int32 Function()>>("pageSize")
.asFunction();

final Pointer<Utf8> Function() _version = _nativeLib
.lookup<NativeFunction<Pointer<Utf8> Function()>>("version")
.asFunction();

final void Function(Pointer<Void>) _trim = _nativeLib
.lookup<NativeFunction<Void Function(Pointer<Void>)>>("trim")
.asFunction();
Expand Down

0 comments on commit 5071368

Please sign in to comment.