Reactant Config System
To start using the reactant config system, you need to firstly define the config model class, following is an example of a config model class.
data keyword is not a must, but it can be useful on debugging while you can easily log the content out.
Remind that the config fields should be
var instead of
val, and all fields should have a default value.
Basically, the config supports the common data structures and nested objects, but it is depends on the config parser. By default, the parsers we provided doesn't support polymorphism on the fields. (i.e. You can't declare something like "Any" type fields, every field type should be a concrete class)
Reactant provided some injection candies which are easily to use for the simple use case,
those injectable support
To inject a config as a non-shared object with the specified config model class,
we can use
Config<T> and specify the model class as T.
ConfigService will automatically read the file by the path provided in the
and use a corresponding
ParserService based on the filename extension.
The config file will be automatically created if it doesn't exist.
You can use the functions of
Config<T> to save, refresh (i.e. discard changes and reload content from disk) and remove the config.
The content of config after being removed will still be accessible from
Config<T>, but will be removed from the disk.
All operations are
(Similar to Observable, but without result), so that you can easily make it asynchronous.
In most of the cases, the operations in
should be synchronoused in order to ensure the operations was completed inside the hook.
Config<T> will generate a new
Config instance for each injections,
which will not share the content state between each component until you call the
Sometimes we may need to synchronize the state between each component, therefore we need
Basically it is similar to
In some cases, you will need to manage a list of configs which having the same model.
For example, multiple configs of 'CustomFoodItemConfig' inside folder
we can just inject it like the previous example.
MultiConfig object, you can use following functions to get/create configs.
You may need to use io scheduler if you would like to do those operations after initialization.
Since the result from the operations of
MultiConfig<T> are also
you can also access the operations of
Config<T> as above described
Advanced usage of Config System
The injectables above are just the candies way for simple usage, if you have more complicated requirements,
you should consider to use the
Before we start using
ConfigService will use the
ParserService to encode your config object to target format
or decode it into a jvm object.
To use the common format like
toml, you are not required to write your own parser,
Reactant come with the following default
Customization of Gson
The default JsonParserService do not support customizing the Gson configuration, if you need to modify it (e.g. adding custom TypeAdapter), you need to create your own parser as following described.
To make your own parser, you need to implement the
The injectables candies will only use the default parser provided by the reactant,
to use your custom parser, you need directly call the