Skip to content

Commit

Permalink
Improve device sorting (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
valterc authored Feb 8, 2023
1 parent a8fed5b commit e10a913
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (service != null) {
try {
List<DeviceId> devices = viewModel.getSavedDevices();
devices.sort((a, b) -> new DevicePreferences(requireContext(), a).getPriority() - new DevicePreferences(requireContext(), b).getPriority());
devices.sort((a, b) -> {
DevicePreferences devicePreferencesA = new DevicePreferences(requireContext(), a);
DevicePreferences devicePreferencesB = new DevicePreferences(requireContext(), b);

int priority = devicePreferencesA.getPriority() - devicePreferencesB.getPriority();
if (priority != 0) {
return priority;
}

return devicePreferencesA.getName().compareToIgnoreCase(devicePreferencesB.getName());
});
listDevicesAdapter.setDevices(devices);
textViewNoSavedDevices.setVisibility(viewModel.anyDevicesSaved() ? View.GONE : View.VISIBLE);
viewModel.startReceivingData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ private boolean tryUpdateCurrentDevice() {
assert preferencesA != null;
assert preferencesB != null;

return preferencesA.getPriority(context) - preferencesB.getPriority(context);
int priority = preferencesA.getPriority(context) - preferencesB.getPriority(context);
if (priority != 0) {
return priority;
}

return preferencesA.getName(context).compareToIgnoreCase(preferencesB.getName(context));
}).collect(Collectors.toList());

for (DeviceData deviceData : sortedDeviceData) {
Expand Down

0 comments on commit e10a913

Please sign in to comment.