Skip to content

Commit

Permalink
Enable different sort per source.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Jan 26, 2016
1 parent bd993ef commit 71971a5
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 129 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/io/plaidapp/data/PlaidItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public abstract class PlaidItem {
public String url; // can't be final as some APIs use different serialized names
public String dataSource;
public int page;
public float weight;
public float weightBoost;
public float weight; // used for sorting
public int colspan;

public PlaidItem(long id,
Expand Down
30 changes: 0 additions & 30 deletions app/src/main/java/io/plaidapp/data/PlaidItemComparator.java

This file was deleted.

59 changes: 59 additions & 0 deletions app/src/main/java/io/plaidapp/data/PlaidItemSorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.data;

import java.util.Comparator;
import java.util.List;

/**
* Classes related to sorting {@link PlaidItem}s.
*/
public class PlaidItemSorting {

/**
* A comparator that compares {@link PlaidItem}s based on their {@code weight} attribute.
*/
public static class PlaidItemComparator implements Comparator<PlaidItem> {

@Override
public int compare(PlaidItem lhs, PlaidItem rhs) {
return Float.compare(lhs.weight, rhs.weight);
}
}

/**
* Interface for weighing a group of {@link PlaidItem}s
*/
public interface PlaidItemGroupWeigher<T extends PlaidItem> {
void weigh(List<T> items);
}

/**
* Applies a weight to a group of {@link PlaidItem}s according to their natural order.
*/
public static class NaturalOrderWeigher implements PlaidItemGroupWeigher<PlaidItem> {

@Override
public void weigh(List<PlaidItem> items) {
final float step = 1f / (float) items.size();
for (int i = 0; i < items.size(); i++) {
PlaidItem item = items.get(i);
item.weight = item.page + ((float) i) * step;
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/io/plaidapp/data/PlayerDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
*/
public abstract class PlayerDataManager extends BaseDataManager {

public static final String SOURCE_PLAYER_SHOTS = "SOURCE_PLAYER_SHOTS";
public static final String SOURCE_TEAM_SHOTS = "SOURCE_TEAM_SHOTS";

private final long userId;
private final boolean isTeam;

Expand Down Expand Up @@ -68,6 +71,7 @@ private void loadUserShots() {
@Override
public void success(List<Shot> shots, Response response) {
setPage(shots, page);
setDataSource(shots, SOURCE_PLAYER_SHOTS);
onDataLoaded(shots);
loadFinished();
moreShotsToLoad = shots.size() == DribbbleService.PER_PAGE_DEFAULT;
Expand All @@ -88,6 +92,7 @@ private void loadTeamShots() {
@Override
public void success(List<Shot> shots, Response response) {
setPage(shots, page);
setDataSource(shots, SOURCE_TEAM_SHOTS);
onDataLoaded(shots);
loadFinished();
moreShotsToLoad = shots.size() == DribbbleService.PER_PAGE_DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.data.api.designernews;

import java.util.List;

import io.plaidapp.data.PlaidItemSorting;
import io.plaidapp.data.api.designernews.model.Story;

/**
* Utility class for applying weights to a group of {@link Story}s for sorting. Weighs stories
* relative to the most upvoted & commented stories in the group.
*/
public class StoryWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Story> {

@Override
public void weigh(List<Story> stories) {
float maxVotes = 0f;
float maxComments = 0f;
for (Story story : stories) {
maxVotes = Math.max(maxVotes, story.vote_count);
maxComments = Math.max(maxComments, story.comment_count);
}
for (Story story : stories) {
float weight = 1f - ((((float) story.comment_count) / maxComments) +
((float) story.vote_count / maxVotes)) / 2f;
story.weight = story.page + weight;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ protected Story(Parcel in) {
}
}

public void weigh(float maxDesignNewsComments, float maxDesignNewsVotes) {
weight = 1f - ((((float) comment_count) / maxDesignNewsComments) +
((float) vote_count / maxDesignNewsVotes)) / 2f;
weight = Math.min(weight + weightBoost, 1f);
}

public static class Builder {
private long id;
private String title;
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/io/plaidapp/data/api/dribbble/ShotWeigher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.data.api.dribbble;

import java.util.List;

import io.plaidapp.data.PlaidItemSorting;
import io.plaidapp.data.api.dribbble.model.Shot;

/**
* Utility class for applying weights to a group of {@link Shot}s for sorting. Weighs shots relative
* to the most liked shot in the group.
*/
public class ShotWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Shot> {

@Override
public void weigh(List<Shot> shots) {
float maxLikes = 0f;
for (Shot shot : shots) {
maxLikes = Math.max(maxLikes, shot.likes_count);
}
for (Shot shot : shots) {
float weight = 1f - ((float) shot.likes_count / maxLikes);
shot.weight = shot.page + weight;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ public Spanned getParsedDescription(ColorStateList linkTextColor,
return parsedDescription;
}

public void weigh(long maxLikes) {
weight = 1f - (float) likes_count / maxLikes * 0.8f;
weight = Math.min(weight + weightBoost, 1f);
}

public static class Builder {
private long id;
private String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.data.api.producthunt;

import java.util.List;

import io.plaidapp.data.PlaidItemSorting;
import io.plaidapp.data.api.producthunt.model.Post;

/**
* Utility class for applying weights to a group of {@link Post}s for sorting. Weighs posts relative
* to the most upvoted & commented hunts in the group.
*/
public class PostWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Post> {

@Override
public void weigh(List<Post> posts) {
float maxVotes = 0f;
float maxComments = 0f;
for (Post post : posts) {
maxVotes = Math.max(maxVotes, post.votes_count);
maxComments = Math.max(maxComments, post.comments_count);
}
for (Post post : posts) {
float weight = 1f - ((((float) post.comments_count) / maxComments) +
((float) post.votes_count / maxVotes)) / 2f;
post.weight = post.page + weight;
}
}

}
30 changes: 14 additions & 16 deletions app/src/main/java/io/plaidapp/data/api/producthunt/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,7 @@
*/
public class Post extends PlaidItem implements Parcelable {

@SuppressWarnings("unused")
public static final Parcelable.Creator<Post> CREATOR = new Parcelable.Creator<Post>() {
@Override
public Post createFromParcel(Parcel in) {
return new Post(in);
}

@Override
public Post[] newArray(int size) {
return new Post[size];
}
};
public final String name;
public final String tagline;
public final String discussion_url;
Expand Down Expand Up @@ -124,11 +113,20 @@ public String getScreenshotUrl(int width) {
return url;
}

public void weigh(float maxProductHuntComments, float maxProductHuntVotes) {
weight = 1f - ((((float) comments_count) / maxProductHuntComments) +
((float) votes_count / maxProductHuntVotes)) / 2f;
weight = Math.min(weight + weightBoost, 1f);
}
/* Parcelable stuff */

@SuppressWarnings("unused")
public static final Parcelable.Creator<Post> CREATOR = new Parcelable.Creator<Post>() {
@Override
public Post createFromParcel(Parcel in) {
return new Post(in);
}

@Override
public Post[] newArray(int size) {
return new Post[size];
}
};

@Override
public int describeContents() {
Expand Down
Loading

0 comments on commit 71971a5

Please sign in to comment.