Skip to content

Built over Jetpack's EncryptedSharedPreferences API with less verbose syntax, efficient read/write mechanism, complex object and null default value support with compile time safety

License

Notifications You must be signed in to change notification settings

ErickSumargo/Pin

Repository files navigation

Pin Download

Here's a look how our session data is stored using Android's normal SharedPreferences API:

Flipper - Normal/Plain Mode


It's plain. With some proper steps, it's easily getting exposed. [See How-to]

Thankfully, Android Jetpack's team comes with new EncryptedSharedPreferences API to support data encryption.

Flipper - Encrypted Mode


As normal SharedPreferences, we still need to deal with some tedious boilerplates, e.g., instantiating, mechanism to read, and write a preference.

This is the background why Pin comes to surface. Pin with additional features helps to abstract away those boilerplates so you can 100% just focus on business.

Features

Pin provides:

  • Encrypted/Normal mode
  • Less verbose syntax
  • Efficient read/write mechanism with coroutine
  • Support complex object and null default value
  • Compile time safety

Download

Since EncryptedSharedPreferences requires minSdkVersion 23+, you should take note if your app supports it.

implementation 'com.bael:pin:${latestRelease}'

Else, you could use the normal version instead:

implementation 'com.bael:pin:${latestRelease}-normal'

Setup

Init Pin in the onCreate method of your application class

override fun onCreate() {
    super.onCreate()
    // Set useEncryptedMode as false to switch back into normal mode
    Pin.init(context = this, "YOUR_PREFERENCES_FILE_NAME", useEncryptedMode = true)
}

Usage

class MainActivity : AppCompatActivity() {

    // Less Verbose
    private var object1: Int by Pin(key = "object1_key", defaultValue = -1)

    // Support Complex Object
    private var object2: Movie by Pin(key = "object2_key", defaultValue = Movie())

    // Support Nullability
    private var object3: Movie? by Pin(key = "object3_key", defaultValue = null)

    /** 
      * Compile Time Safety
      * Below lint will be error, the type should be declared as nullable since the default set null
      * private var object4: Movie by Pin(key = "object4_key", defaultValue = null)
      */

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

        /**
         * Read
         * 1. Read it like a normal variable
         * 2. Seriously, that's all
         */
        println(object1)
        println(object2)
        println(object3)

        /**
         * Write
         * 1. Update it like normal variable assignment
         * 2. Seriously, that's all
         */
        object1 = 1
        object2 = Movie(id = 2)
        object3 = Movie(id = 3)
    }
    
    override fun onDestroy() {
        // Remove a preference value associated with certain key
        Pin.clear("object1_key")
        // Remove all values from preferences
        Pin.clear()
        
        super.onDestroy()
    }
}

License

Copyright 2020 Erick Sumargo

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

   http://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

Built over Jetpack's EncryptedSharedPreferences API with less verbose syntax, efficient read/write mechanism, complex object and null default value support with compile time safety

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages