public class SessionManger {
private static final String SHARED_PREFERENCE_FILE = "KEY";
public static String LANGUAGE_LOCALE = "lang_locale";
public static String LANGUAGE_COUNTRY = "lang_country";
public static String LANGUAGE_FONT = "lang_font";
private SharedPreferences mPreferences;
/**
* Initiate Session manager
*
* @param context current context
*/
public SessionManger(Context context) {
mPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE, Context.MODE_PRIVATE);
}
/**
* add/ update string value
*
* @param key identification
* @param value value for the key
*/
public void setStringKey(String key, String value) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(key, value);
editor.apply();
}
/**
* Bulk Input string
*
* @param bulkUpdate bulk key- value pair
*/
public void setStringKey(ArrayMap<String, String> bulkUpdate) {
SharedPreferences.Editor editor = mPreferences.edit();
Set<String> keys = bulkUpdate.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
editor.putString(key, bulkUpdate.get(key));
}
editor.apply();
}
/**
* add/ update Boolean value
*
* @param key identification
* @param value value for the key
*/
public void setBooleanKey(String key, boolean value) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
/**
* add/ update Int value
*
* @param key identification
* @param value value for the key
*/
public void setIntKey(String key, int value) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
/**
* Store the Array of strings
*
* @param key Name of the key to store
* @param stringArray arrays to store
*/
public void setStringArray(String key, ArrayList<String> stringArray) {
if (stringArray == null || stringArray.size() == 0) {
return;
}
SharedPreferences.Editor editor = mPreferences.edit();
Set<String> strings = new HashSet<>(stringArray);
editor.putStringSet(key, strings);
editor.apply();
}
/* public void removeStringArray(String key, ArrayList<String> stringArray) {
if (stringArray == null || stringArray.size() == 0) {
return;
}
SharedPreferences.Editor editor = mPreferences.edit();
Set<String> strings = new HashSet<>(stringArray);
editor.putStringSet(key, strings);
editor.remove(key);
}
*/
/**
* get the array strings to corresponding key
*
* @param key key which you need it
* @return @{@link ArrayList}
*/
public ArrayList<String> getStringArray(String key) {
return new ArrayList<String>(mPreferences.getStringSet(key, new HashSet<String>()));
}
/**
* Get the session value (Int)
*
* @param key Required value
* @return if key has value or 0 by default
*/
public int getIntKey(String key) {
return mPreferences.getInt(key, 0);
}
/**
* Get the session value (Boolean)
*
* @param key Required value
* @return if key has value or false by default
*/
public boolean getBooleanKey(String key) {
return mPreferences.getBoolean(key, false);
}
/**
* Get the session value (String)
*
* @param key Required value
* @return if key has value or empty by default
*/
public String getStringKey(String key) {
return mPreferences.getString(key, "");
}
/**
* Clear the session <br/>
* Expect LANGUAGE_COUNTRY,LANGUAGE_FONT,LANGUAGE_LOCALE
*/
public void clearSession() {
String fontName = getStringKey(LANGUAGE_FONT);
String country = getStringKey(LANGUAGE_COUNTRY);
String locale = getStringKey(LANGUAGE_LOCALE);
mPreferences.edit().clear().apply();
ArrayMap<String, String> bulkUpdate = new ArrayMap<>();
bulkUpdate.put(LANGUAGE_FONT, fontName);
bulkUpdate.put(LANGUAGE_COUNTRY, country);
bulkUpdate.put(LANGUAGE_LOCALE, locale);
setStringKey(bulkUpdate);
}
}
***********
public class Utils {
public static Retrofit getRetrofit(String baseDomain, Context context) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(BuildConfig.isShowLog ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new NoConnectivityInterceptor(context)).addInterceptor(logging).connectTimeout(0, TimeUnit.MINUTES).readTimeout(0, TimeUnit.MINUTES).build();
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
return new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(baseDomain)
.build();
}
/**
* To check network connection available or not
*
* @return true if available else no, not available
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
}
**********
public interface CallBack<T> {
/** * Success * * @param objects - Model object */ void success(T objects);
void responseFailure(T objects);
void connectionFailure(Throwable errorThrowable);
}
**********
public interface ApiManager {
String segments = "/{path}/index/";
/* @POST(segments + "?xxxx.json")
Call<apiMethodResponse> calledAPIMethod(@Path("path") String path,
@Body apiMethodRequest model, @Query("lang") String lang);
*/
}
Comments
Post a Comment