From 304f6010da4a853a42ec6cb6c91620e4c2e9b87c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 5 Mar 2016 23:53:47 +0100 Subject: [PATCH] allow Undoing file deleting from viewpager --- .../gallery/activities/PhotosActivity.java | 6 +- .../gallery/activities/ViewPagerActivity.java | 86 ++++++++++++++++--- app/src/main/res/layout/activity_photo.xml | 20 +++-- app/src/main/res/values/strings.xml | 1 + 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java index 6eb884e9b..729c2b5c5 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.activities; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.res.Resources; import android.database.Cursor; import android.graphics.Color; import android.media.MediaScannerConnection; @@ -162,9 +163,10 @@ public class PhotosActivity extends AppCompatActivity 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); + final Resources res = getResources(); + final String msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt); snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE); - snackbar.setAction(getResources().getString(R.string.undo), undoDeletion); + snackbar.setAction(res.getString(R.string.undo), undoDeletion); snackbar.setActionTextColor(Color.WHITE); snackbar.show(); isSnackbarShown = true; diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 00f586c2e..e0ff521c9 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -2,17 +2,22 @@ package com.simplemobiletools.gallery.activities; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Resources; 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.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.Toast; @@ -29,7 +34,8 @@ import java.util.List; import java.util.regex.Pattern; public class ViewPagerActivity extends AppCompatActivity - implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, MediaScannerConnection.OnScanCompletedListener { + implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, MediaScannerConnection.OnScanCompletedListener, + ViewPager.OnTouchListener { private int pos; private boolean isFullScreen; private ActionBar actionbar; @@ -37,6 +43,9 @@ public class ViewPagerActivity extends AppCompatActivity private MyViewPager pager; private String path; private String directory; + private Snackbar snackbar; + private boolean isSnackbarShown; + private String toBeDeleted; @Override protected void onCreate(Bundle savedInstanceState) { @@ -46,6 +55,7 @@ public class ViewPagerActivity extends AppCompatActivity pos = 0; isFullScreen = true; actionbar = getSupportActionBar(); + toBeDeleted = ""; hideSystemUI(); path = getIntent().getStringExtra(Constants.PHOTO); @@ -60,6 +70,7 @@ public class ViewPagerActivity extends AppCompatActivity pager.setAdapter(adapter); pager.setCurrentItem(pos); pager.addOnPageChangeListener(this); + pager.setOnTouchListener(this); getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this); updateActionbarTitle(); @@ -73,12 +84,13 @@ public class ViewPagerActivity extends AppCompatActivity @Override public boolean onOptionsItemSelected(MenuItem item) { + deleteFile(); switch (item.getItemId()) { case R.id.menu_share: shareImage(); return true; case R.id.menu_remove: - deleteImage(); + notifyDeletion(); return true; case R.id.menu_edit: editImage(); @@ -99,15 +111,45 @@ public class ViewPagerActivity extends AppCompatActivity startActivity(Intent.createChooser(sendIntent, shareTitle)); } - private void deleteImage() { - Helpers.showToast(this, R.string.deleting); - final File file = getCurrentFile(); + private void notifyDeletion() { + final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); + final Resources res = getResources(); + snackbar = Snackbar.make(coordinator, res.getString(R.string.file_deleted), Snackbar.LENGTH_INDEFINITE); + snackbar.setAction(res.getString(R.string.undo), undoDeletion); + snackbar.setActionTextColor(Color.WHITE); + snackbar.show(); + isSnackbarShown = true; + toBeDeleted = getCurrentFile().getAbsolutePath(); + reloadViewPager(); + } + + private void deleteFile() { + if (toBeDeleted.isEmpty()) + return; + + if (snackbar == null) + return; + + snackbar.dismiss(); + isSnackbarShown = false; + + final File file = new File(toBeDeleted); if (file.delete()) { - final String[] deletedPath = new String[]{file.getAbsolutePath()}; + final String[] deletedPath = new String[]{toBeDeleted}; MediaScannerConnection.scanFile(this, deletedPath, null, this); } } + private View.OnClickListener undoDeletion = new View.OnClickListener() { + @Override + public void onClick(View v) { + snackbar.dismiss(); + isSnackbarShown = false; + toBeDeleted = ""; + reloadViewPager(); + } + }; + private boolean isDirEmpty() { if (photos.size() <= 0) { deleteDirectoryIfEmpty(); @@ -120,12 +162,12 @@ public class ViewPagerActivity extends AppCompatActivity private void editImage() { final File file = getCurrentFile(); final String fullName = file.getName(); - final int pos = fullName.lastIndexOf("."); - if (pos <= 0) + final int dotAt = fullName.lastIndexOf("."); + if (dotAt <= 0) return; - final String name = fullName.substring(0, pos); - final String extension = fullName.substring(pos + 1, fullName.length()); + final String name = fullName.substring(0, dotAt); + final String extension = fullName.substring(dotAt + 1, fullName.length()); final View renameFileView = getLayoutInflater().inflate(R.layout.rename_file, null); final EditText fileNameET = (EditText) renameFileView.findViewById(R.id.file_name); @@ -163,7 +205,7 @@ public class ViewPagerActivity extends AppCompatActivity private void reloadViewPager() { final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter(); - final int pos = pager.getCurrentItem(); + final int curPos = pager.getCurrentItem(); photos = getPhotos(); if (isDirEmpty()) return; @@ -172,7 +214,7 @@ public class ViewPagerActivity extends AppCompatActivity adapter.updateItems(photos); pager.setAdapter(adapter); - final int newPos = Math.min(pos, adapter.getCount()); + final int newPos = Math.min(curPos, adapter.getCount()); pager.setCurrentItem(newPos); updateActionbarTitle(); } @@ -182,6 +224,9 @@ public class ViewPagerActivity extends AppCompatActivity if (file.isDirectory() && file.listFiles().length == 0) { file.delete(); } + + final String[] toBeDeleted = new String[]{directory}; + MediaScannerConnection.scanFile(getApplicationContext(), toBeDeleted, null, null); } private List getPhotos() { @@ -200,7 +245,7 @@ public class ViewPagerActivity extends AppCompatActivity do { final String curPath = cursor.getString(pathIndex); - if (curPath.matches(pattern)) { + if (curPath.matches(pattern) && !curPath.equals(toBeDeleted)) { photos.add(curPath); if (curPath.equals(path)) @@ -283,4 +328,19 @@ public class ViewPagerActivity extends AppCompatActivity } }); } + + @Override + public boolean onTouch(View v, MotionEvent event) { + if (isSnackbarShown) { + deleteFile(); + } + + return false; + } + + @Override + protected void onPause() { + super.onPause(); + deleteFile(); + } } diff --git a/app/src/main/res/layout/activity_photo.xml b/app/src/main/res/layout/activity_photo.xml index 65e9046c1..ff6e9c06d 100644 --- a/app/src/main/res/layout/activity_photo.xml +++ b/app/src/main/res/layout/activity_photo.xml @@ -1,13 +1,19 @@ - + android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:background="@android:color/black"> - + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f57ca8ba..c4db7d16b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,6 +10,7 @@ Could not rename the file File name Extension + File deleted 1 file deleted