ThreadAdapter.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright 2017 Andrew Dawson
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky;
  16. import android.support.annotation.Nullable;
  17. import android.support.v7.widget.RecyclerView;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import com.keylesspalace.tusky.entity.Status;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. class ThreadAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
  25. private List<Status> statuses;
  26. private StatusActionListener statusActionListener;
  27. private int statusIndex;
  28. ThreadAdapter(StatusActionListener listener) {
  29. this.statusActionListener = listener;
  30. this.statuses = new ArrayList<>();
  31. this.statusIndex = 0;
  32. }
  33. @Override
  34. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  35. View view = LayoutInflater.from(parent.getContext())
  36. .inflate(R.layout.item_status, parent, false);
  37. return new StatusViewHolder(view);
  38. }
  39. @Override
  40. public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
  41. StatusViewHolder holder = (StatusViewHolder) viewHolder;
  42. Status status = statuses.get(position);
  43. holder.setupWithStatus(status, statusActionListener);
  44. }
  45. @Override
  46. public int getItemCount() {
  47. return statuses.size();
  48. }
  49. Status getItem(int position) {
  50. return statuses.get(position);
  51. }
  52. public void removeItem(int position) {
  53. statuses.remove(position);
  54. notifyItemRemoved(position);
  55. }
  56. int insertStatus(Status status) {
  57. int i = statusIndex;
  58. statuses.add(i, status);
  59. notifyItemInserted(i);
  60. return i;
  61. }
  62. void addAncestors(List<Status> ancestors) {
  63. statusIndex = ancestors.size();
  64. statuses.addAll(0, ancestors);
  65. notifyItemRangeInserted(0, statusIndex);
  66. }
  67. void addDescendants(List<Status> descendants) {
  68. int end = statuses.size();
  69. statuses.addAll(descendants);
  70. notifyItemRangeInserted(end, descendants.size());
  71. }
  72. }