mirror of https://github.com/tasks/tasks
Replace paging library with @RawQuery
parent
7cd7699b95
commit
f34cdcaa68
@ -1,102 +0,0 @@
|
|||||||
package org.tasks.data;
|
|
||||||
|
|
||||||
import android.database.Cursor;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.annotation.WorkerThread;
|
|
||||||
import androidx.paging.PositionalDataSource;
|
|
||||||
import androidx.room.RoomDatabase;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import org.tasks.analytics.Tracker;
|
|
||||||
|
|
||||||
public class LimitOffsetDataSource extends PositionalDataSource<Task> {
|
|
||||||
|
|
||||||
private final String mCountQuery;
|
|
||||||
private final String mLimitOffsetQuery;
|
|
||||||
private final RoomDatabase mDb;
|
|
||||||
|
|
||||||
public LimitOffsetDataSource(RoomDatabase db, String query) {
|
|
||||||
mDb = db;
|
|
||||||
mCountQuery = "SELECT COUNT(*) FROM ( " + query + " )";
|
|
||||||
mLimitOffsetQuery = "SELECT * FROM ( " + query + " ) LIMIT ? OFFSET ?";
|
|
||||||
}
|
|
||||||
|
|
||||||
@WorkerThread
|
|
||||||
private int countItems() {
|
|
||||||
Cursor cursor;
|
|
||||||
try {
|
|
||||||
cursor = mDb.query(mCountQuery, null);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Tracker.report(e);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
return cursor.getInt(0);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
} finally {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
|
||||||
protected List<Task> convertRows(Cursor cursor) {
|
|
||||||
List<Task> result = new ArrayList<>();
|
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
result.add(new Task(cursor));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@WorkerThread
|
|
||||||
private List<Task> loadRange(int startPosition, int loadCount) {
|
|
||||||
Cursor cursor = mDb.query(mLimitOffsetQuery, new Object[] {loadCount, startPosition});
|
|
||||||
//noinspection TryFinallyCanBeTryWithResources
|
|
||||||
try {
|
|
||||||
return convertRows(cursor);
|
|
||||||
} finally {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void loadInitial(
|
|
||||||
@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<Task> callback) {
|
|
||||||
int totalCount = countItems();
|
|
||||||
if (totalCount == 0) {
|
|
||||||
callback.onResult(Collections.emptyList(), 0, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bound the size requested, based on known count
|
|
||||||
final int firstLoadPosition = computeInitialLoadPosition(params, totalCount);
|
|
||||||
final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, totalCount);
|
|
||||||
|
|
||||||
// convert from legacy behavior
|
|
||||||
List<Task> list = loadRange(firstLoadPosition, firstLoadSize);
|
|
||||||
if (list != null && list.size() == firstLoadSize) {
|
|
||||||
callback.onResult(list, firstLoadPosition, totalCount);
|
|
||||||
} else {
|
|
||||||
// null list, or size doesn't match request
|
|
||||||
// The size check is a WAR for Room 1.0, subsequent versions do the check in Room
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@WorkerThread
|
|
||||||
@Override
|
|
||||||
public void loadRange(
|
|
||||||
@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<Task> callback) {
|
|
||||||
List<Task> list = loadRange(params.startPosition, params.loadSize);
|
|
||||||
if (list != null) {
|
|
||||||
callback.onResult(list);
|
|
||||||
} else {
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,140 @@
|
|||||||
|
package org.tasks.data;
|
||||||
|
|
||||||
|
import androidx.room.Embedded;
|
||||||
|
import com.todoroo.astrid.data.Task;
|
||||||
|
|
||||||
|
public class TaskContainer {
|
||||||
|
@Embedded public Task task;
|
||||||
|
public String tags;
|
||||||
|
public String googletask;
|
||||||
|
public String caldav;
|
||||||
|
public int order;
|
||||||
|
public int indent;
|
||||||
|
|
||||||
|
public int getIndent() {
|
||||||
|
return indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndent(int indent) {
|
||||||
|
this.indent = indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTagsString() {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGoogleTaskList() {
|
||||||
|
return googletask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCaldav() {
|
||||||
|
return caldav;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNotes() {
|
||||||
|
return task.getNotes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNotes() {
|
||||||
|
return task.hasNotes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return task.getTitle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHidden() {
|
||||||
|
return task.isHidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCompleted() {
|
||||||
|
return task.isCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPriority() {
|
||||||
|
return task.getPriority();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRecurrence() {
|
||||||
|
return task.getRecurrence();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasDueDate() {
|
||||||
|
return task.hasDueDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOverdue() {
|
||||||
|
return task.isOverdue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDueDate() {
|
||||||
|
return task.getDueDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task getTask() {
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return task.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskContainer that = (TaskContainer) o;
|
||||||
|
|
||||||
|
if (indent != that.indent) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (task != null ? !task.equals(that.task) : that.task != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (tags != null ? !tags.equals(that.tags) : that.tags != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (googletask != null ? !googletask.equals(that.googletask) : that.googletask != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return caldav != null ? caldav.equals(that.caldav) : that.caldav == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = task != null ? task.hashCode() : 0;
|
||||||
|
result = 31 * result + (tags != null ? tags.hashCode() : 0);
|
||||||
|
result = 31 * result + (googletask != null ? googletask.hashCode() : 0);
|
||||||
|
result = 31 * result + (caldav != null ? caldav.hashCode() : 0);
|
||||||
|
result = 31 * result + indent;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TaskContainer{"
|
||||||
|
+ "task="
|
||||||
|
+ task
|
||||||
|
+ ", tags='"
|
||||||
|
+ tags
|
||||||
|
+ '\''
|
||||||
|
+ ", googletask='"
|
||||||
|
+ googletask
|
||||||
|
+ '\''
|
||||||
|
+ ", caldav='"
|
||||||
|
+ caldav
|
||||||
|
+ '\''
|
||||||
|
+ ", indent="
|
||||||
|
+ indent
|
||||||
|
+ '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUuid() {
|
||||||
|
return task.getUuid();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue