diff --git a/Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java b/Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java index 7286c6c3..dca6e999 100644 --- a/Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java +++ b/Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java @@ -51,10 +51,9 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener pendingFuture; @@ -88,8 +87,6 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener lastPostCount) { lastPostCount = postCount; currentTimeout = 0; } else { currentTimeout++; - if (currentTimeout >= watchTimeouts.length) { - currentTimeout = watchTimeouts.length - 1; - } + currentTimeout = Math.min(currentTimeout, watchTimeouts.length - 1); } - if (!autoReload && currentTimeout < 4) { - currentTimeout = 4; // At least 60 seconds in the background - } + int watchTimeout = watchTimeouts[currentTimeout]; + 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) { - Runnable pendingRunnable = new Runnable() { - @Override - 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); - } + public void clearTimer() { + currentTimeout = -1; + clearPendingRunnable(); } - private void clearTimer() { + private void clearPendingRunnable() { if (pendingFuture != null) { - Logger.d(TAG, "Removed pending runnable"); + Logger.d(TAG, "Cleared timer"); pendingFuture.cancel(false); pendingFuture = null; } diff --git a/Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java b/Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java index bc1c2be6..e512a493 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java +++ b/Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java @@ -113,6 +113,7 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt public void unbindLoadable() { if (chanLoader != null) { + chanLoader.clearTimer(); LoaderPool.getInstance().release(chanLoader, this); chanLoader = null; loadable = null; @@ -138,13 +139,10 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt public void onForegroundChanged(boolean foreground) { if (chanLoader != null) { - if (foreground) { - if (isWatching()) { - chanLoader.setAutoLoadMore(true); - chanLoader.requestMoreDataAndResetTimer(); - } + if (foreground && isWatching()) { + chanLoader.requestMoreDataAndResetTimer(); } 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(); if (loadable.markedNo >= 0) { @@ -506,7 +506,8 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt @Override 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; }