Adding to your project

Then, simply add the dependency to your common source-set dependencies

implementation "com.russhwolf:multiplatform-settings:0.2"

See also the sample project, which uses this structure.

Usage

The Settings interface is implemented by the PlatformSettings class, which has separate implementations for Android and iOS. A PlatformSettings instance can be created using a platform-specific factory. On Android, this factory needs a Context parameter

Experimental API

Listeners

The SettingsListener returned from the call should be used to signal when you're done listening:

settings.removeListener(settingsListener)

This initial listener implementation is not designed with any sort of thread-safety so it's recommended to only interact with these APIs from the main thread of your application.

The listener APIs make use of the Kotlin @Experimental annotation. All usages must be marked with @ExperimentalListener or @UseExperimental(ExperimentalListener::class).

Project Structure

The library logic lives in the commonMain, androidMain, and iosMain sources. The common source holds the Settings interface and an expect implementation called PlatformSettings. These expose apis for persisting values of the Int, Long, String, Float, Double, and Boolean types. The common source also holds property delegate wrappers and other operator functions for cleaner syntax and usage. The android and ios modules then hold actual declarations, delegating to SharedPreferences or NSUserDefaults.

Some simple unit tests are defined which can be run via ./gradlew test. These use Robolectric on Android to mock out the android-specific behavior, and use the ios simulator to run the native tests.

There is also a sample project to demonstrate usage, which is configured as a separate IDEA/gradle project in the sample directory. It includes a shared module with common and platform sources, to demo a shared logic layer consuming the library. The app-android module consumes shared and provides an Android UI. The app-ios directory holds an Xcode project which builds an iOS app in the usual way, consuming a framework produced by shared.

The shared module includes some simple unit tests in common code to demonstrate a way to mock out the Settings interface when testing code that interacts with it.

License

Copyright 2018 Russell Wolf
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.