Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Kotlin: Accessing sharedPreferences inside Retrofit Interceptor

Here is a Retrofit Interceptor used to inject automatically a token inside requests. I'm trying to get this token from sharedPreferences but getSharedPreferences is not available there.

How can i retrieve my token from sharedpreferences inside this Interceptor ?

import android.preference.PreferenceManager
import okhttp3.Interceptor
import okhttp3.Response

class ServiceInterceptor: Interceptor {

    var token : String = "";

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()

        if(request.header("No-Authentication") == null){

            if (request.url.toString().contains("/user/signin") === false) {
                // Add Authorization header only if it's not the user signin request.

                // Get token from shared preferences
                val sharedPreference = PreferenceManager.getSharedPreferences()
                token = sharedPreference.getString("token")

                if (!token.isNullOrEmpty()) {
                    val finalToken = "Bearer " + token
                    request = request.newBuilder()
                        .addHeader("Authorization", finalToken)
                        .build()
                }

            }

        }

        return chain.proceed(request)
    }

}
like image 349
wawanopoulos Avatar asked Oct 15 '22 11:10

wawanopoulos


1 Answers

There's a simple solution for this in Kotlin – just copy & paste the code into a new AppPreferences.kt file and follow the 4 TODO steps outlined in the code:

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import androidx.core.content.edit

object AppPreferences {
    private var sharedPreferences: SharedPreferences? = null

    // TODO step 1: call `AppPreferences.setup(applicationContext)` in your MainActivity's `onCreate` method
    fun setup(context: Context) {
        // TODO step 2: set your app name here
        sharedPreferences = context.getSharedPreferences("<YOUR_APP_NAME>.sharedprefs", MODE_PRIVATE)
    }

    // TODO step 4: replace these example attributes with your stored values
    var heightInCentimeters: Int?
        get() = Key.HEIGHT.getInt()
        set(value) = Key.HEIGHT.setInt(value)

    var birthdayInMilliseconds: Long?
        get() = Key.BIRTHDAY.getLong()
        set(value) = Key.BIRTHDAY.setLong(value)

    private enum class Key {
        HEIGHT, BIRTHDAY; // TODO step 3: replace these cases with your stored values keys

        fun getBoolean(): Boolean? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getBoolean(name, false) else null
        fun getFloat(): Float? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getFloat(name, 0f) else null
        fun getInt(): Int? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getInt(name, 0) else null
        fun getLong(): Long? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getLong(name, 0) else null
        fun getString(): String? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getString(name, "") else null

        fun setBoolean(value: Boolean?) = value?.let { sharedPreferences!!.edit { putBoolean(name, value) } } ?: remove()
        fun setFloat(value: Float?) = value?.let { sharedPreferences!!.edit { putFloat(name, value) } } ?: remove()
        fun setInt(value: Int?) = value?.let { sharedPreferences!!.edit { putInt(name, value) } } ?: remove()
        fun setLong(value: Long?) = value?.let { sharedPreferences!!.edit { putLong(name, value) } } ?: remove()
        fun setString(value: String?) = value?.let { sharedPreferences!!.edit { putString(name, value) } } ?: remove()

        fun exists(): Boolean = sharedPreferences!!.contains(name)
        fun remove() = sharedPreferences!!.edit { remove(name) }
    }
}

Now from anywhere within your app you can get a value like this:

val heightInCentimeters: Int? = AppPreferences.heightInCentimeters
val heightOrDefault: Int = AppPreferences.heightInCentimeters ?: 170

Setting a value to the SharedPreferences is just as easy:

AppPreferences.heightInCentimeters = 160 // sets a new value

The above is extracted from my FitnessTracker project. See this file for a full example.

like image 160
Jeehut Avatar answered Oct 18 '22 22:10

Jeehut