fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/PeertubeNotificationsListAd...

213 lines
11 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2019-01-24 15:44:01 +01:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-01-24 15:44:01 +01: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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-01-24 15:44:01 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-01-24 15:44:01 +01:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2019-01-24 17:41:53 +01:00
import android.os.Build;
2019-01-24 15:44:01 +01:00
import android.os.Bundle;
2019-01-24 17:41:53 +01:00
import android.text.Html;
2019-01-24 15:44:01 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
2019-01-24 15:44:01 +01:00
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.PeertubeActivity;
import app.fedilab.android.activities.ShowAccountActivity;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.PeertubeAccountNotification;
import app.fedilab.android.client.Entities.PeertubeNotification;
import app.fedilab.android.client.Entities.PeertubeVideoNotification;
import app.fedilab.android.fragments.DisplayPeertubeNotificationsFragment;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.helper.Helper;
2019-01-24 15:44:01 +01:00
/**
* Created by Thomas on 23/01/2019.
* Adapter for Peertube notifications
*/
2020-06-16 18:23:13 +02:00
public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
2019-01-24 15:44:01 +01:00
private Context context;
private List<PeertubeNotification> notifications;
2019-01-24 17:41:53 +01:00
2019-09-06 17:55:14 +02:00
public PeertubeNotificationsListAdapter(List<PeertubeNotification> notifications) {
2019-01-24 15:44:01 +01:00
this.notifications = notifications;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
2019-08-19 09:11:23 +02:00
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
2019-01-24 17:41:53 +01:00
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_peertube_notification, parent, false));
2019-01-24 15:44:01 +01:00
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
2019-01-24 17:41:53 +01:00
ViewHolder holder = (ViewHolder) viewHolder;
2019-01-24 15:44:01 +01:00
PeertubeNotification notification = notifications.get(position);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_DARK) {
2019-01-24 15:44:01 +01:00
holder.main_container_trans.setBackgroundColor(ContextCompat.getColor(context, R.color.notif_dark_1));
holder.main_container_trans.setAlpha(.5f);
2019-09-06 17:55:14 +02:00
} else if (theme == Helper.THEME_BLACK) {
2019-01-24 15:44:01 +01:00
holder.main_container_trans.setBackgroundColor(ContextCompat.getColor(context, R.color.notif_black_1));
holder.main_container_trans.setAlpha(.5f);
2019-09-06 17:55:14 +02:00
} else {
2019-01-24 15:44:01 +01:00
holder.main_container_trans.setBackgroundColor(ContextCompat.getColor(context, R.color.notif_light_1));
holder.main_container_trans.setAlpha(.5f);
}
//Follow Notification
PeertubeAccountNotification accountAction = null;
2019-08-19 09:11:23 +02:00
PeertubeVideoNotification videoAction;
2019-09-06 17:55:14 +02:00
if (notification.getPeertubeActorFollow() != null) {
2019-01-24 17:41:53 +01:00
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + notification.getPeertubeActorFollow().getFollower().getAvatar();
2019-09-06 17:55:14 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeActorFollow().getFollower();
2019-01-24 15:44:01 +01:00
String type = notification.getPeertubeActorFollow().getFollowing().getType();
String message;
2019-09-06 17:55:14 +02:00
if (type != null && type.equals("account")) {
2019-01-24 17:41:53 +01:00
message = context.getString(R.string.peertube_follow_channel, notification.getPeertubeActorFollow().getFollower().getDisplayName(), notification.getPeertubeActorFollow().getFollowing().getDisplayName());
2019-09-06 17:55:14 +02:00
} else {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_follow_account, accountAction.getDisplayName());
}
2019-01-24 17:41:53 +01: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));
2019-01-24 15:44:01 +01:00
PeertubeAccountNotification finalAccountAction1 = accountAction;
holder.peertube_notif_pp.setOnClickListener(v -> {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putBoolean("peertubeaccount", true);
b.putString("accountId", finalAccountAction1.getName());
intent.putExtras(b);
context.startActivity(intent);
});
2019-09-06 17:55:14 +02:00
} else if (notification.getPeertubeComment() != null) { //Comment Notification
2019-01-24 17:41:53 +01:00
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + notification.getPeertubeComment().getPeertubeAccountNotification().getAvatar();
2019-08-02 13:41:08 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
2019-01-24 15:44:01 +01:00
accountAction = notification.getPeertubeComment().getPeertubeAccountNotification();
2019-09-06 17:55:14 +02:00
videoAction = notification.getPeertubeComment().getPeertubeVideoNotification();
String message = context.getString(R.string.peertube_comment_on_video, accountAction.getDisplayName(), videoAction.getName());
2019-01-24 17:41:53 +01: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));
2019-01-24 15:44:01 +01:00
PeertubeVideoNotification finalVideoAction1 = videoAction;
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
2019-01-24 17:41:53 +01:00
b.putString("peertube_instance", Helper.getLiveInstance(context));
2019-01-24 15:44:01 +01:00
b.putString("video_id", finalVideoAction1.getUuid());
intent.putExtras(b);
context.startActivity(intent);
});
2019-09-06 17:55:14 +02:00
} else {//Other Notifications
if (notification.getPeertubeVideoNotification() != null && notification.getPeertubeVideoNotification().getPeertubeAccountNotification() != null) {
2019-01-24 17:41:53 +01:00
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + notification.getPeertubeVideoNotification().getPeertubeAccountNotification().getAvatar();
2019-08-02 13:41:08 +02:00
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
2019-01-24 15:44:01 +01:00
accountAction = notification.getPeertubeVideoNotification().getPeertubeAccountNotification();
2019-09-06 17:55:14 +02:00
videoAction = notification.getPeertubeVideoNotification();
2019-01-24 15:44:01 +01:00
String message = "";
2019-09-06 17:55:14 +02:00
if (notification.getType() == DisplayPeertubeNotificationsFragment.MY_VIDEO_PUBLISHED) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_published, videoAction.getName());
2019-09-06 17:55:14 +02:00
} else if (notification.getType() == DisplayPeertubeNotificationsFragment.MY_VIDEO_IMPORT_ERROR) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_import_error, videoAction.getName());
2019-09-06 17:55:14 +02:00
} else if (notification.getType() == DisplayPeertubeNotificationsFragment.MY_VIDEO_IMPORT_SUCCESS) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_import_success, videoAction.getName());
2019-09-06 17:55:14 +02:00
} else if (notification.getType() == DisplayPeertubeNotificationsFragment.NEW_VIDEO_FROM_SUBSCRIPTION) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_from_subscription, accountAction.getDisplayName(), videoAction.getName());
2019-09-06 17:55:14 +02:00
} else if (notification.getType() == DisplayPeertubeNotificationsFragment.BLACKLIST_ON_MY_VIDEO) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_blacklist, videoAction.getName());
2019-09-06 17:55:14 +02:00
} else if (notification.getType() == DisplayPeertubeNotificationsFragment.UNBLACKLIST_ON_MY_VIDEO) {
2019-01-24 15:44:01 +01:00
message = context.getString(R.string.peertube_video_unblacklist, videoAction.getName());
}
2019-01-24 17:41:53 +01: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));
2019-01-24 15:44:01 +01:00
PeertubeVideoNotification finalVideoAction = videoAction;
holder.peertube_notif_message.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
2019-01-24 17:41:53 +01:00
b.putString("peertube_instance", Helper.getLiveInstance(context));
2019-01-24 15:44:01 +01:00
b.putString("video_id", finalVideoAction.getUuid());
intent.putExtras(b);
context.startActivity(intent);
});
}
}
2019-01-24 17:41:53 +01:00
holder.peertube_notif_date.setText(Helper.dateDiff(context, notification.getCreatedAt()));
2019-01-24 15:44:01 +01:00
PeertubeAccountNotification finalAccountAction = accountAction;
holder.peertube_notif_pp.setOnClickListener(v -> {
2019-09-06 17:55:14 +02:00
if (finalAccountAction != null) {
2019-01-24 15:44:01 +01:00
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putBoolean("peertubeaccount", true);
b.putString("accountId", finalAccountAction.getName() + "@" + finalAccountAction.getHost());
2019-01-24 15:44:01 +01:00
intent.putExtras(b);
context.startActivity(intent);
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return notifications.size();
}
2020-03-07 14:16:28 +01:00
static class ViewHolder extends RecyclerView.ViewHolder {
2019-01-24 15:44:01 +01:00
ImageView peertube_notif_pp;
TextView peertube_notif_message, peertube_notif_date;
RelativeLayout main_container_trans;
2019-09-06 17:55:14 +02:00
2019-01-24 15:44:01 +01:00
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);
}
2019-11-15 16:32:25 +01:00
public View getView() {
return itemView;
}
2019-01-24 15:44:01 +01:00
}
}