From 70f8810fc9fbd417c481ae7cb5905239f22090e6 Mon Sep 17 00:00:00 2001 From: Jens Knutson Date: Tue, 2 Feb 2016 17:47:33 -0600 Subject: [PATCH] Better sort order in the device list --- .idea/gradle.xml | 6 +++ app/build.gradle | 1 + .../android/sdk/ui/DeviceListFragment.java | 50 ++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 8d2df476..c3bf4f76 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -12,6 +12,12 @@ + diff --git a/app/build.gradle b/app/build.gradle index 88408754..9b08bcd5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,6 +68,7 @@ dependencies { compile 'com.tumblr:bookends:1.0.0' compile 'com.squareup.okio:okio:1.6.0' compile 'com.squareup.phrase:phrase:1.0.3' + compile 'org.apache.commons:commons-collections4:4.1' compile 'org.apache.commons:commons-lang3:3.3.2' compile 'uk.co.chrisjenx:calligraphy:2.1.0' diff --git a/app/src/main/java/io/particle/android/sdk/ui/DeviceListFragment.java b/app/src/main/java/io/particle/android/sdk/ui/DeviceListFragment.java index ff91fd85..67e58050 100644 --- a/app/src/main/java/io/particle/android/sdk/ui/DeviceListFragment.java +++ b/app/src/main/java/io/particle/android/sdk/ui/DeviceListFragment.java @@ -29,10 +29,19 @@ import com.getbase.floatingactionbutton.AddFloatingActionButton; import com.getbase.floatingactionbutton.FloatingActionsMenu; +import com.google.common.collect.Lists; import com.tumblr.bookends.Bookends; +import org.apache.commons.collections4.comparators.BooleanComparator; +import org.apache.commons.collections4.comparators.ComparatorChain; +import org.apache.commons.collections4.comparators.NullComparator; + +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import javax.annotation.ParametersAreNonnullByDefault; + import io.particle.android.sdk.DevicesLoader; import io.particle.android.sdk.cloud.ParticleDevice; import io.particle.android.sdk.devicesetup.ParticleDeviceSetupLibrary; @@ -48,6 +57,7 @@ import static io.particle.android.sdk.utils.Py.truthy; +@ParametersAreNonnullByDefault public class DeviceListFragment extends Fragment implements LoaderManager.LoaderCallbacks> { @@ -71,11 +81,11 @@ public void onDeviceSelected(ParticleDevice device) { private FloatingActionsMenu fabMenu; private DeviceListAdapter adapter; private Bookends bookends; + private final Comparator comparator = new HelpfulOrderDeviceComparator(); private Callbacks callbacks = dummyCallbacks; private DeviceSetupCompleteReceiver deviceSetupCompleteReceiver; - @Override public void onAttach(Activity activity) { super.onAttach(activity); @@ -208,6 +218,12 @@ public Loader> onCreateLoader(int i, Bundle bundle) { public void onLoadFinished(Loader> loader, List sparkDevices) { refreshLayout.setRefreshing(false); adapter.clear(); + + // defensive copy. Shouldn't be necessary, but it's a small collection, so I'm erring + // on the side of safety. + sparkDevices = Lists.newArrayList(sparkDevices); + Collections.sort(sparkDevices, comparator); + adapter.addAll(sparkDevices); bookends.notifyDataSetChanged(); } @@ -459,4 +475,36 @@ private Pair getStatusTextAndColoredDot(ParticleDevice device) } } + + static class DeviceOnlineStatusComparator implements Comparator { + + @Override + public int compare(ParticleDevice lhs, ParticleDevice rhs) { + return BooleanComparator.getTrueFirstComparator().compare( + lhs.isConnected(), rhs.isConnected()); + } + } + + + static class UnnamedDevicesFirstComparator implements Comparator { + + private final NullComparator nullComparator = new NullComparator<>(false); + + @Override + public int compare(ParticleDevice lhs, ParticleDevice rhs) { + String lhname = lhs.getName(); + String rhname = rhs.getName(); + return nullComparator.compare(lhname, rhname); + } + } + + + static class HelpfulOrderDeviceComparator extends ComparatorChain { + + HelpfulOrderDeviceComparator() { + super(new DeviceOnlineStatusComparator(), false); + this.addComparator(new UnnamedDevicesFirstComparator(), false); + } + } + }