Skip to content

改善所有 SituationText 的取得方式 #43

@MrNegativeTW

Description

@MrNegativeTW

爛 Code 如下

EventCardBindingAdapter

@JvmStatic
    @BindingAdapter("situationType")
    fun setEventCardSituationText(view: TextView, situationType: Long) {
        val resId = when (situationType.toInt()) {
            1 -> R.string.accidentEvent_situationType_1
            2 -> R.string.accidentEvent_situationType_2
            3 -> R.string.accidentEvent_situationType_3
            4 -> R.string.accidentEvent_situationType_4
            5 -> R.string.accidentEvent_situationType_5
            6 -> R.string.accidentEvent_situationType_6
            7 -> R.string.accidentEvent_situationType_7
            8 -> R.string.accidentEvent_situationType_8
            else -> R.string.accidentEvent_situationType_6
        }
        view.text = view.context.getString(resId)
    }

Util.kt

fun getSituationTypeName(context: Context, situationType: Int): String {
        return when (situationType) {
            0 -> context.getString(R.string.accidentEvent_situationType_0)
            1 -> context.getString(R.string.accidentEvent_situationType_1)
            2 -> context.getString(R.string.accidentEvent_situationType_2)
            3 -> context.getString(R.string.accidentEvent_situationType_3)
            4 -> context.getString(R.string.accidentEvent_situationType_4)
            5 -> context.getString(R.string.accidentEvent_situationType_5)
            6 -> context.getString(R.string.accidentEvent_situationType_6)
            7 -> context.getString(R.string.accidentEvent_situationType_7)
            8 -> context.getString(R.string.accidentEvent_situationType_8)
            else -> context.getString(R.string.accidentEvent_situationType_6)
        }
    }

string.xml

<string name="accidentEvent_situationType_0">按我選擇種類</string>
<string name="accidentEvent_situationType_1">事故</string>
<string name="accidentEvent_situationType_2">注意</string>
<string name="accidentEvent_situationType_3">臨檢</string>
<string name="accidentEvent_situationType_4">測速</string>
<string name="accidentEvent_situationType_5">天氣</string>
<string name="accidentEvent_situationType_6">其他</string>
<string name="accidentEvent_situationType_7">坍方</string>
<string name="accidentEvent_situationType_8">落石</string>

array.xml

<array name="eventEditor_eventTypeArray">
    <item>事故</item>
    <item>注意</item>
    <item>臨檢</item>
    <item>測速</item>
    <item>天氣</item>
    <item>其他</item>
    <item>坍方</item>
    <item>落石</item>
</array>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions