From 9c029fc7ee18632f36391c19ff4db787e08c3912 Mon Sep 17 00:00:00 2001 From: Nik-Sch Date: Sun, 12 Apr 2020 13:17:15 +0200 Subject: [PATCH] included suggestions and rebased --- .../dmfs/tasks/QuickAddDialogFragment.java | 2 +- .../rowsets/QueryRowSetSorting.java | 193 ------------------ .../dmfs/opentaskspal/rowsets/Subtasks.java | 8 +- 3 files changed, 7 insertions(+), 196 deletions(-) delete mode 100644 opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/QueryRowSetSorting.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/QuickAddDialogFragment.java b/opentasks/src/main/java/org/dmfs/tasks/QuickAddDialogFragment.java index a7044364d..869c88f6e 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/QuickAddDialogFragment.java +++ b/opentasks/src/main/java/org/dmfs/tasks/QuickAddDialogFragment.java @@ -79,7 +79,7 @@ public class QuickAddDialogFragment extends SupportDialogFragment /** * The maximum time to add for the first time the "Task completed" info is shown. */ - private final static int COMPLETION_DELAY_MAX = 1000; // ms + private final static int COMPLETION_DELAY_MAX = 1500; // ms private final static String ARG_LIST_ID = "list_id"; private final static String ARG_PARENT_ID = "parent_id"; diff --git a/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/QueryRowSetSorting.java b/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/QueryRowSetSorting.java deleted file mode 100644 index 4682bfdb7..000000000 --- a/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/QueryRowSetSorting.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright 2020 dmfs GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.dmfs.opentaskspal.rowsets; - - -import android.database.Cursor; -import android.os.RemoteException; - -import org.dmfs.android.contentpal.ClosableIterator; -import org.dmfs.android.contentpal.Predicate; -import org.dmfs.android.contentpal.Projection; -import org.dmfs.android.contentpal.RowSet; -import org.dmfs.android.contentpal.RowSnapshot; -import org.dmfs.android.contentpal.Table; -import org.dmfs.android.contentpal.View; -import org.dmfs.android.contentpal.rowdatasnapshots.MapRowDataSnapshot; -import org.dmfs.android.contentpal.rowsnapshots.ValuesRowSnapshot; -import org.dmfs.android.contentpal.tools.ClosableEmptyIterator; -import org.dmfs.android.contentpal.tools.uriparams.EmptyUriParams; -import org.dmfs.android.contentpal.transactions.contexts.EmptyTransactionContext; -import org.dmfs.iterators.AbstractBaseIterator; -import org.dmfs.jems.optional.Optional; - -import java.util.HashMap; -import java.util.Map; -import java.util.NoSuchElementException; - -import androidx.annotation.NonNull; - -import static org.dmfs.jems.optional.elementary.Absent.absent; - - -/** - * A base {@link RowSet} which returns a {@link RowSnapshot} for each row of the given {@link Table} that matches the given {@link Predicate}. - * - * @author Marten Gajda - */ -public final class QueryRowSetSorting implements RowSet -{ - private final View mView; - private final Projection mProjection; - private final Predicate mPredicate; - private Optional mSorting; - - - public QueryRowSetSorting(@NonNull View view, @NonNull Projection projection, @NonNull Predicate predicate, Optional sorting) - { - mView = view; - mProjection = projection; - mPredicate = predicate; - mSorting = sorting; - } - - - @NonNull - @Override - public ClosableIterator> iterator() - { - try - { - Cursor cursor = mView.rows(EmptyUriParams.INSTANCE, mProjection, mPredicate, mSorting); - if (!cursor.moveToFirst()) - { - cursor.close(); - return ClosableEmptyIterator.instance(); - } - return new QueryRowSetSorting.RowIterator<>(cursor, mView.table()); - } - catch (RemoteException e) - { - throw new RuntimeException( - String.format("Unable to execute query on view \"%s\" with selection \"%s\"", - mView.toString(), - mPredicate.selection(EmptyTransactionContext.INSTANCE).toString()), e); - } - } - - - private final static class RowIterator extends AbstractBaseIterator> implements ClosableIterator> - { - private final Cursor mCursor; - private final Table mTable; - - - private RowIterator(@NonNull Cursor cursor, @NonNull Table table) - { - mCursor = cursor; - mTable = table; - } - - - @Override - public boolean hasNext() - { - if (mCursor.isClosed()) - { - return false; - } - try - { - if (mCursor.isAfterLast()) - { - mCursor.close(); - return false; - } - } - catch (Exception e) - { - // we can't use finally here, because we want to close the cursor only in an error case. - mCursor.close(); - throw e; - } - - return true; - } - - - @NonNull - @Override - public RowSnapshot next() - { - try - { - if (!hasNext()) - { - throw new NoSuchElementException("No more rows to iterate"); - } - Map charData = new HashMap<>(); - Map byteData = new HashMap<>(); - String[] columnNames = mCursor.getColumnNames(); - for (int i = 0, count = mCursor.getColumnCount(); i < count; ++i) - { - String columnName = columnNames[i]; - if (mCursor.getType(i) == Cursor.FIELD_TYPE_BLOB) - { - byteData.put(columnName, mCursor.getBlob(i)); - } - else - { - charData.put(columnName, mCursor.getString(i)); - } - } - RowSnapshot rowSnapshot = new ValuesRowSnapshot<>(mTable, new MapRowDataSnapshot<>(charData, byteData)); - mCursor.moveToNext(); - return rowSnapshot; - } - catch (Exception e) - { - // we can't use finally here, because we want to close the cursor only in an error case. - mCursor.close(); - throw e; - } - } - - - @Override - protected void finalize() throws Throwable - { - // Note this only serves as the very last resort. Normally the cursor is closed when the last item is iterated or when close() is called. - // However if there is a crash during this the cursor might not be closed properly, hence we try that here. - if (!mCursor.isClosed()) - { - mCursor.close(); - } - super.finalize(); - } - - - @Override - public void close() - { - if (!mCursor.isClosed()) - { - mCursor.close(); - } - } - } - -} diff --git a/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/Subtasks.java b/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/Subtasks.java index b76abfa7a..b2dfac26a 100644 --- a/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/Subtasks.java +++ b/opentaskspal/src/main/java/org/dmfs/opentaskspal/rowsets/Subtasks.java @@ -23,6 +23,7 @@ import org.dmfs.android.contentpal.predicates.ReferringTo; import org.dmfs.android.contentpal.rowsets.DelegatingRowSet; import org.dmfs.android.contentpal.rowsets.QueryRowSet; +import org.dmfs.android.contentpal.views.Sorted; import org.dmfs.jems.optional.Optional; import org.dmfs.jems.optional.elementary.Present; import org.dmfs.tasks.contract.TaskContract.Tasks; @@ -38,12 +39,15 @@ public final class Subtasks extends DelegatingRowSet { - @SuppressWarnings("unchecked") public Subtasks(@NonNull View view, @NonNull Projection projection, @NonNull RowReference parentTask) { - super(new QueryRowSetSorting<>(view, projection, new ReferringTo<>(Tasks.PARENT_ID, parentTask), new Present(Tasks.STATUS + ", " + Tasks.TITLE))); + super( + new QueryRowSet<>( + new Sorted<>(Tasks.PERCENT_COMPLETE + ", " + Tasks.TITLE, view), + projection, + new ReferringTo<>(Tasks.PARENT_ID, parentTask))); } }