Skip to content

Commit

Permalink
Added button to rename files
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciotogneri committed Sep 11, 2016
1 parent 5d88e17 commit f28682a
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public boolean isSelectionMode()
return itemsSelected > 0;
}

public int itemsSelected()
{
return itemsSelected;
}

public boolean allItemsSelected()
{
return itemsSelected == getCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.mauriciotogneri.fileexplorer.R;
Expand Down Expand Up @@ -147,7 +152,7 @@ public synchronized boolean onBackPressed()

private void updateButtonBar()
{
mainActivity.buttonBar().displayButtons(adapter.isSelectionMode(), !adapter.allItemsSelected());
mainActivity.buttonBar().displayButtons(adapter.itemsSelected(), !adapter.allItemsSelected());
}

public String folderName()
Expand Down Expand Up @@ -246,6 +251,55 @@ public void onSelectAll()
updateButtonBar();
}

public void onRename()
{
List<FileInfo> items = adapter.selectedItems(false);

if (items.size() == 1)
{
final FileInfo fileInfo = items.get(0);

View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_rename, null);
final EditText nameField = (EditText) view.findViewById(R.id.item_name);

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setCancelable(false);
builder.setView(view);
builder.setPositiveButton(R.string.dialog_rename, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
renameItem(fileInfo, nameField.getText().toString());
}
});
builder.setNegativeButton(R.string.dialog_cancel, null);

final AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

nameField.setText(fileInfo.name());
nameField.requestFocus();
nameField.selectAll();

nameField.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
dialog.dismiss();
renameItem(fileInfo, nameField.getText().toString());
}

return false;
}
});
}
}

public void onShare()
{
List<FileInfo> selectedItems = adapter.selectedItems(true);
Expand Down Expand Up @@ -348,12 +402,24 @@ protected void onPostExecute(Boolean result)

if (!result)
{
Toast.makeText(mainActivity, R.string.delete_error, Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), R.string.delete_error, Toast.LENGTH_SHORT).show();
}
}
}.execute();
}

private void renameItem(FileInfo fileInfo, String newName)
{
if (fileInfo.rename(newName))
{
refreshFolder();
}
else
{
Toast.makeText(getContext(), R.string.rename_error, Toast.LENGTH_SHORT).show();
}
}

public void refreshFolder()
{
List<FileInfo> files = getFileList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@
public class ButtonBar
{
private final View buttonSelectAll;
private final View buttonRename;
private final View buttonShare;
private final View buttonDelete;

public ButtonBar(View parent, final Stack<FolderFragment> fragments)
{
this.buttonSelectAll = parent.findViewById(R.id.button_selectAll);
this.buttonRename = parent.findViewById(R.id.button_rename);
this.buttonShare = parent.findViewById(R.id.button_share);
this.buttonDelete = parent.findViewById(R.id.button_delete);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
{
fixButtonMargin(parent.getResources(), buttonSelectAll);
fixButtonMargin(parent.getResources(), buttonRename);
fixButtonMargin(parent.getResources(), buttonShare);
fixButtonMargin(parent.getResources(), buttonDelete);
}
Expand All @@ -43,6 +47,20 @@ public void onClick(View view)
}
});

this.buttonRename.setVisibility(View.GONE);
this.buttonRename.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
if (!fragments.isEmpty())
{
FolderFragment fragment = fragments.peek();
fragment.onRename();
}
}
});

this.buttonShare.setVisibility(View.GONE);
this.buttonShare.setOnClickListener(new OnClickListener()
{
Expand Down Expand Up @@ -72,9 +90,9 @@ public void onClick(View view)
});
}

public void displayButtons(boolean display, boolean displaySelectAll)
public void displayButtons(int itemsSelected, boolean displaySelectAll)
{
if (display)
if (itemsSelected > 0)
{
if (displaySelectAll)
{
Expand All @@ -85,12 +103,22 @@ public void displayButtons(boolean display, boolean displaySelectAll)
buttonSelectAll.setVisibility(View.GONE);
}

if (itemsSelected == 1)
{
buttonRename.setVisibility(View.VISIBLE);
}
else
{
buttonRename.setVisibility(View.GONE);
}

buttonShare.setVisibility(View.VISIBLE);
buttonDelete.setVisibility(View.VISIBLE);
}
else
{
buttonSelectAll.setVisibility(View.GONE);
buttonRename.setVisibility(View.GONE);
buttonShare.setVisibility(View.GONE);
buttonDelete.setVisibility(View.GONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public List<FileInfo> files()
return result;
}

public boolean rename(String newName)
{
File newFile = new File(file.getParentFile(), newName);

return !newFile.exists() && file.renameTo(newFile);
}

public boolean delete()
{
if (isDirectory())
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/ic_rename.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<vector android:height="24dp" android:viewportHeight="48.0"
android:viewportWidth="48.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#ffffff"
android:pathData="M23.47,39.58C23.16,38.5 22.83,37.39 22.46,36.25 22.09,35.11 21.73,33.97 21.37,32.83L10.28,32.83C9.92,33.97 9.56,35.11 9.19,36.25 8.82,37.39 8.48,38.5 8.18,39.58L1.52,39.58C2.59,36.22 3.61,33.12 4.56,30.27 5.52,27.42 6.46,24.74 7.38,22.22 8.3,19.71 9.2,17.32 10.09,15.05 10.98,12.79 11.91,10.58 12.86,8.42L18.99,8.42C19.92,10.58 20.84,12.79 21.74,15.05 22.64,17.32 23.55,19.71 24.47,22.22 25.39,24.74 26.33,27.42 27.29,30.27 28.25,33.12 29.26,36.22 30.33,39.58ZM15.78,15.48C15.64,15.93 15.44,16.55 15.17,17.32 14.89,18.1 14.58,19 14.22,20.02 13.86,21.04 13.47,22.16 13.05,23.39 12.62,24.62 12.19,25.91 11.75,27.26L19.85,27.26C19.41,25.91 18.99,24.62 18.58,23.39 18.17,22.16 17.77,21.04 17.4,20.02 17.03,19 16.71,18.1 16.44,17.32 16.17,16.55 15.95,15.93 15.78,15.48Z" android:strokeColor="#00000000"/>
<path android:fillAlpha="1" android:fillColor="#ffffff"
android:pathData="M34.15,1.52 L34.15,6.02 38.26,6.02 38.26,41.98 34.15,41.98 34.15,46.48 46.48,46.48 46.48,41.98 42.41,41.98 42.41,6.02 46.48,6.02 46.48,1.52 34.15,1.52Z"
android:strokeColor="#00000000" android:strokeWidth="80"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/layout/button_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
style="@style/floatingButton"
android:src="@drawable/ic_select"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/button.rename"
style="@style/floatingButton"
android:src="@drawable/ic_rename"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/button.share"
style="@style/floatingButton"
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/dialog_rename.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp">

<EditText
android:id="@+id/item.name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:textColor="@color/gray2"/>

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="app_name">File Explorer</string>

<string name="dialog.delete">Delete</string>
<string name="dialog.rename">Rename</string>
<string name="dialog.cancel">Cancel</string>

<string name="list.empty">No items</string>
Expand All @@ -14,6 +15,8 @@
<item quantity="other">%d items</item>
</plurals>

<string name="rename.error">Error renaming</string>

<string name="delete.confirm">Do you want to delete the selected items?</string>
<string name="delete.deleting">Deleting…</string>
<string name="delete.error">Error deleting</string>
Expand Down

0 comments on commit f28682a

Please sign in to comment.