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

View file

@ -1,7 +1,6 @@
package com.simplemobiletools.gallery.activities; package com.simplemobiletools.gallery.activities;
import android.app.WallpaperManager; import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
@ -15,7 +14,6 @@ import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
@ -26,12 +24,11 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.GridView; import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation; import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget; import com.bumptech.glide.request.target.SimpleTarget;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
@ -50,7 +47,7 @@ import butterknife.ButterKnife;
public class MediaActivity extends SimpleActivity public class MediaActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener, implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener { SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener {
private static final String TAG = MediaActivity.class.getSimpleName(); private static final String TAG = MediaActivity.class.getSimpleName();
@BindView(R.id.media_grid) GridView mGridView; @BindView(R.id.media_grid) GridView mGridView;
@BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout;
@ -146,53 +143,7 @@ public class MediaActivity extends SimpleActivity
} }
private void showSortingDialog() { private void showSortingDialog() {
final View sortingView = getLayoutInflater().inflate(R.layout.change_sorting, null); new ChangeSorting(this);
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();
} }
private void deleteDirectoryIfEmpty() { private void deleteDirectoryIfEmpty() {
@ -241,7 +192,7 @@ public class MediaActivity extends SimpleActivity
} }
} }
Medium.mOrder = mConfig.getSorting(); Medium.mSorting = mConfig.getSorting();
Collections.sort(media); Collections.sort(media);
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]); final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
@ -479,4 +430,9 @@ public class MediaActivity extends SimpleActivity
initializeGallery(); initializeGallery();
mSwipeRefreshLayout.setRefreshing(false); 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); Collections.sort(media);
int j = 0; int j = 0;
for (Medium medium : media) { 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; package com.simplemobiletools.gallery.models;
import com.simplemobiletools.gallery.Constants;
public class Directory implements Comparable { public class Directory implements Comparable {
private final String mPath; private final String mPath;
private final String mThumbnail; private final String mThumbnail;
private final String mName; private final String mName;
private final long mTimestamp; private final long mTimestamp;
private int mMediaCnt; 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) { public Directory(String path, String thumbnail, String name, int mediaCnt, long timestamp, long size) {
mPath = path; mPath = path;
@ -14,7 +17,7 @@ public class Directory implements Comparable {
mName = name; mName = name;
mMediaCnt = mediaCnt; mMediaCnt = mediaCnt;
mTimestamp = timestamp; mTimestamp = timestamp;
mBytes = size; mSize = size;
} }
public String getPath() { public String getPath() {
@ -42,22 +45,29 @@ public class Directory implements Comparable {
} }
public long getSize() { public long getSize() {
return mBytes; return mSize;
} }
public void addSize(long bytes) { public void addSize(long bytes) {
mBytes += bytes; mSize += bytes;
} }
@Override @Override
public int compareTo(Object object) { public int compareTo(Object object) {
final Directory directory = (Directory) object; final Directory directory = (Directory) object;
if (mTimestamp < directory.getTimestamp()) { int res;
return 1; if ((mSorting & Constants.SORT_BY_NAME) != 0) {
} else if (mTimestamp > directory.getTimestamp()) { res = mPath.compareTo(directory.getPath());
return -1; } 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 @Override

View file

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