51 lines
1.5 KiB
Java
51 lines
1.5 KiB
Java
package com.iamthefij.otbeta;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.iamthefij.otbeta.api.Exerciser;
|
|
|
|
/**
|
|
* Store authenticated user information to device shared preferences
|
|
*/
|
|
|
|
class ExerciserStore {
|
|
|
|
private static final String PREF_DEFAULT_STRING = "";
|
|
private static final String PREF_EXERCISER_KEY = "exerciser_json";
|
|
private static final String PREFS_NAME = ExerciserStore.class.getName();
|
|
private final Context mContext;
|
|
|
|
public ExerciserStore(Context context) {
|
|
mContext = context;
|
|
}
|
|
|
|
public Exerciser getExerciser() {
|
|
Exerciser exerciser = null;
|
|
String exerciserJson = getExerciserJsonString();
|
|
if (!exerciserJson.equals(PREF_DEFAULT_STRING)) {
|
|
Gson gson = new Gson();
|
|
exerciser = gson.fromJson(exerciserJson, Exerciser.class);
|
|
}
|
|
|
|
return exerciser;
|
|
}
|
|
|
|
public void saveExerciser(Exerciser exerciser) {
|
|
Gson gson = new Gson();
|
|
String jsonSessionCookieString = gson.toJson(exerciser);
|
|
SharedPreferences.Editor editor = getPrefs().edit();
|
|
editor.putString(PREF_EXERCISER_KEY, jsonSessionCookieString);
|
|
editor.apply();
|
|
}
|
|
|
|
private String getExerciserJsonString() {
|
|
return getPrefs().getString(PREF_EXERCISER_KEY, PREF_DEFAULT_STRING);
|
|
}
|
|
|
|
private SharedPreferences getPrefs() {
|
|
return mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|
}
|
|
}
|