I am trying to convert a Java file to Kotlin just when I encounter this issue coming from the constructor parameter var view: View
. I have seen similar issues but have not encountered one that uses a dialog as I have used here. It's important that I pass a view as a contractor parameter because I use that view for an important feature that depends on the dialog behavior.
Before converting to Kotlin
ackage com.indupendo.landing.ui.dialogs;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.indupendo.R;
import com.indupendo.databinding.DialogLoginBinding;
import com.indupendo.globals.utilities.Utils;
public class LoginDialog extends DialogFragment {
DialogLoginBinding binding;
View view;
public LoginDialog(View view) {
this.view = view;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
binding = DialogLoginBinding.inflate(getLayoutInflater());
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(requireActivity(),
R.style.MaterialAlertDialog_rounded);
dialogBuilder.setView(binding.getRoot());
dialogBuilder.setNegativeButton("Cancel", (dialog, which) -> {
Utils.INSTANCE.showLoginCancellationSnackBar(view, getLayoutInflater());
dialog.cancel();
});
dialogBuilder.setPositiveButton("Login", (dialog, which) -> Toast.makeText(
getActivity(),
"Logged In",
Toast.LENGTH_LONG).show());
return dialogBuilder.create();
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
Utils.INSTANCE.showLoginCancellationSnackBar(view, getLayoutInflater());
dialog.cancel();
}
}
After conversion
package com.indupendo.landing.ui.fragments
import android.app.Dialog
import com.indupendo.globals.utilities.Utils.showLoginCancellationSnackBar
import android.os.Bundle
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.indupendo.R
import android.content.DialogInterface
import android.view.View
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.indupendo.databinding.DialogLoginBinding
class LoginDialog(var view: View) : DialogFragment() {
var binding: DialogLoginBinding? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DialogLoginBinding.inflate(layoutInflater)
val dialogBuilder = MaterialAlertDialogBuilder(
requireActivity(),
R.style.MaterialAlertDialog_rounded
)
dialogBuilder.setView(binding!!.getRoot())
dialogBuilder.setNegativeButton("Cancel") { dialog: DialogInterface, which: Int ->
showLoginCancellationSnackBar(
view, layoutInflater
)
dialog.cancel()
}
dialogBuilder.setPositiveButton("Login") { dialog: DialogInterface?, which: Int ->
Toast.makeText(
activity,
"Logged In",
Toast.LENGTH_LONG
).show()
}
return dialogBuilder.create()
}
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
showLoginCancellationSnackBar(view, layoutInflater)
dialog.cancel()
}
}
How can I rewrite the Kotlin class and achieve the same result but without this bug?