naozumi-nao / IzinBoss-v2

Re-make of IzinBoss with MVVM design principles
1 stars 0 forks source link

AlertDialog does not work well with DialogFragment #3

Open naozumi-nao opened 1 year ago

naozumi-nao commented 1 year ago
    private suspend fun addUserToCompany() {
        val userId = binding?.edUserIdInput?.text.toString()
        val userRole = when (binding?.actvChooseUserRole?.text.toString()) {
            "MANAGER" -> User.UserRole.MANAGER
            "EMPLOYEE" -> User.UserRole.EMPLOYEE
            else -> User.UserRole.EMPLOYEE
        }
        user = viewModel.getUserData(userId)
        viewModel.addUserToCompany(companyId, user, userRole)
            .observe(this) { result ->
                if (result != null) {
                    when (result) {
                        is Result.Loading -> {
                            binding?.progressBar?.visibility = View.VISIBLE
                        }
                        is Result.Success -> {
                            binding?.progressBar?.visibility = View.GONE
                            AlertDialog.Builder(requireActivity()).apply {
                                setTitle(getString(R.string.success))
                                setMessage("Successfully Added User to Company!")
                                setPositiveButton(getString(R.string.continue_on)) { _, _ ->
                                    dismiss()
                                    ViewUtils.replaceFragment(
                                        requireActivity() as AppCompatActivity,
                                        R.id.nav_main_content_container,
                                        HomeFragment(),
                                        HomeFragment::class.java.simpleName
                                    )
                                }
                                create()
                                show()
                            }.apply {
                                setOnCancelListener { // Set an OnCancelListener to handle the case when the user clicks outside of the dialog
                                    dismiss()
                                    ViewUtils.replaceFragment(
                                        requireActivity() as AppCompatActivity,
                                        R.id.nav_main_content_container,
                                        HomeFragment(),
                                        HomeFragment::class.java.simpleName
                                    )
                                }
                                show()
                            }
                        }
                        is Result.Error -> {
                            binding?.progressBar?.visibility = View.GONE
                            AlertDialog.Builder(requireActivity()).apply {
                                setTitle(getString(R.string.error))
                                setMessage(result.error)
                                setPositiveButton(getString(R.string.continue_on)) { _, _ -> }
                                create()
                                show()
                            }
                        }
                    }
                }
            }
    }

dismiss() will cause the DialogFragment in question get unattached from the parent fragment, which in turn makes AlertDialog to lose its context. After that it's going to either:

naozumi-nao commented 1 year ago

Currently, one of the ways to avoid this issue altogether is to use Toast instead of AlertDialog

    private suspend fun addUserToCompany() {
        val userId = binding?.edUserIdInput?.text.toString()
        val userRole = when (binding?.actvChooseUserRole?.text.toString()) {
            "MANAGER" -> User.UserRole.MANAGER
            "EMPLOYEE" -> User.UserRole.EMPLOYEE
            else -> User.UserRole.EMPLOYEE
        }
        user = viewModel.getUserData(userId)
        viewModel.addUserToCompany(companyId, user, userRole)
            .observe(this) { result ->
                if (result != null) {
                    when (result) {
                        is Result.Loading -> {
                            binding?.progressBar?.visibility = View.VISIBLE
                        }
                        is Result.Success -> {
                            binding?.progressBar?.visibility = View.GONE
                            Toast.makeText(
                                requireActivity(),
                                "Successfully added user to company",
                                Toast.LENGTH_LONG
                            ).show()
                            dismiss()
                            ViewUtils.replaceFragment(
                                requireActivity() as AppCompatActivity,
                                R.id.nav_main_content_container,
                                CompanyProfileFragment(),
                                CompanyProfileFragment::class.java.simpleName
                            )
                        }
                        is Result.Error -> {
                            binding?.progressBar?.visibility = View.GONE
                            Toast.makeText(
                                requireActivity(),
                                "Error: " + result.error,
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                }
            }
    }