Last active
November 14, 2018 18:46
-
-
Save enriquebautista/afc5aa655dc84894dd0defad1ec52cf1 to your computer and use it in GitHub Desktop.
Prefs with Kotlin
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
import android.content.Context | |
import android.content.SharedPreferences | |
import android.text.TextUtils | |
class Prefs private constructor(context: Context) { | |
private var sharedPrefs: SharedPreferences | |
private var accessToken: String? = null | |
private var userEmail: String? = null | |
private var userPoints: Int = 0 | |
private var socialProfileId: String? = null | |
private var firebaseToken: String? = null | |
private var filtersConfig: String? = null | |
private var isLoggedIn: Boolean = false | |
private var isFirstTime: Boolean = true | |
init { | |
sharedPrefs = context.applicationContext.getSharedPreferences(KEY_NAME, Context.MODE_PRIVATE) | |
accessToken = sharedPrefs.getString(KEY_ACCESS_TOKEN, null) | |
firebaseToken = sharedPrefs.getString(KEY_FIREBASE_TOKEN, null) | |
isLoggedIn = !TextUtils.isEmpty(accessToken) | |
isFirstTime = sharedPrefs.getBoolean(KEY_IS_FIRST_TIME, true) | |
filtersConfig = sharedPrefs.getString(KEY_JSON_FILTERS_CONFIG, null) | |
if(isLoggedIn) { | |
userEmail = sharedPrefs.getString(KEY_USER_EMAIL, null) | |
userPoints = sharedPrefs.getInt(KEY_USER_POINTS, 0) | |
socialProfileId = sharedPrefs.getString(KEY_SOCIAL_PROFILE_ID, null) | |
} | |
} | |
fun isLoggedIn(): Boolean { | |
return isLoggedIn | |
} | |
fun setAccessToken(accessToken: String) { | |
this.accessToken = accessToken | |
isLoggedIn = true | |
sharedPrefs.edit().putString(KEY_ACCESS_TOKEN, accessToken).apply() | |
} | |
fun getAccessToken(): String? { | |
return accessToken | |
} | |
fun setUserEmail(userEmail: String) { | |
this.userEmail = userEmail | |
sharedPrefs.edit().putString(KEY_USER_EMAIL, userEmail).apply() | |
} | |
fun getUserEmail(): String? { | |
return userEmail | |
} | |
fun setUserPoints(userPoints: Int) { | |
this.userPoints = userPoints | |
sharedPrefs.edit().putInt(KEY_USER_POINTS, userPoints).apply() | |
} | |
fun getUserPoints(): Int { | |
return userPoints | |
} | |
fun setFirebaseToken(firebaseToken: String) { | |
this.firebaseToken = firebaseToken | |
sharedPrefs.edit().putString(KEY_FIREBASE_TOKEN, firebaseToken).apply() | |
} | |
fun getFirebaseToken(): String? { | |
return firebaseToken | |
} | |
fun setIsFirstTime(isFirstTime: Boolean) { | |
this.isFirstTime = isFirstTime | |
sharedPrefs.edit().putBoolean(KEY_IS_FIRST_TIME, isFirstTime).apply() | |
} | |
fun isFirstTime() : Boolean { | |
return isFirstTime | |
} | |
fun setFiltersConfig(filtersConfig: String) { | |
this.filtersConfig = filtersConfig | |
sharedPrefs.edit().putString(KEY_JSON_FILTERS_CONFIG, filtersConfig).apply() | |
} | |
fun getFiltersConfig(): String? { | |
return filtersConfig | |
} | |
fun setSocialProfileId(socialProfileId: String) { | |
this.socialProfileId = socialProfileId | |
sharedPrefs.edit().putString(KEY_SOCIAL_PROFILE_ID, socialProfileId).apply() | |
} | |
fun getSocialProfileId(): String? { | |
return socialProfileId | |
} | |
fun logout() { | |
isLoggedIn = false | |
accessToken = null | |
userEmail = null | |
userPoints = 0 | |
socialProfileId = null | |
with(sharedPrefs.edit()) { | |
putString(KEY_ACCESS_TOKEN, null) | |
putString(KEY_USER_EMAIL, null) | |
putInt(KEY_USER_POINTS, 0) | |
putString(KEY_SOCIAL_PROFILE_ID, null) | |
apply() | |
} | |
} | |
companion object { | |
private const val KEY_NAME = "PasaporteNLEPrefs" | |
private const val KEY_ACCESS_TOKEN = "accessToken" | |
private const val KEY_FIREBASE_TOKEN = "firebaseToken" | |
private const val KEY_USER_POINTS = "userPoints" | |
private const val KEY_USER_EMAIL = "userEmail" | |
private const val KEY_IS_FIRST_TIME = "isFirstTime" | |
private const val KEY_JSON_FILTERS_CONFIG = "jsonFiltersConfig" | |
private const val KEY_SOCIAL_PROFILE_ID = "socialProfileId" | |
@Volatile | |
private var INSTANCE: Prefs? = null | |
fun get(context: Context): Prefs { | |
return INSTANCE ?: synchronized(this) { | |
INSTANCE ?: Prefs(context) | |
.also { INSTANCE = it } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment