TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/drawer/PeertubeNotificationsListAd...

258 lines
13 KiB
Java
Raw Normal View History

2020-07-01 15:08:42 +02:00
package app.fedilab.fedilabtube.drawer;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* 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.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-07-01 15:08:42 +02:00
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.MainActivity.badgeCount;
2020-07-01 15:08:42 +02:00
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import app.fedilab.fedilabtube.AccountActivity;
2020-07-01 15:08:42 +02:00
import app.fedilab.fedilabtube.PeertubeActivity;
import app.fedilab.fedilabtube.R;
2020-09-29 17:42:15 +02:00
import app.fedilab.fedilabtube.ShowAccountActivity;
2020-09-26 16:46:51 +02:00
import app.fedilab.fedilabtube.ShowChannelActivity;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.data.AccountData;
2020-09-29 17:42:15 +02:00
import app.fedilab.fedilabtube.client.data.ChannelData;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.data.NotificationData.Notification;
import app.fedilab.fedilabtube.client.entities.Actor;
2020-10-02 18:22:19 +02:00
import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment;
2020-07-01 15:08:42 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-12-01 18:35:54 +01:00
import app.fedilab.fedilabtube.helper.HelperInstance;
2020-07-01 15:08:42 +02:00
public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
2020-10-15 18:59:08 +02:00
private final List<Notification> notifications;
2020-10-17 18:50:20 +02:00
private Context context;
2020-07-01 15:08:42 +02:00
2020-09-25 18:58:04 +02:00
public PeertubeNotificationsListAdapter(List<Notification> notifications) {
2020-07-01 15:08:42 +02:00
this.notifications = notifications;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_peertube_notification, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
ViewHolder holder = (ViewHolder) viewHolder;
2020-09-25 18:58:04 +02:00
Notification notification = notifications.get(position);
2020-07-01 15:08:42 +02:00
//Follow Notification
2020-12-12 14:45:14 +01:00
boolean clickableNotification = true;
2020-10-16 17:03:07 +02:00
holder.peertube_notif_pp.setVisibility(View.VISIBLE);
2020-09-25 18:58:04 +02:00
AccountData.Account accountAction = null;
2020-09-29 17:42:15 +02:00
ChannelData.Channel channelAction = null;
if (notification.isRead()) {
holder.unread.setVisibility(View.INVISIBLE);
} else {
holder.unread.setVisibility(View.VISIBLE);
}
2020-09-25 18:58:04 +02:00
if (notification.getActorFollow() != null) {
2020-09-29 17:42:15 +02:00
String profileUrl = notification.getActorFollow().getFollower().getAvatar() != null ? notification.getActorFollow().getFollower().getAvatar().getPath() : null;
2020-07-01 15:08:42 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
2020-09-25 18:58:04 +02:00
Actor accountActionFollow = notification.getActorFollow().getFollower();
String type = notification.getActorFollow().getFollowing().getType();
2020-07-01 15:08:42 +02:00
String message;
2020-10-16 17:03:07 +02:00
if (type != null && type.compareTo("channel") == 0) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_follow_channel, notification.getActorFollow().getFollower().getDisplayName(), notification.getActorFollow().getFollowing().getDisplayName());
2020-07-01 15:08:42 +02:00
} else {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_follow_account, accountActionFollow.getDisplayName());
2020-07-01 15:08:42 +02:00
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
holder.peertube_notif_message.setText(Html.fromHtml(message));
2020-10-16 17:03:07 +02:00
Actor actor = notification.getActorFollow().getFollower();
accountAction = new AccountData.Account();
accountAction.setAvatar(actor.getAvatar());
accountAction.setDisplayName(actor.getDisplayName());
accountAction.setHost(actor.getHost());
accountAction.setUsername(actor.getName());
holder.peertube_notif_message.setOnClickListener(v -> markAsRead(notification, position));
2020-09-25 18:58:04 +02:00
} else if (notification.getComment() != null) { //Comment Notification
2020-09-29 17:42:15 +02:00
String profileUrl = notification.getComment().getAccount().getAvatar() != null ? notification.getComment().getAccount().getAvatar().getPath() : null;
2020-07-01 15:08:42 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
2020-09-25 18:58:04 +02:00
accountAction = notification.getComment().getAccount();
2020-09-27 16:33:43 +02:00
String message = context.getString(R.string.peertube_comment_on_video, accountAction.getDisplayName(), accountAction.getUsername());
2020-07-01 15:08:42 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
holder.peertube_notif_message.setText(Html.fromHtml(message));
2020-09-25 18:58:04 +02:00
AccountData.Account finalAccountAction1 = accountAction;
2020-07-01 15:08:42 +02:00
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
b.putParcelable("video", notification.getVideo());
2020-09-25 18:58:04 +02:00
b.putString("peertube_instance", finalAccountAction1.getHost());
2020-10-03 18:37:34 +02:00
b.putString("video_id", notification.getComment().getVideo().getId());
2020-10-06 18:49:15 +02:00
b.putString("video_uuid", notification.getComment().getVideo().getUuid());
2020-07-01 15:08:42 +02:00
intent.putExtras(b);
markAsRead(notification, position);
2020-07-01 15:08:42 +02:00
context.startActivity(intent);
});
2020-09-25 18:58:04 +02:00
} else {
2020-09-29 17:42:15 +02:00
String profileUrl = notification.getVideo() != null && notification.getVideo().getChannel().getAvatar() != null ? notification.getVideo().getChannel().getAvatar().getPath() : null;
2020-09-25 18:58:04 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
String message = "";
2020-10-16 11:37:40 +02:00
boolean myVideo = false;
2020-10-16 17:03:07 +02:00
holder.peertube_notif_pp.setVisibility(View.INVISIBLE);
2020-09-25 18:58:04 +02:00
if (notification.getVideo() != null) {
2020-10-02 18:22:19 +02:00
if (notification.getType() == DisplayNotificationsFragment.MY_VIDEO_PUBLISHED) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_video_published, notification.getVideo().getName());
2020-10-16 11:37:40 +02:00
myVideo = true;
2020-10-02 18:22:19 +02:00
} else if (notification.getType() == DisplayNotificationsFragment.MY_VIDEO_IMPORT_ERROR) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_video_import_error, notification.getVideo().getName());
2020-10-16 11:37:40 +02:00
myVideo = true;
2020-10-02 18:22:19 +02:00
} else if (notification.getType() == DisplayNotificationsFragment.MY_VIDEO_IMPORT_SUCCESS) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_video_import_success, notification.getVideo().getName());
2020-10-16 11:37:40 +02:00
myVideo = true;
2020-10-02 18:22:19 +02:00
} else if (notification.getType() == DisplayNotificationsFragment.NEW_VIDEO_FROM_SUBSCRIPTION) {
2020-09-29 17:42:15 +02:00
channelAction = notification.getVideo().getChannel();
message = context.getString(R.string.peertube_video_from_subscription, channelAction.getDisplayName(), notification.getVideo().getName());
2020-10-16 17:03:07 +02:00
holder.peertube_notif_pp.setVisibility(View.VISIBLE);
2020-10-02 18:22:19 +02:00
} else if (notification.getType() == DisplayNotificationsFragment.BLACKLIST_ON_MY_VIDEO) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_video_blacklist, notification.getVideo().getName());
2020-10-16 17:03:07 +02:00
2020-10-02 18:22:19 +02:00
} else if (notification.getType() == DisplayNotificationsFragment.UNBLACKLIST_ON_MY_VIDEO) {
2020-09-25 18:58:04 +02:00
message = context.getString(R.string.peertube_video_unblacklist, notification.getVideo().getName());
2020-07-01 15:08:42 +02:00
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
holder.peertube_notif_message.setText(Html.fromHtml(message));
2020-10-16 11:37:40 +02:00
boolean finalMyVideo = myVideo;
2020-07-01 15:08:42 +02:00
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
b.putParcelable("video", notification.getVideo());
2020-12-01 18:35:54 +01:00
b.putString("peertube_instance", HelperInstance.getLiveInstance(context));
2020-10-16 11:37:40 +02:00
b.putBoolean("isMyVideo", finalMyVideo);
2020-10-03 18:37:34 +02:00
b.putString("video_id", notification.getVideo().getId());
2020-10-06 18:49:15 +02:00
b.putString("video_uuid", notification.getVideo().getUuid());
2020-07-01 15:08:42 +02:00
intent.putExtras(b);
context.startActivity(intent);
markAsRead(notification, position);
2020-07-01 15:08:42 +02:00
});
2020-10-03 18:37:34 +02:00
} else if (notification.getVideoAbuse() != null && notification.getVideoAbuse().getVideo() != null) {
2020-10-02 18:22:19 +02:00
message = context.getString(R.string.peertube_video_abuse, notification.getVideoAbuse().getVideo().getName());
2020-12-12 14:45:14 +01:00
clickableNotification = false;
2020-10-02 18:22:19 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2020-10-15 18:59:08 +02:00
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
holder.peertube_notif_message.setText(Html.fromHtml(message));
holder.peertube_notif_message.setOnClickListener(v -> markAsRead(notification, position));
2020-10-17 18:50:20 +02:00
} else if (notification.getAbuse() != null) {
2020-12-12 14:45:14 +01:00
clickableNotification = false;
2020-10-15 18:59:08 +02:00
if (notification.getType() == DisplayNotificationsFragment.MY_VIDEO_REPPORT_SUCCESS) {
message = context.getString(R.string.peertube_video_report_success, notification.getAbuse().getId());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2020-10-02 18:22:19 +02:00
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
holder.peertube_notif_message.setText(Html.fromHtml(message));
holder.peertube_notif_message.setOnClickListener(v -> markAsRead(notification, position));
2020-07-01 15:08:42 +02:00
}
}
holder.peertube_notif_date.setText(Helper.dateDiff(context, notification.getCreatedAt()));
2020-09-25 18:58:04 +02:00
AccountData.Account finalAccountAction = accountAction;
2020-09-29 17:42:15 +02:00
ChannelData.Channel finalChannelAction = channelAction;
2020-12-12 14:45:14 +01:00
if (clickableNotification) {
holder.peertube_notif_pp.setOnClickListener(v -> {
Bundle b = new Bundle();
Intent intent = null;
if (finalAccountAction != null) {
intent = new Intent(context, ShowAccountActivity.class);
b.putParcelable("account", finalAccountAction);
b.putString("accountAcct", finalAccountAction.getUsername() + "@" + finalAccountAction.getHost());
} else if (finalChannelAction != null) {
intent = new Intent(context, ShowChannelActivity.class);
b.putParcelable("channel", finalChannelAction);
}
if (intent != null) {
intent.putExtras(b);
context.startActivity(intent);
}
});
}
2020-07-01 15:08:42 +02:00
}
private void markAsRead(Notification notification, int position) {
if (!notification.isRead()) {
notification.setRead(true);
badgeCount--;
if (context instanceof AccountActivity) {
((AccountActivity) context).updateCounter();
}
notifyItemChanged(position);
new Thread(() -> new RetrofitPeertubeAPI(context).markAsRead(notification.getId())).start();
}
}
2020-07-01 15:08:42 +02:00
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return notifications.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView peertube_notif_pp;
TextView peertube_notif_message, peertube_notif_date, unread;
2020-07-01 15:08:42 +02:00
RelativeLayout main_container_trans;
public ViewHolder(View itemView) {
super(itemView);
peertube_notif_pp = itemView.findViewById(R.id.peertube_notif_pp);
peertube_notif_message = itemView.findViewById(R.id.peertube_notif_message);
peertube_notif_date = itemView.findViewById(R.id.peertube_notif_date);
main_container_trans = itemView.findViewById(R.id.container_trans);
unread = itemView.findViewById(R.id.unread);
2020-07-01 15:08:42 +02:00
}
public View getView() {
return itemView;
}
}
}