TimelineAdapter.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 TimelineAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
  25. private static final int VIEW_TYPE_STATUS = 0;
  26. private static final int VIEW_TYPE_FOOTER = 1;
  27. private List<Status> statuses;
  28. private StatusActionListener statusListener;
  29. TimelineAdapter(StatusActionListener statusListener) {
  30. super();
  31. statuses = new ArrayList<>();
  32. this.statusListener = statusListener;
  33. }
  34. @Override
  35. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  36. switch (viewType) {
  37. default:
  38. case VIEW_TYPE_STATUS: {
  39. View view = LayoutInflater.from(viewGroup.getContext())
  40. .inflate(R.layout.item_status, viewGroup, false);
  41. return new StatusViewHolder(view);
  42. }
  43. case VIEW_TYPE_FOOTER: {
  44. View view = LayoutInflater.from(viewGroup.getContext())
  45. .inflate(R.layout.item_footer, viewGroup, false);
  46. return new FooterViewHolder(view);
  47. }
  48. }
  49. }
  50. @Override
  51. public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
  52. if (position < statuses.size()) {
  53. StatusViewHolder holder = (StatusViewHolder) viewHolder;
  54. Status status = statuses.get(position);
  55. holder.setupWithStatus(status, statusListener);
  56. }
  57. }
  58. @Override
  59. public int getItemCount() {
  60. return statuses.size() + 1;
  61. }
  62. @Override
  63. public int getItemViewType(int position) {
  64. if (position == statuses.size()) {
  65. return VIEW_TYPE_FOOTER;
  66. } else {
  67. return VIEW_TYPE_STATUS;
  68. }
  69. }
  70. void update(List<Status> newStatuses) {
  71. if (newStatuses == null || newStatuses.isEmpty()) {
  72. return;
  73. }
  74. if (statuses.isEmpty()) {
  75. statuses = newStatuses;
  76. } else {
  77. int index = statuses.indexOf(newStatuses.get(newStatuses.size() - 1));
  78. for (int i = 0; i < index; i++) {
  79. statuses.remove(0);
  80. }
  81. int newIndex = newStatuses.indexOf(statuses.get(0));
  82. if (newIndex == -1) {
  83. statuses.addAll(0, newStatuses);
  84. } else {
  85. statuses.addAll(0, newStatuses.subList(0, newIndex));
  86. }
  87. }
  88. notifyDataSetChanged();
  89. }
  90. void addItems(List<Status> newStatuses) {
  91. int end = statuses.size();
  92. statuses.addAll(newStatuses);
  93. notifyItemRangeInserted(end, newStatuses.size());
  94. }
  95. public void removeItem(int position) {
  96. statuses.remove(position);
  97. notifyItemRemoved(position);
  98. }
  99. @Nullable
  100. Status getItem(int position) {
  101. if (position >= 0 && position < statuses.size()) {
  102. return statuses.get(position);
  103. }
  104. return null;
  105. }
  106. }