|
3 | 3 | import android.content.Context; |
4 | 4 | import android.view.View; |
5 | 5 | import android.widget.TextView; |
6 | | -import com.google.android.material.snackbar.Snackbar; |
7 | 6 | import com.google.android.material.textfield.TextInputEditText; |
8 | | -import org.xedox.utils.BaseActivity; |
9 | 7 | import org.xedox.utils.dialog.DialogBuilder; |
10 | | -import org.xedox.utils.dialog.ErrorDialog; |
11 | 8 | import org.xedox.webaide.R; |
12 | 9 | import org.xedox.webaide.project.Project; |
13 | 10 | import org.xedox.webaide.project.ProjectsAdapter; |
14 | 11 |
|
15 | 12 | public class RenameProjectDialog { |
| 13 | + |
16 | 14 | public static void show(Context context, ProjectsAdapter projectsAdapter, Project project) { |
| 15 | + |
17 | 16 | DialogBuilder builder = new DialogBuilder(context); |
18 | 17 | builder.setView(R.layout.dialog_input_layout); |
19 | 18 | builder.setTitle(R.string.rename_project); |
20 | 19 | builder.setCancelable(false); |
21 | | - builder.setNegativeButton(R.string.cancel, (d, w) -> d.dismiss()); |
22 | 20 |
|
23 | 21 | TextInputEditText inputView = builder.findViewById(R.id.input); |
24 | 22 | TextView errorMessage = builder.findViewById(R.id.errorMessage); |
| 23 | + |
25 | 24 | inputView.setHint(R.string.project_name); |
26 | 25 |
|
27 | | - builder.setPositiveButton( |
28 | | - R.string.rename, |
29 | | - (dialog, w) -> { |
30 | | - String input = inputView.getText().toString().trim(); |
31 | | - if (input.isBlank()) { |
32 | | - errorMessage.setVisibility(View.VISIBLE); |
33 | | - errorMessage.setText(R.string.project_name_cannot_be_empty); |
34 | | - return; |
35 | | - } else if (Project.existsProject(input)) { |
36 | | - errorMessage.setVisibility(View.VISIBLE); |
37 | | - errorMessage.setText(R.string.project_already_exists); |
38 | | - return; |
39 | | - } |
40 | | - try { |
41 | | - Project.renameProject(project, input); |
42 | | - projectsAdapter.update(project); |
43 | | - dialog.dismiss(); |
44 | | - } catch (Exception err) { |
45 | | - err.printStackTrace(); |
46 | | - errorMessage.setVisibility(View.VISIBLE); |
47 | | - errorMessage.setText(err.getMessage()); |
48 | | - } |
49 | | - }); |
50 | | - builder.show(); |
| 26 | + // Isi nama project lama |
| 27 | + inputView.setText(project.getName()); |
| 28 | + inputView.setSelection(project.getName().length()); |
| 29 | + |
| 30 | + builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); |
| 31 | + |
| 32 | + builder.setPositiveButton(R.string.rename, (dialog, which) -> { |
| 33 | + |
| 34 | + String input = inputView.getText().toString().trim(); |
| 35 | + |
| 36 | + errorMessage.setVisibility(View.GONE); |
| 37 | + |
| 38 | + if (input.isEmpty()) { |
| 39 | + errorMessage.setVisibility(View.VISIBLE); |
| 40 | + errorMessage.setText(R.string.project_name_cannot_be_empty); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Jika nama tidak berubah, langsung tutup dialog |
| 45 | + if (input.equals(project.getName())) { |
| 46 | + dialog.dismiss(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (Project.existsProject(input)) { |
| 51 | + errorMessage.setVisibility(View.VISIBLE); |
| 52 | + errorMessage.setText(R.string.project_already_exists); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + Project.renameProject(project, input); |
| 58 | +projectsAdapter.reload(); |
| 59 | +dialog.dismiss(); |
| 60 | + } catch (Exception e) { |
| 61 | + errorMessage.setVisibility(View.VISIBLE); |
| 62 | + errorMessage.setText(e.getMessage()); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + builder.show(); |
51 | 67 | } |
52 | 68 | } |
0 commit comments