Skip to content

Commit

Permalink
Better sort order in the device list
Browse files Browse the repository at this point in the history
  • Loading branch information
jensck committed Feb 2, 2016
1 parent 2571b2a commit 70f8810
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +57,7 @@
import static io.particle.android.sdk.utils.Py.truthy;


@ParametersAreNonnullByDefault
public class DeviceListFragment extends Fragment implements
LoaderManager.LoaderCallbacks<List<ParticleDevice>> {

Expand All @@ -71,11 +81,11 @@ public void onDeviceSelected(ParticleDevice device) {
private FloatingActionsMenu fabMenu;
private DeviceListAdapter adapter;
private Bookends<DeviceListAdapter> bookends;
private final Comparator<ParticleDevice> comparator = new HelpfulOrderDeviceComparator();

private Callbacks callbacks = dummyCallbacks;
private DeviceSetupCompleteReceiver deviceSetupCompleteReceiver;


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Expand Down Expand Up @@ -208,6 +218,12 @@ public Loader<List<ParticleDevice>> onCreateLoader(int i, Bundle bundle) {
public void onLoadFinished(Loader<List<ParticleDevice>> loader, List<ParticleDevice> 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();
}
Expand Down Expand Up @@ -459,4 +475,36 @@ private Pair<String, Integer> getStatusTextAndColoredDot(ParticleDevice device)
}
}


static class DeviceOnlineStatusComparator implements Comparator<ParticleDevice> {

@Override
public int compare(ParticleDevice lhs, ParticleDevice rhs) {
return BooleanComparator.getTrueFirstComparator().compare(
lhs.isConnected(), rhs.isConnected());
}
}


static class UnnamedDevicesFirstComparator implements Comparator<ParticleDevice> {

private final NullComparator<String> 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<ParticleDevice> {

HelpfulOrderDeviceComparator() {
super(new DeviceOnlineStatusComparator(), false);
this.addComparator(new UnnamedDevicesFirstComparator(), false);
}
}

}

0 comments on commit 70f8810

Please sign in to comment.