aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/common/DatedStringPreferenceSet.java
blob: 93db51ecbb250073f2a1b3f449fa2bc4a74797e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.camilstaps.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.json.JSONException;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * A set of DatedStrings to store in SharedPreferences
 * @author Camil Staps
 */
public class DatedStringPreferenceSet {

    /**
     * Get a Set from the SharedPreferences
     * @param context
     * @param key the SharedPreferences key
     * @param type the type of DatedString to cast to
     * @return the set
     */
    public static Set<Object> get(Context context, String key, Class type) {
        Set<Object> set = new HashSet<>();
        for (String s : PreferenceManager.getDefaultSharedPreferences(context).getStringSet(key, new HashSet<String>())) {
            try {
                set.add(type.cast(DatedString.fromString(s, type)));
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return set;
    }

    /**
     * Put a Set to the SharedPreferences
     * @param context
     * @param key the SharedPreferences key
     * @param set
     */
    public static void put(Context context, String key, Set<DatedString> set) {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        Set<String> putSet = new HashSet<>();
        for (DatedString item : set) {
            putSet.add(item.toString());
        }
        editor.putStringSet(key, putSet);
        editor.apply();
    }

    /**
     * Add one DatedString to a set
     * @param context
     * @param key the SharedPreferences key
     * @param object the DatedString to add
     */
    public static void add(Context context, String key, DatedString object) {
        Set<DatedString> set = (Set) DatedStringPreferenceSet.get(context, key, DatedString.class);
        set.add(object);
        DatedStringPreferenceSet.put(context, key, (Set) set);
    }

    /**
     * Remove all DatedStrings with the same Date as the given DatedString, and put this new DatedString if any object was removed
     * @param context
     * @param key the SharedPreferences key
     * @param object the object to 'update'
     */
    public static void update(Context context, String key, DatedString object) {
        Set<DatedString> set = (Set) get(context, key, object.getClass());
        Iterator<DatedString> iterator = set.iterator();
        boolean add = false;
        while (iterator.hasNext()) {
            DatedString this_object = iterator.next();
            if (this_object.getDate().equals(object.getDate())) {
                iterator.remove();
                add = true;
            }
        }
        if (add) {
            set.add(object);
        }
        put(context, key, set);
    }

    /**
     * Check if a certain Date exists as the Date of a DatedString in some set
     * @param context
     * @param key the SharedPreferences key
     * @param date the Date
     * @return
     */
    public static boolean has(Context context, String key, Date date) {
        Set<DatedString> set = (Set) get(context, key, DatedString.class);
        for (DatedString item : set) {
            if (item.getDate().equals(date)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Update the object if it already exists, or add it if it doesn't
     * @see #update(android.content.Context, String, DatedString)
     * @see #add(android.content.Context, String, DatedString)
     * @param context
     * @param key the SharedPreferences key
     * @param object the object to 'update' or add
     */
    public static void updateOrAdd(Context context, String key, DatedString object) {
        if (has(context, key, object.getDate())) {
            update(context, key, object);
        } else {
            add(context, key, object);
        }
    }

    /**
     * Remove all elements of a set
     * @param context
     * @param key the SharedPreferences key
     */
    public static void clear(Context context, String key) {
        put(context, key, new HashSet<DatedString>());
    }

    /**
     * Remove all elements with a certain Date from a set
     * @param context
     * @param key the SharedPreferences key
     * @param date the Date
     */
    public static void remove(Context context, String key, Date date) {
        Set<DatedString> set = (Set) get(context, key, DatedString.class);
        Iterator<DatedString> iterator = set.iterator();
        boolean add = false;
        while (iterator.hasNext()) {
            DatedString this_object = iterator.next();
            if (this_object.getDate().equals(date)) {
                iterator.remove();
            }
        }
        put(context, key, set);
    }

}