From 51fa51d422a83a7cfcc22dfd2e4925b5e66ae205 Mon Sep 17 00:00:00 2001 From: Kaeso <24925519+ptlthg@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:48:59 -0400 Subject: Prioritize selected profile when fetching weight (#632) Incorrect Farming Weight from comparing Profile Names instead of ID #632 --- .../garden/farming/FarmingWeightDisplay.kt | 34 +++++++++++++++------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src/main') diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt index e8491ae96..41976bf07 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt @@ -426,19 +426,31 @@ class FarmingWeightDisplay { try { val result = withContext(dispatcher) { APIUtil.getJSONResponse(url) }.asJsonObject - for (profileEntry in result["profiles"].asJsonArray) { - val profile = profileEntry.asJsonObject - val profileName = profile["profileName"].asString.lowercase() - if (profileName == localProfile) { - profileId = profile["profileId"].asString - weight = profile["totalWeight"].asDouble + val selectedProfileId = result["selectedProfileId"].asString + val profileEntries = result["profiles"].asJsonArray + + var selectedProfileEntry = profileEntries.find { + it.asJsonObject["profileId"].asString == selectedProfileId + }?.asJsonObject + + // If the selected profile is not found or if the cute name doesn't match look for a different profile + // While it's not optimal to loop twice, this shouldn't happen often + if (selectedProfileEntry == null || selectedProfileEntry["profileName"].asString.lowercase() != localProfile) { + selectedProfileEntry = profileEntries.find { + it.asJsonObject["profileName"].asString.lowercase() == localProfile + }?.asJsonObject + } - localCounter.clear() - dirtyCropWeight = true + if (selectedProfileEntry != null) { + profileId = selectedProfileEntry["profileId"].asString + weight = selectedProfileEntry["totalWeight"].asDouble - return - } + localCounter.clear() + dirtyCropWeight = true + + return } + println("localProfile: '$localProfile'") println("url: '$url'") println("result: '$result'") @@ -531,4 +543,4 @@ class FarmingWeightDisplay { class UpcomingPlayer(val name: String, val weight: Double) } -} \ No newline at end of file +} -- cgit