Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (! item.isChecked()) {
// call the sort function
item.setChecked(true);
adapter.sortPosts(PostAdapter.NEWEST);
}
break;
case R.id.menu_sort_oldest:
if (! item.isChecked()) {
// call the sort function

item.setChecked(true);
adapter.sortPosts(PostAdapter.OLDEST);
}
break;
case R.id.menu_sort_trending:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
import org.mtuosc.techchat.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private ArrayList<Post> posts = new ArrayList<>();

public static final int NEWEST = 0;
public static final int OLDEST = 1;
public static final int TRENDING = 2;


@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand Down Expand Up @@ -53,4 +60,29 @@ public void addPost(Post post) {
}

public void flushPosts() {posts = new ArrayList<Post>();}

public void sortPosts(int sortBy) {
switch (sortBy) {
case NEWEST: {
Collections.sort(posts, new Comparator<Post>() {
@Override
public int compare(Post post, Post t1) {
return post.getTimestamp().compareTo(t1.getTimestamp());
}
});
this.notifyDataSetChanged();
break;
}
case OLDEST: {
Collections.sort(posts, new Comparator<Post>() {
@Override
public int compare(Post post, Post t1) {
return post.getTimestamp().compareTo(t1.getTimestamp()) * -1;
}
});
this.notifyDataSetChanged();
break;
}
}
}
}