Search code examples
javaandroidsession-management

Update a sessionManager


Hellow,

I have a long list of user infos stored in sessionManager. I am looking for the best way to update the user information without having to create again the createUserSession

I have that session manager class:

import android.content.Context;
import android.content.SharedPreferences;

public class SessionManager {

private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private final static String PREFS_Name = "app_prefs";
private final static int PRIVATE_MODE = 0;
private final static String IS_LOGGED = "isLogged";
private final static String TEL_EMAIL = "tel_email";
private final static String PASSWORD = "password";
private final static String ID = "id";
private final static String FIRST_NAME = "first_name";
private final static String LAST_NAME = "last_name";
private final static String TEL = "tel";
.........
.......
.......

public SessionManager(Context context) {
    prefs = context.getSharedPreferences(PREFS_Name, PRIVATE_MODE);
    editor = prefs.edit();
    editor.apply();
}

public boolean isLogged() {
    return prefs.getBoolean(IS_LOGGED, false);
}

public String getTelEmail() {
    return prefs.getString(TEL_EMAIL, null);
}
public String getTel() {
    return prefs.getString(TEL, null);
}
public String getAvatar() {
    return prefs.getString(AVATAR, null);
}
public String getBack() {
    return prefs.getString(BACK, null);
}
public String getIntroduction() {
    return prefs.getString(INTRODUCTION, null);
}
public String getIntroductionAudience() {
    return prefs.getString(INTRODUCTION_AUDIENCE, null);
}
public String getInfosIntroAudience() {
    return prefs.getString(INFOS_INTRO_AUDIENCE, null);
}
public String getWallVisibilityAudience() {
    return prefs.getString(WALL_VISIBILITY_AUDIENCE, null);
}
public String getWallPostInAudience() {
    return prefs.getString(WALL_POST_IN_AUDIENCE, null);
}


public void createUserSession(String id, String tel_email, String password, String first_name, String last_name,
                              String sex, String tel, String email,...........) {
    editor.putBoolean(IS_LOGGED, true);
    editor.putString(ID, id);
    editor.putString(TEL_EMAIL, tel_email);
    editor.putString(PASSWORD, password);
    editor.putString(FIRST_NAME, first_name);
    ......
    ......
    ......
    editor.putString(TEL, tel);
    ......
    ......
    ......
    editor.commit();
}

public void logOut() {
    editor.clear().commit();
    }
}

When I want to edit tel, I have to create again the sessionManager like below:

  // Update SessionManager
                        sessionManager.createUserSession(sessionManager.getId(),
                                sessionManager.getTelEmail(),
                                sessionManager.getPassword(),
                                sessionManager.getFirstName(),
                                sessionManager.getLastName(),
                                sessionManager.getSex(),
                                NewTel,                          // here i want to update the value
                                sessionManager.getEmail(),
                                sessionManager.getCountry(),
                                sessionManager.getIso2(),
                                sessionManager.getDay(),
                                sessionManager.getMonth(),
                                sessionManager.getYear(),
                                sessionManager.getAvatar(),
                                sessionManager.getBack(),
                                sessionManager.getIntroduction(),
                                sessionManager.getIntroductionAudience(),
                                sessionManager.getInfosIntroAudience(),
                                sessionManager.getTelEmailAudience(),
                                sessionManager.getSituation(),
                                sessionManager.getLive(),
                                sessionManager.getFrom(),
                                sessionManager.getSchool(),
                                sessionManager.getSchool_2(),
                                sessionManager.getSchool_3(),
                                sessionManager.getSchool_4(),
                                sessionManager.getSchool_5(),
                                sessionManager.getJob(),
                                sessionManager.getJob_2(),
                                sessionManager.getJob_3(),
                                sessionManager.getJob_4(),
                                sessionManager.getJob_5(),
                                sessionManager.getPassion(),
                                sessionManager.getTotalFriends(),
                                sessionManager.getWallVisibilityAudience(),
                                sessionManager.getWallPostInAudience(),
                                sessionManager.getWhoCanFollowMe(),
                                sessionManager.getWhoCanIdentifyMe(),
                                sessionManager.getWhoCanIdentifyMe2(),
                                sessionManager.getWhoCanIdentifyMe3(),
                                sessionManager.getLast_Post_Audience(),
                                sessionManager.getFriends_Audience(),
                                sessionManager.getShowOrHideGroupPageEvent(),
                                sessionManager.getShowOrHideUserOwnGroupPageEvent());

How could I change only the tel without having to create sessionManager.createUserSession again ?

How could I avoid to repeate creating the sessionManager.createUserSession with all it values again ?

Thanks.


Solution

  • Just Make separate function for this like

    public void setTel(String tel) {
        SharedPreferences.Editor editor = prefs.edit();
         editor.putString(TEL, tel);
    }
    

    Then call only this function;

      sessionManager.setTel("901234567")