/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/util/FavoritesHelper.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/util/FavoritesHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1709 - (hide annotations) (download)
Tue Mar 6 11:02:04 2012 UTC (12 years, 2 months ago) by torben
File size: 1444 byte(s)
refactor favorites handling to a seperate class and add favorite adder/remover to departurelist
1 torben 1709 package dk.thoerup.traininfo.util;
2    
3    
4     import android.content.Context;
5     import android.content.SharedPreferences;
6     import android.content.SharedPreferences.Editor;
7    
8     import dk.thoerup.traininfo.util.IntSet;
9    
10    
11    
12     public class FavoritesHelper {
13    
14     final static String FAVORITES = "favorites";
15    
16     Context cxt;
17     public FavoritesHelper(Context cxt) {
18     this.cxt = cxt;
19     }
20    
21     protected SharedPreferences getPrefs() {
22     return cxt.getSharedPreferences("TrainStation", 0);
23     }
24    
25     protected IntSet getFavorites() {
26     String favoriteString = getPrefs().getString(FAVORITES, "");
27    
28     IntSet favorites = new IntSet();
29     if (! favoriteString.equals("") ) {
30     favorites.fromString(favoriteString);
31     }
32    
33     return favorites;
34     }
35    
36     public int getSize() {
37     return getFavorites().size();
38     }
39    
40     public String getString() {
41     return getFavorites().toString();
42     }
43    
44     public boolean hasFavorite(int station) {
45     IntSet favorites = getFavorites();
46    
47     return favorites.contains(station);
48     }
49    
50     public void addFavorite(int newfav) {
51     SharedPreferences prefs = getPrefs();
52    
53     IntSet favorites = getFavorites();
54     favorites.add(newfav);
55    
56     Editor ed = prefs.edit();
57     ed.putString(FAVORITES, favorites.toString());
58     ed.commit();
59     }
60    
61     public void removeFavorite(int newfav) {
62     SharedPreferences prefs = getPrefs();
63    
64     IntSet favorites = getFavorites();
65     favorites.remove(newfav);
66    
67     Editor ed = prefs.edit();
68     ed.putString(FAVORITES, favorites.toString());
69     ed.commit();
70     }
71    
72    
73    
74     }

  ViewVC Help
Powered by ViewVC 1.1.20