Skip to content

Commit

Permalink
Added column spanning. Currently most popular dribbble per page spans.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Nov 11, 2015
1 parent 4f3fac1 commit f04ada9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/io/plaidapp/data/PlaidItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class PlaidItem {
public int page;
public float weight;
public float weightBoost;
public int colspan;

public PlaidItem(long id,
String title,
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/io/plaidapp/ui/FeedAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final PlaidItemComparator comparator;
private final boolean pocketIsInstalled;
private @Nullable DataLoadingSubject dataLoading;
private final int columns;

private List<PlaidItem> items;

public FeedAdapter(Activity hostActivity,
DataLoadingSubject dataLoading,
int columns,
boolean pocketInstalled) {
this.host = hostActivity;
this.dataLoading = dataLoading;
this.columns = columns;
this.pocketIsInstalled = pocketInstalled;
layoutInflater = LayoutInflater.from(host);
comparator = new PlaidItemComparator();
Expand Down Expand Up @@ -386,6 +389,15 @@ private PlaidItem getItem(int position) {
return items.get(position);
}

public int getItemColumnSpan(int position) {
switch (getItemViewType(position)) {
case TYPE_LOADING_MORE:
return columns;
default:
return getItem(position).colspan;
}
}

private void add(PlaidItem item) {
items.add(item);
}
Expand Down Expand Up @@ -417,6 +429,38 @@ public void addAndResort(Collection<? extends PlaidItem> newItems) {
}
}
sort();
expandPopularItems();
}

private void expandPopularItems() {
// for now just expand the first dribbble image per page which should be
// the most popular according to #sort.
// TODO make this smarter & handle other item types
List<Integer> expandedPositions = new ArrayList<>();
int page = -1;
final int count = items.size();
for (int i = 0; i < count; i++) {
PlaidItem item = getItem(i);
if (item instanceof Shot && item.page > page) {
item.colspan = columns;
page = item.page;
expandedPositions.add(i);
} else {
item.colspan = 1;
}
}

// make sure that any expanded items are at the start of a row
// so that we don't leave any gaps in the grid
for (int expandedPos = 0; expandedPos < expandedPositions.size(); expandedPos++) {
int pos = expandedPositions.get(expandedPos);
int extraSpannedSpaces = expandedPos * (columns - 1);
int rowPosition = (pos + extraSpannedSpaces) % columns;
if (rowPosition != 0) {
int swapWith = pos + (columns - rowPosition);
Collections.swap(items, pos, swapWith);
}
}
}

protected void sort() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ public void onDataLoaded(List<? extends PlaidItem> data) {
checkEmptyState();
}
};
adapter = new FeedAdapter(this, dataManager, PocketUtils.isPocketInstalled(this));
adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this));
grid.setAdapter(adapter);
layoutManager = new GridLayoutManager(this, columns);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == adapter.getDataItemCount() ? columns : 1;
return adapter.getItemColumnSpan(position);
}
});
grid.setLayoutManager(layoutManager);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/plaidapp/ui/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ public void onDataLoaded(List<? extends PlaidItem> data) {
}
}
};
adapter = new FeedAdapter(this, dataManager, PocketUtils.isPocketInstalled(this));
adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this));
results.setAdapter(adapter);
GridLayoutManager layoutManager = new GridLayoutManager(this, columns);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == adapter.getDataItemCount() ? columns : 1;
return adapter.getItemColumnSpan(position);
}
});
results.setLayoutManager(layoutManager);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
</style>

<style name="Widget.Plaid.HomeToolbar" parent="Widget.Plaid.Toolbar.SmallCapsTitle">
<item name="android:background">@color/background_dark</item>
<item name="android:background">@null</item>
<item name="android:elevation">0dp</item>
<item name="android:theme">@android:style/ThemeOverlay.Material.Dark</item>
</style>
Expand Down

0 comments on commit f04ada9

Please sign in to comment.