Created
November 13, 2014 07:55
-
-
Save edBaev/c76f55c8e25eae3b1356 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blacksquared.changers.manager; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import com.blacksquared.changers.Constants; | |
import com.blacksquared.changers.interfaces.Initializer; | |
import com.blacksquared.changers.model.CachedValue; | |
import java.util.HashSet; | |
import java.util.Set; | |
public class SharedPrefManager implements Initializer { | |
private static final String NAME = "sharedPrefs"; | |
public static final String LOGIN_TOKEN = "login_token"; | |
public static final String IS_FIRST = "is_first"; | |
public static final String GUID = "guid"; | |
public static final String USERNAME = "username"; | |
public static final String EMAIL = "email"; | |
public static final String TEMP_EMAIL = "temp_email"; | |
public static final String IS_SOCIALS_LOGGED = "is_socials_logged"; | |
public static final String TYPE_TRACK_CODE = "type_track_code"; | |
private static SharedPreferences sp; | |
private Set<CachedValue> cachedValues; | |
private CachedValue<String> loginToken; | |
private CachedValue<String> username; | |
private CachedValue<Boolean> isFirst; | |
private CachedValue<String> guid; | |
private CachedValue<String> email; | |
private CachedValue<String> tempEmail; | |
private CachedValue<Boolean> socialsLogged; | |
private CachedValue<Integer> typeTrackCode; | |
@Override | |
public void init(Context context) { | |
sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); | |
CachedValue.initialize(sp); | |
cachedValues = new HashSet<>(); | |
cachedValues.add(loginToken = new CachedValue<>(LOGIN_TOKEN, String.class)); | |
cachedValues.add(isFirst = new CachedValue<>(IS_FIRST, false, Boolean.class)); | |
cachedValues.add(guid = new CachedValue<>(GUID, String.class)); | |
cachedValues.add(username = new CachedValue<>(USERNAME, String.class)); | |
cachedValues.add(email = new CachedValue<>(EMAIL, String.class)); | |
cachedValues.add(tempEmail = new CachedValue<>(TEMP_EMAIL, String.class)); | |
cachedValues.add(socialsLogged = new CachedValue<>(IS_SOCIALS_LOGGED, false, Boolean.class)); | |
cachedValues.add(typeTrackCode = new CachedValue<>(TYPE_TRACK_CODE, Constants.ActivityCode.CYCLE_CODE, | |
Integer.class)); | |
} | |
void setEmail(String email) { | |
this.email.setValue(email); | |
} | |
String getEmail() { | |
return email.getValue(); | |
} | |
void setTempEmail(String tempEmail) { | |
this.tempEmail.setValue(tempEmail); | |
} | |
String getTempEmail() { | |
return tempEmail.getValue(); | |
} | |
void setLoginToken(String loginToken) { | |
this.loginToken.setValue(loginToken); | |
} | |
String getLoginToken() { | |
return loginToken.getValue(); | |
} | |
void setGuid(String guid) { | |
this.guid.setValue(guid); | |
} | |
String getGuid() { | |
return guid.getValue(); | |
} | |
void setActityTypeCode(int code) { | |
this.typeTrackCode.setValue(code); | |
} | |
int getActivityTypeCode() { | |
return typeTrackCode.getValue(); | |
} | |
@Override | |
public void clear() { | |
for (CachedValue value : cachedValues) { | |
if (!value.getName().equals(IS_FIRST)) { | |
value.clear(); | |
} | |
} | |
sp.edit().remove(NAME) | |
.remove(LOGIN_TOKEN) | |
.remove(GUID) | |
.remove(USERNAME) | |
.remove(EMAIL) | |
.remove(TEMP_EMAIL) | |
.remove(IS_SOCIALS_LOGGED) | |
.commit(); | |
} | |
boolean isFirst() { | |
return this.isFirst.getValue(); | |
} | |
void setIsFirst(boolean isFirst) { | |
this.isFirst.setValue(isFirst); | |
} | |
boolean isSocialsLogged() { | |
return this.socialsLogged.getValue(); | |
} | |
void setSocialsLogged(boolean socialsLogged) { | |
this.socialsLogged.setValue(socialsLogged); | |
} | |
String getUsername() { | |
return this.username.getValue(); | |
} | |
public void setUsername(String username) { | |
this.username.setValue(username); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment