You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are different ways to define this properties. Here are some possibilities: (see full example here and usage here)
With default value:
varSharedPreferences.canEatPie:Boolean by bindToPreferenceField(true)
varSharedPreferences.allPieInTheWorld:Long by bindToPreferenceField(0)
Nullable:
varSharedPreferences.monstersKilled:Int? by bindToPreferenceFieldNullable()
varSharedPreferences.experience:Float? by bindToPreferenceFieldNullable()
varSharedPreferences.className:String? by bindToPreferenceFieldNullable()
Can also keep other objects:
varSharedPreferences.character:Character by bindToPreferenceFieldNullable()
varSharedPreferences.savedGame:Game? by bindToPreferenceFieldNullable()
Node that this objects are serialized do string using Gson. If they include some, types that needs some serializers, then you need to set gson with converters. Instructions here.
Also keys can be specyfied: (this is important to make values immtuable to property name in case of changes in the app)
varSharedPreferences.monstersKilled:Int? by bindToPreferenceFieldNullable("MonstersKilledKey")
varSharedPreferences.allPieInTheWorld:Long by bindToPreferenceField(0, "AllPieKey")
Install
To add KotlinPreferences to the project, add in build.gradle file:
Copyright 2017 Marcin Moskała
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
https://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.
About
Android Library to make SharedPreferences usage easier.