Skip to content

Commit

Permalink
fix: Support storing Long on Android (#280)
Browse files Browse the repository at this point in the history
Co-authored-by: bannzai<[email protected]>
  • Loading branch information
ABausG authored Jul 18, 2024
1 parent 1db5831 commit 284cd51
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HomeWidgetPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,

private var activity: Activity? = null
private var receiver: BroadcastReceiver? = null
private val doubleLongPrefix : String = "home_widget.double."

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "home_widget")
Expand All @@ -38,25 +39,27 @@ class HomeWidgetPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
eventChannel.setStreamHandler(this)
context = flutterPluginBinding.applicationContext
}

override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"saveWidgetData" -> {
if (call.hasArgument("id") && call.hasArgument("data")) {
val id = call.argument<String>("id")
val data = call.argument<Any>("data")
val prefs = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE).edit()
if(data != null) {
if (data != null) {
prefs.putBoolean("$doubleLongPrefix$id", data is Double)
when (data) {
is Boolean -> prefs.putBoolean(id, data)
is Float -> prefs.putFloat(id, data)
is String -> prefs.putString(id, data)
is Double -> prefs.putLong(id, java.lang.Double.doubleToRawLongBits(data))
is Int -> prefs.putInt(id, data)
is Long -> prefs.putLong(id, data)
else -> result.error("-10", "Invalid Type ${data!!::class.java.simpleName}. Supported types are Boolean, Float, String, Double, Long", IllegalArgumentException())
}
} else {
prefs.remove(id)
prefs.remove("$doubleLongPrefix$id")
}
result.success(prefs.commit())
} else {
Expand All @@ -72,7 +75,7 @@ class HomeWidgetPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,

val value = prefs.all[id] ?: defaultValue

if(value is Long) {
if(value is Long && prefs.getBoolean("$doubleLongPrefix$id", false)) {
result.success(java.lang.Double.longBitsToDouble(value))
} else {
result.success(value)
Expand Down
4 changes: 3 additions & 1 deletion example/integration_test/android_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ void main() {
'intKey': 12,
'boolKey': true,
'floatingNumberKey': 12.1,
'largeDoubleKey': double.infinity,
'nullValueKey': null,
'longKey': DateTime(2024).millisecondsSinceEpoch,
};

const defaultValue = MapEntry('defaultKey', 'defaultValue');
Expand All @@ -24,7 +26,7 @@ void main() {

group('Test Data operations', () {
for (final testSet in testData.entries) {
testWidgets('Test ${testSet.value?.runtimeType}', (tester) async {
testWidgets('Test ${testSet.key}', (tester) async {
// Save Data
await HomeWidget.saveWidgetData(testSet.key, testSet.value);

Expand Down
4 changes: 3 additions & 1 deletion example/integration_test/ios_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ void main() {
'floatingNumberKey': 12.1,
'nullValueKey': null,
'uint8ListKey': Uint8List.fromList([]),
'largeDoubleKey': double.infinity,
'longKey': DateTime(2024).millisecondsSinceEpoch,
};

const defaultValue = MapEntry('defaultKey', 'defaultValue');
Expand All @@ -39,7 +41,7 @@ void main() {

group('Test Data operations', () {
for (final testSet in testData.entries) {
testWidgets('Test ${testSet.value?.runtimeType}', (tester) async {
testWidgets('Test ${testSet.key}', (tester) async {
// Save Data
await HomeWidget.saveWidgetData(testSet.key, testSet.value);

Expand Down

0 comments on commit 284cd51

Please sign in to comment.