allow sorting directories

This commit is contained in:
tibbi 2016-07-24 21:02:02 +02:00
parent 90e8309aa1
commit 8054f51727
6 changed files with 136 additions and 68 deletions

View File

@ -33,6 +33,7 @@ import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.DirectoryAdapter;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.models.Directory;
import java.io.File;
@ -47,7 +48,7 @@ import butterknife.ButterKnife;
public class MainActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener {
@BindView(R.id.directories_grid) GridView mGridView;
@BindView(R.id.directories_holder) SwipeRefreshLayout mSwipeRefreshLayout;
@ -103,6 +104,7 @@ public class MainActivity extends SimpleActivity
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort:
showSortingDialog();
return true;
case R.id.camera:
startActivity(new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA));
@ -223,6 +225,7 @@ public class MainActivity extends SimpleActivity
}
final List<Directory> dirs = new ArrayList<>(directories.values());
Directory.mSorting = mConfig.getSorting();
Collections.sort(dirs);
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
@ -231,6 +234,10 @@ public class MainActivity extends SimpleActivity
return dirs;
}
private void showSortingDialog() {
new ChangeSorting(this);
}
private void prepareForDeleting() {
Utils.showToast(this, R.string.deleting);
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
@ -551,4 +558,9 @@ public class MainActivity extends SimpleActivity
initializeGallery();
mSwipeRefreshLayout.setRefreshing(false);
}
@Override
public void dialogClosed() {
initializeGallery();
}
}

View File

@ -1,7 +1,6 @@
package com.simplemobiletools.gallery.activities;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
@ -15,7 +14,6 @@ import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
@ -26,12 +24,11 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
@ -50,7 +47,7 @@ import butterknife.ButterKnife;
public class MediaActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener {
private static final String TAG = MediaActivity.class.getSimpleName();
@BindView(R.id.media_grid) GridView mGridView;
@BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout;
@ -146,53 +143,7 @@ public class MediaActivity extends SimpleActivity
}
private void showSortingDialog() {
final View sortingView = getLayoutInflater().inflate(R.layout.change_sorting, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.sort_by));
builder.setView(sortingView);
final int currSorting = mConfig.getSorting();
final RadioGroup sortingRadio = (RadioGroup) sortingView.findViewById(R.id.dialog_radio_sorting);
RadioButton sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_name);
if ((currSorting & Constants.SORT_BY_DATE) != 0) {
sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_date);
} else if ((currSorting & Constants.SORT_BY_SIZE) != 0) {
sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_size);
}
sortBtn.setChecked(true);
final RadioGroup orderRadio = (RadioGroup) sortingView.findViewById(R.id.dialog_radio_order);
RadioButton orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_ascending);
if ((currSorting & Constants.SORT_DESCENDING) != 0) {
orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_descending);
}
orderBtn.setChecked(true);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int sorting = Constants.SORT_BY_NAME;
switch (sortingRadio.getCheckedRadioButtonId()) {
case R.id.dialog_radio_date:
sorting = Constants.SORT_BY_DATE;
break;
case R.id.dialog_radio_size:
sorting = Constants.SORT_BY_SIZE;
break;
default:
break;
}
if (orderRadio.getCheckedRadioButtonId() == R.id.dialog_radio_descending) {
sorting |= Constants.SORT_DESCENDING;
}
mConfig.setSorting(sorting);
initializeGallery();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
new ChangeSorting(this);
}
private void deleteDirectoryIfEmpty() {
@ -241,7 +192,7 @@ public class MediaActivity extends SimpleActivity
}
}
Medium.mOrder = mConfig.getSorting();
Medium.mSorting = mConfig.getSorting();
Collections.sort(media);
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
@ -479,4 +430,9 @@ public class MediaActivity extends SimpleActivity
initializeGallery();
mSwipeRefreshLayout.setRefreshing(false);
}
@Override
public void dialogClosed() {
initializeGallery();
}
}

View File

@ -317,7 +317,7 @@ public class ViewPagerActivity extends SimpleActivity
}
}
Medium.mOrder = mConfig.getSorting();
Medium.mSorting = mConfig.getSorting();
Collections.sort(media);
int j = 0;
for (Medium medium : media) {

View File

@ -0,0 +1,90 @@
package com.simplemobiletools.gallery.dialogs;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.simplemobiletools.gallery.Config;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
public class ChangeSorting extends AlertDialog.Builder implements DialogInterface.OnClickListener {
private static Config mConfig;
private static ChangeDialogListener mListener;
private static View mHolder;
private static int mCurrSorting;
public ChangeSorting(Activity act) {
super(act.getApplicationContext());
mListener = (ChangeDialogListener) act;
mConfig = Config.newInstance(getContext());
mHolder = act.getLayoutInflater().inflate(R.layout.change_sorting, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(act);
builder.setTitle(act.getResources().getString(R.string.sort_by));
builder.setView(mHolder);
mCurrSorting = mConfig.getSorting();
setupSortRadio();
setupOrderRadio();
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void setupSortRadio() {
final RadioGroup sortingRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_sorting);
RadioButton sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_name);
if ((mCurrSorting & Constants.SORT_BY_DATE) != 0) {
sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_date);
} else if ((mCurrSorting & Constants.SORT_BY_SIZE) != 0) {
sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_size);
}
sortBtn.setChecked(true);
}
private void setupOrderRadio() {
final RadioGroup orderRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_order);
RadioButton orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_ascending);
if ((mCurrSorting & Constants.SORT_DESCENDING) != 0) {
orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_descending);
}
orderBtn.setChecked(true);
}
@Override
public void onClick(DialogInterface dialog, int which) {
final RadioGroup sortingRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_sorting);
int sorting = Constants.SORT_BY_NAME;
switch (sortingRadio.getCheckedRadioButtonId()) {
case R.id.dialog_radio_date:
sorting = Constants.SORT_BY_DATE;
break;
case R.id.dialog_radio_size:
sorting = Constants.SORT_BY_SIZE;
break;
default:
break;
}
final RadioGroup orderRadio = (RadioGroup) mHolder.findViewById(R.id.dialog_radio_order);
if (orderRadio.getCheckedRadioButtonId() == R.id.dialog_radio_descending) {
sorting |= Constants.SORT_DESCENDING;
}
if (mConfig.getSorting() != sorting) {
mConfig.setSorting(sorting);
mListener.dialogClosed();
}
}
public interface ChangeDialogListener {
void dialogClosed();
}
}

View File

@ -1,12 +1,15 @@
package com.simplemobiletools.gallery.models;
import com.simplemobiletools.gallery.Constants;
public class Directory implements Comparable {
private final String mPath;
private final String mThumbnail;
private final String mName;
private final long mTimestamp;
private int mMediaCnt;
private long mBytes;
private long mSize;
public static int mSorting;
public Directory(String path, String thumbnail, String name, int mediaCnt, long timestamp, long size) {
mPath = path;
@ -14,7 +17,7 @@ public class Directory implements Comparable {
mName = name;
mMediaCnt = mediaCnt;
mTimestamp = timestamp;
mBytes = size;
mSize = size;
}
public String getPath() {
@ -42,22 +45,29 @@ public class Directory implements Comparable {
}
public long getSize() {
return mBytes;
return mSize;
}
public void addSize(long bytes) {
mBytes += bytes;
mSize += bytes;
}
@Override
public int compareTo(Object object) {
final Directory directory = (Directory) object;
if (mTimestamp < directory.getTimestamp()) {
return 1;
} else if (mTimestamp > directory.getTimestamp()) {
return -1;
int res;
if ((mSorting & Constants.SORT_BY_NAME) != 0) {
res = mPath.compareTo(directory.getPath());
} else if ((mSorting & Constants.SORT_BY_DATE) != 0) {
res = (mTimestamp > directory.getTimestamp()) ? 1 : -1;
} else {
res = (mSize > directory.getSize()) ? 1 : -1;
}
return 0;
if ((mSorting & Constants.SORT_DESCENDING) != 0) {
res *= -1;
}
return res;
}
@Override

View File

@ -10,7 +10,7 @@ public class Medium implements Serializable, Comparable {
private final boolean mIsVideo;
private final long mTimestamp;
private final long mSize;
public static int mOrder;
public static int mSorting;
public Medium(String path, boolean isVideo, long timestamp, long size) {
mPath = path;
@ -43,15 +43,15 @@ public class Medium implements Serializable, Comparable {
public int compareTo(Object object) {
final Medium medium = (Medium) object;
int res;
if ((mOrder & Constants.SORT_BY_NAME) == Constants.SORT_BY_NAME) {
if ((mSorting & Constants.SORT_BY_NAME) != 0) {
res = mPath.compareTo(medium.getPath());
} else if ((mOrder & Constants.SORT_BY_DATE) == Constants.SORT_BY_DATE) {
} else if ((mSorting & Constants.SORT_BY_DATE) != 0) {
res = (mTimestamp > medium.getTimestamp()) ? 1 : -1;
} else {
res = (mSize > medium.getSize()) ? 1 : -1;
}
if ((mOrder & Constants.SORT_DESCENDING) == Constants.SORT_DESCENDING) {
if ((mSorting & Constants.SORT_DESCENDING) != 0) {
res *= -1;
}
return res;