-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpHelper.java
182 lines (151 loc) · 6.94 KB
/
SpHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package net.zahrauniversity.zahrauniversity.App;
import android.content.Context;
import android.content.SharedPreferences;
public class SpHelper {
private final static String PREF_FILE = "zahra-pref";
private final static String IS_LOGIN = "is_login";
private final static String ACCESS_TOKEN = "access_token";
private final static String ACCOUNT_TYPE = "account_type";
private final static String STUDENT = "student";
private final static String ADMIN = "admin";
private final static String PHOTOS = "photos";
private final static String UPDATES = "updates";
private final static String VIDEOS = "videos";
/**
* Set a string shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceString(Context context, String key, String value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.apply();
}
/**
* Set a integer shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceInt(Context context, String key, int value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
editor.apply();
}
public static void setPhotoTotal(Context context, int value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(PHOTOS, value);
editor.apply();
}
public static int getPhotoTotal(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(PHOTOS, 0);
}
public static void setUpdateTotal(Context context, int value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(UPDATES, value);
editor.apply();
}
public static int getUpdateTotal(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(UPDATES, 0);
}
public static void setVideoTotal(Context context, int value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(VIDEOS, value);
editor.apply();
}
public static int getVideoTotal(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(VIDEOS, 0);
}
/**
* Set a Boolean shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceBoolean(Context context, String key, boolean value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
editor.apply();
}
/**
* Get a string shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static String getSharedPreferenceString(Context context, String key, String defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getString(key, defValue);
}
/**
* Get a integer shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static int getSharedPreferenceInt(Context context, String key, int defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(key, defValue);
}
/**
* Get a boolean shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static boolean getSharedPreferenceBoolean(Context context, String key, boolean defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getBoolean(key, defValue);
}
public static boolean isLogin(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getBoolean(IS_LOGIN, false) && settings.contains(ACCESS_TOKEN);
}
// this method return true if login with student
// or false if login with admin
public static boolean hasLoginStudent(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
String account_type = settings.getString(ACCOUNT_TYPE, STUDENT);
return account_type.equals(STUDENT);
}
public static String getLocalToken(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return "Bearer " + settings.getString(ACCESS_TOKEN ,"");
}
public static void makeAdminLogin(Context context, String access_token){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IS_LOGIN, true);
editor.apply();
editor.putString(ACCESS_TOKEN, access_token);
editor.apply();
editor.putString(ACCOUNT_TYPE, ADMIN);
editor.apply();
}
public static void makeStudentLogin(Context context, String access_token){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IS_LOGIN, true);
editor.apply();
editor.putString(ACCESS_TOKEN, access_token);
editor.apply();
editor.putString(ACCOUNT_TYPE, STUDENT);
editor.apply();
}
public static void destroyLogin(Context context){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IS_LOGIN, false);
editor.apply();
editor.remove(ACCESS_TOKEN);
editor.remove(ACCOUNT_TYPE);
editor.apply();
}
}