replace the Snackbar with an Undo button

- snackbar would appear behind the transparent navigation bar
This commit is contained in:
tibbi 2016-04-18 21:29:19 +02:00
parent 01a9036c17
commit 4abe15564d
4 changed files with 50 additions and 37 deletions

View File

@ -3,13 +3,11 @@ package com.simplemobiletools.gallery.activities;
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;
import android.graphics.Color;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; 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.v4.view.ViewPager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -19,6 +17,7 @@ import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.RelativeLayout;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.MyViewPager; import com.simplemobiletools.gallery.MyViewPager;
@ -41,9 +40,9 @@ public class ViewPagerActivity extends AppCompatActivity
private MyViewPager pager; private MyViewPager pager;
private String path; private String path;
private String directory; private String directory;
private Snackbar snackbar; private boolean isUndoShown;
private boolean isSnackbarShown;
private String toBeDeleted; private String toBeDeleted;
private View undoBtn;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -57,7 +56,10 @@ public class ViewPagerActivity extends AppCompatActivity
hideSystemUI(); hideSystemUI();
path = getIntent().getStringExtra(Constants.PHOTO); path = getIntent().getStringExtra(Constants.PHOTO);
MediaScannerConnection.scanFile(this, new String[]{path}, null, this); MediaScannerConnection.scanFile(this, new String[]{path}, null, null);
undoBtn = findViewById(R.id.undo_delete);
undoBtn.setOnClickListener(undoDeletion);
addUndoBottomMargin();
directory = new File(path).getParent(); directory = new File(path).getParent();
pager = (MyViewPager) findViewById(R.id.view_pager); pager = (MyViewPager) findViewById(R.id.view_pager);
photos = getPhotos(); photos = getPhotos();
@ -116,14 +118,9 @@ public class ViewPagerActivity extends AppCompatActivity
if (photos.size() <= 1) { if (photos.size() <= 1) {
deleteFile(); deleteFile();
} else { } else {
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); Utils.showToast(this, R.string.file_deleted);
final Resources res = getResources(); undoBtn.setVisibility(View.VISIBLE);
final String curFileName = getCurrentFile().getName() + " "; isUndoShown = true;
snackbar = Snackbar.make(coordinator, curFileName + 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;
reloadViewPager(); reloadViewPager();
} }
} }
@ -132,10 +129,7 @@ public class ViewPagerActivity extends AppCompatActivity
if (toBeDeleted.isEmpty()) if (toBeDeleted.isEmpty())
return; return;
if (snackbar != null) isUndoShown = false;
snackbar.dismiss();
isSnackbarShown = false;
final File file = new File(toBeDeleted); final File file = new File(toBeDeleted);
if (file.delete()) { if (file.delete()) {
@ -143,14 +137,15 @@ public class ViewPagerActivity extends AppCompatActivity
MediaScannerConnection.scanFile(this, deletedPath, null, this); MediaScannerConnection.scanFile(this, deletedPath, null, this);
} }
toBeDeleted = ""; toBeDeleted = "";
undoBtn.setVisibility(View.GONE);
} }
private View.OnClickListener undoDeletion = new View.OnClickListener() { private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
snackbar.dismiss(); isUndoShown = false;
isSnackbarShown = false;
toBeDeleted = ""; toBeDeleted = "";
undoBtn.setVisibility(View.GONE);
reloadViewPager(); reloadViewPager();
} }
}; };
@ -258,12 +253,12 @@ public class ViewPagerActivity extends AppCompatActivity
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do { do {
final String curPath = cursor.getString(pathIndex); final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern) && !curPath.equals(toBeDeleted)) { if (curPath.matches(pattern) && !curPath.equals(toBeDeleted)) {
photos.add(curPath); photos.add(curPath);
if (curPath.equals(path)) if (curPath.equals(path)) {
pos = i; pos = i;
}
i++; i++;
} }
@ -312,6 +307,17 @@ public class ViewPagerActivity extends AppCompatActivity
return new File(photos.get(pager.getCurrentItem())); return new File(photos.get(pager.getCurrentItem()));
} }
private void addUndoBottomMargin() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final Resources resources = getResources();
int id = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (id > 0) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) undoBtn.getLayoutParams();
params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, resources.getDimensionPixelSize(id));
}
}
}
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@ -339,14 +345,15 @@ public class ViewPagerActivity extends AppCompatActivity
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
reloadViewPager(); if (photos.size() <= 1)
reloadViewPager();
} }
}); });
} }
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
if (isSnackbarShown) { if (isUndoShown) {
deleteFile(); deleteFile();
} }

View File

@ -1,19 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout <RelativeLayout
android:id="@+id/coordinator_layout" android:id="@+id/photo_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"
android:background="@android:color/black">
<LinearLayout <com.simplemobiletools.gallery.MyViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"/>
android:background="@android:color/black">
<com.simplemobiletools.gallery.MyViewPager <ImageView
android:id="@+id/view_pager" android:id="@+id/undo_delete"
android:layout_width="match_parent" android:layout_width="64dp"
android:layout_height="match_parent"/> android:layout_height="64dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="@dimen/undo_padding"
android:src="@mipmap/ic_launcher"
android:visibility="gone"/>
</LinearLayout> </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

View File

@ -2,4 +2,5 @@
<dimen name="activity_margin">16dp</dimen> <dimen name="activity_margin">16dp</dimen>
<dimen name="dir_tmb_size">150dp</dimen> <dimen name="dir_tmb_size">150dp</dimen>
<dimen name="photo_tmb_size">100dp</dimen> <dimen name="photo_tmb_size">100dp</dimen>
<dimen name="undo_padding">8dp</dimen>
</resources> </resources>

View File

@ -17,7 +17,7 @@
<string name="file_name">File name</string> <string name="file_name">File name</string>
<string name="folder_name">Folder name</string> <string name="folder_name">Folder name</string>
<string name="extension">Extension</string> <string name="extension">Extension</string>
<string name="file_deleted">deleted</string> <string name="file_deleted">File deleted</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item> <item quantity="one">1 folder deleted</item>