|
16 | 16 |
|
17 | 17 | package com.duy.ide.file.dialogs; |
18 | 18 |
|
19 | | -import android.content.Context; |
20 | | -import android.content.DialogInterface; |
| 19 | +import android.app.Activity; |
| 20 | +import android.content.Intent; |
| 21 | +import android.os.Bundle; |
21 | 22 | import android.support.annotation.NonNull; |
22 | 23 | import android.support.annotation.Nullable; |
23 | | -import android.support.v7.app.AlertDialog; |
| 24 | +import android.support.v7.app.AppCompatDialogFragment; |
24 | 25 | import android.view.LayoutInflater; |
25 | 26 | import android.view.View; |
| 27 | +import android.view.ViewGroup; |
26 | 28 | import android.widget.ArrayAdapter; |
27 | 29 | import android.widget.EditText; |
28 | 30 | import android.widget.Spinner; |
29 | 31 |
|
30 | | -import com.duy.ide.editor.editor.R; |
31 | 32 | import com.duy.common.utils.IOUtils; |
32 | | -import com.jecelyin.editor.v2.dialog.AbstractDialog; |
| 33 | +import com.duy.file.explorer.FileExplorerActivity; |
| 34 | +import com.duy.ide.editor.editor.R; |
33 | 35 |
|
34 | 36 | import java.io.File; |
35 | 37 |
|
36 | 38 | /** |
37 | 39 | * Created by Duy on 30-Apr-18. |
38 | 40 | */ |
39 | 41 |
|
40 | | -public class DialogNewFile extends AbstractDialog { |
41 | | - private final Context context; |
| 42 | +public class DialogNewFile extends AppCompatDialogFragment { |
| 43 | + private static final String KEY_FILE_EXTENSIONS = "fileExtensions"; |
| 44 | + private static final String KEY_CURRENT_DIR = "currentDir"; |
| 45 | + private static final int RC_SELECT_PATH = 991; |
| 46 | + |
42 | 47 | @Nullable |
43 | | - private final OnCreateFileListener mListener; |
44 | | - private final String[] fileExtensions; |
45 | | - private String dir; |
| 48 | + private OnCreateFileListener mListener; |
| 49 | + private String[] fileExtensions; |
| 50 | + private String mCurrentDir; |
46 | 51 | private EditText mPathExitText, mNameEditText; |
47 | 52 | private Spinner mSpinnerExt; |
48 | 53 |
|
49 | | - // TODO: 19-May-18 create new file and callback |
50 | | - public DialogNewFile(@NonNull Context context, @NonNull String[] fileExtensions, |
51 | | - @NonNull String dir, |
52 | | - @Nullable final OnCreateFileListener listener) { |
53 | | - super(context); |
54 | | - this.context = context; |
55 | | - this.fileExtensions = fileExtensions; |
56 | | - this.dir = dir; |
57 | | - this.mListener = listener; |
| 54 | + public static DialogNewFile newInstance(@NonNull String[] fileExtensions, |
| 55 | + @NonNull String dir, OnCreateFileListener onCreateFileListener) { |
| 56 | + |
| 57 | + Bundle args = new Bundle(); |
| 58 | + |
| 59 | + DialogNewFile fragment = new DialogNewFile(); |
| 60 | + fragment.setArguments(args); |
| 61 | + args.putStringArray(KEY_FILE_EXTENSIONS, fileExtensions); |
| 62 | + args.putString(KEY_CURRENT_DIR, dir); |
| 63 | + fragment.mListener = onCreateFileListener; |
| 64 | + return fragment; |
58 | 65 | } |
59 | 66 |
|
| 67 | + @Nullable |
60 | 68 | @Override |
61 | | - public void show() { |
62 | | - AlertDialog.Builder builder = getBuilder(); |
63 | | - LayoutInflater inflater = LayoutInflater.from(context); |
64 | | - View view = inflater.inflate(R.layout.dialog_new_file, null); |
65 | | - builder.setView(view); |
| 69 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 70 | + View view = inflater.inflate(R.layout.dialog_new_file, container, false); |
| 71 | + return view; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 76 | + super.onViewCreated(view, savedInstanceState); |
| 77 | + if (mCurrentDir == null) { |
| 78 | + mCurrentDir = getArguments().getString(KEY_CURRENT_DIR); |
| 79 | + } |
| 80 | + if (fileExtensions == null) { |
| 81 | + fileExtensions = getArguments().getStringArray(KEY_FILE_EXTENSIONS); |
| 82 | + } |
| 83 | + |
66 | 84 | mPathExitText = view.findViewById(R.id.edit_path); |
67 | | - mPathExitText.setText(dir); |
| 85 | + mPathExitText.setText(mCurrentDir); |
68 | 86 | mNameEditText = view.findViewById(R.id.edit_input); |
69 | 87 | mSpinnerExt = view.findViewById(R.id.spinner_exts); |
70 | | - ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, fileExtensions); |
| 88 | + ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, fileExtensions); |
71 | 89 | adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice); |
72 | 90 | mSpinnerExt.setAdapter(adapter); |
73 | 91 |
|
74 | | - builder.setTitle(R.string.create_new_file); |
75 | | - builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 92 | +// setTitle(R.string.create_new_file); |
| 93 | + view.findViewById(R.id.btn_create).setOnClickListener(new View.OnClickListener() { |
| 94 | + @Override |
| 95 | + public void onClick(View v) { |
| 96 | + if (createNewFile()) { |
| 97 | + dismiss(); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() { |
76 | 102 | @Override |
77 | | - public void onClick(DialogInterface dialog, int which) { |
78 | | - createNewFile(); |
79 | | - dialog.dismiss(); |
| 103 | + public void onClick(View v) { |
| 104 | + dismiss(); |
80 | 105 | } |
81 | 106 | }); |
82 | | - builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { |
| 107 | + view.findViewById(R.id.btn_select_path).setOnClickListener(new View.OnClickListener() { |
83 | 108 | @Override |
84 | | - public void onClick(DialogInterface dialog, int which) { |
85 | | - dialog.cancel(); |
| 109 | + public void onClick(View v) { |
| 110 | + selectPath(); |
86 | 111 | } |
87 | 112 | }); |
88 | | - builder.create().show(); |
89 | 113 | } |
90 | 114 |
|
91 | | - private void createNewFile() { |
| 115 | + private void selectPath() { |
| 116 | + FileExplorerActivity.startPickPathActivity(this, mCurrentDir, "UTF-8", RC_SELECT_PATH); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 121 | + super.onActivityResult(requestCode, resultCode, data); |
| 122 | + if (requestCode == RC_SELECT_PATH && resultCode == Activity.RESULT_OK) { |
| 123 | + String file = FileExplorerActivity.getFile(data); |
| 124 | + if (file != null) { |
| 125 | + mCurrentDir = file; |
| 126 | + mPathExitText.setText(file); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private boolean createNewFile() { |
92 | 132 | String name = mNameEditText.getText().toString(); |
93 | 133 | if (mNameEditText.length() == 0 || |
94 | 134 | !name.matches("[A-Za-z0-9_./ ]+")) { |
95 | | - mNameEditText.setText(R.string.invalid_name); |
96 | | - return; |
| 135 | + mNameEditText.setError(getContext().getString(R.string.invalid_name)); |
| 136 | + return false; |
97 | 137 | } |
98 | 138 | String path = mPathExitText.getText().toString(); |
99 | 139 | if (mPathExitText.length() == 0 || |
100 | 140 | !path.matches("[A-Za-z0-9_./ ]+")) { |
101 | 141 |
|
102 | | - mNameEditText.setText(R.string.invalid_name); |
103 | | - return; |
| 142 | + mNameEditText.setError(getContext().getString(R.string.invalid_name)); |
| 143 | + return false; |
104 | 144 | } |
105 | | - if (!name.contains(".")){ |
| 145 | + if (!name.contains(".")) { |
106 | 146 | name += mSpinnerExt.getSelectedItem().toString(); |
107 | 147 | } |
108 | 148 | File file = new File(path, name); |
109 | 149 | if (IOUtils.createNewFile(file)) { |
110 | 150 | if (mListener != null) { |
111 | 151 | mListener.onFileCreated(file); |
112 | 152 | } |
| 153 | + return true; |
113 | 154 | } else { |
114 | | - mNameEditText.setText(R.string.cannot_create_new_file); |
| 155 | + mNameEditText.setError(getContext().getString(R.string.cannot_create_new_file)); |
115 | 156 | } |
| 157 | + return false; |
116 | 158 | } |
117 | 159 |
|
118 | 160 | public interface OnCreateFileListener { |
|
0 commit comments