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

194 lines
9.8 KiB
Java

package app.fedilab.fedilabtube.drawer;
/* 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>. */
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.LoginActivity;
import app.fedilab.fedilabtube.PeertubeActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.ShowAccountActivity;
import app.fedilab.fedilabtube.client.entities.PeertubeAccountNotification;
import app.fedilab.fedilabtube.client.entities.PeertubeNotification;
import app.fedilab.fedilabtube.client.entities.PeertubeVideoNotification;
import app.fedilab.fedilabtube.helper.Helper;
public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<PeertubeNotification> notifications;
public PeertubeNotificationsListAdapter(List<PeertubeNotification> notifications) {
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;
PeertubeNotification notification = notifications.get(position);
//Follow Notification
PeertubeAccountNotification accountAction = null;
PeertubeVideoNotification videoAction;
if (notification.getPeertubeActorFollow() != null) {
String profileUrl = notification.getPeertubeActorFollow().getFollower().getAvatar();
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeActorFollow().getFollower();
String type = notification.getPeertubeActorFollow().getFollowing().getType();
String message;
if (type != null && type.equals("account")) {
message = context.getString(R.string.peertube_follow_channel, notification.getPeertubeActorFollow().getFollower().getDisplayName(), notification.getPeertubeActorFollow().getFollowing().getDisplayName());
} else {
message = context.getString(R.string.peertube_follow_account, accountAction.getDisplayName());
}
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));
PeertubeAccountNotification finalAccountAction1 = accountAction;
holder.peertube_notif_pp.setOnClickListener(v -> {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", finalAccountAction1.getName() + "@" + finalAccountAction1.getHost());
b.putBoolean("ischannel", true);
intent.putExtras(b);
context.startActivity(intent);
});
} else if (notification.getPeertubeComment() != null) { //Comment Notification
String profileUrl = notification.getPeertubeComment().getPeertubeAccountNotification().getAvatar();
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeComment().getPeertubeAccountNotification();
videoAction = notification.getPeertubeComment().getPeertubeVideoNotification();
String message = context.getString(R.string.peertube_comment_on_video, accountAction.getDisplayName(), videoAction.getName());
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));
PeertubeVideoNotification finalVideoAction1 = videoAction;
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
b.putString("peertube_instance", Helper.getLiveInstance(context));
b.putString("video_id", finalVideoAction1.getUuid());
intent.putExtras(b);
context.startActivity(intent);
});
} else {//Other Notifications
if (notification.getPeertubeVideoNotification() != null && notification.getPeertubeVideoNotification().getPeertubeAccountNotification() != null) {
String profileUrl = notification.getPeertubeVideoNotification().getPeertubeAccountNotification().getAvatar();
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeVideoNotification().getPeertubeAccountNotification();
videoAction = notification.getPeertubeVideoNotification();
String message = "";
if (notification.getType() == LoginActivity.MY_VIDEO_PUBLISHED) {
message = context.getString(R.string.peertube_video_published, videoAction.getName());
} else if (notification.getType() == LoginActivity.MY_VIDEO_IMPORT_ERROR) {
message = context.getString(R.string.peertube_video_import_error, videoAction.getName());
} else if (notification.getType() == LoginActivity.MY_VIDEO_IMPORT_SUCCESS) {
message = context.getString(R.string.peertube_video_import_success, videoAction.getName());
} else if (notification.getType() == LoginActivity.NEW_VIDEO_FROM_SUBSCRIPTION) {
message = context.getString(R.string.peertube_video_from_subscription, accountAction.getDisplayName(), videoAction.getName());
} else if (notification.getType() == LoginActivity.BLACKLIST_ON_MY_VIDEO) {
message = context.getString(R.string.peertube_video_blacklist, videoAction.getName());
} else if (notification.getType() == LoginActivity.UNBLACKLIST_ON_MY_VIDEO) {
message = context.getString(R.string.peertube_video_unblacklist, videoAction.getName());
}
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));
PeertubeVideoNotification finalVideoAction = videoAction;
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
b.putString("peertube_instance", Helper.getLiveInstance(context));
b.putString("video_id", finalVideoAction.getUuid());
intent.putExtras(b);
context.startActivity(intent);
});
}
}
holder.peertube_notif_date.setText(Helper.dateDiff(context, notification.getCreatedAt()));
PeertubeAccountNotification finalAccountAction = accountAction;
holder.peertube_notif_pp.setOnClickListener(v -> {
if (finalAccountAction != null) {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", finalAccountAction.getName() + "@" + finalAccountAction.getHost());
b.putBoolean("ischannel", true);
intent.putExtras(b);
context.startActivity(intent);
}
});
}
@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;
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);
}
public View getView() {
return itemView;
}
}
}