-
Notifications
You must be signed in to change notification settings - Fork 2
/
icloud_kv_storage_method_channel.dart
77 lines (58 loc) · 1.85 KB
/
icloud_kv_storage_method_channel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'icloud_kv_storage_platform_interface.dart';
typedef GetNativeCallBackFuture = Future Function(String route, Map<String, dynamic> params);
/// An implementation of [IcloudKvStoragePlatform] that uses method channels.
class MethodChannelICloudKvStorage extends ICloudKvStoragePlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('icloud_kv_storage');
static const String _prefix = 'flutter.';
@override
String getRealKey(String key) {
return "$_prefix$key";
}
@override
void setNativeCallBack({required GetNativeCallBackFuture onCallBack}) {
Future<dynamic> platformCallHandler(MethodCall call) async {
return await onCallBack(call.method, Map<String, dynamic>.from(call.arguments));
}
methodChannel.setMethodCallHandler(platformCallHandler);
}
@override
Future<void> write<T>({required String key, required T value}) async {
if (!Platform.isIOS && !Platform.isMacOS) {
return;
}
if (key.isEmpty) {
return;
}
await methodChannel.invokeMethod('SAVE_VALUE',
{"key": "$_prefix$key", "value": value});
}
@override
Future<T?> read<T>({required String key}) async {
if (!Platform.isIOS && !Platform.isMacOS) {
return null;
}
if (key.isEmpty) {
return null;
}
T? value = await (methodChannel
.invokeMethod('GET_VALUE', {"key": "$_prefix$key"}));
return value;
}
@override
Future<void> delete({required String key}) async {
if (!Platform.isIOS && !Platform.isMacOS) {
return;
}
if (key.isEmpty) {
return;
}
await methodChannel.invokeMethod('DELETE_VALUE', {
"key": "$_prefix$key",
});
}
}