2017-01-20 19:09:10 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-01-20 19:09:10 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-01-20 19:09:10 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-20 19:09:10 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-01-20 19:09:10 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-03-08 06:42:01 +11:00
|
|
|
import android.graphics.Typeface;
|
2017-01-23 16:19:30 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2017-03-08 06:42:01 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-03-08 06:42:01 +11:00
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.Spanned;
|
2017-04-05 06:36:42 +10:00
|
|
|
import android.text.style.StyleSpan;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-01-23 16:19:30 +11:00
|
|
|
import android.widget.ImageView;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-03-08 06:42:01 +11:00
|
|
|
import com.squareup.picasso.Picasso;
|
2017-02-27 12:14:13 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
import java.util.ArrayList;
|
2017-04-22 10:58:44 +10:00
|
|
|
import java.util.Iterator;
|
2017-01-08 09:24:02 +11:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
class NotificationsAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
|
2017-01-23 16:19:30 +11:00
|
|
|
private static final int VIEW_TYPE_MENTION = 0;
|
|
|
|
private static final int VIEW_TYPE_FOOTER = 1;
|
|
|
|
private static final int VIEW_TYPE_STATUS_NOTIFICATION = 2;
|
|
|
|
private static final int VIEW_TYPE_FOLLOW = 3;
|
|
|
|
|
2017-04-16 03:25:39 +10:00
|
|
|
enum FooterState {
|
|
|
|
EMPTY,
|
|
|
|
END,
|
|
|
|
LOADING
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
private List<Notification> notifications;
|
|
|
|
private StatusActionListener statusListener;
|
2017-03-21 07:34:23 +11:00
|
|
|
private NotificationActionListener notificationActionListener;
|
2017-04-16 03:25:39 +10:00
|
|
|
private FooterState footerState = FooterState.END;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
2017-03-21 07:34:23 +11:00
|
|
|
NotificationsAdapter(StatusActionListener statusListener,
|
|
|
|
NotificationActionListener notificationActionListener) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super();
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
this.statusListener = statusListener;
|
2017-03-21 07:34:23 +11:00
|
|
|
this.notificationActionListener = notificationActionListener;
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-04-16 03:25:39 +10:00
|
|
|
|
2017-04-22 10:58:44 +10:00
|
|
|
void setFooterState(FooterState newFooterState) {
|
2017-04-16 03:25:39 +10:00
|
|
|
FooterState oldValue = footerState;
|
|
|
|
footerState = newFooterState;
|
|
|
|
if (footerState != oldValue) {
|
|
|
|
notifyItemChanged(notifications.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
2017-01-23 16:19:30 +11:00
|
|
|
switch (viewType) {
|
|
|
|
default:
|
|
|
|
case VIEW_TYPE_MENTION: {
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_status, parent, false);
|
|
|
|
return new StatusViewHolder(view);
|
|
|
|
}
|
|
|
|
case VIEW_TYPE_FOOTER: {
|
2017-04-16 03:25:39 +10:00
|
|
|
View view;
|
|
|
|
switch (footerState) {
|
|
|
|
default:
|
|
|
|
case LOADING:
|
|
|
|
view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_footer, parent, false);
|
|
|
|
break;
|
|
|
|
case END: {
|
|
|
|
view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_footer_end, parent, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EMPTY: {
|
|
|
|
view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_footer_empty, parent, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
return new FooterViewHolder(view);
|
|
|
|
}
|
|
|
|
case VIEW_TYPE_STATUS_NOTIFICATION: {
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_status_notification, parent, false);
|
|
|
|
return new StatusNotificationViewHolder(view);
|
|
|
|
}
|
|
|
|
case VIEW_TYPE_FOLLOW: {
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_follow, parent, false);
|
|
|
|
return new FollowViewHolder(view);
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
if (position < notifications.size()) {
|
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
Notification.Type type = notification.type;
|
2017-01-23 16:19:30 +11:00
|
|
|
switch (type) {
|
|
|
|
case MENTION: {
|
|
|
|
StatusViewHolder holder = (StatusViewHolder) viewHolder;
|
2017-03-09 10:27:37 +11:00
|
|
|
Status status = notification.status;
|
2017-03-08 15:52:17 +11:00
|
|
|
holder.setupWithStatus(status, statusListener);
|
2017-01-23 16:19:30 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FAVOURITE:
|
|
|
|
case REBLOG: {
|
|
|
|
StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder;
|
2017-03-11 09:31:08 +11:00
|
|
|
holder.setMessage(type, notification.account.getDisplayName(),
|
2017-03-09 10:27:37 +11:00
|
|
|
notification.status);
|
2017-03-21 07:34:23 +11:00
|
|
|
holder.setupButtons(notificationActionListener, notification.account.id);
|
2017-01-23 16:19:30 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FOLLOW: {
|
|
|
|
FollowViewHolder holder = (FollowViewHolder) viewHolder;
|
2017-03-11 09:31:08 +11:00
|
|
|
holder.setMessage(notification.account.getDisplayName(), notification.account.username,
|
2017-03-09 10:27:37 +11:00
|
|
|
notification.account.avatar);
|
2017-03-21 07:34:23 +11:00
|
|
|
holder.setupButtons(notificationActionListener, notification.account.id);
|
2017-01-23 16:19:30 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2017-01-23 16:19:30 +11:00
|
|
|
return notifications.size() + 1;
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
if (position == notifications.size()) {
|
|
|
|
return VIEW_TYPE_FOOTER;
|
|
|
|
} else {
|
|
|
|
Notification notification = notifications.get(position);
|
2017-03-09 10:27:37 +11:00
|
|
|
switch (notification.type) {
|
2017-01-23 16:19:30 +11:00
|
|
|
default:
|
|
|
|
case MENTION: {
|
|
|
|
return VIEW_TYPE_MENTION;
|
|
|
|
}
|
|
|
|
case FAVOURITE:
|
|
|
|
case REBLOG: {
|
|
|
|
return VIEW_TYPE_STATUS_NOTIFICATION;
|
|
|
|
}
|
|
|
|
case FOLLOW: {
|
|
|
|
return VIEW_TYPE_FOLLOW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public @Nullable Notification getItem(int position) {
|
|
|
|
if (position >= 0 && position < notifications.size()) {
|
|
|
|
return notifications.get(position);
|
|
|
|
}
|
|
|
|
return null;
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
2017-03-20 18:03:03 +11:00
|
|
|
void update(List<Notification> newNotifications) {
|
|
|
|
if (newNotifications == null || newNotifications.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (notifications.isEmpty()) {
|
|
|
|
notifications = newNotifications;
|
2017-01-08 09:24:02 +11:00
|
|
|
} else {
|
2017-03-20 18:03:03 +11:00
|
|
|
int index = notifications.indexOf(newNotifications.get(newNotifications.size() - 1));
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
notifications.remove(0);
|
|
|
|
}
|
|
|
|
int newIndex = newNotifications.indexOf(notifications.get(0));
|
|
|
|
if (newIndex == -1) {
|
|
|
|
notifications.addAll(0, newNotifications);
|
2017-01-08 09:24:02 +11:00
|
|
|
} else {
|
2017-03-20 18:03:03 +11:00
|
|
|
notifications.addAll(0, newNotifications.subList(0, newIndex));
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
void addItems(List<Notification> new_notifications) {
|
2017-01-08 09:24:02 +11:00
|
|
|
int end = notifications.size();
|
|
|
|
notifications.addAll(new_notifications);
|
|
|
|
notifyItemRangeInserted(end, new_notifications.size());
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
public void removeItem(int position) {
|
|
|
|
notifications.remove(position);
|
|
|
|
notifyItemChanged(position);
|
|
|
|
}
|
|
|
|
|
2017-04-22 10:58:44 +10:00
|
|
|
public void removeAllByAccountId(String id) {
|
|
|
|
for (int i = 0; i < notifications.size();) {
|
|
|
|
Notification notification = notifications.get(i);
|
|
|
|
if (id.equals(notification.account.id)) {
|
|
|
|
notifications.remove(i);
|
|
|
|
notifyItemRemoved(i);
|
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:34:23 +11:00
|
|
|
interface NotificationActionListener {
|
2017-03-03 11:25:35 +11:00
|
|
|
void onViewAccount(String id);
|
2017-02-27 12:14:13 +11:00
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
private static class FollowViewHolder extends RecyclerView.ViewHolder {
|
2017-01-08 09:24:02 +11:00
|
|
|
private TextView message;
|
2017-02-27 12:14:13 +11:00
|
|
|
private TextView usernameView;
|
|
|
|
private TextView displayNameView;
|
2017-03-08 06:42:01 +11:00
|
|
|
private ImageView avatar;
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
FollowViewHolder(View itemView) {
|
2017-01-08 09:24:02 +11:00
|
|
|
super(itemView);
|
|
|
|
message = (TextView) itemView.findViewById(R.id.notification_text);
|
2017-02-27 12:14:13 +11:00
|
|
|
usernameView = (TextView) itemView.findViewById(R.id.notification_username);
|
|
|
|
displayNameView = (TextView) itemView.findViewById(R.id.notification_display_name);
|
2017-03-08 06:42:01 +11:00
|
|
|
avatar = (ImageView) itemView.findViewById(R.id.notification_avatar);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:14:13 +11:00
|
|
|
void setMessage(String displayName, String username, String avatarUrl) {
|
2017-01-08 09:24:02 +11:00
|
|
|
Context context = message.getContext();
|
2017-02-27 12:14:13 +11:00
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
String format = context.getString(R.string.notification_follow_format);
|
|
|
|
String wholeMessage = String.format(format, displayName);
|
|
|
|
message.setText(wholeMessage);
|
2017-02-27 12:14:13 +11:00
|
|
|
|
|
|
|
format = context.getString(R.string.status_username_format);
|
|
|
|
String wholeUsername = String.format(format, username);
|
|
|
|
usernameView.setText(wholeUsername);
|
|
|
|
|
|
|
|
displayNameView.setText(displayName);
|
|
|
|
|
2017-03-08 06:42:01 +11:00
|
|
|
Picasso.with(context)
|
|
|
|
.load(avatarUrl)
|
|
|
|
.placeholder(R.drawable.avatar_default)
|
|
|
|
.error(R.drawable.avatar_error)
|
|
|
|
.into(avatar);
|
2017-02-27 12:14:13 +11:00
|
|
|
}
|
|
|
|
|
2017-03-21 07:34:23 +11:00
|
|
|
void setupButtons(final NotificationActionListener listener, final String accountId) {
|
2017-02-27 12:14:13 +11:00
|
|
|
avatar.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-03-03 11:25:35 +11:00
|
|
|
listener.onViewAccount(accountId);
|
2017-02-27 12:14:13 +11:00
|
|
|
}
|
|
|
|
});
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
private static class StatusNotificationViewHolder extends RecyclerView.ViewHolder {
|
2017-01-23 16:19:30 +11:00
|
|
|
private TextView message;
|
|
|
|
private ImageView icon;
|
|
|
|
private TextView statusContent;
|
2017-03-21 07:34:23 +11:00
|
|
|
private ViewGroup container;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
StatusNotificationViewHolder(View itemView) {
|
2017-01-23 16:19:30 +11:00
|
|
|
super(itemView);
|
|
|
|
message = (TextView) itemView.findViewById(R.id.notification_text);
|
|
|
|
icon = (ImageView) itemView.findViewById(R.id.notification_icon);
|
|
|
|
statusContent = (TextView) itemView.findViewById(R.id.notification_content);
|
2017-03-21 07:34:23 +11:00
|
|
|
container = (ViewGroup) itemView.findViewById(R.id.notification_container);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
void setMessage(Notification.Type type, String displayName, Status status) {
|
2017-01-23 16:19:30 +11:00
|
|
|
Context context = message.getContext();
|
|
|
|
String format;
|
2017-01-08 09:24:02 +11:00
|
|
|
switch (type) {
|
2017-01-23 16:19:30 +11:00
|
|
|
default:
|
2017-01-08 09:24:02 +11:00
|
|
|
case FAVOURITE: {
|
2017-03-08 06:42:01 +11:00
|
|
|
icon.setImageResource(R.drawable.ic_star_24dp);
|
2017-03-08 14:11:03 +11:00
|
|
|
icon.setColorFilter(ContextCompat.getColor(context,
|
|
|
|
R.color.status_favourite_button_marked_dark));
|
2017-01-23 16:19:30 +11:00
|
|
|
format = context.getString(R.string.notification_favourite_format);
|
2017-01-08 09:24:02 +11:00
|
|
|
break;
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
case REBLOG: {
|
2017-03-08 06:42:01 +11:00
|
|
|
icon.setImageResource(R.drawable.ic_repeat_24dp);
|
2017-03-08 14:11:03 +11:00
|
|
|
icon.setColorFilter(ContextCompat.getColor(context,
|
|
|
|
R.color.color_accent_dark));
|
2017-01-23 16:19:30 +11:00
|
|
|
format = context.getString(R.string.notification_reblog_format);
|
2017-01-08 09:24:02 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
String wholeMessage = String.format(format, displayName);
|
2017-03-08 06:42:01 +11:00
|
|
|
final SpannableStringBuilder str = new SpannableStringBuilder(wholeMessage);
|
2017-04-05 06:36:42 +10:00
|
|
|
str.setSpan(new StyleSpan(Typeface.BOLD), 0, displayName.length(),
|
2017-03-08 14:11:03 +11:00
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2017-03-08 06:42:01 +11:00
|
|
|
message.setText(str);
|
2017-03-09 10:27:37 +11:00
|
|
|
statusContent.setText(status.content);
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
2017-03-21 07:34:23 +11:00
|
|
|
|
|
|
|
void setupButtons(final NotificationActionListener listener, final String accountId) {
|
|
|
|
container.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onViewAccount(accountId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
}
|
|
|
|
}
|