Skip to content
/ config Public

Utility library for Android with Kotlin to help you to create and manage simple _settings_ of an application.

License

Notifications You must be signed in to change notification settings

AChep/config

Repository files navigation

config

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.

Download Build Status Code Climate Codacy Badge

Utility library for Android with Kotlin to help you to create and manage simple settings of application.

Download

Gradle:

repositories {
  jcenter()
}

dependencies {
  implementation 'com.artemchep.config:config:${latestVersion}'
}

How to use

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!

Read value

val value = Cfg.intProperty + 1

Please note that getting a value will not actually update it from the SharedPreferences, it returns last set value.

Write value

Cfg.edit(context) { 
    Cfg.intProperty = 100
    Cfg.stringProperty = ""
}

Observe changes

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()

Sample project

You may want to check a sample project for a working example.

Restrictions

  1. Property can not be null.

Report a bug or request a feature

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.

Create new issue

Versioning

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 https://semver.org/.

Build

Clone the project and come in:

$ git clone git:https://github.com/AChep/config.git
$ cd config/

About

Utility library for Android with Kotlin to help you to create and manage simple _settings_ of an application.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages