Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace use of preference array resources and constants with enums #1041

Open
bellerbrock opened this issue Sep 5, 2024 · 0 comments
Open

Comments

@bellerbrock
Copy link
Collaborator

Discussed here: #986 (comment)

The idea being to replace use of preference array resources and constants


    <string-array name="pref_general_location_acquisition_level_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

    <string-array name="pref_general_location_acquisition_level_entries">
        <item>@string/location_level_off</item>
        <item>@string/location_level_study</item>
        <item>@string/location_level_observation_unit</item>
        <item>@string/location_level_observation</item>
    </string-array>

        <ListPreference
            android:icon="@drawable/ic_selection_marker"
            android:key="com.fieldbook.tracker.GENERAL_LOCATION_COLLECTION"
            android:summary="%s"
            android:title="@string/pref_general_location_collection_title"
            android:entries="@array/pref_general_location_acquisition_level_entries"
            android:entryValues="@array/pref_general_location_acquisition_level_values"
            android:defaultValue="1"/>

companion object {
        const val LOCATION_COLLECTION_OFF = 0
        const val LOCATION_COLLECTION_STUDY = 1
        const val LOCATION_COLLECTION_OBS_UNIT = 2
        const val LOCATION_COLLECTION_OBS = 3
    }



with enum classes

package com.fieldbook.tracker.enums

import com.fieldbook.tracker.R

enum class LocationCollectionMode(val value: Int, val labelResId: Int) {
    OFF(0, R.string.location_level_off),
    STUDY(1, R.string.location_level_study),
    OBS_UNIT(2, R.string.location_level_observation_unit),
    OBS(3, R.string.location_level_observation);

    companion object {
        fun fromValue(value: Int): LocationCollectionMode? = values().find { it.value == value }
    }
}

Using the enums to populate preferences programatically something like this (in addition to checking preference values in other parts of the app)

val locationModePreference = findPreference<ListPreference>("com.fieldbook.tracker.GENERAL_LOCATION_COLLECTION")
val locationModes = LocationCollectionMode.values()

locationModePreference?.entries = locationModes.map { getString(it.labelResId) }.toTypedArray()
locationModePreference?.entryValues = locationModes.map { it.value.toString() }.toTypedArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant