Created
January 24, 2020 04:20
-
-
Save vamjakuldip/586a086a6d90218328a0e5980935f073 to your computer and use it in GitHub Desktop.
Preferance Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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