diff --git a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java index e7559b9e..15f94435 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -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(); } }); } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt new file mode 100644 index 00000000..09a89c73 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt @@ -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() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt index 59ba5480..5c3fd08a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt @@ -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) +}