Yuito-app-android/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java

324 lines
13 KiB
Java
Raw Normal View History

2017-01-20 09:09:10 +01:00
/* Copyright 2017 Andrew Dawson
*
2017-04-10 02:12:31 +02:00
* This file is a part of Tusky.
2017-01-20 09:09:10 +01:00
*
2017-04-10 02:12:31 +02: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 09:09:10 +01:00
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-04-10 02:12:31 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
2017-01-20 09:09:10 +01:00
*
2017-04-10 02:12:31 +02: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 09:09:10 +01:00
2017-05-05 00:55:34 +02:00
package com.keylesspalace.tusky.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
2017-03-07 20:42:01 +01:00
import android.graphics.Typeface;
import android.support.annotation.Nullable;
2017-03-07 20:42:01 +01:00
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
2017-03-07 20:42:01 +01:00
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
2017-05-05 00:55:34 +02:00
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.entity.Notification;
2017-05-05 00:55:34 +02:00
import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.view.RoundedTransformation;
import com.keylesspalace.tusky.viewdata.NotificationViewData;
import com.keylesspalace.tusky.viewdata.StatusViewData;
2017-03-07 20:42:01 +01:00
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class NotificationsAdapter extends RecyclerView.Adapter {
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;
private List<NotificationViewData> notifications;
private StatusActionListener statusListener;
private NotificationActionListener notificationActionListener;
private FooterViewHolder.State footerState;
private boolean mediaPreviewEnabled;
2017-05-05 00:55:34 +02:00
public NotificationsAdapter(StatusActionListener statusListener,
NotificationActionListener notificationActionListener) {
super();
notifications = new ArrayList<>();
this.statusListener = statusListener;
this.notificationActionListener = notificationActionListener;
footerState = FooterViewHolder.State.END;
mediaPreviewEnabled = true;
2017-04-15 19:25:39 +02:00
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
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: {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_footer, parent, false);
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);
}
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (position < notifications.size()) {
NotificationViewData notification = notifications.get(position);
Notification.Type type = notification.getType();
switch (type) {
case MENTION: {
StatusViewHolder holder = (StatusViewHolder) viewHolder;
StatusViewData status = notification.getStatusViewData();
holder.setupWithStatus(status,
statusListener, mediaPreviewEnabled);
break;
}
case FAVOURITE:
case REBLOG: {
StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder;
holder.setMessage(type, notification.getAccount().getDisplayName(),
notification.getStatusViewData());
holder.setupButtons(notificationActionListener, notification.getAccount().id);
holder.setAvatars(notification.getStatusViewData().getAvatar(),
notification.getAccount().avatar);
break;
}
case FOLLOW: {
FollowViewHolder holder = (FollowViewHolder) viewHolder;
holder.setMessage(notification.getAccount().getDisplayName(),
notification.getAccount().username, notification.getAccount().avatar);
holder.setupButtons(notificationActionListener, notification.getAccount().id);
break;
}
}
} else {
FooterViewHolder holder = (FooterViewHolder) viewHolder;
holder.setState(footerState);
}
}
@Override
public int getItemCount() {
return notifications.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (position == notifications.size()) {
return VIEW_TYPE_FOOTER;
} else {
NotificationViewData notification = notifications.get(position);
switch (notification.getType()) {
default:
case MENTION: {
return VIEW_TYPE_MENTION;
}
case FAVOURITE:
case REBLOG: {
return VIEW_TYPE_STATUS_NOTIFICATION;
}
case FOLLOW: {
return VIEW_TYPE_FOLLOW;
}
}
}
}
public void update(@Nullable List<NotificationViewData> newNotifications) {
if (newNotifications == null || newNotifications.isEmpty()) {
return;
}
notifications.clear();
notifications.addAll(newNotifications);
notifyDataSetChanged();
}
public void updateItemWithNotify(int position, NotificationViewData notification,
boolean notifyAdapter) {
notifications.set(position, notification);
if (notifyAdapter) notifyDataSetChanged();
}
public void addItems(List<NotificationViewData> newNotifications) {
notifications.addAll(newNotifications);
notifyItemRangeInserted(notifications.size(), newNotifications.size());
}
public void clear() {
notifications.clear();
notifyDataSetChanged();
}
public void setFooterState(FooterViewHolder.State newFooterState) {
footerState = newFooterState;
}
public void setMediaPreviewEnabled(boolean enabled) {
mediaPreviewEnabled = enabled;
}
2017-05-05 00:55:34 +02:00
public interface NotificationActionListener {
void onViewAccount(String id);
}
private static class FollowViewHolder extends RecyclerView.ViewHolder {
private TextView message;
private TextView usernameView;
private TextView displayNameView;
2017-03-07 20:42:01 +01:00
private ImageView avatar;
FollowViewHolder(View itemView) {
super(itemView);
message = itemView.findViewById(R.id.notification_text);
usernameView = itemView.findViewById(R.id.notification_username);
displayNameView = itemView.findViewById(R.id.notification_display_name);
avatar = itemView.findViewById(R.id.notification_avatar);
}
void setMessage(String displayName, String username, String avatarUrl) {
Context context = message.getContext();
String format = context.getString(R.string.notification_follow_format);
String wholeMessage = String.format(format, displayName);
message.setText(wholeMessage);
format = context.getString(R.string.status_username_format);
String wholeUsername = String.format(format, username);
usernameView.setText(wholeUsername);
displayNameView.setText(displayName);
2017-03-07 20:42:01 +01:00
Picasso.with(context)
.load(avatarUrl)
.fit()
.transform(new RoundedTransformation(7, 0))
2017-03-07 20:42:01 +01:00
.placeholder(R.drawable.avatar_default)
.into(avatar);
}
void setupButtons(final NotificationActionListener listener, final String accountId) {
avatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onViewAccount(accountId);
}
});
}
}
private static class StatusNotificationViewHolder extends RecyclerView.ViewHolder {
private TextView message;
private ImageView icon;
private TextView statusContent;
private ViewGroup container;
private ImageView statusAvatar;
private ImageView notificationAvatar;
StatusNotificationViewHolder(View itemView) {
super(itemView);
message = itemView.findViewById(R.id.notification_text);
icon = itemView.findViewById(R.id.notification_icon);
statusContent = itemView.findViewById(R.id.notification_content);
container = itemView.findViewById(R.id.notification_container);
statusAvatar = itemView.findViewById(R.id.notification_status_avatar);
notificationAvatar = itemView.findViewById(R.id.notification_notification_avatar);
int darkerFilter = Color.rgb(123, 123, 123);
statusAvatar.setColorFilter(darkerFilter, PorterDuff.Mode.MULTIPLY);
notificationAvatar.setColorFilter(darkerFilter, PorterDuff.Mode.MULTIPLY);
}
void setMessage(Notification.Type type, String displayName, StatusViewData status) {
Context context = message.getContext();
String format;
switch (type) {
default:
case FAVOURITE: {
2017-03-07 20:42:01 +01:00
icon.setImageResource(R.drawable.ic_star_24dp);
icon.setColorFilter(ContextCompat.getColor(context,
R.color.status_favourite_button_marked_dark));
format = context.getString(R.string.notification_favourite_format);
break;
}
case REBLOG: {
2017-03-07 20:42:01 +01:00
icon.setImageResource(R.drawable.ic_repeat_24dp);
icon.setColorFilter(ContextCompat.getColor(context,
R.color.color_accent_dark));
format = context.getString(R.string.notification_reblog_format);
break;
}
}
String wholeMessage = String.format(format, displayName);
2017-03-07 20:42:01 +01:00
final SpannableStringBuilder str = new SpannableStringBuilder(wholeMessage);
str.setSpan(new StyleSpan(Typeface.BOLD), 0, displayName.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2017-03-07 20:42:01 +01:00
message.setText(str);
statusContent.setText(status.getContent());
}
void setupButtons(final NotificationActionListener listener, final String accountId) {
container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onViewAccount(accountId);
}
});
}
void setAvatars(@Nullable String statusAvatarUrl, @Nullable String notificationAvatarUrl) {
Context context = statusAvatar.getContext();
if (statusAvatarUrl == null || statusAvatarUrl.isEmpty()) {
statusAvatar.setImageResource(R.drawable.avatar_default);
} else {
Picasso.with(context)
.load(statusAvatarUrl)
.placeholder(R.drawable.avatar_default)
.transform(new RoundedTransformation(7, 0))
.into(statusAvatar);
}
if (notificationAvatarUrl == null || notificationAvatarUrl.isEmpty()) {
notificationAvatar.setVisibility(View.GONE);
} else {
notificationAvatar.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(notificationAvatarUrl)
.fit()
.transform(new RoundedTransformation(7, 0))
.into(notificationAvatar);
}
}
}
}