Como voce otimizaria esse código e por que?


package feature.settings.menus

import android.content.DialogInterface
import feature.settings.SettingsActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import core.base.BaseFragment
import core.extensions.fadeInAnimationNoRepeat
import core.extensions.fadeOutAnimationNoRepeat
import core.extensions.gone
import core.extensions.runWhenSafe
import feature.settings.R
import feature.settings.databinding.FragmentMenusBinding
import feature.settings.menus.adapter.MenuAdapter
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf
import ui.components.dialogs.BaseDialog2
import ui.components.toast.ToastManager
import ui.components.toast.ToastType


class MenusFragment() : BaseFragment(), MenusContract.View {
    private val presenter: MenusContract.Presenter by inject { parametersOf(this) }
    override val layoutResource: Int = R.layout.fragment_menus

    private lateinit var binding: FragmentMenusBinding
    private lateinit var dialogYesOrNotBaseClass: BaseDialog2
    private lateinit var toastManager: ToastManager

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMenusBinding.inflate(inflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //this.setUpExitButton()
        toastManager = ToastManager(requireActivity())
        presenter.setUpMenus(this.requireContext())
    }


    override fun adicionarRecyclerView(list: List<String>) {
        runWhenSafe {
            binding.recyclerViewMenu.layoutManager = LinearLayoutManager(activity)

            val activity = requireActivity() as SettingsActivity

            val menuAdapter = MenuAdapter(list,this@MenusFragment, activity)
            binding.recyclerViewMenu.adapter = menuAdapter
        }
    }


    override fun onMenuItemClicked(menuItem: String, menuItemId:Int) {
        val activity = requireActivity() as SettingsActivity
        activity.changeFragment(menuItem, menuItemId)
    }


    override fun showLogoutModalLoading(show: Boolean) {
        runWhenSafe {
            if(::dialogYesOrNotBaseClass.isInitialized) dialogYesOrNotBaseClass.showLoading(show)
        }
    }


    override fun showToast(type: Int, title: String?, description: String?, time: Long, onFinish: () -> Unit) {
        runWhenSafe {
            toastManager.showToast(
                type = type,
                title = title,
                description = description,
                time = time,
                onFinish = onFinish
            )
        }
    }

    override fun showLoading(status: Boolean) {
        runWhenSafe {
            if (status) {
                binding.recyclerViewMenu.fadeOutAnimationNoRepeat(300) {
                    runWhenSafe {
                        binding.recyclerViewMenu.gone()
                        binding.loading.fadeInAnimationNoRepeat(300) { }
                    }
                }
            } else {
                binding.loading.fadeOutAnimationNoRepeat(300) {
                    runWhenSafe {
                        binding.loading.gone()
                        binding.recyclerViewMenu.fadeInAnimationNoRepeat(300) { }
                    }
                }
            }
        }
    }

    override fun showDialog(
        title: String?,
        message: String?,
        positiveActionText: String?,
        positiveAction: (dialogInterface: DialogInterface, i: Int) -> Unit,
        negativeActionText: String?,
        negativeAction: (dialogInterface: DialogInterface, i: Int) -> Unit
    ) {
        val toastManager = ToastManager(requireActivity())
        toastManager.showToast(ToastType.TYPE_ERROR, "Error", message)
    }



    companion object {
        fun newInstance(): MenusFragment {
            return MenusFragment()
        }
    }
}