1 package com.moiseum.wolnelektury.utils;
3 import android.content.Context;
4 import android.content.SharedPreferences;
6 import com.moiseum.wolnelektury.connection.models.OAuthTokenModel;
8 import de.adorsys.android.securestoragelibrary.SecurePreferences;
11 * Created by Piotr Ostrowski on 14.06.2018.
13 public final class SharedPreferencesUtils {
15 private static final String PREFERENCES_FILENAME = "WolneLekturyPreferences";
16 private static final String ACCESS_TOKEN_KEY = "AccessToken";
17 private static final String ACCESS_TOKEN_SECRET_KEY = "AccessTokenSecret";
18 private static final String USERNAME_KEY = "Username";
19 private static final String PREMIUM_KEY = "Premium";
20 private static final String NOTIFICATIONS_KEY = "Notifications";
21 private static final String TEMPORARY_LOGIN_TOKEN_KEY = "TemporaryLoginTokenKey";
23 private OAuthTokenModel currentToken;
24 private String username;
25 private Boolean isPremium;
26 private Boolean notifications;
27 private String temporaryLoginToken;
28 private SharedPreferences preferences;
30 public SharedPreferencesUtils(Context context) {
31 this.preferences = context.getSharedPreferences(PREFERENCES_FILENAME, Context.MODE_PRIVATE);
34 public void storeAccessToken(OAuthTokenModel tokenModel) {
35 currentToken = tokenModel;
36 SecurePreferences.setValue(ACCESS_TOKEN_KEY, tokenModel.getToken());
37 SecurePreferences.setValue(ACCESS_TOKEN_SECRET_KEY, tokenModel.getTokenSecret());
40 public OAuthTokenModel getAccessToken() {
41 if (currentToken != null) {
45 String token = SecurePreferences.getStringValue(ACCESS_TOKEN_KEY, null);
46 String tokenSecret = SecurePreferences.getStringValue(ACCESS_TOKEN_SECRET_KEY, null);
48 if (token == null || tokenSecret == null) {
51 currentToken = new OAuthTokenModel();
52 currentToken.setToken(token);
53 currentToken.setTokenSecret(tokenSecret);
57 public String getUsername() {
58 if (username == null) {
59 username = preferences.getString(USERNAME_KEY, null);
64 public void setUsername(String username) {
65 this.username = username;
66 preferences.edit().putString(USERNAME_KEY, username).apply();
69 public boolean isUserLoggedIn() {
70 return getAccessToken() != null;
73 public boolean isUserPremium() {
74 if (isPremium == null) {
75 isPremium = preferences.getBoolean(PREMIUM_KEY, false);
77 return isPremium && isUserLoggedIn();
80 public void setPremium(boolean isPremium) {
81 this.isPremium = isPremium;
82 preferences.edit().putBoolean(PREMIUM_KEY, isPremium).apply();
85 public boolean getNotifications() {
86 if (notifications == null) {
87 notifications = preferences.getBoolean(NOTIFICATIONS_KEY, true);
92 public void setNotifications(Boolean notifications) {
93 this.notifications = notifications;
94 preferences.edit().putBoolean(NOTIFICATIONS_KEY, notifications).apply();
97 public String getTemporaryLoginToken() {
98 if (temporaryLoginToken == null) {
99 temporaryLoginToken = preferences.getString(TEMPORARY_LOGIN_TOKEN_KEY, null);
101 return temporaryLoginToken;
104 public void setTemporaryLoginToken(String temporaryLoginToken) {
105 this.temporaryLoginToken = temporaryLoginToken;
106 preferences.edit().putString(TEMPORARY_LOGIN_TOKEN_KEY, temporaryLoginToken).apply();
109 public void clearUserData() {
113 notifications = null;
114 temporaryLoginToken = null;
115 SecurePreferences.clearAllValues();
117 .remove(USERNAME_KEY)
119 .remove(NOTIFICATIONS_KEY)
120 .remove(TEMPORARY_LOGIN_TOKEN_KEY)