Skip to content
Advertisement

AlertDialog In MVVM

I am building an app in MVVM pattern and I am using 3 different styles of dialogs(some of them have 2 buttons,some of them 3). Since the AlertDialog refers to the view, I wrote the code in the fragment. I have 3 dialogs in the same fragment and its look kind of messy.

My questions:

  • Is this a good practice to use the dialogs code in the fragment itself, like I did in the first place?
  • Is there anyway to make it clearer ?
  • Is there any way I make the dialog code shorter? So I don’t have to write the same code over and over? (Reminder: I am using 3 different styles of dialogs)

This is the code for one of the dialogs:

        AlertDialog optionsDialog = new AlertDialog.Builder(requireContext()).create();
        View dialogView = getLayoutInflater().inflate(R.layout.tasks_long_click_options_design, null);
        optionsDialog.setView(dialogView);

        optionsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // Transparent Corners

        Button editButton = dialogView.findViewById(R.id.editTaskButton);
        Button deleteButton = dialogView.findViewById(R.id.deleteTaskButton);
        Button cancelButton = dialogView.findViewById(R.id.cancelTaskButton);

        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent editTask = new Intent(requireActivity(), AddTaskActivity.class);

                editTask.putExtra("taskName", task.getTaskName());
                editTask.putExtra("taskPriority", task.getTaskPriority());
                editTask.putExtra("taskTag", task.getTaskTag());
                editTask.putExtra("taskDate", task.getTaskDate());
                editTask.putExtra("taskTime", task.getTaskTime());
                editTask.putExtra("taskID", task.getTaskID());

                Log.e("DailyFragment", "Send Task By Intent: Name- " + task.getTaskName() + " Color-" + task.getTaskPriority() + " Tag- " + task.getTaskTag() + " Date- "
                        + task.getTaskDate() + " Time- " + task.getTaskTime());

                optionsDialog.dismiss();

The second dialog is with 2 buttons,and the third dialog is with 4.

Thank you !

Advertisement

Answer

  1. Is this a good practice to use the dialogs code in the fragment itself?
    Yes, the Fragment is a UI layer. It is correct to use an AlertDialog there.

  2. Is there anyway to make it clearer?
    That completely depends on your implementation, however there is nothing wrong with the current implementation as it is readable & quite understandable.

  3. Is there any way I make the dialog code shorter? So I don’t have to write the same code over and over? (Reminder: I am using 3 different styles of dialogs)
    Yes, if you are using same strategy for dialog creation where only certain styles are different then you can create simple method with relevant parameters like:

fun showDialogWith(style: String) {
    val styleTypeView = when(style) {
        "normal" -> R.layot.normal_dialog
        "info" -> R.layot.info_dialog
        "error" -> R.layot.error_dialog
    }

    layoutInflater.inflate(styleTypeView, null);
    
    /* Handle other theming / operations depending on the view type */
}

And call it like:
Normal Dialog – showDialogWith("normal")
Info. Dialog – showDialogWith("info")
Error Dialog – showDialogWith("error")

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement