move directory rename dialog in a separate file

This commit is contained in:
tibbi 2016-11-06 21:16:41 +01:00
parent fc8f8c3fa7
commit c7408fb9fe
5 changed files with 94 additions and 61 deletions

View file

@ -14,7 +14,6 @@ import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
@ -23,9 +22,7 @@ import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Config;
@ -35,6 +32,7 @@ import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.DirectoryAdapter;
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog;
import com.simplemobiletools.gallery.models.Directory;
import org.jetbrains.annotations.NotNull;
@ -290,59 +288,16 @@ public class MainActivity extends SimpleActivity
private void renameDir(final String path) {
final File dir = new File(path);
final View renameDirView = getLayoutInflater().inflate(R.layout.rename_directory, null);
final EditText dirNameET = (EditText) renameDirView.findViewById(R.id.directory_name);
dirNameET.setText(dir.getName());
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.rename_folder));
builder.setView(renameDirView);
final TextView dirPath = (TextView) renameDirView.findViewById(R.id.directory_path);
dirPath.setText(dir.getParent() + "/");
builder.setPositiveButton(R.string.ok, null);
builder.setNegativeButton(R.string.cancel, null);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
new RenameDirectoryDialog(this, dir, new RenameDirectoryDialog.OnRenameDirListener() {
@Override
public void onClick(View v) {
final String newDirName = dirNameET.getText().toString().trim();
if (newDirName.isEmpty()) {
Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_empty);
return;
}
final List<String> updatedFiles = new ArrayList<>();
updatedFiles.add(dir.getAbsolutePath());
final File newDir = new File(dir.getParent(), newDirName);
if (dir.renameTo(newDir)) {
Utils.Companion.showToast(getApplicationContext(), R.string.renaming_folder);
alertDialog.dismiss();
public void onRenameDirSuccess(@NotNull String[] changedFiles) {
mActionMode.finish();
final File[] files = newDir.listFiles();
for (File f : files) {
updatedFiles.add(f.getAbsolutePath());
}
updatedFiles.add(newDir.getAbsolutePath());
final String[] changedFiles = updatedFiles.toArray(new String[updatedFiles.size()]);
MediaScannerConnection
.scanFile(getApplicationContext(), changedFiles, null, new MediaScannerConnection.OnScanCompletedListener() {
MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
scanCompleted(path);
}
});
} else {
Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_error);
}
}
});
}

View file

@ -301,9 +301,9 @@ public class ViewPagerActivity extends SimpleActivity
}
private void editMedium() {
new RenameFileDialog(this, getCurrentFile(), new RenameFileDialog.OnRenameItemListener() {
new RenameFileDialog(this, getCurrentFile(), new RenameFileDialog.OnRenameFileListener() {
@Override
public void onRenameSuccess(@NotNull File newFile) {
public void onRenameFileSuccess(@NotNull File newFile) {
mMedia.get(mPager.getCurrentItem()).setPath(newFile.getAbsolutePath());
updateActionbarTitle();
}

View file

@ -129,7 +129,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
if (dir.exists() && dir.isDirectory) {
val res = dir.list { file, filename -> filename == ".nomedia" }
if (res.size > 0)
if (res != null && res.size > 0)
ignoreDirs.add(d)
}
}

View file

@ -0,0 +1,78 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_directory.view.*
import java.io.File
import java.util.*
class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: OnRenameDirListener) {
val context = activity
init {
val view = LayoutInflater.from(context).inflate(R.layout.rename_directory, null)
view.directory_name.setText(dir.name)
view.directory_path.text = "${dir.parent}/"
AlertDialog.Builder(context)
.setTitle(context.resources.getString(R.string.rename_folder))
.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 newDirName = view.directory_name.value
if (newDirName.isEmpty()) {
context.toast(R.string.rename_folder_empty)
return@setOnClickListener
}
val updatedFiles = ArrayList<String>()
updatedFiles.add(dir.absolutePath)
val newDir = File(dir.parent, newDirName)
if (Utils.needsStupidWritePermissions(context, dir.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, dir))
return@setOnClickListener
val document = Utils.Companion.getFileDocument(context, dir.absolutePath)
if (document.canWrite())
document.renameTo(newDirName)
sendSuccess(updatedFiles, newDir)
dismiss()
} else if (dir.renameTo(newDir)) {
sendSuccess(updatedFiles, newDir)
dismiss()
} else {
context.toast(R.string.rename_folder_error)
}
})
}
}
private fun sendSuccess(updatedFiles: ArrayList<String>, newDir: File) {
context.toast(R.string.renaming_folder)
val files = newDir.listFiles()
for (file in files) {
updatedFiles.add(file.absolutePath)
}
updatedFiles.add(newDir.absolutePath)
val changedFiles = updatedFiles.toTypedArray()
listener.onRenameDirSuccess(changedFiles)
}
interface OnRenameDirListener {
fun onRenameDirSuccess(changedFiles: Array<String>)
}
}

View file

@ -12,7 +12,7 @@ import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File
class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameItemListener) {
class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameFileListener) {
init {
val context = activity
@ -71,10 +71,10 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR
private fun sendSuccess(currFile: File, newFile: File) {
val changedFiles = arrayOf(currFile.absolutePath, newFile.absolutePath)
MediaScannerConnection.scanFile(activity.applicationContext, changedFiles, null, null)
listener.onRenameSuccess(newFile)
listener.onRenameFileSuccess(newFile)
}
interface OnRenameItemListener {
fun onRenameSuccess(newFile: File)
interface OnRenameFileListener {
fun onRenameFileSuccess(newFile: File)
}
}