在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):russhwolf/multiplatform-settings开源软件地址(OpenSource Url):https://github.com/russhwolf/multiplatform-settings开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):Multiplatform SettingsThis is a Kotlin library for Multiplatform apps, so that common code can persist key-value data. Table of contentsAdding to your projectMultiplatform Settings is currently published to Maven Central, so add that to repositories. repositories {
mavenCentral()
// ...
} Then, simply add the dependency to your common source-set dependencies commonMain {
dependencies {
// ...
implementation("com.russhwolf:multiplatform-settings:0.9")
}
} See also the sample project, which uses this structure. UsageThe Creating a Settings instanceWhen writing multiplatform code, you might need to interoperate with platform-specific code which needs to share the same data-source. To facilitate this, all Since that delegate is a constructor argument, it should be possible to connect it via any dependency-injection strategy you might already be using. If your project doesn't have such a system already in place, one strategy is to use expect val settings: Settings
// or
expect fun createSettings(): Settings Then the Some platform implementations also include val settings1: Settings = factory.create("my_first_settings")
val settings2: Settings = factory.create("my_other_settings") See Factories below for more details. However, if all of your key-value logic exists in a single instance in common code, these ways of
instantiation val settings: Settings = Settings() See No-arg module below for more details. Platform constructorsThe Android implementation is val delegate: SharedPreferences // ...
val settings: Settings = AndroidSettings(delegate) On iOS, macOS, tvOS, or watchOS, val delegate: NSUserDefaults // ...
val settings: Settings = AppleSettings(delegate) On JS, val delegate: Storage // ...
val settings: Settings = JsSettings(delegate)
val settings: Settings = JsSettings() // use localStorage by default FactoriesFor some platforms, a On Android, this factory needs a val context: Context // ...
val factory: Settings.Factory = AndroidSettings.Factory(context) On iOS and macOS, the factory can be instantiated without passing any parameter val factory: Settings.Factory = AppleSettings.Factory() No-arg moduleTo create a implementation("com.russhwolf:multiplatform-settings-no-arg:0.9") Then from common code, you can write val settings: Settings = Settings() This is implemented via an extension function On Android, this delegates to the equivalent of On Apple platforms, it uses Note that while the main Settings APIOnce the settings.putInt("key", 3)
settings["key"] = 3 You can retrieve stored values via the val a: Int = settings.getInt("key")
val b: Int = settings.getInt("key", defaultValue = -1)
val c: Int = settings["key", -1] Nullable methods are also available to avoid the need to use a default value. Instead, val a: Int? = settings.getIntOrNull("key")
val b: Int? = settings["key"] The val a: Int by settings.int("key")
val b: Int by settings.int("key", defaultValue = -1) Nullable delegates exists so that absence of a key can be indicated by val a: Int? by settings.nullableInt("key") The val a: Int by settings.int() // internally, key is "a" Existence of a key can be queried val a: Boolean = settings.hasKey("key")
val b: Boolean = "key" in settings Values can also be removed by key settings.remove("key")
settings -= "key"
settings["key"] = null Finally, all values in a settings.clear() The set of keys and amount of entries can be retrieved val keys: Set<String> = settings.keys
val size: Int = settings.size Note that for the TestingA testing dependency is available to aid in testing code that interacts with this library. implementation("com.russhwolf:multiplatform-settings-test:0.9") This includes a Other platformsThe Experimental APIThis is a pre-1.0 library based on the alpha-release multiplatform functionality, so some occasional API breakage may occur. Certain APIs are marked with Experimental ImplementationsApple KeychainIn addition to the default val serviceName: String // ...
val settings: Settings = KeychainSettings(serviceName) JVMTwo pure-JVM implementations exist. val delegate: Preferences // ...
val settings: Settings = JvmPreferencesSettings(delegate)
val delegate: Properties // ...
val settings: Settings = JvmPropertiesSettings(delegate) WindowsThere is a Windows implementation val rootKey: String = "SOFTWARE\\..." // Will be interpreted as subkey of HKEY_CURRENT_USER
val settings: Settings = WindowsSettings(rootKey) ListenersUpdate listeners are available using an experimental API, only for the val observableSettings: ObservableSettings // ...
val settingsListener: SettingsListener = observableSettings.addListener(key) { /* ... */ }
// Typed listener extension functions are also available
val settingsListener: SettingsListener = observableSettings.addIntListener(key) { int: Int -> /* ... */ }
val settingsListener: SettingsListener = observableSettings.addNullableIntListener(key) { int: Int? -> /* ... */ } The settingsListener.deactivate() On Apple platforms, the Serialization moduleA implementation("com.russhwolf:multiplatform-settings-serialization:0.9") This essentially uses the @Serializable
class SomeClass(val someProperty: String, anotherProperty: Int) an instance can be stored or retrieved val someClass: SomeClass
val settings: Settings
// Store values for the properties of someClass in settings
settings.encodeValue(SomeClass.serializer(), "key", someClass)
// Create a new instance of SomeClass based on the data in settings
val newInstance: SomeClass = settings.decodeValue(SomeClass.serializer(), "someClass", defaultValue)
val nullableNewInstance: SomeClass = settings.decodeValueOrNull(SomeClass.serializer(), "someClass") There's also a delegate API, similar to that for primitives val someClass: SomeClass by settings.serializedValue(SomeClass.serializer(), "someClass", defaultValue)
val nullableSomeClass: SomeClass? by settings.nullableSerializedValue(SomeClass.serializer(), "someClass") Usage requires accepting both the Coroutine APIsA separate implementation("com.russhwolf:multiplatform-settings-coroutines:0.9")
// Or, if you use native-mt coroutines release
implementation("com.russhwolf:multiplatform-settings-coroutines-native-mt:0.9") This adds flow extensions for all types which use the listener APIs internally. val observableSettings: ObservableSettings // Only works with ObservableSettings
val flow: Flow<Int> by observableSettings.intFlow("key", defaultValue)
val nullableFlow: Flow<Int?> by observableSettings.intOrNullFlow("key") Usage requires accepting both the In addition, there are two new val suspendSettings: SuspendSettings // ...
val a: Int = suspendSettings.getInt("key") // This call will suspend
val flowSettings: FlowSettings // ...
val flow: Flow<Int> = flowSettings.getIntFlow("key") There are APIs provided to convert between these different interfaces so that you can select one to use primarily from common. val settings: Settings // ...
val suspendSettings: SuspendSettings = settings.toSuspendSettings()
val observableSettings: ObservableSettings // ...
val flowSettings: FlowSettings = observableSettings.toFlowSettings()
// Wrap suspend calls in runBlocking
val blockingSettings: Settings = suspendSettings.toBlockingSettings() DataStoreAn implementation of implementation("com.russhwolf:multiplatform-settings-datastore:0.9") This provides a val dataStore: DataStore // = ...
val settings: FlowSettings = DataStoreSettings(dataStore) You can use this in shared code by converting other // Common
expect val settings: FlowSettings
// Android
actual val settings: FlowSettings = DataStoreSettings(/*...*/)
// iOS
actual val settings: FlowSettings = AppleSettings(/*...*/).toFlowSettings() Or, if you also include platforms without listener support, you can use // Common
expect val settings: SuspendSettings
// Android
actual val settings: SuspendSettings = DataStoreSettings(/*...*/)
// iOS
actual val settings: SuspendSettings = AppleSettings(/*...*/).toSuspendSettings()
// JS
actual val settings: SuspendSettings = JsSettings().toSuspendSettings() BuildingThe project includes multiple CI jobs configured using Azure pipelines. On PRs or updates to the An addition pipeline is defined in Project StructureThe library logic lives in the Some unit tests are defined which can be run via There is also a sample project to demonstrate usage, which is configured as a separate IDEA/gradle project in the The License
Made with JetBrains too |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论