Skip to content

Commit

Permalink
AdaptiveUnitPreference fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Dec 11, 2024
1 parent c437b50 commit 446d5cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
11 changes: 0 additions & 11 deletions app/src/main/kotlin/app/aaps/activities/MyPreferenceFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen
import androidx.preference.size
import app.aaps.R
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.data.plugin.PluginType
import app.aaps.core.interfaces.configuration.Config
import app.aaps.core.interfaces.plugin.ActivePlugin
Expand All @@ -39,7 +38,6 @@ import app.aaps.core.keys.DoubleKey
import app.aaps.core.keys.IntKey
import app.aaps.core.keys.Preferences
import app.aaps.core.keys.StringKey
import app.aaps.core.keys.UnitDoubleKey
import app.aaps.core.ui.dialogs.OKDialog
import app.aaps.core.utils.extensions.safeGetSerializable
import app.aaps.core.validators.DefaultEditTextValidator
Expand All @@ -56,8 +54,6 @@ import app.aaps.plugins.configuration.maintenance.MaintenancePlugin
import app.aaps.plugins.main.general.smsCommunicator.SmsCommunicatorPlugin
import app.aaps.plugins.main.skins.SkinProvider
import dagger.android.support.AndroidSupportInjection
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.Vector
import javax.inject.Inject

Expand Down Expand Up @@ -304,13 +300,6 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang
}
}

is UnitDoubleKey -> {
// convert preferences values to current units
val value = profileUtil.valueInCurrentUnitsDetect(preferences.get(keyDefinition)).toString()
val precision = if (profileUtil.units == GlucoseUnit.MGDL) 0 else 1
val converted = BigDecimal(value).setScale(precision, RoundingMode.HALF_UP)
pref.summary = converted.toPlainString()
}
}
for (plugin in activePlugin.getPluginsList()) pref.key?.let { plugin.updatePreferenceSummary(pref) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.AttributeSet
import androidx.annotation.StringRes
import androidx.preference.EditTextPreference
import androidx.preference.PreferenceViewHolder
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.interfaces.profile.ProfileUtil
import app.aaps.core.interfaces.utils.SafeParse
import app.aaps.core.keys.Preferences
Expand All @@ -15,6 +16,8 @@ import app.aaps.core.validators.DefaultEditTextValidator
import app.aaps.core.validators.EditTextValidator
import app.aaps.core.validators.R
import dagger.android.HasAndroidInjector
import java.math.BigDecimal
import java.math.RoundingMode
import javax.inject.Inject

class AdaptiveUnitPreference(
Expand All @@ -36,6 +39,8 @@ class AdaptiveUnitPreference(
// Inflater constructor
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, unitKey = null, title = null)

private var converted: BigDecimal

init {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)

Expand All @@ -45,6 +50,13 @@ class AdaptiveUnitPreference(
title?.let { this.title = context.getString(it) }

preferenceKey = unitKey ?: preferences.get(key) as UnitDoublePreferenceKey

// convert to current unit
val value = profileUtil.valueInCurrentUnitsDetect(preferences.get(preferenceKey)).toString()
val precision = if (profileUtil.units == GlucoseUnit.MGDL) 0 else 1
converted = BigDecimal(value).setScale(precision, RoundingMode.HALF_UP)
summary = converted.toPlainString()

if (preferences.simpleMode && preferenceKey.defaultedBySM) isVisible = false
if (preferences.apsMode && !preferenceKey.showInApsMode) {
isVisible = false; isEnabled = false
Expand Down Expand Up @@ -104,19 +116,16 @@ class AdaptiveUnitPreference(
}

override fun onSetInitialValue(defaultValue: Any?) {
text = profileUtil.fromMgdlToUnits(
try {
SafeParse.stringToDouble(getPersistedString(defaultValue as String?))
} catch (ignored: Exception) {
getPersistedFloat(preferenceKey.defaultValue.toFloat()).toDouble()
}, profileUtil.units
).toString()
text = converted.toPlainString()
}

override fun persistString(value: String?): Boolean =
try {
super.persistString(profileUtil.convertToMgdl(SafeParse.stringToDouble(value, preferenceKey.defaultValue), profileUtil.units).toString())
} catch (ignored: Exception) {
val numericValue = SafeParse.stringToDouble(value, preferenceKey.defaultValue)
summary = numericValue.toString()
val store = profileUtil.convertToMgdl(numericValue, profileUtil.units)
super.persistString(store.toString())
} catch (_: Exception) {
false
}
}

0 comments on commit 446d5cf

Please sign in to comment.