As I was advised, I broke my code into 2 classes. Could you take a look at the repository class and report what problems this code has
public class JsonSharedPreferences {
private static Gson gson = new Gson();
private Context context;
public JsonSharedPreferences(Context context) {
this.context = context.getApplicationContext();
}
public <T> T loadObject(Class<T> classType, String key) {
SharedPreferences settings = context.getSharedPreferences(key, Context.MODE_PRIVATE);
String json = settings.getString(key, null);
return gson.fromJson(json, classType);
}
public <T> void saveObject(T objective, String key) {
SharedPreferences settings = context.getSharedPreferences(key, Context.MODE_PRIVATE);
String json = gson.toJson(objective);
settings.edit().putString(key, json).apply();
}
}
Objectiveclass does not hold a single responsibility. It acts as a POJO and Service layer class. I would create 2 different classes and segregate the functionalities. Using static will tempt to cause runtime rabbit holes. Therefore, verify your static methods would work properly on concurrent scenarios. \$\endgroup\$