mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-25 03:16:51 +01:00
Sport Activity Summary: fix group order instability
Previously group order was determined on the fly by available entries order or appearance. This commit changes it to use group definition order.
This commit is contained in:
parent
9095ffad8b
commit
414cf5e472
@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -122,7 +123,7 @@ public class ActivitySummaryJsonSummary {
|
||||
|
||||
if (summaryDatalist == null) return null;
|
||||
Iterator<String> keys = summaryDatalist.keys();
|
||||
JSONObject list=new JSONObject();
|
||||
Map<String, JSONArray> activeGroups = new HashMap<>();
|
||||
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
@ -130,27 +131,43 @@ public class ActivitySummaryJsonSummary {
|
||||
JSONObject innerData = (JSONObject) summaryDatalist.get(key);
|
||||
Object value = innerData.get("value");
|
||||
String unit = innerData.getString("unit");
|
||||
String group = getGroup(key);
|
||||
String groupName = getGroup(key);
|
||||
|
||||
if (!list.has(group)) {
|
||||
list.put(group,new JSONArray());
|
||||
JSONArray group = activeGroups.get(groupName);
|
||||
if (group == null) {
|
||||
group = new JSONArray();
|
||||
activeGroups.put(groupName, group);
|
||||
}
|
||||
|
||||
JSONArray tmpl = (JSONArray) list.get(group);
|
||||
JSONObject innernew = new JSONObject();
|
||||
innernew.put("name", key);
|
||||
innernew.put("value", value);
|
||||
innernew.put("unit", unit);
|
||||
tmpl.put(innernew);
|
||||
list.put(group, tmpl);
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("name", key);
|
||||
item.put("value", value);
|
||||
item.put("unit", unit);
|
||||
group.put(item);
|
||||
} catch (JSONException e) {
|
||||
LOG.error("SportsActivity", e);
|
||||
LOG.error("SportsActivity internal error building grouped summary", e);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
|
||||
// Reorder collected groups according to the order set by this.groupData.
|
||||
JSONObject grouped = new JSONObject();
|
||||
Iterator<String> names = this.groupData.keys();
|
||||
while(names.hasNext()) {
|
||||
String groupName = names.next();
|
||||
JSONArray group = activeGroups.get(groupName);
|
||||
if (group != null) {
|
||||
try {
|
||||
grouped.put(groupName, group);
|
||||
} catch (JSONException e) {
|
||||
LOG.error("SportsActivity internal error building grouped summary", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return grouped;
|
||||
}
|
||||
|
||||
private String getGroup(String searchItem) {
|
||||
// NB: Default group must be present in group JSONObject created by createActivitySummaryGroups
|
||||
String defaultGroup = "Activity";
|
||||
if (groupData == null) return defaultGroup;
|
||||
Iterator<String> keys = groupData.keys();
|
||||
@ -170,31 +187,33 @@ public class ActivitySummaryJsonSummary {
|
||||
return defaultGroup;
|
||||
}
|
||||
private JSONObject createActivitySummaryGroups(){
|
||||
final Map<String, List<String>> groupDefinitions = new HashMap<String, List<String>>() {{
|
||||
put("Strokes", Arrays.asList(
|
||||
STROKE_DISTANCE_AVG, STROKE_AVG_PER_SECOND, STROKES,
|
||||
STROKE_RATE_AVG, STROKE_RATE_MAX
|
||||
final Map<String, List<String>> groupDefinitions = new LinkedHashMap<String, List<String>>() {{
|
||||
// NB: Default group Activity must be present in this definition, otherwise it wouldn't
|
||||
// be shown.
|
||||
put("Activity", Arrays.asList(
|
||||
DISTANCE_METERS, STEPS, ACTIVE_SECONDS, CALORIES_BURNT, STRIDE_TOTAL,
|
||||
HR_AVG, HR_MAX, HR_MIN, STRIDE_AVG, STRIDE_MAX, STRIDE_MIN
|
||||
));
|
||||
put("Swimming", Arrays.asList(
|
||||
SWOLF_INDEX, SWIM_STYLE
|
||||
put("Speed", Arrays.asList(
|
||||
SPEED_AVG, SPEED_MAX, SPEED_MIN, PACE_AVG_SECONDS_KM, PACE_MIN,
|
||||
PACE_MAX, "averageSpeed2", CADENCE_AVG, CADENCE_MAX, CADENCE_MIN
|
||||
));
|
||||
put("Elevation", Arrays.asList(
|
||||
ASCENT_METERS, DESCENT_METERS, ALTITUDE_MAX, ALTITUDE_MIN, ALTITUDE_AVG,
|
||||
ALTITUDE_BASE, ASCENT_SECONDS, DESCENT_SECONDS, FLAT_SECONDS, ASCENT_DISTANCE,
|
||||
DESCENT_DISTANCE, FLAT_DISTANCE, ELEVATION_GAIN, ELEVATION_LOSS
|
||||
));
|
||||
put("Speed", Arrays.asList(
|
||||
SPEED_AVG, SPEED_MAX, SPEED_MIN, PACE_AVG_SECONDS_KM, PACE_MIN,
|
||||
PACE_MAX, "averageSpeed2", CADENCE_AVG, CADENCE_MAX, CADENCE_MIN
|
||||
));
|
||||
put("Activity", Arrays.asList(
|
||||
DISTANCE_METERS, STEPS, ACTIVE_SECONDS, CALORIES_BURNT, STRIDE_TOTAL,
|
||||
HR_AVG, HR_MAX, HR_MIN, STRIDE_AVG, STRIDE_MAX, STRIDE_MIN
|
||||
));
|
||||
put("HeartRateZones", Arrays.asList(
|
||||
HR_ZONE_NA, HR_ZONE_WARM_UP, HR_ZONE_FAT_BURN, HR_ZONE_AEROBIC, HR_ZONE_ANAEROBIC,
|
||||
HR_ZONE_EXTREME
|
||||
));
|
||||
put("Strokes", Arrays.asList(
|
||||
STROKE_DISTANCE_AVG, STROKE_AVG_PER_SECOND, STROKES,
|
||||
STROKE_RATE_AVG, STROKE_RATE_MAX
|
||||
));
|
||||
put("Swimming", Arrays.asList(
|
||||
SWOLF_INDEX, SWIM_STYLE
|
||||
));
|
||||
put("TrainingEffect", Arrays.asList(
|
||||
TRAINING_EFFECT_AEROBIC, TRAINING_EFFECT_ANAEROBIC, WORKOUT_LOAD,
|
||||
MAXIMUM_OXYGEN_UPTAKE
|
||||
|
Loading…
Reference in New Issue
Block a user