0

I am trying to ask for permissions in an activity (using easyPermissions) when a user clicks on a text input field. My current code looks like this:

class PageFragment : Fragment(), EasyPermissions.PermissionCallbacks {

    private lateinit var viewModel: PageViewModel
    private lateinit var binding: PageBinding
    private lateinit var interpreter: PageInterpreter
    private val messagesCoordinator: MessageCoordinator by lazy {
        MessageCoordinator(viewModel, interpreter)
    }

    companion object {
        private const val RC_READ_PERMISSIONS = 102
        @JvmStatic
        fun newInstance() = PageFragment().apply {
            arguments = Bundle().apply {
            }
        }
    }

    interface PageCallback {
        fun goBack()
    }

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

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    @AfterPermissionGranted(RC_READ_PERMISSIONS)
    private fun getPermissions() {
        Log.i("Permission asked","Get Permission function activated")
        context?.let {
            if (EasyPermissions.hasPermissions(it, android.Manifest.permission.READ_CONTACTS)) {
                //TODO
            } else {
                EasyPermissions.requestPermissions(this, it.getString(R.string.fragment_page_explanation), RC_READ_PERMISSIONS,
                    android.Manifest.permission.READ_CONTACTS)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.i("View Created","View has been created")
        editName.setOnClickListener {
            Log.i("Name","on click listener activated")
            this.getPermissions()
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        //TODO
    }
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        //TODO
    }
}

Basically, I have 6 input fields on the fragment layout and I want the permission to be asked if any of the text input fields are click on by the user. Here I am only trying with one.

Thanks,

Feras A.

FerasAS
  • 273
  • 2
  • 14

1 Answers1

1

As far as I understood, if you sure that all EditTexts will behave identically, you can create single instance of clickListener, and set it to EditTexts.

    val clickListener = View.OnClickListener {
        Log.i("Name","on click listener activated")
        this@PageFragment.getPermissions()
    }

    editName.setOnClickListener(clickListener)
    editSomethingElse.setOnClickListener(clickListener)
    editSurname.setOnClickListener(clickListener)

Or even create list of similar EditTexts and set listener with following:

    listOf(editName,  editSomethingElse, editSurname).map {
        it.setOnClickListener(clickListener)
    }

EDIT May be this is the case? One more related topic.

Ilya E
  • 700
  • 7
  • 13