Note: The tool is fine for small projects, but for bigger ones I would recommend using the Datastore library wrapped with your own layer of abstraction, or https://github.com/tfcporciuncula/flow-preferences if you use coroutines heavily.
Utility library for Android with Kotlin to help you to create and manage simple settings of application.
Gradle:
repositories {
jcenter()
}
dependencies {
implementation 'com.artemchep.config:config:${latestVersion}'
}
Firstly, create your configuration class based on Config
, for example:
object Cfg : SharedPrefConfig("cfg") {
const val KEY_INT = "my_int_key"
const val KEY_STRING = "my_string_key"
var intProperty by configDelegate(KEY_INT, 0)
var stringProperty by configDelegate(KEY_STRING, "")
}
Then, init it on application create:
class App : Application() {
override fun onCreate() {
super.onCreate()
Cfg.init(this) // loads all variables from shared preferences
}
}
That's all!
val value = Cfg.intProperty + 1
Please note that getting a value will not actually update it from the SharedPreferences
, it returns last set value.
Cfg.edit(context) {
Cfg.intProperty = 100
Cfg.stringProperty = ""
}
val observer = object : Config.OnConfigChangedListener<String> {
override fun onConfigChanged(keys: Set<String>) {
// Check if the keys include your
// key and update something.
}
}
val registration = Cfg.observe(observer)
// Do not forget to unregister it later on
// by calling:
// Cfg.removeObserver(observer)
//
// or via the created Registration instance:
// registration.unregister()
You may want to check a sample project for a working example.
- Property can not be
null
.
Before creating a new issue please make sure that same or similar issue is not already created by checking open issues and closed issues (please note that there might be multiple pages). If your issue is already there, don't create a new one, but leave a comment under already existing one.
Checklist for creating issues:
- Keep titles short but descriptive.
- For feature requests leave a clear description about the feature with examples where appropriate.
- For bug reports leave as much information as possible about your device, android version, etc.
- For bug reports also write steps to reproduce the issue.
For transparency in a release cycle and in striving to maintain backward compatibility, a project should be maintained under the Semantic Versioning guidelines. Sometimes we screw up, but we should adhere to these rules whenever possible.
Releases will be numbered with the following format: <major>.<minor>.<patch>
and constructed with the following guidelines:
- Breaking backward compatibility bumps the major while resetting minor and patch
- New additions without breaking backward compatibility bumps the minor while resetting the patch
- Bug fixes and misc changes bumps only the patch
For more information on SemVer, please visit http://semver.org/.
Clone the project and come in:
$ git clone git://github.com/AChep/config.git
$ cd config/