Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to choose sort order #737

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -163,7 +164,7 @@ public SelectionCreator maxSelectable(int maxSelectable) {
*
* @param maxImageSelectable Maximum selectable count for image.
* @param maxVideoSelectable Maximum selectable count for video.
* @return {@link SelectionCreator} for fluent API.
* @return {@link SelectionCreator} for fluent API.
*/
public SelectionCreator maxSelectablePerMediaType(int maxImageSelectable, int maxVideoSelectable) {
if (maxImageSelectable < 1 || maxVideoSelectable < 1)
Expand Down Expand Up @@ -216,6 +217,7 @@ public SelectionCreator originalEnable(boolean enable) {

/**
* Determines Whether to hide top and bottom toolbar in PreView mode ,when user tap the picture
*
* @param enable
* @return {@link SelectionCreator} for fluent API.
*/
Expand Down Expand Up @@ -317,6 +319,20 @@ public SelectionCreator imageEngine(ImageEngine imageEngine) {
return this;
}

/***
* Set a Album sort order
* taken : Sort by date taken
* added : Sort by date added
* size : Sort by file size
* default value : Sort by album default order
* @param sortBy
* @return
*/
public SelectionCreator albumOrder(String sortBy) {
mSelectionSpec.setOrderCondition(sortBy);
return this;
}

/**
* Set listener for callback immediately when user select or unselect something.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
import java.util.List;
import java.util.Set;

import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DATE_TAKEN;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_MEDIA_ADDED;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_SIZE;

public final class SelectionSpec {

public Set<MimeType> mimeTypeSet;
public boolean mediaTypeExclusive;
public boolean showSingleMediaType;
public String orderCondition;
@StyleRes
public int themeId;
public int orientation;
Expand Down Expand Up @@ -93,6 +99,7 @@ private void reset() {
autoHideToobar = false;
originalMaxSize = Integer.MAX_VALUE;
showPreview = true;
orderCondition = ORDER_BY_DEFAULT;
}

public boolean singleSelectionModeEnabled() {
Expand All @@ -118,4 +125,19 @@ public boolean onlyShowGif() {
private static final class InstanceHolder {
private static final SelectionSpec INSTANCE = new SelectionSpec();
}

public void setOrderCondition(String condition) {
switch (condition) {
case "default":
this.orderCondition = ORDER_BY_DEFAULT;
case "taken":
this.orderCondition = ORDER_BY_DATE_TAKEN;
case "added":
this.orderCondition = ORDER_BY_MEDIA_ADDED;
case "size":
this.orderCondition = ORDER_BY_SIZE;
default:
this.orderCondition = ORDER_BY_DEFAULT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import java.util.Map;
import java.util.Set;

import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DATE_TAKEN;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_MEDIA_ADDED;
import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_SIZE;

/**
* Load all albums (grouped by bucket_id) into a single cursor.
*/
Expand Down Expand Up @@ -118,20 +123,21 @@ private static String[] getSelectionArgsForSingleMediaGifType(int mediaType) {

private static final String BUCKET_ORDER_BY = "datetaken DESC";

private AlbumLoader(Context context, String selection, String[] selectionArgs) {
private AlbumLoader(Context context, String selection, String[] selectionArgs, String orderBy) {
super(
context,
QUERY_URI,
beforeAndroidTen() ? PROJECTION : PROJECTION_29,
selection,
selectionArgs,
BUCKET_ORDER_BY
orderBy
);
}

public static CursorLoader newInstance(Context context) {
String selection;
String[] selectionArgs;
String order;
if (SelectionSpec.getInstance().onlyShowGif()) {
selection = beforeAndroidTen()
? SELECTION_FOR_SINGLE_MEDIA_GIF_TYPE : SELECTION_FOR_SINGLE_MEDIA_GIF_TYPE_29;
Expand All @@ -151,7 +157,20 @@ public static CursorLoader newInstance(Context context) {
selection = beforeAndroidTen() ? SELECTION : SELECTION_29;
selectionArgs = SELECTION_ARGS;
}
return new AlbumLoader(context, selection, selectionArgs);

switch (SelectionSpec.getInstance().orderCondition) {
case "default":
order = ORDER_BY_DEFAULT;
case "taken":
order = ORDER_BY_DATE_TAKEN;
case "added":
order = ORDER_BY_MEDIA_ADDED;
case "size":
order = ORDER_BY_SIZE;
default:
order = ORDER_BY_DEFAULT;
}
return new AlbumLoader(context, selection, selectionArgs, order);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ private static String[] getSelectionAlbumArgsForGifType(int mediaType, String al
}
// ===============================================================

private static final String ORDER_BY = MediaStore.Images.Media.DATE_TAKEN + " DESC";
public static final String ORDER_BY_DEFAULT = MediaStore.Images.Media.DEFAULT_SORT_ORDER + " DESC";
public static final String ORDER_BY_DATE_TAKEN = MediaStore.Images.Media.DATE_TAKEN + " DESC";
public static final String ORDER_BY_MEDIA_ADDED = MediaStore.Images.Media.DATE_ADDED + " DESC";
public static final String ORDER_BY_SIZE = MediaStore.Images.Media.SIZE + " DESC";
private final boolean mEnableCapture;

private AlbumMediaLoader(Context context, String selection, String[] selectionArgs, boolean capture) {
super(context, QUERY_URI, PROJECTION, selection, selectionArgs, ORDER_BY);
private AlbumMediaLoader(Context context, String selection, String[] selectionArgs, boolean capture, String orderCondition) {
super(context, QUERY_URI, PROJECTION, selection, selectionArgs, orderCondition);
mEnableCapture = capture;
}

public static CursorLoader newInstance(Context context, Album album, boolean capture) {
public static CursorLoader newInstance(Context context, Album album, boolean capture, String orderCondition) {
String selection;
String[] selectionArgs;
boolean enableCapture;
Expand Down Expand Up @@ -176,7 +179,7 @@ public static CursorLoader newInstance(Context context, Album album, boolean cap
}
enableCapture = false;
}
return new AlbumMediaLoader(context, selection, selectionArgs, enableCapture);
return new AlbumMediaLoader(context, selection, selectionArgs, enableCapture, orderCondition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@

import java.lang.ref.WeakReference;

import static com.zhihu.matisse.internal.loader.AlbumMediaLoader.ORDER_BY_DEFAULT;

public class AlbumMediaCollection implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 2;
private static final String ARGS_ALBUM = "args_album";
private static final String ARGS_ENABLE_CAPTURE = "args_enable_capture";
private static final String ARGS_SORT_ORDER = "args_album_sort_order";
private WeakReference<Context> mContext;
private LoaderManager mLoaderManager;
private AlbumMediaCallbacks mCallbacks;
Expand All @@ -51,7 +54,7 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
}

return AlbumMediaLoader.newInstance(context, album,
album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false));
album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false), args.getString(ARGS_SORT_ORDER));
}

@Override
Expand Down Expand Up @@ -88,13 +91,14 @@ public void onDestroy() {
}

public void load(@Nullable Album target) {
load(target, false);
load(target, false, ORDER_BY_DEFAULT);
}

public void load(@Nullable Album target, boolean enableCapture) {
public void load(@Nullable Album target, boolean enableCapture, String order) {
Bundle args = new Bundle();
args.putParcelable(ARGS_ALBUM, target);
args.putBoolean(ARGS_ENABLE_CAPTURE, enableCapture);
args.putString(ARGS_SORT_ORDER, order);
mLoaderManager.initLoader(LOADER_ID, args, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mRecyclerView.addItemDecoration(new MediaGridInset(spanCount, spacing, false));
mRecyclerView.setAdapter(mAdapter);
mAlbumMediaCollection.onCreate(getActivity(), this);
mAlbumMediaCollection.load(album, selectionSpec.capture);
mAlbumMediaCollection.load(album, selectionSpec.capture, selectionSpec.orderCondition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private void startAction(View v) {
.setOnCheckedListener(isChecked -> {
Log.e("isChecked", "onCheck: isChecked=" + isChecked);
})
.albumOrder("added")
.forResult(REQUEST_CODE_CHOOSE);
break;
case R.id.dracula:
Expand Down