NotificationsAdapter.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.content.Context;
  17. import android.graphics.Typeface;
  18. import android.support.annotation.Nullable;
  19. import android.support.v4.content.ContextCompat;
  20. import android.support.v7.widget.RecyclerView;
  21. import android.text.SpannableStringBuilder;
  22. import android.text.Spanned;
  23. import android.text.style.StyleSpan;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.ImageView;
  28. import android.widget.TextView;
  29. import com.keylesspalace.tusky.entity.Notification;
  30. import com.keylesspalace.tusky.entity.Status;
  31. import com.squareup.picasso.Picasso;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. class NotificationsAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
  35. private static final int VIEW_TYPE_MENTION = 0;
  36. private static final int VIEW_TYPE_FOOTER = 1;
  37. private static final int VIEW_TYPE_STATUS_NOTIFICATION = 2;
  38. private static final int VIEW_TYPE_FOLLOW = 3;
  39. private List<Notification> notifications;
  40. private StatusActionListener statusListener;
  41. private NotificationActionListener notificationActionListener;
  42. NotificationsAdapter(StatusActionListener statusListener,
  43. NotificationActionListener notificationActionListener) {
  44. super();
  45. notifications = new ArrayList<>();
  46. this.statusListener = statusListener;
  47. this.notificationActionListener = notificationActionListener;
  48. }
  49. @Override
  50. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  51. switch (viewType) {
  52. default:
  53. case VIEW_TYPE_MENTION: {
  54. View view = LayoutInflater.from(parent.getContext())
  55. .inflate(R.layout.item_status, parent, false);
  56. return new StatusViewHolder(view);
  57. }
  58. case VIEW_TYPE_FOOTER: {
  59. View view = LayoutInflater.from(parent.getContext())
  60. .inflate(R.layout.item_footer, parent, false);
  61. return new FooterViewHolder(view);
  62. }
  63. case VIEW_TYPE_STATUS_NOTIFICATION: {
  64. View view = LayoutInflater.from(parent.getContext())
  65. .inflate(R.layout.item_status_notification, parent, false);
  66. return new StatusNotificationViewHolder(view);
  67. }
  68. case VIEW_TYPE_FOLLOW: {
  69. View view = LayoutInflater.from(parent.getContext())
  70. .inflate(R.layout.item_follow, parent, false);
  71. return new FollowViewHolder(view);
  72. }
  73. }
  74. }
  75. @Override
  76. public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
  77. if (position < notifications.size()) {
  78. Notification notification = notifications.get(position);
  79. Notification.Type type = notification.type;
  80. switch (type) {
  81. case MENTION: {
  82. StatusViewHolder holder = (StatusViewHolder) viewHolder;
  83. Status status = notification.status;
  84. holder.setupWithStatus(status, statusListener);
  85. break;
  86. }
  87. case FAVOURITE:
  88. case REBLOG: {
  89. StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder;
  90. holder.setMessage(type, notification.account.getDisplayName(),
  91. notification.status);
  92. holder.setupButtons(notificationActionListener, notification.account.id);
  93. break;
  94. }
  95. case FOLLOW: {
  96. FollowViewHolder holder = (FollowViewHolder) viewHolder;
  97. holder.setMessage(notification.account.getDisplayName(), notification.account.username,
  98. notification.account.avatar);
  99. holder.setupButtons(notificationActionListener, notification.account.id);
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. @Override
  106. public int getItemCount() {
  107. return notifications.size() + 1;
  108. }
  109. @Override
  110. public int getItemViewType(int position) {
  111. if (position == notifications.size()) {
  112. return VIEW_TYPE_FOOTER;
  113. } else {
  114. Notification notification = notifications.get(position);
  115. switch (notification.type) {
  116. default:
  117. case MENTION: {
  118. return VIEW_TYPE_MENTION;
  119. }
  120. case FAVOURITE:
  121. case REBLOG: {
  122. return VIEW_TYPE_STATUS_NOTIFICATION;
  123. }
  124. case FOLLOW: {
  125. return VIEW_TYPE_FOLLOW;
  126. }
  127. }
  128. }
  129. }
  130. public @Nullable Notification getItem(int position) {
  131. if (position >= 0 && position < notifications.size()) {
  132. return notifications.get(position);
  133. }
  134. return null;
  135. }
  136. void update(List<Notification> newNotifications) {
  137. if (newNotifications == null || newNotifications.isEmpty()) {
  138. return;
  139. }
  140. if (notifications.isEmpty()) {
  141. notifications = newNotifications;
  142. } else {
  143. int index = notifications.indexOf(newNotifications.get(newNotifications.size() - 1));
  144. for (int i = 0; i < index; i++) {
  145. notifications.remove(0);
  146. }
  147. int newIndex = newNotifications.indexOf(notifications.get(0));
  148. if (newIndex == -1) {
  149. notifications.addAll(0, newNotifications);
  150. } else {
  151. notifications.addAll(0, newNotifications.subList(0, newIndex));
  152. }
  153. }
  154. notifyDataSetChanged();
  155. }
  156. void addItems(List<Notification> new_notifications) {
  157. int end = notifications.size();
  158. notifications.addAll(new_notifications);
  159. notifyItemRangeInserted(end, new_notifications.size());
  160. }
  161. public void removeItem(int position) {
  162. notifications.remove(position);
  163. notifyItemChanged(position);
  164. }
  165. interface NotificationActionListener {
  166. void onViewAccount(String id);
  167. }
  168. private static class FollowViewHolder extends RecyclerView.ViewHolder {
  169. private TextView message;
  170. private TextView usernameView;
  171. private TextView displayNameView;
  172. private ImageView avatar;
  173. FollowViewHolder(View itemView) {
  174. super(itemView);
  175. message = (TextView) itemView.findViewById(R.id.notification_text);
  176. usernameView = (TextView) itemView.findViewById(R.id.notification_username);
  177. displayNameView = (TextView) itemView.findViewById(R.id.notification_display_name);
  178. avatar = (ImageView) itemView.findViewById(R.id.notification_avatar);
  179. }
  180. void setMessage(String displayName, String username, String avatarUrl) {
  181. Context context = message.getContext();
  182. String format = context.getString(R.string.notification_follow_format);
  183. String wholeMessage = String.format(format, displayName);
  184. message.setText(wholeMessage);
  185. format = context.getString(R.string.status_username_format);
  186. String wholeUsername = String.format(format, username);
  187. usernameView.setText(wholeUsername);
  188. displayNameView.setText(displayName);
  189. Picasso.with(context)
  190. .load(avatarUrl)
  191. .placeholder(R.drawable.avatar_default)
  192. .error(R.drawable.avatar_error)
  193. .into(avatar);
  194. }
  195. void setupButtons(final NotificationActionListener listener, final String accountId) {
  196. avatar.setOnClickListener(new View.OnClickListener() {
  197. @Override
  198. public void onClick(View v) {
  199. listener.onViewAccount(accountId);
  200. }
  201. });
  202. }
  203. }
  204. private static class StatusNotificationViewHolder extends RecyclerView.ViewHolder {
  205. private TextView message;
  206. private ImageView icon;
  207. private TextView statusContent;
  208. private ViewGroup container;
  209. StatusNotificationViewHolder(View itemView) {
  210. super(itemView);
  211. message = (TextView) itemView.findViewById(R.id.notification_text);
  212. icon = (ImageView) itemView.findViewById(R.id.notification_icon);
  213. statusContent = (TextView) itemView.findViewById(R.id.notification_content);
  214. container = (ViewGroup) itemView.findViewById(R.id.notification_container);
  215. }
  216. void setMessage(Notification.Type type, String displayName, Status status) {
  217. Context context = message.getContext();
  218. String format;
  219. switch (type) {
  220. default:
  221. case FAVOURITE: {
  222. icon.setImageResource(R.drawable.ic_star_24dp);
  223. icon.setColorFilter(ContextCompat.getColor(context,
  224. R.color.status_favourite_button_marked_dark));
  225. format = context.getString(R.string.notification_favourite_format);
  226. break;
  227. }
  228. case REBLOG: {
  229. icon.setImageResource(R.drawable.ic_repeat_24dp);
  230. icon.setColorFilter(ContextCompat.getColor(context,
  231. R.color.color_accent_dark));
  232. format = context.getString(R.string.notification_reblog_format);
  233. break;
  234. }
  235. }
  236. String wholeMessage = String.format(format, displayName);
  237. final SpannableStringBuilder str = new SpannableStringBuilder(wholeMessage);
  238. str.setSpan(new StyleSpan(Typeface.BOLD), 0, displayName.length(),
  239. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  240. message.setText(str);
  241. statusContent.setText(status.content);
  242. }
  243. void setupButtons(final NotificationActionListener listener, final String accountId) {
  244. container.setOnClickListener(new View.OnClickListener() {
  245. @Override
  246. public void onClick(View v) {
  247. listener.onViewAccount(accountId);
  248. }
  249. });
  250. }
  251. }
  252. }