Search code examples
androidkotlinandroid-architecture-componentsandroid-jetpackandroid-viewmodel

should I write this code in my fragment/activity or in my viewmodel?


I am new in Android MVVM, so I need to perform image cropping and asking permission to access image gallery, and I am confused whether these should be performed in my fragment or in my viewmodel.

to start cropping activity for pre-acquired image saved on the device I use this code

    CropImage.activity(selectedImagePosterFromGalleryUri)
        .setGuidelines(CropImageView.Guidelines.ON)
        .setRequestedSize(posterMaxWidthSize, posterMaxHeightSize, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
        .start(mContext, this)

the result (cropped image) from this code can be accessed in onActivityResult in my fragment when requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE

I am confused if it is considered as user input so I can put this code in my fragment or it is considered as logic / process so that should be placed in viewmodel ?

and also I need to ask permission to access gallery, to ask permisson I use a library and the code will be like this. do I have to put in fragment or viewmodel ?

    @AfterPermissionGranted(PERMISSION_READ_EXTERNAL_STORAGE)
    private fun checkPermission() {


        if (EasyPermissions.hasPermissions(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)) {


            chooseImageFromGallery()

        } else {

            EasyPermissions.requestPermissions(
                this,
                "we need your permission to access your gallery",
                PERMISSION_READ_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
        }



    }

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

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

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {

        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {

            // showing app setting, so the user eventually can switch the permission manually in the setting section
            AppSettingsDialog.Builder(this).build().show()
        }

    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {


    }

the one that make me confuse is this sentence from here https://developer.android.com/jetpack/docs/guide ,it is said :

It's a common mistake to write all your code in an Activity or a Fragment. These UI-based classes should only contain logic that handles UI and operating system interactions.

I am confused if cropping image and accessing gallery is considered as logic for handles UI and operating system interactions or not

I do not really understand to choose a code should be in viewmodel or in view (fragment/activity). for business logic, it is clear that is should be in viewmodel, but how about codes like these ? please help


Solution

  • According to the google guy,

    ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.

    If your cropping operation fulfills this condition then definately throw it in ViewModel.

    For more details https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54