Improve ChanLoader auto refreshing

multisite
Floens 10 years ago
parent fe045fd0fc
commit 08723c3e49
  1. 109
      Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java
  2. 17
      Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java

@ -51,10 +51,9 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
private final RequestQueue volleyRequestQueue; private final RequestQueue volleyRequestQueue;
private ChanThread thread; private ChanThread thread;
private boolean autoReload = false;
private ChanReaderRequest request; private ChanReaderRequest request;
private int currentTimeout; private int currentTimeout = -1;
private int lastPostCount; private int lastPostCount;
private long lastLoadTime; private long lastLoadTime;
private ScheduledFuture<?> pendingFuture; private ScheduledFuture<?> pendingFuture;
@ -88,8 +87,6 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
listeners.remove(listener); listeners.remove(listener);
if (listeners.isEmpty()) { if (listeners.isEmpty()) {
clearTimer(); clearTimer();
currentTimeout = 0;
autoReload = false;
if (request != null) { if (request != null) {
request.cancel(); request.cancel();
request = null; request = null;
@ -100,32 +97,6 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
} }
} }
/**
* Enable this if requestMoreData should me called automatically when the
* timer hits 0
*/
public void setAutoLoadMore(boolean autoReload) {
if (this.autoReload != autoReload) {
Logger.d(TAG, "Setting autoreload to " + autoReload);
this.autoReload = autoReload;
if (!autoReload) {
clearTimer();
}
}
}
/**
* Request more data if the time left is below 0 If auto load more is
* disabled, this needs to be called manually. Otherwise this is called
* automatically when the timer hits 0.
*/
public void loadMoreIfTime() {
if (getTimeUntilLoadMore() < 0L) {
requestMoreData();
}
}
/** /**
* Request data for the first time. * Request data for the first time.
*/ */
@ -143,7 +114,7 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
loadable.listViewTop = 0; loadable.listViewTop = 0;
} }
currentTimeout = 0; currentTimeout = -1;
thread = null; thread = null;
request = getData(); request = getData();
@ -160,6 +131,17 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
} }
} }
/**
* Request more data if the time left is below 0 If auto load more is
* disabled, this needs to be called manually. Otherwise this is called
* automatically when the timer hits 0.
*/
public void loadMoreIfTime() {
if (getTimeUntilLoadMore() < 0L) {
requestMoreData();
}
}
public void quickLoad() { public void quickLoad() {
if (thread == null) { if (thread == null) {
throw new IllegalStateException("Cannot quick load without already loaded thread"); throw new IllegalStateException("Cannot quick load without already loaded thread");
@ -176,8 +158,10 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
* Request more data and reset the watch timer. * Request more data and reset the watch timer.
*/ */
public void requestMoreDataAndResetTimer() { public void requestMoreDataAndResetTimer() {
currentTimeout = 0; if (request == null) {
requestMoreData(); currentTimeout = 0;
requestMoreData();
}
} }
public boolean isLoading() { public boolean isLoading() {
@ -195,7 +179,7 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
if (request != null) { if (request != null) {
return 0L; return 0L;
} else { } else {
long waitTime = watchTimeouts[currentTimeout] * 1000L; long waitTime = watchTimeouts[Math.max(0, currentTimeout)] * 1000L;
return lastLoadTime + waitTime - Time.get(); return lastLoadTime + waitTime - Time.get();
} }
} }
@ -232,10 +216,6 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
lastLoadTime = Time.get(); lastLoadTime = Time.get();
if (loadable.isThreadMode()) {
setTimer(response.posts.size());
}
for (ChanLoaderCallback l : listeners) { for (ChanLoaderCallback l : listeners) {
l.onChanLoaderData(thread); l.onChanLoaderData(thread);
} }
@ -279,46 +259,43 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
} }
} }
private void setTimer(int postCount) { public void setTimer() {
clearTimer(); clearPendingRunnable();
int postCount = thread == null ? 0 : thread.posts.size();
if (postCount > lastPostCount) { if (postCount > lastPostCount) {
lastPostCount = postCount; lastPostCount = postCount;
currentTimeout = 0; currentTimeout = 0;
} else { } else {
currentTimeout++; currentTimeout++;
if (currentTimeout >= watchTimeouts.length) { currentTimeout = Math.min(currentTimeout, watchTimeouts.length - 1);
currentTimeout = watchTimeouts.length - 1;
}
} }
if (!autoReload && currentTimeout < 4) { int watchTimeout = watchTimeouts[currentTimeout];
currentTimeout = 4; // At least 60 seconds in the background Logger.d(TAG, "Scheduled reload in " + watchTimeout + "s");
}
pendingFuture = executor.schedule(new Runnable() {
@Override
public void run() {
AndroidUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
pendingFuture = null;
requestMoreData();
}
});
}
}, watchTimeout, TimeUnit.SECONDS);
}
if (autoReload) { public void clearTimer() {
Runnable pendingRunnable = new Runnable() { currentTimeout = -1;
@Override clearPendingRunnable();
public void run() {
AndroidUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
pendingFuture = null;
// Always reload, it's always time to reload when the timer fires
requestMoreData();
}
});
}
};
Logger.d(TAG, "Scheduled reload in " + watchTimeouts[currentTimeout] + "s");
pendingFuture = executor.schedule(pendingRunnable, watchTimeouts[currentTimeout], TimeUnit.SECONDS);
}
} }
private void clearTimer() { private void clearPendingRunnable() {
if (pendingFuture != null) { if (pendingFuture != null) {
Logger.d(TAG, "Removed pending runnable"); Logger.d(TAG, "Cleared timer");
pendingFuture.cancel(false); pendingFuture.cancel(false);
pendingFuture = null; pendingFuture = null;
} }

@ -113,6 +113,7 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt
public void unbindLoadable() { public void unbindLoadable() {
if (chanLoader != null) { if (chanLoader != null) {
chanLoader.clearTimer();
LoaderPool.getInstance().release(chanLoader, this); LoaderPool.getInstance().release(chanLoader, this);
chanLoader = null; chanLoader = null;
loadable = null; loadable = null;
@ -138,13 +139,10 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt
public void onForegroundChanged(boolean foreground) { public void onForegroundChanged(boolean foreground) {
if (chanLoader != null) { if (chanLoader != null) {
if (foreground) { if (foreground && isWatching()) {
if (isWatching()) { chanLoader.requestMoreDataAndResetTimer();
chanLoader.setAutoLoadMore(true);
chanLoader.requestMoreDataAndResetTimer();
}
} else { } else {
chanLoader.setAutoLoadMore(false); chanLoader.clearTimer();
} }
} }
} }
@ -240,7 +238,9 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt
} }
} }
chanLoader.setAutoLoadMore(isWatching()); if (isWatching()) {
chanLoader.setTimer();
}
showPosts(); showPosts();
if (loadable.markedNo >= 0) { if (loadable.markedNo >= 0) {
@ -506,7 +506,8 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt
@Override @Override
public boolean isWatching() { public boolean isWatching() {
return loadable.isThreadMode() && ChanSettings.autoRefreshThread.get() && chanLoader.getThread() != null && return loadable.isThreadMode() && ChanSettings.autoRefreshThread.get() &&
Chan.getInstance().getApplicationInForeground() && chanLoader.getThread() != null &&
!chanLoader.getThread().closed && !chanLoader.getThread().archived; !chanLoader.getThread().closed && !chanLoader.getThread().archived;
} }

Loading…
Cancel
Save