diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt index 9f21006c..5f6f233f 100644 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/NcPhotosPlugin.kt @@ -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 diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/PreferenceChannelHandler.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/PreferenceChannelHandler.kt new file mode 100644 index 00000000..95c39ed6 --- /dev/null +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/PreferenceChannelHandler.kt @@ -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 +} diff --git a/plugin/lib/nc_photos_plugin.dart b/plugin/lib/nc_photos_plugin.dart index f8e101f4..28e82847 100644 --- a/plugin/lib/nc_photos_plugin.dart +++ b/plugin/lib/nc_photos_plugin.dart @@ -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'; diff --git a/plugin/lib/src/preference.dart b/plugin/lib/src/preference.dart new file mode 100644 index 00000000..4064685e --- /dev/null +++ b/plugin/lib/src/preference.dart @@ -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 setBool(String prefName, String key, bool value) => + _methodChannel.invokeMethod("setBool", { + "prefName": prefName, + "key": key, + "value": value, + }); + + static Future getBool(String prefName, String key, [bool? defValue]) => + _methodChannel.invokeMethod("getBool", { + "prefName": prefName, + "key": key, + "defValue": defValue, + }); + + static const _methodChannel = MethodChannel("${k.libId}/preference_method"); +}