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 | |
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')
9 files changed, 257 insertions, 172 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>()); + } + +} diff --git a/app/src/main/java/com/camilstaps/rss/RetrieveFeedTask.java b/app/src/main/java/com/camilstaps/rss/RetrieveFeedTask.java index d56c48a..543c9d6 100644 --- a/app/src/main/java/com/camilstaps/rss/RetrieveFeedTask.java +++ b/app/src/main/java/com/camilstaps/rss/RetrieveFeedTask.java @@ -1,7 +1,6 @@ package com.camilstaps.rss; import android.os.AsyncTask; -import android.util.Log; import com.camilstaps.common.Listener; @@ -30,10 +29,8 @@ public class RetrieveFeedTask extends AsyncTask<String, Void, RssParser> { @Override protected void onPostExecute(RssParser results) { if (results != null) { - Log.d("RFT", "success"); listener.success(results); } else { - Log.d("RFT", "failure"); listener.failure(); } } diff --git a/app/src/main/java/com/camilstaps/taize/Bible.java b/app/src/main/java/com/camilstaps/taize/Bible.java index 451c7cb..83fcba4 100644 --- a/app/src/main/java/com/camilstaps/taize/Bible.java +++ b/app/src/main/java/com/camilstaps/taize/Bible.java @@ -2,6 +2,7 @@ package com.camilstaps.taize; import android.content.Context; import android.preference.PreferenceManager; +import android.util.Log; import com.android.volley.Request; import com.android.volley.RequestQueue; @@ -14,7 +15,10 @@ import org.json.JSONException; import org.json.JSONObject; import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; +import java.util.List; /** * Use the Bible API of getbible.net @@ -63,13 +67,19 @@ public class Bible { public void onResponse(String s) { StringBuilder passage = new StringBuilder(); try { - JSONObject response = new JSONObject(s.substring( 1, s.length() - 2)); + JSONObject response = new JSONObject(s.substring(1, s.length() - 2)); JSONArray books = response.getJSONArray("book"); for (int b = 0; b < books.length(); b++) { JSONObject chapter = books.getJSONObject(b).getJSONObject("chapter"); + + List<String> sortedKeys = new ArrayList<String>(); Iterator<?> verses = chapter.keys(); while (verses.hasNext()) { String verse_number = (String) verses.next(); + sortedKeys.add(verse_number); + } + Collections.sort(sortedKeys); + for (String verse_number : sortedKeys) { JSONObject verse = chapter.getJSONObject(verse_number); passage.append(verse.getString("verse")).append(" "); } diff --git a/app/src/main/java/com/camilstaps/taize/DailyReading.java b/app/src/main/java/com/camilstaps/taize/DailyReading.java index 71c38ac..fa5115e 100644 --- a/app/src/main/java/com/camilstaps/taize/DailyReading.java +++ b/app/src/main/java/com/camilstaps/taize/DailyReading.java @@ -8,53 +8,27 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.camilstaps.common.Date; +import com.camilstaps.common.DatedString; /** * Created by camilstaps on 7-4-15. */ -public class DailyReading { +public class DailyReading extends DatedString { - private final Date date; - private final String text; - - public DailyReading(Date date, String text) { - this.date = date; - this.text = text; - } - - public String getText() { - return text; - } - - public Date getDate() { - return date; + public DailyReading(Date date, String string) { + super(date, string); } public String getTextWithoutReference() { Matcher bible_ref_m = referenceMatcher(); if (bible_ref_m.find()) { - return text.substring(0, bible_ref_m.start() - 1); + return string.substring(0, bible_ref_m.start() - 1); } else { - return text; + return string; } } - @Override - public String toString() { - JSONObject json = new JSONObject(); - try { - json.put("date", date.toString()); - json.put("text", text); - } catch (JSONException e) {} - return json.toString(); - } - - public static DailyReading fromString(String s) throws JSONException, ParseException { - JSONObject json = new JSONObject(s); - return new DailyReading(new Date(json.getString("date")), json.getString("text")); - } - public BibleText getBibleReference() { final Matcher bible_ref_m = referenceMatcher(); @@ -67,6 +41,10 @@ public class DailyReading { private Matcher referenceMatcher() { Pattern bible_ref = Pattern.compile("\\((\\d? ?[a-zA-Z]+) (\\d{1,2})(?::|,)(\\d+)-?(\\d+?)\\)"); - return bible_ref.matcher(text); + return bible_ref.matcher(string); + } + + public static DailyReading fromString(String s) throws JSONException, ParseException { + return (DailyReading) DatedString.fromString(s, DailyReading.class); } } diff --git a/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java b/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java index 29ae77f..b5cde3c 100644 --- a/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java +++ b/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java @@ -99,7 +99,7 @@ public class DailyReadingFragment extends Fragment { public void failure() { ((TextView) rootView.findViewById(R.id.textDailyReadingBibleText)).setText("No bible text found."); } - }); + }, date); } } diff --git a/app/src/main/java/com/camilstaps/taize/MainActivity.java b/app/src/main/java/com/camilstaps/taize/MainActivity.java index bd5ba1e..c7ca14c 100644 --- a/app/src/main/java/com/camilstaps/taize/MainActivity.java +++ b/app/src/main/java/com/camilstaps/taize/MainActivity.java @@ -27,7 +27,7 @@ public class MainActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Taize.cleanDailyReadingsSet(this); + Taize.cleanTextSets(this); setContentView(R.layout.activity_main); diff --git a/app/src/main/java/com/camilstaps/taize/SettingsActivity.java b/app/src/main/java/com/camilstaps/taize/SettingsActivity.java index c275d5e..3dd2881 100644 --- a/app/src/main/java/com/camilstaps/taize/SettingsActivity.java +++ b/app/src/main/java/com/camilstaps/taize/SettingsActivity.java @@ -11,6 +11,7 @@ import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; +import com.camilstaps.common.DatedStringPreferenceSet; import com.camilstaps.common.NullListener; import java.util.HashSet; @@ -67,9 +68,9 @@ public class SettingsActivity extends PreferenceActivity { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals(SettingsFragment.PREF_LANGUAGE_READINGS)) { - Taize.clearDailyReadingsSet(context); + DatedStringPreferenceSet.clear(context, "dailyreadings"); } else if (key.equals(SettingsFragment.PREF_VERSION_BIBLE)) { - Taize.forceNewDailyReadingBibleText(context, new NullListener<String>()); + DatedStringPreferenceSet.clear(context, "dailyreadingbibletexts"); } } }; diff --git a/app/src/main/java/com/camilstaps/taize/Taize.java b/app/src/main/java/com/camilstaps/taize/Taize.java index 00438be..c9e912f 100644 --- a/app/src/main/java/com/camilstaps/taize/Taize.java +++ b/app/src/main/java/com/camilstaps/taize/Taize.java @@ -9,6 +9,8 @@ import android.widget.Toast; import com.android.volley.Response; import com.android.volley.VolleyError; import com.camilstaps.common.Date; +import com.camilstaps.common.DatedString; +import com.camilstaps.common.DatedStringPreferenceSet; import com.camilstaps.common.Listener; import com.camilstaps.common.NullListener; import com.camilstaps.rss.RetrieveFeedTask; @@ -36,133 +38,109 @@ public class Taize { private static final int MAX_DIFFDATE_READING = 10; /** - * Force fetching a new bible text for the daily reading - * @see this#getDailyReadingBibleText + * Fetch the bible text corresponding to the daily reading * @param context * @param listener */ - public static void forceNewDailyReadingBibleText(final Context context, final Listener<String> listener) { - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); - editor.putLong("dailyreadingbiblefetch", 0); - editor.putLong("englishdailyreadingfetch", 0); - editor.commit(); - getDailyReadingBibleText(context, listener); + public static void getDailyReadingBibleText(final Context context, final Listener<String> listener, Date date) { + Set<DatedString> texts = (Set) DatedStringPreferenceSet.get(context, "dailyreadingbibletexts", DatedString.class); + for (DatedString text : texts) { + if (text.getDate().equals(date)) { + listener.success(text.getString()); + return; + } + } + fetchDailyReadingBibleText(context, listener, date); } - /** - * Fetch the bible text corresponding to the daily reading - * @see this#forceNewDailyReadingBibleText - * @param context - * @param listener - */ - public static void getDailyReadingBibleText(final Context context, final Listener<String> listener) { - if (System.currentTimeMillis() - PreferenceManager.getDefaultSharedPreferences(context).getLong("dailyreadingbiblefetch", 0) > 3600000) { + private static void fetchDailyReadingBibleText(final Context context, final Listener<String> listener, final Date date) { + DatedStringPreferenceSet.add(context, "dailyreadingbibletexts", new DatedString(date, "Fetching bible text...")); - getEnglishDailyReading(context, new Listener<String>() { - @Override - public void success(String reading) { + getEnglishDailyReading(context, new Listener<DailyReading>() { + @Override + public void success(DailyReading reading) { + BibleText text = reading.getBibleReference(); + if (text == null) { + failure(); + return; + } - BibleText text = null; - text = new DailyReading(new Date(), reading).getBibleReference(); - if (text == null) { + text.getText(context, new Response.Listener<String>() { + @Override + public void onResponse(String s) { + DatedStringPreferenceSet.updateOrAdd(context, "dailyreadingbibletexts", new DatedString(date, s)); + listener.success(s); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError volleyError) { failure(); - return; } + }); + } - text.getText(context, new Response.Listener<String>() { - @Override - public void onResponse(String s) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putString("dailyreadingbible", s).apply(); - listener.success(s); - } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError volleyError) { - Log.e("NET", "Failed to fetch bible text."); - } - }); - - } - - @Override - public void failure() { - Toast.makeText(context, "Failed to fetch bible text.", Toast.LENGTH_SHORT).show(); - - listener.failure(); - } - }); - - PreferenceManager.getDefaultSharedPreferences(context).edit().putLong("dailyreadingbiblefetch", System.currentTimeMillis()).apply(); - - } else { - - listener.success(PreferenceManager.getDefaultSharedPreferences(context).getString("dailyreadingbible", "No bible text found.")); - - } + @Override + public void failure() { + listener.failure(); + } + }, date); } /** - * Force fetching a new English daily reading - * @see this#getEnglishDailyReading + * Get the English daily reading (needed for the English Bible reference) * @param context * @param listener */ - private static void forceNewEnglishDailyReading(final Context context, final Listener<String> listener) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putLong("englishdailyreadingfetch", 0).commit(); - getEnglishDailyReading(context, listener); + private static void getEnglishDailyReading(final Context context, final Listener<DailyReading> listener, Date date) { + Set<DailyReading> englishDailyReadings = (Set) DatedStringPreferenceSet.get(context, "englishdailyreadings", DailyReading.class); + + for (DailyReading reading : englishDailyReadings) { + if (reading.getDate().equals(date)) { + listener.success(reading); + return; + } + } + + fetchEnglishDailyReading(context, listener, date); } - /** - * Get the English daily reading (needed for the English Bible reference) - * @see this#forceNewEnglishDailyReading - * @param context - * @param listener - */ - private static void getEnglishDailyReading(final Context context, final Listener<String> listener) { - if (System.currentTimeMillis() - PreferenceManager.getDefaultSharedPreferences(context).getLong("englishdailyreadingfetch", 0) > 3600000) { - - RetrieveFeedTask retrieve = new RetrieveFeedTask(); - retrieve.setRssDownloadListener(new Listener<RssParser>() { - @Override - public void success(RssParser parser) { - try { - String reading = parser.getItem(0).description.trim(); - - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); - editor.putString("englishdailyreading", reading); - editor.putLong("englishdailyreadingfetch", System.currentTimeMillis()); - editor.apply(); - - listener.success(reading); - } catch (Exception e) { - failure(); - } - } + private static void fetchEnglishDailyReading(final Context context, final Listener<DailyReading> listener, final Date date) { - @Override - public void failure() { - listener.failure(); + RetrieveFeedTask retrieve = new RetrieveFeedTask(); + retrieve.setRssDownloadListener(new Listener<RssParser>() { + @Override + public void success(RssParser parser) { + try { + String text = parser.getItem(0).description.trim(); + DailyReading reading = new DailyReading(date, text); + + DatedStringPreferenceSet.add(context, "englishdailyreadings", reading); + + listener.success(reading); + } catch (Exception e) { + failure(); } - }); - - try { - retrieve.execute(url_reading_feed_en).get(); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); } - } else { - - listener.success(PreferenceManager.getDefaultSharedPreferences(context).getString("englishdailyreading", "No daily reading found.")); + @Override + public void failure() { + Toast.makeText(context, "Failed to fetch daily reading.", Toast.LENGTH_SHORT).show(); + listener.failure(); + } + }); + try { + retrieve.execute(url_reading_feed + "?lang=en&date=" + date.toString()).get(); + } catch (Exception e) { + e.printStackTrace(); + listener.failure(); } + } public static void getDailyReading(final Context context, final Listener<DailyReading> listener, Date date) { - Set<DailyReading> dailyReadings = getDailyReadingsSet(context); + Set<DailyReading> dailyReadings = (Set) DatedStringPreferenceSet.get(context, "dailyreadings", DailyReading.class); for (DailyReading reading : dailyReadings) { if (reading.getDate().equals(date)) { @@ -185,9 +163,7 @@ public class Taize { String text = parser.getItem(0).description.trim(); DailyReading reading = new DailyReading(date, text); - Set<DailyReading> dailyReadings = getDailyReadingsSet(context); - dailyReadings.add(reading); - putDailyReadingsSet(context, dailyReadings); + DatedStringPreferenceSet.add(context, "dailyreadings", reading); listener.success(reading); } catch (Exception e) { @@ -214,36 +190,14 @@ public class Taize { } - public static Set<DailyReading> getDailyReadingsSet(Context context) { - Set<DailyReading> dailyReadings= new HashSet<DailyReading>(); - for (String s : PreferenceManager.getDefaultSharedPreferences(context).getStringSet("dailyreadings", new HashSet<String>())) { - try { - dailyReadings.add(DailyReading.fromString(s)); - } catch (JSONException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); - } - } - return dailyReadings; - } - - private static void putDailyReadingsSet(Context context, Set<DailyReading> set) { - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); - Set<String> putSet = new HashSet<>(); - for (DailyReading reading : set) { - putSet.add(reading.toString()); - } - editor.putStringSet("dailyreadings", putSet); - editor.apply(); - } - - public static void clearDailyReadingsSet(Context context) { - putDailyReadingsSet(context, new HashSet<DailyReading>()); + public static void cleanTextSets(Context context) { + cleanDailyReadingsSet(context, "dailyreadings"); + cleanDailyReadingsSet(context, "englishdailyreadings"); + DatedStringPreferenceSet.clear(context, "dailyreadingbibletexts"); } - public static void cleanDailyReadingsSet(Context context) { - Set<DailyReading> set = getDailyReadingsSet(context); + public static void cleanDailyReadingsSet(Context context, String key) { + Set<DailyReading> set = (Set) DatedStringPreferenceSet.get(context, key, DailyReading.class); Iterator<DailyReading> iterator = set.iterator(); while (iterator.hasNext()) { DailyReading reading = iterator.next(); @@ -260,7 +214,7 @@ public class Taize { } } } - putDailyReadingsSet(context, set); + DatedStringPreferenceSet.put(context, key, (Set) set); } //http://www.taize.fr/pdcrss.xml |