diff options
author | Camil Staps | 2015-04-11 13:45:58 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-11 13:45:58 +0200 |
commit | 95bf4cf545e4491c05a509837c3fc8ea3c782cc6 (patch) | |
tree | 5cb2cf58c33e14a016e2d4c018cc18dc49d490b4 /app/src/main/java/com/camilstaps/common | |
parent | Swiping to previous dates works for daily readings (not for their bible texts) (diff) |
General improvements & continuation of development
Diffstat (limited to 'app/src/main/java/com/camilstaps/common')
-rw-r--r-- | app/src/main/java/com/camilstaps/common/DatedString.java | 55 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/common/DatedStringPreferenceSet.java | 90 |
2 files changed, 145 insertions, 0 deletions
diff --git a/app/src/main/java/com/camilstaps/common/DatedString.java b/app/src/main/java/com/camilstaps/common/DatedString.java new file mode 100644 index 0000000..a452ab0 --- /dev/null +++ b/app/src/main/java/com/camilstaps/common/DatedString.java @@ -0,0 +1,55 @@ +package com.camilstaps.common; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.lang.reflect.InvocationTargetException; +import java.text.ParseException; + +/** + * Created by camilstaps on 11-4-15. + */ +public class DatedString { + + protected final Date date; + protected final String string; + + public DatedString(Date date, String string) { + this.date = date; + this.string = string; + } + + public String getString() { + return string; + } + + public Date getDate() { + return date; + } + + @Override + public String toString() { + JSONObject json = new JSONObject(); + try { + json.put("date", date.toString()); + json.put("text", string); + } catch (JSONException e) {} + return json.toString(); + } + + public static Object fromString(String s, Class castTo) throws JSONException, ParseException { + JSONObject json = new JSONObject(s); + try { + return castTo.cast(castTo.getDeclaredConstructor(Date.class, String.class).newInstance(new Date(json.getString("date")), json.getString("text"))); + } catch (InstantiationException e) { + return null; + } catch (IllegalAccessException e) { + return null; + } catch (InvocationTargetException e) { + return null; + } catch (NoSuchMethodException e) { + return null; + } + } + +} diff --git a/app/src/main/java/com/camilstaps/common/DatedStringPreferenceSet.java b/app/src/main/java/com/camilstaps/common/DatedStringPreferenceSet.java new file mode 100644 index 0000000..d61ba37 --- /dev/null +++ b/app/src/main/java/com/camilstaps/common/DatedStringPreferenceSet.java @@ -0,0 +1,90 @@ +package com.camilstaps.common; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import com.camilstaps.taize.DailyReading; + +import org.json.JSONException; + +import java.text.ParseException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * Created by camilstaps on 11-4-15. + */ +public class DatedStringPreferenceSet { + + 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; + } + + 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(); + } + + 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); + } + + 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); + } + + 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; + } + + 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); + } + } + + public static void clear(Context context, String key) { + put(context, key, new HashSet<DatedString>()); + } + +} |