move the rename dialog in a separate file
This commit is contained in:
parent
ee059eb319
commit
10d9653560
3 changed files with 76 additions and 39 deletions
|
@ -23,10 +23,8 @@ import android.view.MenuItem;
|
|||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
|
@ -39,6 +37,7 @@ import com.simplemobiletools.filemanager.adapters.ItemsAdapter;
|
|||
import com.simplemobiletools.filemanager.asynctasks.CopyTask;
|
||||
import com.simplemobiletools.filemanager.dialogs.CreateNewItemDialog;
|
||||
import com.simplemobiletools.filemanager.dialogs.PropertiesDialog;
|
||||
import com.simplemobiletools.filemanager.dialogs.RenameItemDialog;
|
||||
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog;
|
||||
import com.simplemobiletools.filepicker.models.FileDirItem;
|
||||
|
||||
|
@ -358,44 +357,10 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||
if (item == null)
|
||||
return;
|
||||
|
||||
final View renameView = getActivity().getLayoutInflater().inflate(R.layout.rename_item, null);
|
||||
final EditText itemName = (EditText) renameView.findViewById(R.id.item_name);
|
||||
itemName.setText(item.getName());
|
||||
|
||||
final int title = (item.isDirectory()) ? R.string.rename_directory : R.string.rename_file;
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
builder.setTitle(getResources().getString(title));
|
||||
builder.setView(renameView);
|
||||
builder.setPositiveButton(R.string.ok, null);
|
||||
builder.setNegativeButton(R.string.cancel, null);
|
||||
|
||||
final AlertDialog alertDialog = builder.create();
|
||||
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
||||
alertDialog.show();
|
||||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
|
||||
new RenameItemDialog(getContext(), mPath, item, new RenameItemDialog.OnRenameItemListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String name = itemName.getText().toString().trim();
|
||||
if (Utils.isNameValid(name)) {
|
||||
final File currFile = new File(mPath, item.getName());
|
||||
final File newFile = new File(mPath, name);
|
||||
|
||||
if (newFile.exists()) {
|
||||
Utils.showToast(getContext(), R.string.name_taken);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currFile.renameTo(newFile)) {
|
||||
rescanItem(newFile);
|
||||
MediaScannerConnection.scanFile(getContext(), new String[]{currFile.getAbsolutePath(), newFile.getAbsolutePath()}, null, null);
|
||||
alertDialog.dismiss();
|
||||
fillItems();
|
||||
} else {
|
||||
Utils.showToast(getContext(), R.string.error_occurred);
|
||||
}
|
||||
} else {
|
||||
Utils.showToast(getContext(), R.string.invalid_name);
|
||||
}
|
||||
public void onSuccess() {
|
||||
fillItems();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package com.simplemobiletools.filemanager.dialogs
|
||||
|
||||
import android.content.Context
|
||||
import android.media.MediaScannerConnection
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.LayoutInflater
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.filemanager.R
|
||||
import com.simplemobiletools.filemanager.Utils
|
||||
import com.simplemobiletools.filemanager.extensions.rescanItem
|
||||
import com.simplemobiletools.filemanager.extensions.toast
|
||||
import com.simplemobiletools.filemanager.extensions.value
|
||||
import com.simplemobiletools.filepicker.models.FileDirItem
|
||||
import kotlinx.android.synthetic.main.rename_item.view.*
|
||||
import java.io.File
|
||||
|
||||
class RenameItemDialog(val context: Context, val path: String, val item: FileDirItem, val listener: OnRenameItemListener) {
|
||||
|
||||
init {
|
||||
val view = LayoutInflater.from(context).inflate(R.layout.rename_item, null)
|
||||
view.item_name.setText(item.name)
|
||||
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(context.resources.getString(R.string.create_new))
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
show()
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||
val name = view.item_name.value
|
||||
if (Utils.isNameValid(name)) {
|
||||
val currFile = File(path, item.name)
|
||||
val newFile = File(path, name)
|
||||
|
||||
if (newFile.exists()) {
|
||||
context.toast(R.string.name_taken)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
if (currFile.renameTo(newFile)) {
|
||||
context.rescanItem(newFile)
|
||||
MediaScannerConnection.scanFile(context, arrayOf(currFile.absolutePath, newFile.absolutePath), null, null)
|
||||
dismiss()
|
||||
listener.onSuccess()
|
||||
} else {
|
||||
context.toast(R.string.error_occurred)
|
||||
}
|
||||
} else {
|
||||
context.toast(R.string.invalid_name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
interface OnRenameItemListener {
|
||||
fun onSuccess()
|
||||
}
|
||||
}
|
|
@ -1,8 +1,20 @@
|
|||
package com.simplemobiletools.filemanager.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.media.MediaScannerConnection
|
||||
import android.widget.Toast
|
||||
import java.io.File
|
||||
|
||||
fun Context.toast(id: Int) = Toast.makeText(this, resources.getString(id), Toast.LENGTH_SHORT).show()
|
||||
|
||||
fun Context.toast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
|
||||
|
||||
fun Context.rescanItem(item: File) {
|
||||
if (item.isDirectory) {
|
||||
for (child in item.listFiles()) {
|
||||
rescanItem(child)
|
||||
}
|
||||
}
|
||||
|
||||
MediaScannerConnection.scanFile(this, arrayOf(item.absolutePath), null, null)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue