(Android) Alternative native preference

This commit is contained in:
Ming Ming 2022-06-08 02:43:49 +08:00
parent 5f65f66be4
commit d63bdc4393
4 changed files with 110 additions and 0 deletions

View file

@ -105,6 +105,14 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
LogcatChannelHandler.METHOD_CHANNEL
)
logcatMethodChannel.setMethodCallHandler(logcatChannelHandler)
val preferenceChannelHandler =
PreferenceChannelHandler(flutterPluginBinding.applicationContext)
preferenceMethodChannel = MethodChannel(
flutterPluginBinding.binaryMessenger,
PreferenceChannelHandler.METHOD_CHANNEL
)
preferenceMethodChannel.setMethodCallHandler(preferenceChannelHandler)
}
override fun onDetachedFromEngine(
@ -122,6 +130,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
permissionChannel.setStreamHandler(null)
permissionMethodChannel.setMethodCallHandler(null)
logcatMethodChannel.setMethodCallHandler(null)
preferenceMethodChannel.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
@ -209,6 +218,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
private lateinit var permissionChannel: EventChannel
private lateinit var permissionMethodChannel: MethodChannel
private lateinit var logcatMethodChannel: MethodChannel
private lateinit var preferenceMethodChannel: MethodChannel
private lateinit var lockChannelHandler: LockChannelHandler
private lateinit var mediaStoreChannelHandler: MediaStoreChannelHandler

View file

@ -0,0 +1,77 @@
package com.nkming.nc_photos.plugin
import android.content.Context
import android.content.SharedPreferences
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class PreferenceChannelHandler(context: Context) :
MethodChannel.MethodCallHandler {
companion object {
const val METHOD_CHANNEL = "${K.LIB_ID}/preference_method"
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"setBool" -> {
try {
setBool(
call.argument("prefName")!!,
call.argument("key")!!,
call.argument("value")!!,
result
)
} catch (e: Throwable) {
result.error("systemException", e.toString(), null)
}
}
"getBool" -> {
try {
getBool(
call.argument("prefName")!!,
call.argument("key")!!,
call.argument("defValue"),
result
)
} catch (e: Throwable) {
result.error("systemException", e.toString(), null)
}
}
else -> result.notImplemented()
}
}
private fun setBool(
prefName: String, key: String, value: Boolean,
result: MethodChannel.Result
) {
openPref(prefName).run {
edit().run {
putBoolean(key, value)
}.apply()
}
result.success(null)
}
private fun getBool(
prefName: String, key: String, defValue: Boolean?,
result: MethodChannel.Result
) {
val product = openPref(prefName).run {
if (contains(key)) {
getBoolean(key, false)
} else {
defValue
}
}
result.success(product)
}
private fun openPref(prefName: String): SharedPreferences {
return context.getSharedPreferences(prefName, Context.MODE_PRIVATE)
}
private val context = context
}

View file

@ -9,3 +9,4 @@ export 'src/media_store.dart';
export 'src/native_event.dart';
export 'src/notification.dart';
export 'src/permission.dart';
export 'src/preference.dart';

View file

@ -0,0 +1,22 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:nc_photos_plugin/src/k.dart' as k;
class Preference {
static Future<void> setBool(String prefName, String key, bool value) =>
_methodChannel.invokeMethod("setBool", {
"prefName": prefName,
"key": key,
"value": value,
});
static Future<bool?> getBool(String prefName, String key, [bool? defValue]) =>
_methodChannel.invokeMethod("getBool", {
"prefName": prefName,
"key": key,
"defValue": defValue,
});
static const _methodChannel = MethodChannel("${k.libId}/preference_method");
}