package dk.thoerup.traininfo.util; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import dk.thoerup.traininfo.util.IntSet; public class FavoritesHelper { final static String FAVORITES = "favorites"; Context cxt; public FavoritesHelper(Context cxt) { this.cxt = cxt; } protected SharedPreferences getPrefs() { return cxt.getSharedPreferences("TrainStation", 0); } protected IntSet getFavorites() { String favoriteString = getPrefs().getString(FAVORITES, ""); IntSet favorites = new IntSet(); if (! favoriteString.equals("") ) { favorites.fromString(favoriteString); } return favorites; } public int getSize() { return getFavorites().size(); } public String getString() { return getFavorites().toString(); } public boolean hasFavorite(int station) { IntSet favorites = getFavorites(); return favorites.contains(station); } public void addFavorite(int newfav) { SharedPreferences prefs = getPrefs(); IntSet favorites = getFavorites(); favorites.add(newfav); Editor ed = prefs.edit(); ed.putString(FAVORITES, favorites.toString()); ed.commit(); } public void removeFavorite(int newfav) { SharedPreferences prefs = getPrefs(); IntSet favorites = getFavorites(); favorites.remove(newfav); Editor ed = prefs.edit(); ed.putString(FAVORITES, favorites.toString()); ed.commit(); } }