HSE-Observation-Android-App/app/src/main/java/com/utopiaindustries/hseobservationsapp/helper/Helper.java

150 lines
6.0 KiB
Java

package com.utopiaindustries.hseobservationsapp.helper;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import androidx.preference.PreferenceManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Helper {
public static final String project_file = "Quality-Control";
public static final String departmentId = "departId";
public static final String departmentName = "departName";
public static final String locationSiteId = "locationSiteId";
public static final String locationSiteName = "locationSiteName";
public static final String unitId = "unitId";
public static final String unitName = "unitName";
public static final String floorId = "floorId";
public static final String floorName = "floorName";
public static final String logInUser = "LogInUser";
public static final String firstTimeApiCall = "isFirstTimeApiCall";
public static final String homeSite = "Sites";
public static final String superVisor = "SuperVisor";
public static final String recordType = "RecordType";
public static final String observationClass = "observationClass";
public static final String observationSubClass = "observationSubClass";
public static final String hseFloors = "hseFloors";
public static final String hseActivities = "hseActivities";
public static final String hseDepartment = "hseDepartment";
public static final String hseIncidentTypes = "hseIncidentTypes";
public static final String hseInjuredBodyPart = "hseInjuredBodyPart";
public static final String hsePtwType = "hsePtwType";
public static final String hseSafetyTrainingTopics = "hseSafetyTrainingTopics";
public static final String hseBuildings = "hseBuildings";
public static final String hseInjuries = "hseInjuries";
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
public static boolean isNetworkConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
static public void setPreferenceObject(Context c, Object modal, String key) {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(c.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String jsonObject = gson.toJson(modal);
prefsEditor.putString(key, jsonObject);
prefsEditor.commit();
}
/*static public QualityControlResponse getPreferenceObjectJson(Context c, String key) {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(c.getApplicationContext());
String json = appSharedPrefs.getString(key, "");
Gson gson = new Gson();
QualityControlResponse selectedUser = gson.fromJson(json, QualityControlResponse.class);
return selectedUser;
}*/
/*static public void saveArrayList(List<ItemModel> list, String key, Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(key, json);
editor.apply();
}*/
/*static public List<ItemModel> getArrayList(String key, Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Check if the key exists
if (!prefs.contains(key)) {
return null; // Return null if the key doesn't exist
}
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = new TypeToken<ArrayList<ItemModel>>() {}.getType();
return gson.fromJson(json, type);
}*/
static public void RemoveArrayList(String key, Context context) {
if (context != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.contains(key)) { // Check if the key exists
SharedPreferences.Editor editor = prefs.edit();
editor.remove(key); // Remove the key-value pair
editor.apply(); // Apply changes
///Log.e("SharedPreferences", "Key '" + key + "' removed successfully.");
} else {
Log.e("SharedPreferences", "Key '" + key + "' does not exist.");
}
}
}
//for department, site, unit, floor
public static <T> void saveList(List<T> list, String key, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list); // Convert the list to JSON
editor.putString(key, json);
editor.apply(); // Save to SharedPreferences
}
public static <T> List<T> getList(String key, Context context, Class<T> clazz) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Check if the key exists
if (!prefs.contains(key)) {
return new ArrayList<>(); // Return an empty list if the key doesn't exist
}
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = TypeToken.getParameterized(ArrayList.class, clazz).getType(); // Use the provided class type
return gson.fromJson(json, type); // Convert JSON back to the list
}
}