Files
Pizzalemon/app/src/main/java/ch/pizzacucina/android/helper/SharedPrefsHelper.java

314 lines
12 KiB
Java

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.GeneralCouponModel;
import ch.pizzacucina.android.model.PersonalCouponModel;
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_GENERAL_COUPON = SHARED_PREFS_NAME + "selectedGeneralCoupon";
private static final String PREF_KEY_SELECTED_PERSONAL_COUPON = SHARED_PREFS_NAME + "selectedPersonalCoupon";
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<CategoryModel> categoryList){
String categoriesJsonString = gson.toJson(categoryList, new TypeToken<ArrayList<CategoryModel>>() {}.getType());
editor.putString(PREF_KEY_CATEGORY_LIST, categoriesJsonString);
editor.apply();
}
public static ArrayList<CategoryModel> readCategoryList(){
Type categoryListType = new TypeToken<ArrayList<CategoryModel>>(){}.getType();
return gson.fromJson(sharedPreferences.getString(PREF_KEY_CATEGORY_LIST, ""), categoryListType);
}
public static void saveIgnoredCategoryIdList(ArrayList<Integer> ignoredCategoryIdList){
String ignoredCategoryIdsJsonString = gson.toJson(ignoredCategoryIdList, new TypeToken<ArrayList<Integer>>() {}.getType());
editor.putString(PREF_KEY_IGNORED_CATEGORY_ID_LIST, ignoredCategoryIdsJsonString);
editor.apply();
}
public static ArrayList<Integer> readIgnoredCategoryIdList(){
Type ignoredCategoryIdListType = new TypeToken<ArrayList<Integer>>(){}.getType();
return gson.fromJson(sharedPreferences.getString(PREF_KEY_IGNORED_CATEGORY_ID_LIST, ""), ignoredCategoryIdListType);
}
public static void savePizzaCategoryIdList(ArrayList<Integer> pizzaCategoryIdList){
String ignoredCategoryIdsJsonString = gson.toJson(pizzaCategoryIdList, new TypeToken<ArrayList<Integer>>() {}.getType());
editor.putString(PREF_KEY_PIZZA_CATEGORY_ID_LIST, ignoredCategoryIdsJsonString);
editor.apply();
}
public static ArrayList<Integer> readPizzaCategoryIdList(){
Type pizzaCategoryIdList = new TypeToken<ArrayList<Integer>>(){}.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_GENERAL_COUPON);
editor.remove(PREF_KEY_SELECTED_PERSONAL_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 saveSelectedGeneralCoupon(GeneralCouponModel couponModel){
editor.putString(PREF_KEY_SELECTED_GENERAL_COUPON, gson.toJson(couponModel));
editor.apply();
}
public static GeneralCouponModel getSelectedGeneralCoupon(){
GeneralCouponModel selectedCouponModel = gson.fromJson(sharedPreferences.getString(PREF_KEY_SELECTED_GENERAL_COUPON, ""), GeneralCouponModel.class);
if(selectedCouponModel != null &&
selectedCouponModel.getStoreName().toLowerCase().equals(SessionHelper.getSelectedStore().getStoreName().toLowerCase())){
return selectedCouponModel;
}
else {
return null;
}
}
public static void clearSelectedGeneralCoupon(){
editor.remove(PREF_KEY_SELECTED_GENERAL_COUPON);
editor.apply();
}
public static void saveSelectedPersonalCoupon(PersonalCouponModel couponModel){
editor.putString(PREF_KEY_SELECTED_PERSONAL_COUPON, gson.toJson(couponModel));
editor.apply();
}
public static PersonalCouponModel getSelectedPersonalCoupon(){
PersonalCouponModel selectedCouponModel = gson.fromJson(sharedPreferences.getString(PREF_KEY_SELECTED_PERSONAL_COUPON, ""), PersonalCouponModel.class);
if(selectedCouponModel != null &&
selectedCouponModel.getStoreName().toLowerCase().equals(SessionHelper.getSelectedStore().getStoreName().toLowerCase())){
return selectedCouponModel;
}
else {
return null;
}
}
public static void clearSelectedPersonalCoupon(){
editor.remove(PREF_KEY_SELECTED_PERSONAL_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<Category> cats){
ArrayList<Category> categories = new ArrayList<>();
for(Category category : cats){
if(category.getName().toLowerCase().contains("&amp;"))
category.setName(category.getName().replaceAll("(?i)&amp;", "&"));
if(!(category.getId().equals("1370") || category.getName().toLowerCase().equals("Uncategorized")))
categories.add(category);
}
sortCategories(categories);
String categoriesJsonString = gson.toJson(categories, new TypeToken<ArrayList<Category>>() {}.getType());
editor.putString(PREF_KEY_CATEGORY_LIST, categoriesJsonString);
editor.apply();
}
public static List<Category> readCategoryList(){
Type categoryListType = new TypeToken<ArrayList<Category>>(){}.getType();
return gson.fromJson(sharedPreferences.getString(PREF_KEY_CATEGORY_LIST, ""), categoryListType);
}
public static void saveAuthorList(ArrayList<Author> authors){
String authorsJsonString = gson.toJson(authors, new TypeToken<ArrayList<Author>>() {}.getType());
editor.putString(PREF_KEY_AUTHOR_LIST, authorsJsonString);
editor.apply();
}
public static List<Author> readAuthorList(){
Type authorListType = new TypeToken<ArrayList<Author>>(){}.getType();
return gson.fromJson(sharedPreferences.getString(PREF_KEY_AUTHOR_LIST, ""), authorListType);
}
public static void saveMagazineList(ArrayList<Magazine> magazines){
String magazinesJsonString = gson.toJson(magazines, new TypeToken<ArrayList<Magazine>>() {}.getType());
editor.putString(PREF_KEY_MAGAZINE_LIST, magazinesJsonString);
editor.apply();
}
public static List<Magazine> readMagazineList(){
Type magazineListType = new TypeToken<ArrayList<Magazine>>(){}.getType();
return gson.fromJson(sharedPreferences.getString(PREF_KEY_MAGAZINE_LIST, ""), magazineListType);
}
private static void sortCategories(ArrayList<Category> categories){
ArrayList<Category> 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();
}
*/
}