Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe199cb1 authored by Garfield Tan's avatar Garfield Tan
Browse files

Enable sorting in search result.

Also remove unused API in sort model.

Change-Id: I62a43dd576efc458a3444270fdebaa94d69e87f4
parent 2010ff78
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -209,7 +209,6 @@ public abstract class BaseActivity

            @Override
            public void onSearchViewChanged(boolean opened) {
                mState.sortModel.setSortEnabled(!opened);
                mNavigator.update();
            }
        };
+4 −24
Original line number Diff line number Diff line
@@ -21,18 +21,14 @@ import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.android.documentsui.R;
import com.android.documentsui.sorting.SortController.WidgetController;
import com.android.documentsui.sorting.SortDimension;
import com.android.documentsui.sorting.SortDimension.SortDirection;
import com.android.documentsui.sorting.SortModel;
import com.android.documentsui.sorting.SortModel.SortDimensionId;
import com.android.documentsui.sorting.SortModel.UpdateListener;
import com.android.documentsui.sorting.SortModel.UpdateType;

/**
@@ -49,24 +45,23 @@ public final class DropdownSortWidgetController implements WidgetController {
    private final PopupMenu mMenu;
    private final ImageView mArrow;

    private final OnClickListener mDimensionButtonClickListener = this::showMenu;
    private final OnClickListener mArrowClickListener = this::onChangeDirection;
    private final UpdateListener mUpdateListener = this::onModelUpdate;

    public DropdownSortWidgetController(SortModel model, View widget) {
        mModel = model;
        mWidget = widget;

        mDimensionButton = (TextView) mWidget.findViewById(R.id.sort_dimen_dropdown);
        mDimensionButton.setOnClickListener(this::showMenu);

        mMenu = new PopupMenu(widget.getContext(), mDimensionButton, Gravity.END | Gravity.TOP);
        mMenu.setOnMenuItemClickListener(this::onSelectDimension);

        mArrow = (ImageView) mWidget.findViewById(R.id.sort_arrow);
        mArrow.setOnClickListener(this::onChangeDirection);

        populateMenuItems();
        onModelUpdate(mModel, SortModel.UPDATE_TYPE_UNSPECIFIED);

        mModel.addListener(mUpdateListener);
        mModel.addListener(this::onModelUpdate);
    }

    @Override
@@ -92,10 +87,6 @@ public final class DropdownSortWidgetController implements WidgetController {
    private void onModelUpdate(SortModel model, @UpdateType int updateType) {
        final @SortDimensionId int sortedId = model.getSortedDimensionId();

        if ((updateType & SortModel.UPDATE_TYPE_STATUS) != 0) {
            setEnabled(mModel.isSortEnabled());
        }

        if ((updateType & SortModel.UPDATE_TYPE_VISIBILITY) != 0) {
            updateVisibility();
        }
@@ -106,17 +97,6 @@ public final class DropdownSortWidgetController implements WidgetController {
        }
    }

    private void setEnabled(boolean enabled) {
        if (enabled) {
            mDimensionButton.setOnClickListener(mDimensionButtonClickListener);
            mArrow.setOnClickListener(mArrowClickListener);
        } else {
            mMenu.dismiss();
            mDimensionButton.setOnClickListener(null);
            mArrow.setOnClickListener(null);
        }
    }

    private void updateVisibility() {
        Menu menu = mMenu.getMenu();

+3 −39
Original line number Diff line number Diff line
@@ -60,7 +60,6 @@ public class SortModel implements Parcelable {
    @IntDef(flag = true, value = {
            UPDATE_TYPE_NONE,
            UPDATE_TYPE_UNSPECIFIED,
            UPDATE_TYPE_STATUS,
            UPDATE_TYPE_VISIBILITY,
            UPDATE_TYPE_SORTING
    })
@@ -70,19 +69,15 @@ public class SortModel implements Parcelable {
     * Default value for update type. Nothing is updated.
     */
    public static final int UPDATE_TYPE_NONE = 0;
    /**
     * Indicates the status of sorting has changed, i.e. whether soring is enabled.
     */
    public static final int UPDATE_TYPE_STATUS = 1;
    /**
     * Indicates the visibility of at least one dimension has changed.
     */
    public static final int UPDATE_TYPE_VISIBILITY = 1 << 1;
    public static final int UPDATE_TYPE_VISIBILITY = 1;
    /**
     * Indicates the sorting order has changed, either because the sorted dimension has changed or
     * the sort direction has changed.
     */
    public static final int UPDATE_TYPE_SORTING = 1 << 2;
    public static final int UPDATE_TYPE_SORTING = 1 << 1;
    /**
     * Anything can be changed if the type is unspecified.
     */
@@ -99,8 +94,6 @@ public class SortModel implements Parcelable {
    private boolean mIsUserSpecified = false;
    private @Nullable SortDimension mSortedDimension;

    private boolean mIsSortEnabled = true;

    public SortModel(Collection<SortDimension> columns) {
        mDimensions = new SparseArray<>(columns.size());

@@ -146,16 +139,6 @@ public class SortModel implements Parcelable {
                : SortDimension.SORT_DIRECTION_NONE;
    }

    public void setSortEnabled(boolean enabled) {
        mIsSortEnabled = enabled;

        notifyListeners(UPDATE_TYPE_STATUS);
    }

    public boolean isSortEnabled() {
        return mIsSortEnabled;
    }

    /**
     * Sort by the default direction of the given dimension if user has never specified any sort
     * direction before.
@@ -182,10 +165,6 @@ public class SortModel implements Parcelable {
     * @param direction the direction to sort docs in
     */
    public void sortByUser(int dimensionId, @SortDirection int direction) {
        if (!mIsSortEnabled) {
            throw new IllegalStateException("Sort is not enabled.");
        }

        SortDimension dimension = mDimensions.get(dimensionId);
        if (dimension == null) {
            throw new IllegalArgumentException("Unknown column id: " + dimensionId);
@@ -299,17 +278,6 @@ public class SortModel implements Parcelable {
        mListeners.remove(listener);
    }

    public void clearSortDirection() {
        if (mSortedDimension != null) {
            mSortedDimension.mSortDirection = SortDimension.SORT_DIRECTION_NONE;
            mSortedDimension = null;
        }

        mIsUserSpecified = false;

        sortOnDefault();
    }

    /**
     * Sort by default dimension and direction if there is no history of user specifying a sort
     * order.
@@ -349,7 +317,6 @@ public class SortModel implements Parcelable {
        }

        return mDefaultDimensionId == other.mDefaultDimensionId
                && mIsSortEnabled == other.mIsSortEnabled
                && (mSortedDimension == other.mSortedDimension
                    || mSortedDimension.equals(other.mSortedDimension));
    }
@@ -358,8 +325,7 @@ public class SortModel implements Parcelable {
    public String toString() {
        return new StringBuilder()
                .append("SortModel{")
                .append("enabled=").append(mIsSortEnabled)
                .append(", dimensions=").append(mDimensions)
                .append("dimensions=").append(mDimensions)
                .append(", defaultDimensionId=").append(mDefaultDimensionId)
                .append(", sortedDimension=").append(mSortedDimension)
                .append("}")
@@ -379,7 +345,6 @@ public class SortModel implements Parcelable {
        }

        out.writeInt(mDefaultDimensionId);
        out.writeInt(mIsSortEnabled ? 1 : 0);
        out.writeInt(getSortedDimensionId());
    }

@@ -395,7 +360,6 @@ public class SortModel implements Parcelable {
            SortModel model = new SortModel(columns);

            model.mDefaultDimensionId = in.readInt();
            model.mIsSortEnabled = (in.readInt() == 1);
            model.mSortedDimension = model.getDimensionById(in.readInt());

            return model;
+1 −2
Original line number Diff line number Diff line
@@ -76,8 +76,7 @@ public final class TableHeaderController implements SortController.WidgetControl
        cell.setTag(dimension);

        cell.onBind(dimension);
        if (mModel.isSortEnabled()
                && dimension.getVisibility() == View.VISIBLE
        if (dimension.getVisibility() == View.VISIBLE
                && dimension.getSortCapability() != SortDimension.SORT_CAPABILITY_NONE) {
            cell.setOnClickListener(mOnCellClickListener);
        } else {
+0 −37
Original line number Diff line number Diff line
@@ -17,9 +17,7 @@
package com.android.documentsui.sorting;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.support.annotation.Nullable;
@@ -81,11 +79,6 @@ public class SortModelTest {
        mModel.addListener(DUMMY_LISTENER);
    }

    @Test
    public void testEnabledByDefault() {
        assertTrue(mModel.isSortEnabled());
    }

    @Test
    public void testSizeEquals() {
        assertEquals(DIMENSIONS.length, mModel.getSize());
@@ -192,36 +185,6 @@ public class SortModelTest {
        assertEquals(SortDimension.SORT_DIRECTION_ASCENDING, sortedDimension.getSortDirection());
    }

    @Test
    public void testSetSortEnabled() {
        mModel.setSortEnabled(false);

        assertFalse(mModel.isSortEnabled());
    }

    @Test
    public void testSetDefaultDimension_sortDisabled() {
        mModel.setSortEnabled(false);

        mModel.setDefaultDimension(DIMENSION_1.getId());

        SortDimension sortedDimension = getSortedDimension();
        assertSame(DIMENSION_1, sortedDimension);
        assertEquals(DIMENSION_1.getDefaultSortDirection(), sortedDimension.getSortDirection());
    }

    @Test
    public void testSortByUser_sortDisabled() {
        mModel.setSortEnabled(false);

        try {
            mModel.sortByUser(DIMENSION_1.getId(), SortDimension.SORT_DIRECTION_ASCENDING);
            fail("Expect exception but not raised.");
        } catch(IllegalStateException expected) {
            // Expected
        }
    }

    @Test
    public void testSetDefaultDimension_noSortingCapability() {
        try {