handle item deletion
This commit is contained in:
parent
1d45e4e0cb
commit
7293976971
|
@ -2,15 +2,21 @@ package com.simplemobiletools.filemanager.fragments;
|
||||||
|
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.design.widget.CoordinatorLayout;
|
||||||
|
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.support.v7.app.AlertDialog;
|
||||||
|
import android.util.SparseBooleanArray;
|
||||||
import android.view.ActionMode;
|
import android.view.ActionMode;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
@ -38,13 +44,17 @@ import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
|
|
||||||
public class ItemsFragment extends android.support.v4.app.Fragment
|
public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, ListView.MultiChoiceModeListener {
|
implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, ListView.MultiChoiceModeListener,
|
||||||
|
ListView.OnTouchListener {
|
||||||
@BindView(R.id.items_list) ListView mListView;
|
@BindView(R.id.items_list) ListView mListView;
|
||||||
@BindView(R.id.items_swipe_refresh) SwipeRefreshLayout mSwipeRefreshLayout;
|
@BindView(R.id.items_swipe_refresh) SwipeRefreshLayout mSwipeRefreshLayout;
|
||||||
|
@BindView(R.id.items_holder) CoordinatorLayout mCoordinatorLayout;
|
||||||
|
|
||||||
private List<FileDirItem> mItems;
|
private List<FileDirItem> mItems;
|
||||||
private ItemInteractionListener mListener;
|
private ItemInteractionListener mListener;
|
||||||
|
private List<String> mToBeDeleted;
|
||||||
private String mPath;
|
private String mPath;
|
||||||
|
private Snackbar mSnackbar;
|
||||||
|
|
||||||
private boolean mShowHidden;
|
private boolean mShowHidden;
|
||||||
private int mSelectedItemsCnt;
|
private int mSelectedItemsCnt;
|
||||||
|
@ -62,6 +72,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
mShowHidden = Config.newInstance(getContext()).getShowHidden();
|
mShowHidden = Config.newInstance(getContext()).getShowHidden();
|
||||||
mItems = new ArrayList<>();
|
mItems = new ArrayList<>();
|
||||||
|
mToBeDeleted = new ArrayList<>();
|
||||||
fillItems();
|
fillItems();
|
||||||
mSwipeRefreshLayout.setOnRefreshListener(this);
|
mSwipeRefreshLayout.setOnRefreshListener(this);
|
||||||
}
|
}
|
||||||
|
@ -75,6 +86,12 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
deleteItems();
|
||||||
|
}
|
||||||
|
|
||||||
private void fillItems() {
|
private void fillItems() {
|
||||||
mPath = getArguments().getString(Constants.PATH);
|
mPath = getArguments().getString(Constants.PATH);
|
||||||
final List<FileDirItem> newItems = getItems();
|
final List<FileDirItem> newItems = getItems();
|
||||||
|
@ -89,6 +106,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
mListView.setAdapter(adapter);
|
mListView.setAdapter(adapter);
|
||||||
mListView.setOnItemClickListener(this);
|
mListView.setOnItemClickListener(this);
|
||||||
mListView.setMultiChoiceModeListener(this);
|
mListView.setMultiChoiceModeListener(this);
|
||||||
|
mListView.setOnTouchListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListener(ItemInteractionListener listener) {
|
public void setListener(ItemInteractionListener listener) {
|
||||||
|
@ -105,6 +123,9 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
if (!mShowHidden && curName.startsWith("."))
|
if (!mShowHidden && curName.startsWith("."))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (mToBeDeleted.contains(curPath))
|
||||||
|
continue;
|
||||||
|
|
||||||
items.add(new FileDirItem(curPath, curName, file.isDirectory()));
|
items.add(new FileDirItem(curPath, curName, file.isDirectory()));
|
||||||
}
|
}
|
||||||
return items;
|
return items;
|
||||||
|
@ -255,6 +276,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.cab_delete:
|
case R.id.cab_delete:
|
||||||
|
prepareForDeleting();
|
||||||
mode.finish();
|
mode.finish();
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
|
@ -262,6 +284,80 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void prepareForDeleting() {
|
||||||
|
mToBeDeleted.clear();
|
||||||
|
final SparseBooleanArray items = mListView.getCheckedItemPositions();
|
||||||
|
final int cnt = items.size();
|
||||||
|
int deletedCnt = 0;
|
||||||
|
for (int i = 0; i < cnt; i++) {
|
||||||
|
if (items.valueAt(i)) {
|
||||||
|
final int id = items.keyAt(i);
|
||||||
|
final String path = mItems.get(id).getPath();
|
||||||
|
mToBeDeleted.add(path);
|
||||||
|
deletedCnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyDeletion(deletedCnt);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyDeletion(int cnt) {
|
||||||
|
final Resources res = getResources();
|
||||||
|
final String msg = res.getQuantityString(R.plurals.items_deleted, cnt, cnt);
|
||||||
|
mSnackbar = Snackbar.make(mCoordinatorLayout, msg, Snackbar.LENGTH_INDEFINITE);
|
||||||
|
mSnackbar.setAction(res.getString(R.string.undo), undoDeletion);
|
||||||
|
mSnackbar.setActionTextColor(Color.WHITE);
|
||||||
|
mSnackbar.show();
|
||||||
|
fillItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
if (mSnackbar != null && mSnackbar.isShown()) {
|
||||||
|
deleteItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteItems() {
|
||||||
|
if (mToBeDeleted == null || mToBeDeleted.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (mSnackbar != null) {
|
||||||
|
mSnackbar.dismiss();
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> updatedFiles = new ArrayList<>();
|
||||||
|
for (String delPath : mToBeDeleted) {
|
||||||
|
final File file = new File(delPath);
|
||||||
|
if (file.exists()) {
|
||||||
|
deleteItem(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mToBeDeleted.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteItem(File item) {
|
||||||
|
if (item.isDirectory()) {
|
||||||
|
for (File child : item.listFiles()) {
|
||||||
|
deleteItem(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
item.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
private View.OnClickListener undoDeletion = new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
mToBeDeleted.clear();
|
||||||
|
mSnackbar.dismiss();
|
||||||
|
fillItems();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroyActionMode(ActionMode mode) {
|
public void onDestroyActionMode(ActionMode mode) {
|
||||||
mSelectedItemsCnt = 0;
|
mSelectedItemsCnt = 0;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<android.support.design.widget.CoordinatorLayout
|
<android.support.design.widget.CoordinatorLayout
|
||||||
android:id="@+id/item_holder"
|
android:id="@+id/items_holder"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
|
@ -9,6 +9,12 @@
|
||||||
<string name="invalid_name">The name contains invalid characters</string>
|
<string name="invalid_name">The name contains invalid characters</string>
|
||||||
<string name="error_occurred">An unknown error occurred</string>
|
<string name="error_occurred">An unknown error occurred</string>
|
||||||
<string name="delete">Delete</string>
|
<string name="delete">Delete</string>
|
||||||
|
<string name="undo">Undo</string>
|
||||||
|
|
||||||
|
<plurals name="items_deleted">
|
||||||
|
<item quantity="one">1 item deleted</item>
|
||||||
|
<item quantity="other">%1$d items deleted</item>
|
||||||
|
</plurals>
|
||||||
|
|
||||||
<!-- About -->
|
<!-- About -->
|
||||||
<string name="about">About</string>
|
<string name="about">About</string>
|
||||||
|
|
Loading…
Reference in New Issue