Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created January 24, 2020 04:20
Show Gist options
  • Save vamjakuldip/586a086a6d90218328a0e5980935f073 to your computer and use it in GitHub Desktop.
Save vamjakuldip/586a086a6d90218328a0e5980935f073 to your computer and use it in GitHub Desktop.
Preferance Helper
package com.vk.android.helper
import android.content.Context
import android.content.SharedPreferences
class PreferenceHelper private constructor(context: Context) {
private val sharedPref: SharedPreferences = context.getSharedPreferences(context.packageName + ".PREFERENCE", Context.MODE_PRIVATE)
companion object {
private var instance: PreferenceHelper? = null
private const val SERVER_TOKEN = "server_token"
private const val USER_ID = "user_id"
private const val DEVICE_TOKEN = "device_token"
fun getInstance(context: Context) = instance ?: synchronized(this) {
instance ?: PreferenceHelper(context).also { instance = it }
}
}
var severToken: String?
get() = sharedPref.getString(SERVER_TOKEN, "")
set(value) {
val edit = sharedPref.edit()
edit.putString(SERVER_TOKEN, value)
edit.apply()
}
var userId: String?
get() = sharedPref.getString(USER_ID, "")
set(value) {
val edit = sharedPref.edit()
edit.putString(USER_ID, value)
edit.apply()
}
var deviceToken: String?
get() = sharedPref.getString(DEVICE_TOKEN, "")
set(value) {
val edit = sharedPref.edit()
edit.putString(DEVICE_TOKEN, value)
edit.apply()
}
fun logOut() {
severToken = ""
userId = ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment