在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):AutSoft/Krate开源软件地址(OpenSource Url):https://github.com/AutSoft/Krate开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):KrateKrate is a Here's what its basic usage looks like, extending the provided class UserSettings(context: Context) : SimpleKrate(context) {
var notificationsEnabled by booleanPref().withDefault(false)
var loginCount by intPref().withDefault(0)
var nickname by stringPref()
}
val settings = UserSettings(context)
settings.loginCount = 10
Log.d("LOGIN_COUNT", "Count: ${settings.loginCount}") DependencyKrate is available from implementation 'hu.autsoft:krate:2.0.0' BasicsA Krate property is nullable by default. It will have a var username: String? by stringPref() Default valuesYou can provide a default value for the property by chaining a var username: String by stringPref().withDefault("admin") Reading from this property will return either the value it was last set to, or the default value if it's never been set.
Custom keysBy default, the the property will be stored under the key of the property's name in the underlying You can change this behaviour by explicitly providing the key as an argument: var username: String? by stringPref(key = "USER_NAME")
ValidationYou can add validation rules to your Krate properties by calling var percentage: Int by intPref()
.withDefault(0)
.validate { it in 0..100 } If this validation fails, an Custom Krate implementationsYou can usually get away with extending However, you can also implement the class ExampleCustomKrate(context: Context) : Krate {
override val sharedPreferences: SharedPreferences
init {
sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE)
}
var exampleBoolean by booleanPref().withDefault(false)
} For simple applications, your class MainActivity : AppCompatActivity(), Krate {
override val sharedPreferences: SharedPreferences by lazy {
getPreferences(Context.MODE_PRIVATE) // Could also fetch a named or default SharedPrefs
}
var username by stringPref().withDefault("")
} Third party implementationsYou can create the Here's how you'd use EncryptedSharedPreferences with Krate (see this source file for the full code): class EncryptedKrate(applicationContext: Context) : Krate {
override val sharedPreferences: SharedPreferences
init {
/* ... */
sharedPreferences = EncryptedSharedPreferences.create(applicationContext, ...)
}
val myStringValue: String by stringPref().withDefault("")
} Serialization addonsKrate, by default, supports the types that If you don't find support for the library or type you're looking for, implementing your own delegate in your own project based on the code of existing delegates should be quite simple, this is very much a supported use case. If you think your type might be commonly used, you can also open an issue to ask for an addon library for that type. Moshi supportThis addon provides a Since Moshi supports both reflection-based serialization and code generation, there are multiple Krate artifacts for different use cases. You should always include only one of the dependencies below, otherwise you'll end up with a dexing error. The usage of the Krate integration is the same for both setups: class MoshiKrate(context: Context) : SimpleKrate(context) {
var user: User? by moshiPref()
var savedArticles: List<Article>? by moshiPref()
} If you want to provide your own class CustomMoshiKrate(context: Context) : SimpleKrate(context) {
init {
moshi = Moshi.Builder().build()
}
} Only codegenIf you only want to use Moshi adapters that you generate via Moshi's codegen facilities, you can use the following Krate artifact in your project to make use of these adapters: implementation 'hu.autsoft:krate-moshi-codegen:2.0.0' This will give you a default Only reflection, or mixed use of reflection and codegenIf you rely on reflection for your Moshi serialization, and therefore need a implementation 'hu.autsoft:krate-moshi-reflect:2.0.0' The default You may choose to use Moshi's codegen for some classes in your project, and serialize the ones with no adapters generated with the default approach via reflection. For this mixed use case, you should also choose this dependency (unless you set your own custom Moshi instances as described above). Kotlinx.serialization supportThe implementation 'hu.autsoft:krate-kotlinx:2.0.0' Its usage is the same as with any of the base library's delegates: class KotlinxKrate(context: Context) : SimpleKrate(context) {
var user: User? by kotlinxPref()
var savedArticles: List<Article>? by kotlinxPref()
} By default, the class CustomKotlinxKrate(context: Context) : SimpleKrate(context) {
init {
json = Json {
coerceInputValues = true
...
}
}
var user: User? by kotlinxPref()
} Gson supportThe implementation 'hu.autsoft:krate-gson:2.0.0' Its basic usage is the same as with any of the base library's delegates: class GsonKrate(context: Context) : SimpleKrate(context) {
var user: User? by gsonPref()
var savedArticles: List<Article>? by gsonPref()
} By default, the class CustomGsonKrate(context: Context) : SimpleKrate(context) {
init {
gson = GsonBuilder().create()
}
var user: User? by gsonPref()
} License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论