From 3dbd1986288981cf1ebf4ccf19db296c8dc24c6a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 30 Oct 2016 14:12:24 +0100 Subject: [PATCH] move CopyDialog in a separate file --- .../filemanager/fragments/ItemsFragment.java | 89 +++---------------- .../filemanager/dialogs/CopyDialog.kt | 87 ++++++++++++++++++ .../filemanager/extensions/textView.kt | 5 ++ 3 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/textView.kt 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 15f94435..fa32780b 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -1,7 +1,6 @@ package com.simplemobiletools.filemanager.fragments; import android.content.ActivityNotFoundException; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.graphics.Color; @@ -12,7 +11,6 @@ import android.os.Parcelable; import android.support.annotation.Nullable; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; -import android.support.v4.util.Pair; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AlertDialog; import android.util.SparseBooleanArray; @@ -26,7 +24,6 @@ import android.view.ViewGroup; import android.webkit.MimeTypeMap; import android.widget.AdapterView; import android.widget.ListView; -import android.widget.RadioGroup; import android.widget.TextView; import com.simplemobiletools.filemanager.Config; @@ -35,10 +32,10 @@ import com.simplemobiletools.filemanager.R; import com.simplemobiletools.filemanager.Utils; import com.simplemobiletools.filemanager.adapters.ItemsAdapter; import com.simplemobiletools.filemanager.asynctasks.CopyTask; +import com.simplemobiletools.filemanager.dialogs.CopyDialog; 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; import java.io.File; @@ -370,65 +367,20 @@ public class ItemsFragment extends android.support.v4.app.Fragment if (itemIndexes.isEmpty()) return; - final View copyView = getActivity().getLayoutInflater().inflate(R.layout.copy_item, null); + final List itemsToCopy = new ArrayList<>(itemIndexes.size()); + for (Integer i : itemIndexes) { + FileDirItem item = mItems.get(i); + itemsToCopy.add(new File(item.getPath())); + } - final TextView source = (TextView) copyView.findViewById(R.id.source); - source.setText(mPath + "/"); - - mDestinationView = (TextView) copyView.findViewById(R.id.destination); - mDestinationView.setOnClickListener(destinationPicker); - - final int copyString = (itemIndexes.size() == 1) ? R.string.copy_item : R.string.copy_items; - final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(getResources().getString(copyString)); - builder.setView(copyView); - builder.setPositiveButton(R.string.ok, null); - builder.setNegativeButton(R.string.cancel, null); - - mCopyDialog = builder.create(); - mCopyDialog.show(); - mCopyDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + new CopyDialog(getActivity(), itemsToCopy, mPath, this, new CopyDialog.OnCopyListener() { @Override - public void onClick(View v) { - final String destinationPath = mDestinationView.getText().toString().trim(); - if (destinationPath.equals(getResources().getString(R.string.select_destination))) { - Utils.showToast(getContext(), R.string.please_select_destination); - return; - } - - final File destinationDir = new File(destinationPath); - if (!destinationDir.exists()) { - Utils.showToast(getContext(), R.string.invalid_destination); - return; - } - - final List itemsToCopy = new ArrayList<>(itemIndexes.size()); - for (Integer i : itemIndexes) { - FileDirItem item = mItems.get(i); - itemsToCopy.add(new File(item.getPath())); - } - - final RadioGroup radio = (RadioGroup) copyView.findViewById(R.id.dialog_radio_group); - if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_copy) { - Utils.showToast(getContext(), R.string.copying); - final Pair, File> pair = new Pair<>(itemsToCopy, destinationDir); - new CopyTask(ItemsFragment.this).execute(pair); - } else { - for (File f : itemsToCopy) { - final File destination = new File(destinationDir, f.getName()); - f.renameTo(destination); - rescanItem(destination); - } - - mCopyDialog.dismiss(); - fillItems(); - } + public void onSuccess() { + fillItems(); } - }); - mCopyDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override - public void onDismiss(DialogInterface dialog) { + public void onCancel() { mCopyDestinationPath = mPath; } }); @@ -482,26 +434,6 @@ public class ItemsFragment extends android.support.v4.app.Fragment fillItems(); } - private View.OnClickListener destinationPicker = new View.OnClickListener() { - @Override - public void onClick(final View view) { - final boolean showHiddenItems = mConfig.getShowHidden(); - final boolean showFullPath = mConfig.getShowFullPath(); - new FilePickerDialog(getContext(), mCopyDestinationPath, false, showHiddenItems, showFullPath, new FilePickerDialog.OnFilePickerListener() { - @Override - public void onFail(FilePickerDialog.FilePickerResult pickFolderResult) { - - } - - @Override - public void onSuccess(String path) { - mCopyDestinationPath = path; - mDestinationView.setText(path); - } - }); - } - }; - @Override public boolean onTouch(View v, MotionEvent event) { if (mSnackbar != null && mSnackbar.isShown()) { @@ -567,7 +499,6 @@ public class ItemsFragment extends android.support.v4.app.Fragment @Override public void copySucceeded(File file) { rescanItem(file); - mCopyDialog.dismiss(); fillItems(); } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt new file mode 100644 index 00000000..20abcf3c --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -0,0 +1,87 @@ +package com.simplemobiletools.filemanager.dialogs + +import android.app.Activity +import android.support.v4.util.Pair +import android.support.v7.app.AlertDialog +import android.view.LayoutInflater +import android.view.WindowManager +import com.simplemobiletools.filemanager.Config +import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.Utils +import com.simplemobiletools.filemanager.asynctasks.CopyTask +import com.simplemobiletools.filemanager.extensions.rescanItem +import com.simplemobiletools.filemanager.extensions.toast +import com.simplemobiletools.filemanager.extensions.value +import com.simplemobiletools.filepicker.dialogs.FilePickerDialog +import kotlinx.android.synthetic.main.copy_item.view.* +import java.io.File + +class CopyDialog(val activity: Activity, val files: List, val path: String, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) { + val mContext = activity + init { + val view = LayoutInflater.from(mContext).inflate(R.layout.copy_item, null) + view.source.text = "$path/" + + view.destination.setOnClickListener { + val config = Config.newInstance(mContext) + FilePickerDialog(activity, path, false, config.showHidden, true, object: FilePickerDialog.OnFilePickerListener { + override fun onFail(error: FilePickerDialog.FilePickerResult) { + } + + override fun onSuccess(pickedPath: String) { + view.destination.text = pickedPath + } + }) + } + + AlertDialog.Builder(mContext) + .setTitle(mContext.resources.getString(R.string.create_new)) + .setView(view) + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, { dialog, which -> dialogDismissed() }) + .setOnCancelListener { dialogDismissed() } + .create().apply { + window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) + show() + getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ + val destinationPath = view.destination.value + if (destinationPath == context.resources.getString(R.string.select_destination)) { + context.toast(R.string.please_select_destination) + return@setOnClickListener + } + + val destinationDir = File(destinationPath) + if (!destinationDir.exists()) { + context.toast(R.string.invalid_destination) + return@setOnClickListener + } + + if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { + Utils.showToast(context, R.string.copying) + val pair = Pair, File>(files, destinationDir) + CopyTask(copyListener).execute(pair) + dismiss() + } else { + for (f in files) { + val destination = File(destinationDir, f.name) + f.renameTo(destination) + context.rescanItem(destination) + } + + dismiss() + listener.onSuccess() + } + }) + } + } + + private fun dialogDismissed() { + listener.onCancel() + } + + interface OnCopyListener { + fun onSuccess() + + fun onCancel() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/textView.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/textView.kt new file mode 100644 index 00000000..44eba86e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/textView.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.filemanager.extensions + +import android.widget.TextView + +val TextView.value: String get() = this.text.toString().trim()