0

I have this piece of code that repeats in 3 places

      if (orientation == AppConstants.LANDSCAPE) {
        width = 
        height = 
      }else{
        width = 
        height = 
      }
      imageUrl.let {
        val options = RequestOptions()
            .centerCrop()
            .override(width, height)
        Glide.with(context)
            .load(Uri.parse(imageUrl))
            .apply(options)
            .into(cardView.mainImageView)
      }

I don't know which is the best solution to have in one place the code of calculating width and height based on orientation ? I don't think I can replace it with extension function

Artur A
  • 257
  • 3
  • 20

3 Answers3

0

I am not sure about the calculation part but if you want to create a utility class, you can create MyUtils.kt as follows:

object MyUtils {

    fun myFunction(imageUrl: String) {
        // Your code above...
    }

    ...
}

You can call it later like this:

MyUtils.myFunction(imageUrl);
Mehmed
  • 2,880
  • 4
  • 41
  • 62
0

You can create a funtion like this:

fun getWidthHeight() = if (orientation == AppConstants.LANDSCAPE) {
    Pair(45, 86)
} else {
    Pair(78, 35)
}

And you can call this function like this:

val (width, height) = getWidthHeight()
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
-1

Create simple class and call method by getting instance of class.no need to make it static to avoid unnecessary increasing app memory burden.

Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14