As we all know, every App requires a login, and the login information is saved in a local file, and then we write a bunch of code to operate SharedPreferences.
Now, you can get rid of that completely, and you can do login information, user information management in one line of code. Implement global operations. Decouple your program.
Get started
public void sessionDemo(a) {
// Get the login information
TokenInfo token = SessionManager.getDefault().getUserToken();
// Get user information
UserInfo userInfo = SessionManager.getDefault().getUser();
// Whether to log in
boolean isLogin = SessionManager.getDefault().isLogin();
// Log out
SessionManager.getDefault().clear();
// Set user information after successful login
SessionManager.getDefault().setUser(new UserInfo());
// Set the login information
SessionManager.getDefault().setUserToken(new TokenInfo());
Log.i("rae"."Login Information:" + token);
Log.i("rae"."User Information:" + userInfo);
Log.i("rae"."Login or not:" + isLogin);
}
Copy the code
Initial Configuration
// Call the initial configuration information in Application onCreate
SessionManager.initWithConfig(
new SessionManager.ConfigBuilder()
.context(this)
.tokenClass(TokenInfo.class)
.userClass(UserInfo.class)
.build());
Copy the code
User-defined user information UserInfo Entity class
Does the default UserInfo type field not satisfy the requirement? No problem, you can customize it!
// Initialize the configuration information
SessionManager.initWithConfig(
new SessionManager.ConfigBuilder()
.context(this)
.tokenClass(MyTokenInfo.class) // Your custom TokenInfo class
.userClass(MyUserInfo.class) // Your custom UserInfo class
.build());
Copy the code
Reference library:
implementation 'com. Making. Raedev: session: 1.0.0'
Copy the code
If the download fails, add it to the root directory build.gradle:
allprojects {
repositories {
maven { url "https://dl.bintray.com/raee/Android"}}}Copy the code
A, design,
Abstract factory model, the overall UML as shown below, the overall idea is:
Abstract out our SessionManager class, actual or PreferencesSessionManager realized this abstract class, locally SharePreferences preservation.
Config: maintains the entity class for JSON serialization and saves it locally. This class performs generic parsing, and the objects returned by getUser() are maintained by this class. Thus achieving the role of decoupling.
ConfigBuild: Builder mode, which provides easy session configuration.
Our SessionManager. GetDefault (); The default return is PreferencesSessionManager instance.
Custom SessionManger
If you don’t want to use SharePreferences to store user information, you can also inherit the SessionManger class to implement abstract methods to implement custom Session management.
If your customizations also want global access, add an AppSessionManger class that implements a singleton method that returns your custom implementation.
Refer to the implementation of sessionManger.getDefault ().
SessionManger code implementation
/** * Created by ChenRui on 2017/4/28 0028 17:27. */
public abstract class SessionManager {
public static class Config { Class<? > userTokenClass; Class<? > userClass; Context context; }public static class ConfigBuilder {
private final Config mConfig;
public ConfigBuilder(a) {
mConfig = new Config();
}
public ConfigBuilder tokenClass(Class
cls) {
mConfig.userTokenClass = cls;
return this;
}
public ConfigBuilder userClass(Class
cls) {
mConfig.userClass = cls;
return this;
}
public ConfigBuilder context(Context applicationContext) {
mConfig.context = applicationContext;
return this;
}
public Config build(a) {
returnmConfig; }}private static Config sConfig;
private static WeakReference<SessionManager> managerWeakReference;
/** * gets the default session manager, which is the cookie manager. * Please use {@link#initWithConfig(Config)} for initial configuration. * /
public static SessionManager getDefault(a) {
if (sConfig == null) {
Log.w("SessionManager"."session config from default");
sConfig = new ConfigBuilder().tokenClass(SessionToken.class).userClass(SessionUserInfo.class).build();
}
if (managerWeakReference == null || managerWeakReference.get() == null) {
synchronized (SessionManager.class) {
if (managerWeakReference == null || managerWeakReference.get() == null) {
managerWeakReference = new WeakReference<SessionManager>(newPreferencesSessionManager(sConfig)); }}}return managerWeakReference.get();
}
/** * Initializes the session manager */
public static void initWithConfig(Config config) {
if(sConfig ! =null) {
sConfig = null;
System.gc();
}
sConfig = config;
}
SessionManager() {
}
/** * Whether to log in */
public abstract boolean isLogin(a);
/** * Clear session information, that is, log out. * /
public abstract void clear(a);
/** * get the information of the current logged-in user.@link#isLogin()} to determine whether to log in */
public abstract <T> T getUser(a);
/** * Sets the current user information */
public abstract <T> void setUser(T user);
/** * Set user authorization information **@paramToken Authorization information */
public abstract <T> void setUserToken(T token);
/** * Obtain user authorization information */
public abstract <T> T getUserToken(a);
}
Copy the code
The end of this article, very simple implementation, but very practical!
Open source: github.com/raedev/Andr…