From 365b1029c597ab91e5978a08b20ff7b288ac86a8 Mon Sep 17 00:00:00 2001 From: oliexdev Date: Fri, 15 Aug 2025 12:57:58 +0200 Subject: [PATCH] Fix sync selectedUser field in driver with setSelectedScaleUser() --- .../BluetoothStandardWeightProfile.java | 15 +++++++++++---- .../bluetooth/scalesJava/LegacyScaleAdapter.kt | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/BluetoothStandardWeightProfile.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/BluetoothStandardWeightProfile.java index 23f36252..97829d80 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/BluetoothStandardWeightProfile.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/BluetoothStandardWeightProfile.java @@ -110,6 +110,13 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat MAX_STEP } + @Override + public void setSelectedScaleUser(ScaleUser user) { + super.setSelectedScaleUser(user); + this.selectedUser = user; + LogManager.d(TAG, "setSelectedScaleUser: id=" + (user != null ? user.getId() : -1)); + } + @Override protected boolean onNextStep(int stepNr) { @@ -746,7 +753,7 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat } protected synchronized void storeUserScaleConsentCode(int userId, int consentCode) { - prefs.edit().putInt("userConsentCode" + userId, consentCode).apply(); + prefs.edit().putInt("userConsentCode" + userId, consentCode).commit(); LogManager.d(TAG, "storeUserScaleConsentCode: userId=" + userId + " now=" + getUserScaleConsent(userId)); } @@ -757,11 +764,11 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat protected synchronized void storeUserScaleIndex(int userId, int userIndex) { int currentUserIndex = getUserScaleIndex(userId); if (currentUserIndex != -1) { - prefs.edit().putInt("userIdFromUserScaleIndex" + currentUserIndex, -1).apply(); + prefs.edit().putInt("userIdFromUserScaleIndex" + currentUserIndex, -1).commit(); } - prefs.edit().putInt("userScaleIndex" + userId, userIndex).apply(); + prefs.edit().putInt("userScaleIndex" + userId, userIndex).commit(); if (userIndex != -1) { - prefs.edit().putInt("userIdFromUserScaleIndex" + userIndex, userId).apply(); + prefs.edit().putInt("userIdFromUserScaleIndex" + userIndex, userId).commit(); } LogManager.d(TAG, "storeUserScaleIndex: userId=" + userId + " now=" + getUserScaleIndex(userId)); } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/LegacyScaleAdapter.kt b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/LegacyScaleAdapter.kt index fd6450fa..e2e38802 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/LegacyScaleAdapter.kt +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/scalesJava/LegacyScaleAdapter.kt @@ -101,6 +101,8 @@ class LegacyScaleAdapter( private suspend fun provideUserDataToLegacyDriver() { try { + currentInternalUser?.let { bluetoothDriverInstance.setSelectedScaleUser(it) } + LogManager.d(TAG, "Attempting to load user data for legacy driver: ${bluetoothDriverInstance.driverName()}") val userListFromDb = databaseRepository.getAllUsers().first() @@ -325,8 +327,6 @@ class LegacyScaleAdapter( provideUserDataToLegacyDriver() - currentInternalUser?.let { bluetoothDriverInstance.setSelectedScaleUser(it) } - LogManager.d(TAG, "connect: Calling connect() on Java driver instance (${bluetoothDriverInstance.driverName()}) for $address.") try { bluetoothDriverInstance.connect(address)