Android Open Source - Gazetti_Newspaper_Reader User Pref Util






From Project

Back to project page Gazetti_Newspaper_Reader.

License

The source code is released under:

MIT License

If you think the Android project Gazetti_Newspaper_Reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package in.sahildave.gazetti.util;
//from w w w.j  a  va 2s . c  om
import android.content.Context;
import in.sahildave.gazetti.homescreen.adapter.CellModel;
import in.sahildave.gazetti.util.GazettiEnums.Category;
import in.sahildave.gazetti.util.GazettiEnums.Newspapers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sahil on 9/11/14.
 */
public class UserPrefUtil {
    private static final String LOG_TAG = UserPrefUtil.class.getName();
    private static UserPrefUtil _instance = null;
    private Context context;

    public static synchronized UserPrefUtil getInstance(Context context){
        if (_instance == null) {
            _instance = new UserPrefUtil(context.getApplicationContext());
        }
        return _instance;
    }

    private UserPrefUtil(Context parentContext) {
        context = parentContext;
    }

    public List<CellModel> getUserPrefCellList(){

        List<CellModel> returnList = new ArrayList<CellModel>();
        Map<String, List<String>> userPrefMap = NewsCatFileUtil.getInstance(context).getUserSelectionMap();

        GazettiEnums gazettiEnums = new GazettiEnums();
        for (String newspaper : userPrefMap.keySet()) {
            List<String> categoriesSelected = userPrefMap.get(newspaper);
            Newspapers npEnum = gazettiEnums.getNewspaperFromName(newspaper);
            for (String category : categoriesSelected) {
                Category catEnum = gazettiEnums.getCategoryFromName(category);
                CellModel cellModel = new CellModel(npEnum, catEnum);
                returnList.add(cellModel);
            }
        }

        //Log.d(LOG_TAG, "Returning CellList - "+returnList);
        return returnList;
    }

    public void replaceUserPref(CellModel oldCell, CellModel newCell){
        Map<String, List<String>> userPrefMap = NewsCatFileUtil.getInstance(context).getUserSelectionMap();
        String oldNewspaper = oldCell.getNewspaperTitle();
        String oldCategory = oldCell.getCategoryTitle();
        String newNewspaper = newCell.getNewspaperTitle();
        String newCategory = newCell.getCategoryTitle();

        if(oldNewspaper.equalsIgnoreCase(newNewspaper)){
            if(userPrefMap.containsKey(oldNewspaper)) {
                List<String> categories = userPrefMap.get(oldNewspaper);
                if(categories.contains(oldCategory)){
                    categories.remove(oldCategory);
                    categories.add(newCategory);

                    userPrefMap.remove(oldNewspaper);
                    //Log.d(LOG_TAG, "Removed - "+oldCell.toString()+", Added - "+newCell.toString());
                    updateUserSelectionMap(userPrefMap, oldNewspaper, categories);
                }
            }
        } else {
            deleteUserPref(oldCell);
            addUserPref(newCell);
        }
    }

    public void addUserPref(CellModel newCell){
        Map<String, List<String>> userPrefMap = NewsCatFileUtil.getInstance(context).getUserSelectionMap();
        String newspaper = newCell.getNewspaperTitle();
        String category = newCell.getCategoryTitle();

        List<String> categories;
        if(userPrefMap.containsKey(newspaper)){
            categories = userPrefMap.get(newspaper);
            if(!categories.contains(category)){
                categories.add(category);
                userPrefMap.remove(newspaper);
            }
        } else {
            categories = new ArrayList<String>();
            categories.add(category);
        }
        //Log.d(LOG_TAG, "Added - "+newCell.toString());
        updateUserSelectionMap(userPrefMap, newspaper, categories);
    }

    public void deleteUserPref(CellModel deleteCell){
        Map<String, List<String>> userPrefMap = NewsCatFileUtil.getInstance(context).getUserSelectionMap();
        String newspaper = deleteCell.getNewspaperTitle();
        String category = deleteCell.getCategoryTitle();

        if(userPrefMap.containsKey(newspaper)){
            List<String> categories = userPrefMap.get(newspaper);
            if(categories.contains(category)){
                categories.remove(category);
                userPrefMap.remove(newspaper);

                //Log.d(LOG_TAG, "Deleted - "+deleteCell.toString());
                updateUserSelectionMap(userPrefMap, newspaper, categories);
            }
        }
    }

    private void updateUserSelectionMap(Map<String, List<String>> userPrefMap, String newspaper, List<String> categories) {
        userPrefMap.put(newspaper, categories);
        NewsCatFileUtil.getInstance(context).setUserSelectionMap(userPrefMap);

        updateJsonMapFile();
    }

    private void updateJsonMapFile(){
        NewsCatFileUtil.getInstance(context).convertUserFeedMapToJsonMap();
    }

    public void destroyUtil() {
        _instance = null;
        context = null;
    }
}




Java Source Code List

in.sahildave.gazetti.StarterApplication.java
in.sahildave.gazetti.bookmarks.BookmarkAdapter.java
in.sahildave.gazetti.bookmarks.BookmarkDetailActivity.java
in.sahildave.gazetti.bookmarks.BookmarkDetailFragment.java
in.sahildave.gazetti.bookmarks.BookmarkListActivity.java
in.sahildave.gazetti.bookmarks.BookmarkListFragment.java
in.sahildave.gazetti.bookmarks.BookmarkLoadingCallback.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkDataSource.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkModel.java
in.sahildave.gazetti.bookmarks.sqlite.SQLiteHelper.java
in.sahildave.gazetti.homescreen.HomeScreenActivity.java
in.sahildave.gazetti.homescreen.HomeScreenFragment.java
in.sahildave.gazetti.homescreen.adapter.AddCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.CellModel.java
in.sahildave.gazetti.homescreen.adapter.EditCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.GridAdapter.java
in.sahildave.gazetti.homescreen.adapter.NewsCatModel.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContentExpListAdaper.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContent.java
in.sahildave.gazetti.news_activities.ArticleLoadingCallback.java
in.sahildave.gazetti.news_activities.WebViewFragment.java
in.sahildave.gazetti.news_activities.WebsiteDetailActivity.java
in.sahildave.gazetti.news_activities.WebsiteDetailFragment.java
in.sahildave.gazetti.news_activities.WebsiteListActivity.java
in.sahildave.gazetti.news_activities.WebsiteListFragment.java
in.sahildave.gazetti.news_activities.adapter.NavDrawerListAdapter.java
in.sahildave.gazetti.news_activities.adapter.NewsAdapter.java
in.sahildave.gazetti.news_activities.adapter.RobotoLight.java
in.sahildave.gazetti.news_activities.fetch.firstPost.java
in.sahildave.gazetti.news_activities.fetch.hindu.java
in.sahildave.gazetti.news_activities.fetch.indianExpressBusiness.java
in.sahildave.gazetti.news_activities.fetch.indianExpress.java
in.sahildave.gazetti.news_activities.fetch.toi.java
in.sahildave.gazetti.preference.FeedSelectFragment.java
in.sahildave.gazetti.preference.FeedSelectSettingsActivity.java
in.sahildave.gazetti.preference.LicensesActivity.java
in.sahildave.gazetti.preference.PreferenceExpListAdapter.java
in.sahildave.gazetti.preference.SettingsActivity.java
in.sahildave.gazetti.util.BitmapTransform.java
in.sahildave.gazetti.util.ConfigService.java
in.sahildave.gazetti.util.Constants.java
in.sahildave.gazetti.util.GazettiEnums.java
in.sahildave.gazetti.util.JsonHelper.java
in.sahildave.gazetti.util.NewsCatFileUtil.java
in.sahildave.gazetti.util.ShareButtonListener.java
in.sahildave.gazetti.util.UserPrefUtil.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenExpListAdapter.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentExpList.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentFirst.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenViewPagerActivity.java