ReportAdapter.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.v7.widget.RecyclerView;
  17. import android.text.Spanned;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.CheckBox;
  22. import android.widget.CompoundButton;
  23. import android.widget.TextView;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. class ReportAdapter extends RecyclerView.Adapter {
  27. static class ReportStatus {
  28. String id;
  29. Spanned content;
  30. boolean checked;
  31. ReportStatus(String id, Spanned content, boolean checked) {
  32. this.id = id;
  33. this.content = content;
  34. this.checked = checked;
  35. }
  36. @Override
  37. public int hashCode() {
  38. return id.hashCode();
  39. }
  40. @Override
  41. public boolean equals(Object other) {
  42. if (this.id == null) {
  43. return this == other;
  44. } else if (!(other instanceof ReportStatus)) {
  45. return false;
  46. }
  47. ReportStatus status = (ReportStatus) other;
  48. return status.id.equals(this.id);
  49. }
  50. }
  51. private List<ReportStatus> statusList;
  52. ReportAdapter() {
  53. super();
  54. statusList = new ArrayList<>();
  55. }
  56. @Override
  57. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  58. View view = LayoutInflater.from(parent.getContext())
  59. .inflate(R.layout.item_report_status, parent, false);
  60. return new ReportStatusViewHolder(view);
  61. }
  62. @Override
  63. public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
  64. ReportStatusViewHolder holder = (ReportStatusViewHolder) viewHolder;
  65. ReportStatus status = statusList.get(position);
  66. holder.setupWithStatus(status);
  67. }
  68. @Override
  69. public int getItemCount() {
  70. return statusList.size();
  71. }
  72. void addItem(ReportStatus status) {
  73. int end = statusList.size();
  74. statusList.add(status);
  75. notifyItemInserted(end);
  76. }
  77. void addItems(List<ReportStatus> newStatuses) {
  78. int end = statusList.size();
  79. int added = 0;
  80. for (ReportStatus status : newStatuses) {
  81. if (!statusList.contains(status)) {
  82. statusList.add(status);
  83. added += 1;
  84. }
  85. }
  86. if (added > 0) {
  87. notifyItemRangeInserted(end, added);
  88. }
  89. }
  90. String[] getCheckedStatusIds() {
  91. List<String> idList = new ArrayList<>();
  92. for (ReportStatus status : statusList) {
  93. if (status.checked) {
  94. idList.add(status.id);
  95. }
  96. }
  97. return idList.toArray(new String[0]);
  98. }
  99. private static class ReportStatusViewHolder extends RecyclerView.ViewHolder {
  100. private TextView content;
  101. private CheckBox checkBox;
  102. ReportStatusViewHolder(View view) {
  103. super(view);
  104. content = (TextView) view.findViewById(R.id.report_status_content);
  105. checkBox = (CheckBox) view.findViewById(R.id.report_status_check_box);
  106. }
  107. void setupWithStatus(final ReportStatus status) {
  108. content.setText(status.content);
  109. checkBox.setChecked(status.checked);
  110. checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  111. @Override
  112. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  113. status.checked = isChecked;
  114. }
  115. });
  116. }
  117. }
  118. }