Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / utils / SharedPreferencesUtils.java
1 package com.moiseum.wolnelektury.utils;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5
6 import com.moiseum.wolnelektury.connection.models.OAuthTokenModel;
7
8 import de.adorsys.android.securestoragelibrary.SecurePreferences;
9
10 /**
11  * Created by Piotr Ostrowski on 14.06.2018.
12  */
13 public final class SharedPreferencesUtils {
14
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";
22
23         private OAuthTokenModel currentToken;
24         private String username;
25         private Boolean isPremium;
26         private Boolean notifications;
27         private String temporaryLoginToken;
28         private SharedPreferences preferences;
29
30         public SharedPreferencesUtils(Context context) {
31                 this.preferences = context.getSharedPreferences(PREFERENCES_FILENAME, Context.MODE_PRIVATE);
32         }
33
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());
38         }
39
40         public OAuthTokenModel getAccessToken() {
41                 if (currentToken != null) {
42                         return currentToken;
43                 }
44
45                 String token = SecurePreferences.getStringValue(ACCESS_TOKEN_KEY, null);
46                 String tokenSecret = SecurePreferences.getStringValue(ACCESS_TOKEN_SECRET_KEY, null);
47
48                 if (token == null || tokenSecret == null) {
49                         return null;
50                 }
51                 currentToken = new OAuthTokenModel();
52                 currentToken.setToken(token);
53                 currentToken.setTokenSecret(tokenSecret);
54                 return currentToken;
55         }
56
57         public String getUsername() {
58                 if (username == null) {
59                         username = preferences.getString(USERNAME_KEY, null);
60                 }
61                 return username;
62         }
63
64         public void setUsername(String username) {
65                 this.username = username;
66                 preferences.edit().putString(USERNAME_KEY, username).apply();
67         }
68
69         public boolean isUserLoggedIn() {
70                 return getAccessToken() != null;
71         }
72
73         public boolean isUserPremium() {
74                 if (isPremium == null) {
75                         isPremium = preferences.getBoolean(PREMIUM_KEY, false);
76                 }
77                 return isPremium && isUserLoggedIn();
78         }
79
80         public void setPremium(boolean isPremium) {
81                 this.isPremium = isPremium;
82                 preferences.edit().putBoolean(PREMIUM_KEY, isPremium).apply();
83         }
84
85         public boolean getNotifications() {
86                 if (notifications == null) {
87                         notifications = preferences.getBoolean(NOTIFICATIONS_KEY, true);
88                 }
89                 return notifications;
90         }
91
92         public void setNotifications(Boolean notifications) {
93                 this.notifications = notifications;
94                 preferences.edit().putBoolean(NOTIFICATIONS_KEY, notifications).apply();
95         }
96
97         public String getTemporaryLoginToken() {
98                 if (temporaryLoginToken == null) {
99                         temporaryLoginToken = preferences.getString(TEMPORARY_LOGIN_TOKEN_KEY, null);
100                 }
101                 return temporaryLoginToken;
102         }
103
104         public void setTemporaryLoginToken(String temporaryLoginToken) {
105                 this.temporaryLoginToken = temporaryLoginToken;
106                 preferences.edit().putString(TEMPORARY_LOGIN_TOKEN_KEY, temporaryLoginToken).apply();
107         }
108
109         public void clearUserData() {
110                 currentToken = null;
111                 username = null;
112                 isPremium = null;
113                 notifications = null;
114                 temporaryLoginToken = null;
115                 SecurePreferences.clearAllValues();
116                 preferences.edit()
117                                 .remove(USERNAME_KEY)
118                                 .remove(PREMIUM_KEY)
119                                 .remove(NOTIFICATIONS_KEY)
120                                 .remove(TEMPORARY_LOGIN_TOKEN_KEY)
121                                 .apply();
122         }
123 }