androidkotlincode-duplication

How to fix duplicate code fragment in android


I have this function to update my ImageView in my android project

    private fun weatherImageSetup(currentWeather: CurrentWeather) {
        fun setImage(imageId: Int) {
            viewBinding.weatherStatusImg.setImageDrawable(
                ContextCompat.getDrawable(requireContext(), imageId)
            )
        }
        fun partOfDay(day: Int, night: Int) = if (viewModel.currentDateTime.toInt() in 6..18) {
            day
        } else {
            night
        }
        when (currentWeather.weather[0].id.toString()[0]) {
            '2' -> {
                setImage(R.drawable.thunderstorm_img)
            }
            '3' -> {
                val imageId: Int = partOfDay(R.drawable.drizzle_day_img, R.drawable.drizzle_night_img)
                setImage(imageId)
            }
            '5' -> {
                val imageId: Int = partOfDay(R.drawable.rainy_day_img, R.drawable.rainy_night_img)
                setImage(imageId)
            }
            '6' -> {
                val imageId: Int = partOfDay(R.drawable.snow_day_img, R.drawable.snow_night_img)
                setImage(imageId)
            }
            '7' -> {
                val imageId: Int = if (currentWeather.weather[0].id == 781) {
                    R.drawable.tornado_img
                } else {
                    R.drawable.fog_img
                }
                setImage(imageId)
            }
            '8' -> {
                val imageId: Int = if (currentWeather.weather[0].id == 800) {
                    partOfDay(R.drawable.clear_day_sky_img, R.drawable.clear_night_sky_img)
                } else {
                    partOfDay(R.drawable.cloud_day_img, R.drawable.cloud_night_img)
                }
                setImage(imageId)
            }
        }
    }

I have to use this function in two places in my project. The first one is in my fragment to update the ImageView, and the second one is in the RecyclerView adapter to update the ImageView in the RecyclerView. However, when I implemented it, the IDE showed me a warning of "duplicate code fragment". I wonder how to fix this warning because I use this function in two separate classes and in two separate files.

Warning image


Solution

  • The first way would to put this into a Class. Since it has view references though, this is a very bad approach (unless you keep the setView outside of it). The second, and what I'd go for, is to make this an extension function:

    fun CurrentWeather.getImage(): Int {
    val currentWeatherId: Int = this.weather[0].id
    val currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
    fun isDay() = currentTime in 6..18
    return when (currentWeatherId.toString()[0]) {
        '2' -> R.drawable.thunderstorm_img
        '3' -> if (isDay()) R.drawable.drizzle_day_img else R.drawable.drizzle_night_img
        '5' -> if (isDay()) R.drawable.rainy_day_img else R.drawable.rainy_night_img
        '6' -> if (isDay()) R.drawable.snow_day_img else R.drawable.snow_night_img
        '7' -> if (currentWeatherId == 781) R.drawable.tornado_img else R.drawable.fog_img
        '8' -> if (currentWeatherId == 800) {
            if (isDay()) R.drawable.clear_day_sky_img else R.drawable.clear_night_sky_img)
        } else {
            if (isDay()) R.drawable.cloud_day_img else R.drawable.cloud_night_img)
        }
        else -> R.drawable.unknown
     }
    }
    

    Then to set the view you keep the same method you have:

    viewBinding.weatherStatusImg.setImageDrawable(
        ContextCompat.getDrawable(requireContext(), currentWeather.getImage)
    )