Skip to content

Commit

Permalink
Add descriptions for tide estimation types
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 committed Nov 28, 2024
1 parent 56d9f25 commit 70b1bbc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.kylecorry.trail_sense.shared.views
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.kylecorry.andromeda.pickers.Pickers
Expand All @@ -14,6 +17,7 @@ class MaterialSpinnerView(context: Context, attrs: AttributeSet?) : FrameLayout(
private val holder: TextInputLayout
private var listener: ((Int?) -> Unit)? = null
private var items: List<String> = listOf()
private var descriptions: List<String> = listOf()

var selectedItemPosition: Int = 0
private set
Expand All @@ -27,7 +31,32 @@ class MaterialSpinnerView(context: Context, attrs: AttributeSet?) : FrameLayout(
holder = findViewById(R.id.material_spinner_holder)

edittext.setOnClickListener {
Pickers.item(context, holder.hint ?: "", items, selectedItemPosition) {
val pickerItems = items.mapIndexed { index, item ->
buildSpannedString {
val hasDescription = descriptions.getOrNull(index)?.isNotBlank() == true
if (hasDescription) {
bold {
append(item)
}
} else {
append(item)
}
if (hasDescription) {
append("\n")
scale(0.2f) {
append("\n")
}
scale(0.65f) {
append(descriptions[index])
}
scale(0.2f) {
append("\n")
}
}
}
}

Pickers.item(context, holder.hint ?: "", pickerItems, selectedItemPosition) {
it ?: return@item
setSelection(it)
}
Expand All @@ -38,6 +67,10 @@ class MaterialSpinnerView(context: Context, attrs: AttributeSet?) : FrameLayout(
this.items = items
}

fun setDescriptions(descriptions: List<String>) {
this.descriptions = descriptions
}

fun setSelection(position: Int) {
selectedItemPosition = position
listener?.invoke(position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ class CreateTideFragment : BoundFragment<FragmentCreateTideBinding>() {
EstimateType.LunitidalIntervalManualLocal to getString(R.string.lunitidal_interval_local),
EstimateType.LunitidalIntervalManualUTC to getString(R.string.lunitidal_interval_utc),
EstimateType.TideModel to getString(R.string.tide_model_auto),
EstimateType.Harmonic to getString(R.string.harmonic) + " (!! NEED TO MANUALLY INSERT INTO DB !!)"
EstimateType.Harmonic to getString(R.string.harmonic)
)
}

private val estimateTypeDescriptionMap by lazy {
mapOf(
EstimateType.Clock to getString(R.string.tide_estimation_description_tide_clock),
EstimateType.LunitidalIntervalAuto to getString(R.string.tide_estimation_description_lunitidal_interval_auto),
EstimateType.LunitidalIntervalManualLocal to getString(R.string.tide_estimation_description_lunitidal_interval_local),
EstimateType.LunitidalIntervalManualUTC to getString(R.string.tide_estimation_description_lunitidal_interval_utc),
EstimateType.TideModel to getString(R.string.tide_estimation_description_tide_model),
EstimateType.Harmonic to "!! NEED TO MANUALLY INSERT INTO DB !!"
)
}

Expand Down Expand Up @@ -117,6 +128,9 @@ class CreateTideFragment : BoundFragment<FragmentCreateTideBinding>() {
binding.estimateAlgorithmSpinner.setItems(
estimateTypes.map { estimateTypeNameMap[it] ?: "" },
)
binding.estimateAlgorithmSpinner.setDescriptions(
estimateTypes.map { estimateTypeDescriptionMap[it] ?: "" }
)
binding.estimateAlgorithmSpinner.setSelection(0)

binding.estimateAlgorithmSpinner.setOnItemSelectedListener { position ->
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1514,4 +1514,9 @@
<string name="nearby_beacons">Nearby beacons</string>
<string name="pref_tide_model_enabled" translatable="false">pref_tide_model_enabled</string>
<string name="tide_model_auto">Tide model (auto)</string>
<string name="tide_estimation_description_tide_clock">Uses the clock to estimate tides. Requires at least one known tide.</string>
<string name="tide_estimation_description_lunitidal_interval_auto">Uses the moon\'s position to estimate tides. Requires at least one known tide.</string>
<string name="tide_estimation_description_lunitidal_interval_local">Uses the moon\'s position to estimate tides. Requires the local lunitidal interval and the location.</string>
<string name="tide_estimation_description_lunitidal_interval_utc">Uses the moon\'s position to estimate tides. Requires the UTC lunitidal interval.</string>
<string name="tide_estimation_description_tide_model">Uses a built-in tide model to estimate tides for the entered location or your location if none is entered.</string>
</resources>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ androidXTestRunner = "1.6.2"
androidXTestUiAutomator = "2.3.0"
androidXArchCore = "2.2.0"
androidXCoreRemoteViews = "1.1.0"
andromedaVersion = "11.2.0"
andromedaVersion = "11.3.0"
cameraxVersion = "1.4.0"
coreKtx = "1.15.0"
appcompat = "1.7.0"
Expand Down

0 comments on commit 70b1bbc

Please sign in to comment.