From e189fff85696d504b91a6df5f1ca06ce707db06f Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 22 May 2018 10:28:12 -0400 Subject: [PATCH] Fixed some cursor-related bugs in Search. --- .../thoughtcrime/securesms/database/CursorList.java | 11 +++++------ .../securesms/search/SearchViewModel.java | 1 + src/org/thoughtcrime/securesms/util/Debouncer.java | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/CursorList.java b/src/org/thoughtcrime/securesms/database/CursorList.java index 218550e3ef..0223f08a14 100644 --- a/src/org/thoughtcrime/securesms/database/CursorList.java +++ b/src/org/thoughtcrime/securesms/database/CursorList.java @@ -30,8 +30,6 @@ public class CursorList implements List, Closeable { public CursorList(@NonNull Cursor cursor, @NonNull ModelBuilder modelBuilder) { this.cursor = cursor; this.modelBuilder = modelBuilder; - - this.cursor.moveToFirst(); } public static CursorList emptyList() { @@ -58,6 +56,8 @@ public class CursorList implements List, Closeable { @Override public Iterator iterator() { return new Iterator() { + int index = 0; + @Override public boolean hasNext() { return cursor.getCount() > 0 && !cursor.isLast(); @@ -65,9 +65,8 @@ public class CursorList implements List, Closeable { @Override public T next() { - T model = modelBuilder.build(cursor); - cursor.moveToNext(); - return model; + cursor.moveToPosition(index++); + return modelBuilder.build(cursor); } }; } @@ -179,7 +178,7 @@ public class CursorList implements List, Closeable { @Override public void close() { - if (cursor != null) { + if (!cursor.isClosed()) { cursor.close(); } } diff --git a/src/org/thoughtcrime/securesms/search/SearchViewModel.java b/src/org/thoughtcrime/securesms/search/SearchViewModel.java index 360d516460..d05aeeb4cc 100644 --- a/src/org/thoughtcrime/securesms/search/SearchViewModel.java +++ b/src/org/thoughtcrime/securesms/search/SearchViewModel.java @@ -47,6 +47,7 @@ class SearchViewModel extends ViewModel { @Override protected void onCleared() { + debouncer.clear(); searchResult.close(); } diff --git a/src/org/thoughtcrime/securesms/util/Debouncer.java b/src/org/thoughtcrime/securesms/util/Debouncer.java index d02fe8d05d..9389728ff1 100644 --- a/src/org/thoughtcrime/securesms/util/Debouncer.java +++ b/src/org/thoughtcrime/securesms/util/Debouncer.java @@ -29,4 +29,8 @@ public class Debouncer { handler.removeCallbacksAndMessages(null); handler.postDelayed(runnable, threshold); } + + public void clear() { + handler.removeCallbacksAndMessages(null); + } }