Commit b21a9e47 authored by Varun Patil's avatar Varun Patil

Memcache current user in utils

parent e79b2aa7
......@@ -45,6 +45,7 @@ public final class Utils {
public static UpdatableList<Event> eventCache = new UpdatableList<>();
public static UpdatableList<Notification> notificationCache = null;
public static UpdatableList<Body> bodyCache = new UpdatableList<>();
public static User currentUserCache = null;
private static String sessionId;
private static RetrofitInterface retrofitInterface;
......
......@@ -427,6 +427,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if (response.isSuccessful()) {
session.createLoginSession(response.body().getUserName(), response.body(), session.getSessionID());
currentUser = response.body();
Utils.currentUserCache = currentUser;
} else {
session.logout();
currentUser = null;
......
......@@ -111,24 +111,29 @@ public class SettingsFragment extends PreferenceFragmentCompat {
toolbar.setTitle("Settings");
Utils.setSelectedMenuItem(getActivity(), R.id.nav_settings);
// Get the user id
Bundle bundle = getArguments();
String userID = bundle.getString(Constants.USER_ID);
// Fill in the user
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getUser(Utils.getSessionIDHeader(), userID).enqueue(new EmptyCallback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
if(getActivity() == null || getView() == null) return;
User user = response.body();
showContactPref.setChecked(user.getShowContactNumber());
showContactPref.setEnabled(true);
if (Utils.currentUserCache == null) {
// Get the user id
Bundle bundle = getArguments();
String userID = bundle.getString(Constants.USER_ID);
// Fill in the user
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getUser(Utils.getSessionIDHeader(), userID).enqueue(new EmptyCallback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
if (getActivity() == null || getView() == null) return;
User user = response.body();
showContactPref.setChecked(user.getShowContactNumber());
showContactPref.setEnabled(true);
Utils.currentUserCache = user;
}
}
}
});
});
} else {
showContactPref.setChecked(Utils.currentUserCache.getShowContactNumber());
showContactPref.setEnabled(true);
}
}
public void toggleShowContact(final SwitchPreferenceCompat showContactPref, Object o) {
......@@ -142,6 +147,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
if (response.isSuccessful()) {
showContactPref.setChecked(response.body().getShowContactNumber());
showContactPref.setEnabled(true);
Utils.currentUserCache = response.body();
} else {
showContactPref.setChecked(!isChecked);
showContactPref.setEnabled(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment