如何修复Android中的重复代码片段

huangapple go评论66阅读模式
英文:

How to fix duplicate code fragment in android

问题

我有这个函数来更新我的Android项目中的ImageView。

我必须在我的项目中的两个地方使用这个函数。第一个地方是在我的片段中,用于更新ImageView,第二个地方是在RecyclerView适配器中,用于更新RecyclerView中的ImageView。然而,当我实现它时,IDE显示了一个“重复的代码片段”警告。我想知道如何修复这个警告,因为我在两个单独的类和两个单独的文件中使用了这个函数。

请注意,我已经删除了代码部分,只保留了您要翻译的内容。

英文:

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.

如何修复Android中的重复代码片段

答案1

得分: 1

第一种方法是将此内容放入一个类中。由于它涉及视图引用,这是一种非常不好的方法(除非您将setView方法放在外部)。第二种方法,也是我会选择的方法,是将其作为扩展函数:

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
    }
}

然后,要设置视图,您可以保持您已经使用的相同方法:

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

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)
)

答案2

得分: 0

我不太熟悉Android,但你可以实现一个公共类,其中包括这个(公共)函数,然后在需要时使用这个类吗?也许可以使用一个“静态”函数。如果需要的话,你可以将变量作为参数传递。

英文:

I have not much experience with android but can you implement one public class with this (public) function and then to use this class when needing?
Maybe to use a "static" function.
You can pass variables as parameters if you need it.

huangapple
  • 本文由 发表于 2023年6月27日 17:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定