Added lastseenpost to loadables, fixed PinWatcher

captchafix
Florens Douwes 11 years ago
parent dc9419db3b
commit a9b32a3bb1
  1. 31
      Clover/app/src/main/java/org/floens/chan/core/manager/ThreadManager.java
  2. 35
      Clover/app/src/main/java/org/floens/chan/core/manager/WatchManager.java
  3. 7
      Clover/app/src/main/java/org/floens/chan/core/model/Loadable.java
  4. 40
      Clover/app/src/main/java/org/floens/chan/core/model/Pin.java
  5. 7
      Clover/app/src/main/java/org/floens/chan/core/model/Post.java
  6. 9
      Clover/app/src/main/java/org/floens/chan/core/watch/PinWatcher.java

@ -68,7 +68,6 @@ public class ThreadManager implements Loader.LoaderListener {
private final List<List<Post>> popupQueue = new ArrayList<>(); private final List<List<Post>> popupQueue = new ArrayList<>();
private PostRepliesFragment currentPopupFragment; private PostRepliesFragment currentPopupFragment;
private int highlightedPost = -1; private int highlightedPost = -1;
private int lastSeenPost = -1;
private int lastPost = -1; private int lastPost = -1;
private Loader loader; private Loader loader;
@ -118,18 +117,21 @@ public class ThreadManager implements Loader.LoaderListener {
} }
highlightedPost = -1; highlightedPost = -1;
lastSeenPost = -1;
lastPost = -1; lastPost = -1;
} }
public void bottomPostViewed() { public void bottomPostViewed() {
if (loader != null && loader.getLoadable().isThreadMode()) { if (loader != null && loader.getLoadable().isThreadMode()) {
Pin pin = ChanApplication.getWatchManager().findPinByLoadable(loader.getLoadable()); List<Post> posts = loader.getCachedPosts();
if (pin != null) { if (posts.size() > 0) {
ChanApplication.getWatchManager().onPinViewed(pin); loader.getLoadable().lastViewed = posts.get(posts.size() - 1).no;
} }
}
updateLastSeen(); Pin pin = ChanApplication.getWatchManager().findPinByLoadable(loader.getLoadable());
if (pin != null) {
pin.onBottomPostViewed();
ChanApplication.getWatchManager().onPinsChanged();
} }
} }
@ -176,7 +178,6 @@ public class ThreadManager implements Loader.LoaderListener {
if (result.size() > 0) { if (result.size() > 0) {
lastPost = result.get(result.size() - 1).no; lastPost = result.get(result.size() - 1).no;
} }
updateLastSeen();
threadManagerListener.onThreadLoaded(result, append); threadManagerListener.onThreadLoaded(result, append);
} }
@ -297,7 +298,7 @@ public class ThreadManager implements Loader.LoaderListener {
} }
public boolean isPostLastSeen(Post post) { public boolean isPostLastSeen(Post post) {
return post.no == lastSeenPost && post.no != lastPost; return post.no == loader.getLoadable().lastViewed && post.no != lastPost;
} }
private void copyToClipboard(String comment) { private void copyToClipboard(String comment) {
@ -564,20 +565,6 @@ public class ThreadManager implements Loader.LoaderListener {
}); });
} }
private void updateLastSeen() {
if (ChanApplication.getWatchManager().getWatchEnabled()) {
Pin pin = ChanApplication.getWatchManager().findPinByLoadable(loader.getLoadable());
if (pin != null) {
Post last = pin.getLastSeenPost();
if (last != null) {
lastSeenPost = last.no;
} else {
lastSeenPost = -1;
}
}
}
}
public interface ThreadManagerListener { public interface ThreadManagerListener {
public void onThreadLoaded(List<Post> result, boolean append); public void onThreadLoaded(List<Post> result, boolean append);

@ -55,6 +55,7 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
updateTimerState(); updateTimerState();
updateNotificationServiceState(); updateNotificationServiceState();
updatePinWatchers();
} }
/** /**
@ -134,7 +135,6 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
public void removePin(Pin pin) { public void removePin(Pin pin) {
pins.remove(pin); pins.remove(pin);
ChanApplication.getDatabaseManager().removePin(pin); ChanApplication.getDatabaseManager().removePin(pin);
pin.destroy();
onPinsChanged(); onPinsChanged();
} }
@ -164,13 +164,6 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
}).start(); }).start();
} }
public void onPinViewed(Pin pin) {
if (getWatchEnabled()) {
pin.getPinWatcher().onViewed();
onPinsChanged();
}
}
public void addPinListener(PinListener l) { public void addPinListener(PinListener l) {
listeners.add(l); listeners.add(l);
} }
@ -186,6 +179,7 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
updateTimerState(); updateTimerState();
updateNotificationServiceState(); updateNotificationServiceState();
updatePinWatchers();
} }
public void pausePins() { public void pausePins() {
@ -201,11 +195,12 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
public void onWatchEnabledChanged(boolean watchEnabled) { public void onWatchEnabledChanged(boolean watchEnabled) {
updateNotificationServiceState(watchEnabled, getWatchBackgroundEnabled()); updateNotificationServiceState(watchEnabled, getWatchBackgroundEnabled());
updateTimerState(watchEnabled, getWatchBackgroundEnabled()); updateTimerState(watchEnabled, getWatchBackgroundEnabled());
updatePinWatchers(watchEnabled);
} }
public void onBackgroundWatchingChanged(boolean backgroundEnabled) { public void onBackgroundWatchingChanged(boolean backgroundEnabled) {
updateNotificationServiceState(getWatchEnabled(), backgroundEnabled); updateNotificationServiceState(getTimerEnabled(), backgroundEnabled);
updateTimerState(getWatchEnabled(), backgroundEnabled); updateTimerState(getTimerEnabled(), backgroundEnabled);
} }
@Override @Override
@ -214,7 +209,7 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
updateTimerState(); updateTimerState();
} }
public boolean getWatchEnabled() { private boolean getTimerEnabled() {
// getWatchingPins returns an empty list when ChanPreferences.getWatchEnabled() is false // getWatchingPins returns an empty list when ChanPreferences.getWatchEnabled() is false
return getWatchingPins().size() > 0; return getWatchingPins().size() > 0;
} }
@ -223,8 +218,22 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
return ChanPreferences.getWatchBackgroundEnabled(); return ChanPreferences.getWatchBackgroundEnabled();
} }
private void updatePinWatchers() {
updatePinWatchers(ChanPreferences.getWatchEnabled());
}
private void updatePinWatchers(boolean watchEnabled) {
for (Pin pin : pins) {
if (watchEnabled) {
pin.createWatcher();
} else {
pin.destroyWatcher();
}
}
}
private void updateNotificationServiceState() { private void updateNotificationServiceState() {
updateNotificationServiceState(getWatchEnabled(), getWatchBackgroundEnabled()); updateNotificationServiceState(getTimerEnabled(), getWatchBackgroundEnabled());
} }
private void updateNotificationServiceState(boolean watchEnabled, boolean backgroundEnabled) { private void updateNotificationServiceState(boolean watchEnabled, boolean backgroundEnabled) {
@ -237,7 +246,7 @@ public class WatchManager implements ChanApplication.ForegroundChangedListener {
} }
private void updateTimerState() { private void updateTimerState() {
updateTimerState(getWatchEnabled(), getWatchBackgroundEnabled()); updateTimerState(getTimerEnabled(), getWatchBackgroundEnabled());
} }
private void updateTimerState(boolean watchEnabled, boolean backgroundEnabled) { private void updateTimerState(boolean watchEnabled, boolean backgroundEnabled) {

@ -49,11 +49,7 @@ public class Loadable {
@DatabaseField @DatabaseField
public int listViewTop; public int listViewTop;
/** public int lastViewed = -1;
* When simple mode is enabled, CPU intensive methods won't get called. This
* is used for the thread watcher.
*/
public boolean simpleMode = false;
/** /**
* Constructs an empty loadable. The mode is INVALID. * Constructs an empty loadable. The mode is INVALID.
@ -171,7 +167,6 @@ public class Loadable {
copy.title = title; copy.title = title;
copy.listViewIndex = listViewIndex; copy.listViewIndex = listViewIndex;
copy.listViewTop = listViewTop; copy.listViewTop = listViewTop;
copy.simpleMode = simpleMode;
return copy; return copy;
} }

@ -31,8 +31,6 @@ public class Pin {
@DatabaseField(canBeNull = false, foreign = true) @DatabaseField(canBeNull = false, foreign = true)
public Loadable loadable = new Loadable("", -1); public Loadable loadable = new Loadable("", -1);
public PinWatcher pinWatcher;
@DatabaseField @DatabaseField
public boolean watching = true; public boolean watching = true;
@ -50,6 +48,8 @@ public class Pin {
public boolean isError = false; public boolean isError = false;
private PinWatcher pinWatcher;
public int getNewPostsCount() { public int getNewPostsCount() {
if (watchLastCount < 0 || watchNewCount < 0) { if (watchLastCount < 0 || watchNewCount < 0) {
return 0; return 0;
@ -62,15 +62,29 @@ public class Pin {
return Math.max(0, quoteNewCount - quoteLastCount); return Math.max(0, quoteNewCount - quoteLastCount);
} }
public Post getLastSeenPost() { public PinWatcher getPinWatcher() {
return getPinWatcher().getLastSeenPost(); return pinWatcher;
}
public void onBottomPostViewed() {
if (pinWatcher != null) {
pinWatcher.onViewed();
}
} }
public void update() { public void update() {
getPinWatcher().update(); if (pinWatcher != null) {
pinWatcher.update();
}
}
public void createWatcher() {
if (pinWatcher == null) {
pinWatcher = new PinWatcher(this);
}
} }
public void destroy() { public void destroyWatcher() {
if (pinWatcher != null) { if (pinWatcher != null) {
pinWatcher.destroy(); pinWatcher.destroy();
pinWatcher = null; pinWatcher = null;
@ -81,16 +95,8 @@ public class Pin {
watching = !watching; watching = !watching;
ChanApplication.getWatchManager().onPinsChanged(); ChanApplication.getWatchManager().onPinsChanged();
if (watching) { // if (watching && pinWatcher != null) {
getPinWatcher().update(); // .update();
} // }
}
public PinWatcher getPinWatcher() {
if (pinWatcher == null) {
pinWatcher = new PinWatcher(this);
}
return pinWatcher;
} }
} }

@ -141,7 +141,7 @@ public class Post {
} }
if (rawComment != null) { if (rawComment != null) {
comment = parseComment(rawComment, loadable.simpleMode); comment = parseComment(rawComment);
} }
try { try {
@ -159,10 +159,7 @@ public class Post {
return true; return true;
} }
private CharSequence parseComment(String commentRaw, boolean simpleMode) { private CharSequence parseComment(String commentRaw) {
if (simpleMode)
return "";
CharSequence total = new SpannableString(""); CharSequence total = new SpannableString("");
try { try {

@ -104,15 +104,6 @@ public class PinWatcher implements Loader.LoaderListener {
} }
} }
public Post getLastSeenPost() {
int i = posts.size() - pin.getNewPostsCount() - 1;
if (i >= 0 && i < posts.size()) {
return posts.get(i);
} else {
return null;
}
}
@Override @Override
public void onError(VolleyError error) { public void onError(VolleyError error) {
Logger.e(TAG, "PinWatcher onError: ", error); Logger.e(TAG, "PinWatcher onError: ", error);

Loading…
Cancel
Save