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

Persistent High threshold fix #3734

Closed

Conversation

Navid200
Copy link
Collaborator

@Navid200 Navid200 commented Oct 26, 2024

This PR fixes two issues I caused adding Persistent High threshold (#3704).

1- You can enter any value as persistent high threshold.
This PR rejects all values that are outside the 40-400 mg/dL range.

2- If xDrip is already installed and mmol/L is selected and xDrip is updated to our latest Nightly (2024.10.11), the threshold will be set to 170 mmol/L.
The user will need to change units to mg/dL and back to mmol/L to correct that to 9.4 mmol/L.
This PR does the correction automatically.

@Navid200
Copy link
Collaborator Author

This is ready for a review.

@@ -385,6 +386,9 @@ protected void onCreate(Bundle savedInstanceState) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);

checkedeula = checkEula();
if (!Pref.getString("units", "mgdl").equals("mgdl")) { // Only if the selected unit is mmol/L
handleUnitsChange(null, "mmol", null); // Trigger the correction of values (defaults) if needed based on the selected unit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be here, we should only need to call handleUnitsChange() when the units are changed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create a video clip and add here showing why we need this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Install a previous version of xDrip on a test phone. Change the unit to mmol/L.
Then, update xDrip to our current Nightly. Go to the Persistent High Alert page. You will see this as the threshold:

170 mmol/L

Change the unit to mg/dL and back to mmol/L. You will see it change to 9.4 mmol/L.

Triggering the handle unit change right after xDrip start guarantees that will never happen.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but the onCreate method is not once on application start it is whenever the Home activity is created. It should be in IdempotentMigrations.performAll() but you need to check that there are no side effects as well because that handleUnitsChange() method does a few different things.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. I will fix all of this in a few hours.

Thanks

@@ -755,6 +758,11 @@ public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (isNumeric(stringValue)) {
final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen
double submissionMgdl = domgdl ? Double.parseDouble(stringValue) : Double.parseDouble(stringValue) * Constants.MMOLL_TO_MGDL;
if (submissionMgdl > MAX_GLUCOSE_INPUT || submissionMgdl < MIN_GLUCOSE_INPUT) {
JoH.static_toast_long("The value must be between " + unitsConvert2Disp(domgdl, MIN_GLUCOSE_INPUT) + " and " + unitsConvert2Disp(domgdl, MAX_GLUCOSE_INPUT));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to continue improving localization this should be a string resource format string that has these values formatted in to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left everything in English intentionally to make sure it all looks good before adding any of it to strings.

@@ -755,6 +758,11 @@ public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (isNumeric(stringValue)) {
final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen
double submissionMgdl = domgdl ? Double.parseDouble(stringValue) : Double.parseDouble(stringValue) * Constants.MMOLL_TO_MGDL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tolerantParseDouble() is typically much safer to use and supports localized number input where , can be the decimal point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks
Will change it

@Navid200
Copy link
Collaborator Author

Navid200 commented Oct 29, 2024

Thanks for the catch.

I have changed to tolerantParseDouble().
I have moved the call to handleUnitChaange to IdempotentMigrations.

Thanks for the guidance.


I have added the strings for translation as well.

@Navid200
Copy link
Collaborator Author

I don't know why/how the file "output-metadata.json" was added to the pull request.
Should I remove it somehow? Or, can you exclude it from merge assuming you want to approve?

@@ -0,0 +1,20 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not be in the PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing it right now

@@ -755,6 +759,11 @@ public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (isNumeric(stringValue)) {
final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen
double submissionMgdl = domgdl ? tolerantParseDouble(stringValue) : tolerantParseDouble(stringValue) * Constants.MMOLL_TO_MGDL;
if (submissionMgdl > MAX_GLUCOSE_INPUT || submissionMgdl < MIN_GLUCOSE_INPUT) {
JoH.static_toast_long(xdrip.gs(R.string.the_value_must_be_between_space) + unitsConvert2Disp(domgdl, MIN_GLUCOSE_INPUT) + xdrip.gs(R.string.space_and_space) + unitsConvert2Disp(domgdl, MAX_GLUCOSE_INPUT));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For good localization you shouldn't be building the string yourself with concatenation. Instead use a format string. See how xdrip_software_changed_format is handled for an example.

@Navid200
Copy link
Collaborator Author

Navid200 commented Nov 3, 2024

Thanks for the multiple reviews. I don't know how a compilation file got added to the PR.

I hope you don't mind I will have to close this PR yet again and open a new clean PR without that file and with the string set up as you have advised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants