package ch.pizzacucina.android.helper; import android.content.SharedPreferences; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; import ch.pizzacucina.android.activity.BaseActivity; import ch.pizzacucina.android.model.CategoryModel; import ch.pizzacucina.android.model.CheckCouponModel; import ch.pizzacucina.android.model.CustomerTokenModel; import ch.pizzacucina.android.model.StoreModel; import ch.pizzacucina.android.model.UserModel; import static android.content.Context.MODE_PRIVATE; /** * Created by cimenmus on 25/06/2017. */ public class SharedPrefsHelper { private static final String SHARED_PREFS_NAME = "ch.pizzalink.android.preferences."; private static final String PREF_KEY_CATEGORY_LIST = SHARED_PREFS_NAME + "categoryList"; private static final String PREF_KEY_IGNORED_CATEGORY_ID_LIST = SHARED_PREFS_NAME + "ignoredCategoryIdList"; private static final String PREF_KEY_PIZZA_CATEGORY_ID_LIST = SHARED_PREFS_NAME + "pizzaCategoryIdList"; private static final String PREF_KEY_USER = SHARED_PREFS_NAME + "user"; private static final String PREF_KEY_CUSTOMER_TOKEN = SHARED_PREFS_NAME + "customerToken"; private static final String PREF_KEY_USER_LOG_IN_STATUS = SHARED_PREFS_NAME + "userLoginStatus"; private static final String PREF_KEY_CART_ITEM_COUNT = SHARED_PREFS_NAME + "cartItemCount"; private static final String PREF_KEY_CART_TOTAL_PRICE = SHARED_PREFS_NAME + "cartTotalPrice"; private static final String PREF_KEY_USER_SELECTED_STORE = SHARED_PREFS_NAME + "selectedStore"; private static final String PREF_KEY_IS_FIRST_TIME = SHARED_PREFS_NAME + "isFirstTime"; private static final String PREF_KEY_SELECTED_COUPON = SHARED_PREFS_NAME + "selectedCoupon"; private static final String PREF_KEY_SESSION_ID = SHARED_PREFS_NAME + "sessionId"; private static SharedPreferences sharedPreferences = BaseActivity.currentActivity .getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); private static SharedPreferences.Editor editor = sharedPreferences.edit(); private static Gson gson = new Gson(); public static void saveCategoryList(ArrayList categoryList){ String categoriesJsonString = gson.toJson(categoryList, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_CATEGORY_LIST, categoriesJsonString); editor.apply(); } public static ArrayList readCategoryList(){ Type categoryListType = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_CATEGORY_LIST, ""), categoryListType); } public static void saveIgnoredCategoryIdList(ArrayList ignoredCategoryIdList){ String ignoredCategoryIdsJsonString = gson.toJson(ignoredCategoryIdList, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_IGNORED_CATEGORY_ID_LIST, ignoredCategoryIdsJsonString); editor.apply(); } public static ArrayList readIgnoredCategoryIdList(){ Type ignoredCategoryIdListType = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_IGNORED_CATEGORY_ID_LIST, ""), ignoredCategoryIdListType); } public static void savePizzaCategoryIdList(ArrayList pizzaCategoryIdList){ String ignoredCategoryIdsJsonString = gson.toJson(pizzaCategoryIdList, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_PIZZA_CATEGORY_ID_LIST, ignoredCategoryIdsJsonString); editor.apply(); } public static ArrayList readPizzaCategoryIdList(){ Type pizzaCategoryIdList = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_PIZZA_CATEGORY_ID_LIST, ""), pizzaCategoryIdList); } public static void saveUser(UserModel user){ editor.putString(PREF_KEY_USER, gson.toJson(user)); editor.apply(); } public static UserModel getUser(){ return gson.fromJson(sharedPreferences.getString(PREF_KEY_USER, ""), UserModel.class); } public static void clearCustomerInfo(){ editor.remove(PREF_KEY_USER); editor.remove(PREF_KEY_CUSTOMER_TOKEN); editor.remove(PREF_KEY_CART_ITEM_COUNT); editor.remove(PREF_KEY_CART_TOTAL_PRICE); editor.remove(PREF_KEY_SELECTED_COUPON); editor.remove(PREF_KEY_SESSION_ID); editor.apply(); } public static void saveCustomerToken(CustomerTokenModel customerToken){ editor.putString(PREF_KEY_CUSTOMER_TOKEN, gson.toJson(customerToken)); editor.apply(); } public static CustomerTokenModel getCustomerToken(){ return gson.fromJson(sharedPreferences.getString(PREF_KEY_CUSTOMER_TOKEN, ""), CustomerTokenModel.class); } public static void clearCustomerToken(){ editor.remove(PREF_KEY_CUSTOMER_TOKEN); editor.apply(); } public static void setCustomerLoggedIn(boolean loggedId){ editor.putBoolean(PREF_KEY_USER_LOG_IN_STATUS, loggedId); editor.apply(); } public static boolean isCustomerLoggedIn(){ return sharedPreferences.getBoolean(PREF_KEY_USER_LOG_IN_STATUS, false); } public static void setCartItemCount(int cartItemCount){ editor.putInt(PREF_KEY_CART_ITEM_COUNT, cartItemCount); editor.apply(); } public static int getCartItemCount(){ return sharedPreferences.getInt(PREF_KEY_CART_ITEM_COUNT, 0); } public static String getCartTotalPrice(){ return sharedPreferences.getString(PREF_KEY_CART_TOTAL_PRICE, "0"); } public static void setCartTotalPrice(String cartTotalPrice){ editor.putString(PREF_KEY_CART_TOTAL_PRICE, cartTotalPrice); editor.apply(); } public static void saveSelectedStore(StoreModel storeModel){ editor.putString(PREF_KEY_USER_SELECTED_STORE, gson.toJson(storeModel)); editor.apply(); } public static StoreModel getSelectedStore(){ StoreModel selectedStoreModel = gson.fromJson(sharedPreferences.getString(PREF_KEY_USER_SELECTED_STORE, ""), StoreModel.class); if(selectedStoreModel == null){ selectedStoreModel = new StoreModel(); } return selectedStoreModel; } public static void clearSelectedStore(){ editor.remove(PREF_KEY_USER_SELECTED_STORE); editor.apply(); } public static void setIsFirstTime(boolean isFirstTime){ editor.putBoolean(PREF_KEY_IS_FIRST_TIME, isFirstTime); editor.apply(); } public static boolean isFirstTime(){ return sharedPreferences.getBoolean(PREF_KEY_IS_FIRST_TIME, true); } public static void saveSelectedCoupon(CheckCouponModel couponModel){ editor.putString(PREF_KEY_SELECTED_COUPON, gson.toJson(couponModel)); editor.apply(); } public static CheckCouponModel getSelectedCoupon(){ CheckCouponModel selectedCouponModel = gson.fromJson(sharedPreferences.getString(PREF_KEY_SELECTED_COUPON, ""), CheckCouponModel.class); if(selectedCouponModel != null && selectedCouponModel.getStoreName().toLowerCase().equals(SessionHelper.getSelectedStore().getStoreName().toLowerCase())){ return selectedCouponModel; } else { return null; } } public static void clearSelectedCoupon(){ editor.remove(PREF_KEY_SELECTED_COUPON); editor.apply(); } public static void saveSessionId(String sessionId){ editor.putString(PREF_KEY_SESSION_ID, sessionId); editor.apply(); } public static String getSessionId(){ return sharedPreferences.getString(PREF_KEY_SESSION_ID, ""); } public static void clearSessionId(){ editor.remove(PREF_KEY_SESSION_ID); editor.apply(); } /* public static void saveCategoryList(ArrayList cats){ ArrayList categories = new ArrayList<>(); for(Category category : cats){ if(category.getName().toLowerCase().contains("&")) category.setName(category.getName().replaceAll("(?i)&", "&")); if(!(category.getId().equals("1370") || category.getName().toLowerCase().equals("Uncategorized"))) categories.add(category); } sortCategories(categories); String categoriesJsonString = gson.toJson(categories, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_CATEGORY_LIST, categoriesJsonString); editor.apply(); } public static List readCategoryList(){ Type categoryListType = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_CATEGORY_LIST, ""), categoryListType); } public static void saveAuthorList(ArrayList authors){ String authorsJsonString = gson.toJson(authors, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_AUTHOR_LIST, authorsJsonString); editor.apply(); } public static List readAuthorList(){ Type authorListType = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_AUTHOR_LIST, ""), authorListType); } public static void saveMagazineList(ArrayList magazines){ String magazinesJsonString = gson.toJson(magazines, new TypeToken>() {}.getType()); editor.putString(PREF_KEY_MAGAZINE_LIST, magazinesJsonString); editor.apply(); } public static List readMagazineList(){ Type magazineListType = new TypeToken>(){}.getType(); return gson.fromJson(sharedPreferences.getString(PREF_KEY_MAGAZINE_LIST, ""), magazineListType); } private static void sortCategories(ArrayList categories){ ArrayList sortedCategories = new ArrayList<>(); for(int i = 0; i < 5; i++){ sortedCategories.add(categories.get(i)); } for(Category category : categories){ if(category.getName().toLowerCase(new Locale("tr", "TR")).equals("isviçre haberleri")) { sortedCategories.add(category); break; } } for(Category category : categories){ if(category.getName().toLowerCase(new Locale("tr", "TR")).equals("türkiyeden haberler")) { sortedCategories.add(category); break; } } for(Category category : categories){ if(!sortedCategories.contains(category)) sortedCategories.add(category); } categories.clear(); categories.addAll(sortedCategories); sortedCategories.clear(); } */ }