Skip to content
No description, website, or topics provided.
Kotlin Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
app
docs
gradle/wrapper
spork
.gitignore
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

Shared Preferences Object Relations in Kotlin

A Retrofit-inspired ORM for Android's SharedPreferences that turns an annotated interface into a SharedPreference accessor object.

Why ?

There are multiple ORMs for SQLite in Android, and they make it very easy to treat tables and rows as classes and objects. But still, there is a lot of local data we store in SharedPreferences and accessing/modifying them still requires writing a couple of boilerplate lines -

val prefs = context.getSharedPreferences('app_prefs', Context.MODE_PRIVATE)

// read
val currCount = prefs.getInt('launch_count', 0) 
// ----------------

// write
val editor = prefs.edit()
editor.putInt('launch_count', currCount + 1) 
editor.apply()
// -- or if using android ktx -- 
prefs.edit {
  putInt('launch_count', currCount + 1)
}

It is time to reduce all of that.

How it works ?

1. Create your preference interface

AppPrefs.kt

import tech.arnav.spork.annotations.Pref
import tech.arnav.spork.annotations.PreferenceFile

@PreferenceFile("app_prefs")
interface AppPrefs {

    @Pref("count")
    var count: Int

    @Pref("screen")
    var screenName: String

}

2. Access it from your Activity/Service

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val appPrefs = Spork.create(this, AppPrefs::class)
        // write prefs
        appPrefs.screenName = "MAIN" 
        appPrefs.count++
        
        // read prefs
        Toast.makeText(this, "Visit number " + appPrefs.count, Toast.LENGTH_SHORT).show()

    }
}

Other Benefits

Over time in large long running projects, you might have added prefs earlier, that are not used anymore. Your users might be having huge pref files with ages old keys not used anymore. But every time you do getSharedPreferences the entire file is still read into memory.

While initialising, SPORK will automatically remove old keys that are not in AppPrefs.kt anymore. Thus your app_prefs.xml is always an up-to-date and pruned implementation of AppPrefs.kt.

Installation

Add this into your app's build.gradle

repositories {
    ...
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'tech.arnav:SPORK:0.1.0'
}

TODO List

  • Annotation Processor / Codegen support
  • remove kotlin-reflect
  • allow a way to mark default get values if pref not already present
  • allow turning on/off pruning of old pref keys
You can’t perform that action at this time.