Skip to content

Commit 0a53764

Browse files
authored
Refactor RenameProjectDialog to improve error handling
Minor update and fix bug rename project
1 parent 4751bd4 commit 0a53764

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

main/src/main/java/org/xedox/webaide/dialog/RenameProjectDialog.java

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,66 @@
33
import android.content.Context;
44
import android.view.View;
55
import android.widget.TextView;
6-
import com.google.android.material.snackbar.Snackbar;
76
import com.google.android.material.textfield.TextInputEditText;
8-
import org.xedox.utils.BaseActivity;
97
import org.xedox.utils.dialog.DialogBuilder;
10-
import org.xedox.utils.dialog.ErrorDialog;
118
import org.xedox.webaide.R;
129
import org.xedox.webaide.project.Project;
1310
import org.xedox.webaide.project.ProjectsAdapter;
1411

1512
public class RenameProjectDialog {
13+
1614
public static void show(Context context, ProjectsAdapter projectsAdapter, Project project) {
15+
1716
DialogBuilder builder = new DialogBuilder(context);
1817
builder.setView(R.layout.dialog_input_layout);
1918
builder.setTitle(R.string.rename_project);
2019
builder.setCancelable(false);
21-
builder.setNegativeButton(R.string.cancel, (d, w) -> d.dismiss());
2220

2321
TextInputEditText inputView = builder.findViewById(R.id.input);
2422
TextView errorMessage = builder.findViewById(R.id.errorMessage);
23+
2524
inputView.setHint(R.string.project_name);
2625

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();
5167
}
5268
}

0 commit comments

Comments
 (0)