move CreateNewItem dialog into an own file

This commit is contained in:
tibbi 2016-10-28 23:19:02 +02:00
parent 5be76578a4
commit 6a2841a48e
4 changed files with 108 additions and 62 deletions

View file

@ -37,13 +37,13 @@ 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.CreateNewItemDialog;
import com.simplemobiletools.filemanager.dialogs.PropertiesDialog;
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog;
import com.simplemobiletools.filepicker.models.FileDirItem;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -221,71 +221,14 @@ public class ItemsFragment extends android.support.v4.app.Fragment
@OnClick(R.id.items_fab)
public void fabClicked(View view) {
final View newItemView = getActivity().getLayoutInflater().inflate(R.layout.create_new, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getResources().getString(R.string.create_new));
builder.setView(newItemView);
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 CreateNewItemDialog(getContext(), mPath, new CreateNewItemDialog.OnCreateNewItemListener() {
@Override
public void onClick(View v) {
final EditText itemName = (EditText) newItemView.findViewById(R.id.item_name);
final String name = itemName.getText().toString().trim();
if (Utils.isNameValid(name)) {
final File file = new File(mPath, name);
if (file.exists()) {
Utils.showToast(getContext(), R.string.name_taken);
return;
}
final RadioGroup radio = (RadioGroup) newItemView.findViewById(R.id.dialog_radio_group);
if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_directory) {
if (!createDirectory(file, alertDialog)) {
errorOccurred();
}
} else {
if (!createFile(file, alertDialog)) {
errorOccurred();
}
}
} else {
Utils.showToast(getContext(), R.string.invalid_name);
}
public void onSuccess() {
fillItems();
}
});
}
private boolean createDirectory(File file, AlertDialog alertDialog) {
if (file.mkdirs()) {
alertDialog.dismiss();
fillItems();
return true;
}
return false;
}
private void errorOccurred() {
Utils.showToast(getContext(), R.string.error_occurred);
}
private boolean createFile(File file, AlertDialog alertDialog) {
try {
if (file.createNewFile()) {
alertDialog.dismiss();
fillItems();
return true;
}
} catch (IOException ignored) {
}
return false;
}
@Override
public void onRefresh() {
fillItems();
@ -448,7 +391,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
alertDialog.dismiss();
fillItems();
} else {
errorOccurred();
Utils.showToast(getContext(), R.string.error_occurred);
}
} else {
Utils.showToast(getContext(), R.string.invalid_name);

View file

@ -0,0 +1,90 @@
package com.simplemobiletools.filemanager.dialogs
import android.content.Context
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.extensions.toast
import com.simplemobiletools.filemanager.extensions.value
import kotlinx.android.synthetic.main.create_new.view.*
import java.io.File
import java.io.IOException
class CreateNewItemDialog() {
interface OnCreateNewItemListener {
fun onSuccess()
}
lateinit var mContext: Context
var mListener: OnCreateNewItemListener? = null
constructor(context: Context, path: String, listener: OnCreateNewItemListener) : this() {
mContext = context
mListener = listener
val view = LayoutInflater.from(context).inflate(R.layout.create_new, null)
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(View.OnClickListener {
val name = view.item_name.value
if (Utils.isNameValid(name)) {
val file = File(path, name)
if (file.exists()) {
context.toast(R.string.name_taken)
return@OnClickListener
}
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_directory) {
if (!createDirectory(file, this)) {
errorOccurred()
}
} else {
if (!createFile(file, this)) {
errorOccurred()
}
}
} else {
context.toast(R.string.invalid_name)
}
})
}
}
private fun createDirectory(file: File, alertDialog: AlertDialog): Boolean {
return if (file.mkdirs()) {
alertDialog.dismiss()
mListener?.onSuccess()
true
} else
false
}
private fun errorOccurred() {
mContext.toast(R.string.error_occurred)
}
private fun createFile(file: File, alertDialog: AlertDialog): Boolean {
try {
if (file.createNewFile()) {
alertDialog.dismiss()
mListener?.onSuccess()
return true
}
} catch (ignored: IOException) {
}
return false
}
}

View file

@ -0,0 +1,8 @@
package com.simplemobiletools.filemanager.extensions
import android.content.Context
import android.widget.Toast
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()

View file

@ -0,0 +1,5 @@
package com.simplemobiletools.filemanager.extensions
import android.widget.EditText
val EditText.value: String get() = this.text.toString().trim()