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 |
|
|
} |