Skip to content

Commit

Permalink
Auto-refresh old post lists (with preference)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Oct 2, 2016
1 parent adac409 commit 807f06f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions assets/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
75/1.9.6
Multireddit viewing support
Auto-refresh old post lists (with preference)
Ability to view custom user pages
Preference to hide Android status bar
Preference to use un-shortened link for "Share Comments" (thanks to fhtagn)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.cache.downloadstrategy;

import org.quantumbadger.redreader.cache.CacheEntry;
import org.quantumbadger.redreader.common.TimestampBound;

public class DownloadStrategyIfTimestampOutsideBounds implements DownloadStrategy {

private final TimestampBound mTimestampBound;

public DownloadStrategyIfTimestampOutsideBounds(final TimestampBound timestampBound) {
mTimestampBound = timestampBound;
}

@Override
public boolean shouldDownloadWithoutCheckingCache() {
return false;
}

@Override
public boolean shouldDownloadIfCacheEntryFound(final CacheEntry entry) {
return !mTimestampBound.verifyTimestamp(entry.timestamp);
}

@Override
public boolean shouldDownloadIfNotCached() {
return true;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/quantumbadger/redreader/common/General.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public static boolean isConnectionWifi(final Context context){
return info != null && info.getDetailedState() == NetworkInfo.DetailedState.CONNECTED;
}

public static boolean isNetworkConnected(final Context context) {
final ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

public static RRError getGeneralErrorForFailure(Context context, @CacheRequest.RequestFailureType int type, Throwable t, Integer status, String url) {

final int title, message;
Expand Down Expand Up @@ -576,4 +582,8 @@ public static void recreateActivityNoAnimation(final AppCompatActivity activity)
activity.overridePendingTransition(0, 0);
activity.startActivity(intent);
}

public static long hoursToMs(final long hours) {
return hours * 60L * 60L * 1000L;
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/quantumbadger/redreader/common/PrefsUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,18 @@ public static void pref_cache_location(Context context,
.apply();
}

public static long pref_cache_rerequest_postlist_age_ms(final Context context, final SharedPreferences sharedPreferences) {
try {
final int hours = Integer.parseInt(
getString(R.string.pref_cache_rerequest_postlist_age_key, "1", context, sharedPreferences));

return General.hoursToMs(hours);

} catch(Throwable e) {
return 1;
}
}

// pref_cache_maxage

public static HashMap<Integer, Long> pref_cache_maxage(final Context context, final SharedPreferences sharedPreferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategy;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyAlways;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfTimestampOutsideBounds;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyNever;
import org.quantumbadger.redreader.common.AndroidApi;
import org.quantumbadger.redreader.common.Constants;
Expand Down Expand Up @@ -222,6 +223,11 @@ public void onScrolled(final RecyclerView recyclerView, final int dx, final int
if(forceDownload) {
downloadStrategy = DownloadStrategyAlways.INSTANCE;

} else if(session == null && savedInstanceState == null && General.isNetworkConnected(context)) {

final long maxAgeMs = PrefsUtility.pref_cache_rerequest_postlist_age_ms(context, mSharedPreferences);
downloadStrategy = new DownloadStrategyIfTimestampOutsideBounds(TimestampBound.notOlderThan(maxAgeMs));

} else {
downloadStrategy = DownloadStrategyIfNotCached.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.text.Html;

import org.quantumbadger.redreader.BuildConfig;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.activities.ChangelogActivity;
Expand Down Expand Up @@ -96,7 +95,8 @@ public void onCreate(final Bundle savedInstanceState) {
R.string.pref_behaviour_screenorientation_key,
R.string.pref_behaviour_gallery_swipe_length_key,
R.string.pref_behaviour_pinned_subredditsort_key,
R.string.pref_behaviour_blocked_subredditsort_key
R.string.pref_behaviour_blocked_subredditsort_key,
R.string.pref_cache_rerequest_postlist_age_key
};

final int[] editTextPrefsToUpdate = {
Expand Down
4 changes: 4 additions & 0 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@

<string-array name="pref_cache_maxage">
<item>@string/time_1hr</item>
<item>@string/time_2hr</item>
<item>@string/time_3hr</item>
<item>@string/time_6hr</item>
<item>@string/time_1day</item>
<item>@string/time_3day</item>
Expand All @@ -239,6 +241,8 @@
<!-- Constants. Do not change. -->
<string-array name="pref_cache_maxage_return">
<item>1</item>
<item>2</item>
<item>3</item>
<item>6</item>
<item>24</item>
<item>72</item>
Expand Down
8 changes: 8 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,12 @@
<!-- 2016-09-21 -->
<string name="pref_appearance_show_blocked_subreddits_main_menu_key">pref_appearance_show_blocked_subreddits_main_menu</string>
<string name="pref_appearance_show_blocked_subreddits_main_menu_title">Show blocked subreddits in main menu</string>

<!-- 2016-10-02 -->
<string name="pref_cache_rerequest_postlist_age_key">pref_cache_rerequest_postlist_age</string>
<string name="pref_cache_rerequest_postlist_age_title">Refresh posts if cache older than</string>

<string name="time_2hr">2 hours</string>
<string name="time_3hr">3 hours</string>

</resources>
6 changes: 6 additions & 0 deletions src/main/res/xml/prefs_cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<Preference android:title="@string/pref_cache_location_title"
android:key="@string/pref_cache_location_key"/>

<ListPreference android:title="@string/pref_cache_rerequest_postlist_age_title"
android:key="@string/pref_cache_rerequest_postlist_age_key"
android:entries="@array/pref_cache_maxage"
android:entryValues="@array/pref_cache_maxage_return"
android:defaultValue="1"/>

</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_cache_precache_images_header">
Expand Down

0 comments on commit 807f06f

Please sign in to comment.