项目作者: bapspatil

项目描述 :
A Kotlin library that makes using SharedPreferences less scary.
高级语言: Kotlin
项目地址: git://github.com/bapspatil/ScaredPreferences.git
创建时间: 2019-03-31T13:54:29Z
项目社区:https://github.com/bapspatil/ScaredPreferences

开源协议:Apache License 2.0

下载


ScaredPreferences

GitHub license
Jitpack

A Kotlin library that makes using SharedPreferences less scary.

Download

Add this in your root build.gradle file (not your module build.gradle file):

  1. allprojects {
  2. repositories {
  3. maven { url "https://jitpack.io" }
  4. }
  5. }

Then, add the library to your module build.gradle:

  1. dependencies {
  2. implementation 'com.github.bapspatil:ScaredPreferences:1.0.0'
  3. }

Usage

The sample app included in this repo is still under development.

In the meantime, you can take a look at how to use the library here:

Create a new class that takes in a parameter of type ScaredPreferences:

  1. class MyPreferences(scaredPreferences: ScaredPreferences) {
  2. // Key used here for SharedPreferences is "userId"
  3. // Default value is null
  4. var userId: String? by scaredPreferences.delegate(null)
  5. // Key used here for SharedPreferences is "KEY_PROFILE_PIC"
  6. // Default value is null
  7. var userProfilePic: String? by scaredPreferences.delegate(null, "KEY_PROFILE_PIC")
  8. }

Use this in your Activity/Fragment required like so:

  1. class MainActivity : AppCompatActivity() {
  2. private lateinit var scaredPreferences: ScaredPreferences
  3. private lateinit var myPreferences: MyPreferences
  4. override fun onCreate(...) {
  5. ...
  6. // Get default SharedPreferences
  7. scaredPreferences = ScaredPreferences.Buider().withDefaultSharedPreferences(this).build()
  8. // Or, get a custom SharedPreferences
  9. scaredPreferences = ScaredPreferences.Builder().withSharedPreferences(myCustomSharedPreferences).build()
  10. // Create your MyPreferences with ScaredPreferences
  11. myPreferences = MyPreferences(scaredPreferences)
  12. // Set your MyPreferences
  13. myPreferences.userId = "ADSF09U23GADSG"
  14. myPreferences.userProfilePic = "https://github.com/bapspatil.png"
  15. // Get your MyPreferences
  16. Log.d("MY_USER_ID", myPreferences.userId)
  17. Log.d("MY_USER_PIC", myPreferences.userProfilePic)
  18. }
  19. }

Developed By

Bapusaheb Patil

https://bapspatil.com

License

  1. Copyright 2019 Bapusaheb Patil
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.