allow Undoing file deletion

This commit is contained in:
tibbi 2016-03-03 00:12:10 +01:00
parent a2f508378e
commit b8f4bcbd0a
4 changed files with 90 additions and 20 deletions

View File

@ -23,6 +23,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.4.1'
}

View File

@ -4,10 +4,13 @@ import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
@ -16,6 +19,7 @@ import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
@ -32,13 +36,17 @@ import java.util.List;
import java.util.regex.Pattern;
public class PhotosActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener {
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener,
GridView.OnTouchListener {
private final int STORAGE_PERMISSION = 1;
private List<String> photos;
private int selectedItemsCnt;
private GridView gridView;
private String path;
private PhotosAdapter adapter;
private Snackbar snackbar;
private boolean isSnackbarShown;
private List<String> toBeDeleted;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -74,6 +82,7 @@ public class PhotosActivity extends AppCompatActivity
}
private void initializeGallery() {
toBeDeleted = new ArrayList<>();
path = getIntent().getStringExtra(Constants.DIRECTORY);
photos = getPhotos();
if (isDirEmpty())
@ -84,6 +93,8 @@ public class PhotosActivity extends AppCompatActivity
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
gridView.setMultiChoiceModeListener(this);
gridView.setOnTouchListener(this);
isSnackbarShown = false;
final String dirName = Helpers.getFilename(path);
setTitle(dirName);
@ -110,7 +121,7 @@ public class PhotosActivity extends AppCompatActivity
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do {
final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern)) {
if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) {
myPhotos.add(cursor.getString(pathIndex));
}
} while (cursor.moveToNext());
@ -128,19 +139,56 @@ public class PhotosActivity extends AppCompatActivity
return false;
}
private void deleteSelectedItems() {
private void prepareForDeleting() {
Helpers.showToast(this, R.string.deleting);
final SparseBooleanArray items = gridView.getCheckedItemPositions();
int cnt = items.size();
for (int i = 0; i < cnt; i++) {
final int id = items.keyAt(i);
final File file = new File(photos.get(id));
file.delete();
toBeDeleted.add(photos.get(id));
}
notifyDeletion(toBeDeleted.size());
MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
}
private void notifyDeletion(int cnt) {
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
final String msg = getResources().getQuantityString(R.plurals.files_deleted, cnt, cnt);
snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(getResources().getString(R.string.undo), undoDeletion);
snackbar.setActionTextColor(Color.WHITE);
snackbar.show();
isSnackbarShown = true;
}
private void deleteFiles() {
for (String delPath : toBeDeleted) {
final File file = new File(delPath);
if (file.exists())
file.delete();
}
MediaScannerConnection.scanFile(this, new String[]{path}, null, null);
}
private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
isSnackbarShown = false;
toBeDeleted.clear();
updateGridView();
}
};
private void updateGridView() {
photos = getPhotos();
if (!isDirEmpty()) {
adapter.updateItems(photos);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(this, ViewPagerActivity.class);
@ -175,7 +223,7 @@ public class PhotosActivity extends AppCompatActivity
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.cab_remove:
deleteSelectedItems();
prepareForDeleting();
mode.finish();
return true;
default:
@ -193,11 +241,19 @@ public class PhotosActivity extends AppCompatActivity
runOnUiThread(new Runnable() {
@Override
public void run() {
photos = getPhotos();
if (!isDirEmpty()) {
adapter.updateItems(photos);
}
updateGridView();
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isSnackbarShown) {
deleteFiles();
snackbar.dismiss();
isSnackbarShown = false;
}
return false;
}
}

View File

@ -1,13 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<GridView
android:id="@+id/photos_grid"
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:choiceMode="multipleChoiceModal"
android:columnWidth="@dimen/photo_tmb_size"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"/>
android:layout_height="match_parent">
<GridView
android:id="@+id/photos_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:choiceMode="multipleChoiceModal"
android:columnWidth="@dimen/photo_tmb_size"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -4,4 +4,10 @@
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
<string name="remove">Remove</string>
<string name="deleting">Deleting</string>
<string name="undo">Undo</string>
<plurals name="files_deleted">
<item quantity="one">1 file deleted</item>
<item quantity="other">%1$d files deleted</item>
</plurals>
</resources>