BasePreferenceHelper.kt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.fastest.pass.helpers
  2. import android.content.Context
  3. import com.fastest.pass.login.data.model.UserInfo
  4. import com.google.gson.Gson
  5. import com.google.gson.reflect.TypeToken
  6. class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
  7. fun clearAllData() {
  8. removePreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN)
  9. removePreference(context, KEY_FILENAME, KEY_BEARER_TOKEN)
  10. removePreference(context, KEY_FILENAME, KEY_USERINFO)
  11. removePreference(context, KEY_FILENAME, KEY_SALT_KEY)
  12. }
  13. fun setLoggedInState(state: Boolean) {
  14. putBooleanPreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN, state)
  15. }
  16. fun getLoggedInState(): Boolean {
  17. return getBooleanPreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN)
  18. }
  19. fun saveToken(token: String) {
  20. putStringPreference(context, KEY_FILENAME, KEY_BEARER_TOKEN, token)
  21. }
  22. fun getToken(): String? {
  23. return getStringPreference(context, KEY_FILENAME, KEY_BEARER_TOKEN)
  24. }
  25. fun saveUserInfo(userInfo: UserInfo?) {
  26. putStringPreference(context, KEY_FILENAME, KEY_USERINFO, Gson().toJson(userInfo))
  27. }
  28. fun getUserInfo() : UserInfo? {
  29. val type = object : TypeToken<UserInfo?>() {}.type
  30. return Gson().fromJson<UserInfo?>(
  31. getStringPreference(
  32. context, KEY_FILENAME, KEY_USERINFO
  33. ), type
  34. )
  35. }
  36. fun saveCustomSaltKey(saltKey: String) {
  37. putStringPreference(context, KEY_FILENAME, KEY_SALT_KEY, saltKey)
  38. }
  39. fun getCustomSaltKey(): String? {
  40. return getStringPreference(context, KEY_FILENAME, KEY_SALT_KEY)
  41. }
  42. companion object {
  43. private const val KEY_FILENAME = "file_fastestpass"
  44. private const val KEY_IS_LOGGED_IN = "key_isloggedin"
  45. private const val KEY_BEARER_TOKEN = "key_bearer_token"
  46. private const val KEY_USERINFO = "key_userinfo"
  47. private const val KEY_SALT_KEY = "key_salt_key"
  48. }
  49. }