Frontend notifications

This commit is contained in:
stom79 2019-01-24 17:41:53 +01:00
parent aa7d0236f9
commit 88f8637e23
5 changed files with 115 additions and 150 deletions

View File

@ -1483,7 +1483,7 @@ public class PeertubeAPI {
* @param resobj JSONObject
* @return Peertube
*/
public static PeertubeNotification parsePeertubeNotifications(Context context,JSONObject resobj){
private static PeertubeNotification parsePeertubeNotifications(Context context,JSONObject resobj){
PeertubeNotification peertubeNotification = new PeertubeNotification();
try {
peertubeNotification.setId(resobj.get("id").toString());
@ -1495,19 +1495,25 @@ public class PeertubeAPI {
if( resobj.has("comment")){
PeertubeComment peertubeComment = new PeertubeComment();
JSONObject comment = resobj.getJSONObject("comment");
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
peertubeAccountNotification.setDisplayName(comment.get("displayName").toString());
peertubeAccountNotification.setName(comment.get("name").toString());
peertubeAccountNotification.setId(comment.get("id").toString());
if( comment.has("avatar")){
peertubeAccountNotification.setAvatar(comment.getJSONObject("avatar").get("path").toString());
if( comment.has("account")){
JSONObject account = comment.getJSONObject("account");
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
peertubeAccountNotification.setDisplayName(account.get("displayName").toString());
peertubeAccountNotification.setName(account.get("name").toString());
peertubeAccountNotification.setId(account.get("id").toString());
peertubeAccountNotification.setAvatar(account.getJSONObject("avatar").get("path").toString());
peertubeComment.setPeertubeAccountNotification(peertubeAccountNotification);
}
peertubeComment.setPeertubeAccountNotification(peertubeAccountNotification);
PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
peertubeVideoNotification.setUuid(comment.get("uuid").toString());
peertubeVideoNotification.setName(comment.get("name").toString());
peertubeVideoNotification.setId(comment.get("id").toString());
peertubeComment.setPeertubeVideoNotification(peertubeVideoNotification);
if( comment.has("video")){
JSONObject video = comment.getJSONObject("video");
PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
peertubeVideoNotification.setUuid(video.get("uuid").toString());
peertubeVideoNotification.setName(video.get("name").toString());
peertubeVideoNotification.setId(video.get("id").toString());
peertubeComment.setPeertubeVideoNotification(peertubeVideoNotification);
}
peertubeComment.setId(comment.get("id").toString());
peertubeComment.setThreadId(comment.get("threadId").toString());
peertubeNotification.setPeertubeComment(peertubeComment);
}
@ -1519,7 +1525,7 @@ public class PeertubeAPI {
peertubeVideoNotification.setId(video.get("id").toString());
if( video.has("channel")){
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
JSONObject channel = resobj.getJSONObject("video");
JSONObject channel = video.getJSONObject("channel");
peertubeAccountNotification.setDisplayName(channel.get("displayName").toString());
peertubeAccountNotification.setName(channel.get("name").toString());
peertubeAccountNotification.setId(channel.get("id").toString());
@ -1550,7 +1556,9 @@ public class PeertubeAPI {
PeertubeAccountNotification peertubeAccounFollowingNotification = new PeertubeAccountNotification();
peertubeAccounFollowingNotification.setDisplayName(following.get("displayName").toString());
peertubeAccounFollowingNotification.setName(following.get("name").toString());
peertubeAccounFollowingNotification.setId(following.get("id").toString());
try {
peertubeAccounFollowingNotification.setId(following.get("id").toString());
}catch (Exception ignored){}
if( following.has("avatar")){
peertubeAccounFollowingNotification.setAvatar(following.getJSONObject("avatar").get("path").toString());
}

View File

@ -17,10 +17,12 @@ package fr.gouv.etalab.mastodon.drawers;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -56,7 +58,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter {
private List<PeertubeNotification> notifications;
private LayoutInflater layoutInflater;
private PeertubeNotificationsListAdapter peertubeNotificationsListAdapter;
private PeertubeNotificationsListAdapter.ViewHolder holder;
private int style;
public PeertubeNotificationsListAdapter(Context context, List<PeertubeNotification> notifications){
@ -71,13 +73,13 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_notification, parent, false));
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_peertube_notification, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
holder = (PeertubeNotificationsListAdapter.ViewHolder) viewHolder;
ViewHolder holder = (ViewHolder) viewHolder;
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);
@ -102,17 +104,21 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter {
PeertubeAccountNotification accountAction = null;
PeertubeVideoNotification videoAction = null;
if( notification.getPeertubeActorFollow() != null){
Helper.loadGiF(context, notification.getPeertubeActorFollow().getFollower().getAvatar(), holder.peertube_notif_pp);
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + 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.equals("account")){
message = context.getString(R.string.peertube_follow_channel, accountAction.getDisplayName(), notification.getPeertubeActorFollow().getFollowing().getDisplayName());
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());
}
holder.peertube_notif_message.setText(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
holder.peertube_notif_message.setText(Html.fromHtml(message));
PeertubeAccountNotification finalAccountAction1 = accountAction;
holder.peertube_notif_pp.setOnClickListener(v -> {
Intent intent = new Intent(context, ShowAccountActivity.class);
@ -122,22 +128,31 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter {
intent.putExtras(b);
context.startActivity(intent);
});
}else if( notification.getPeertubeComment() != null){ //Comment Notification
Helper.loadGiF(context, notification.getPeertubeComment().getPeertubeAccountNotification().getAvatar(), holder.peertube_notif_pp);
}else if( notification.getPeertubeComment() != null){ //Comment Notification
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + notification.getPeertubeComment().getPeertubeAccountNotification().getAvatar();
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeComment().getPeertubeAccountNotification();
videoAction = notification.getPeertubeComment().getPeertubeVideoNotification();
holder.peertube_notif_message.setText(context.getString(R.string.peertube_comment_on_video,accountAction.getDisplayName(), videoAction.getName()));
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
//noinspection deprecation
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){
Helper.loadGiF(context, notification.getPeertubeVideoNotification().getPeertubeAccountNotification().getAvatar(), holder.peertube_notif_pp);
String profileUrl = Helper.getLiveInstanceWithProtocol(context) + notification.getPeertubeVideoNotification().getPeertubeAccountNotification().getAvatar();
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
accountAction = notification.getPeertubeVideoNotification().getPeertubeAccountNotification();
videoAction = notification.getPeertubeVideoNotification();
String message = "";
@ -154,18 +169,23 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter {
}else if(notification.getType() == DisplayPeertubeNotificationsFragment.UNBLACKLIST_ON_MY_VIDEO){
message = context.getString(R.string.peertube_video_unblacklist, videoAction.getName());
}
holder.peertube_notif_message.setText(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
holder.peertube_notif_message.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
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){

View File

@ -38,14 +38,12 @@ import java.util.List;
import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeNotificationsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
import fr.gouv.etalab.mastodon.client.Entities.PeertubeNotification;
import fr.gouv.etalab.mastodon.drawers.PeertubeNotificationsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeNotificationsInterface;
@ -53,16 +51,16 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeNotificationsInterfa
* Created by Thomas on 24/01/2019.
* Fragment to display peertube notifications
*/
public class DisplayPeertubeNotificationsFragment extends Fragment implements OnRetrieveMissingNotificationsInterface, OnRetrievePeertubeNotificationsInterface {
public class DisplayPeertubeNotificationsFragment extends Fragment implements OnRetrievePeertubeNotificationsInterface {
private boolean flag_loading;
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
private NotificationsListAdapter notificationsListAdapter;
private PeertubeNotificationsListAdapter notificationsListAdapter;
private String max_id;
private List<Notification> notifications;
private List<PeertubeNotification> notifications;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
@ -113,7 +111,7 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, context!=null?Helper.getLiveInstance(context):null);
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments,this.notifications);
notificationsListAdapter = new PeertubeNotificationsListAdapter(context,this.notifications);
lv_notifications.setAdapter(notificationsListAdapter);
mLayoutManager = new LinearLayoutManager(context);
lv_notifications.setLayoutManager(mLayoutManager);
@ -145,15 +143,12 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
firstLoad = true;
flag_loading = true;
swiped = true;
MainActivity.countNewNotifications = 0;
try {
((MainActivity) context).updateNotifCounter();
}catch (Exception ignored){}
String sinceId = null;
if( notifications != null && notifications.size() > 0 )
sinceId = notifications.get(0).getId();
if( context != null)
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( context != null) {
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -216,26 +211,6 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
/**
* Called from main activity in onResume to retrieve missing notifications
* @param sinceId String
*/
public void retrieveMissingNotifications(String sinceId){
asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if( context == null)
return;
//Store last notification id to avoid to notify for those that have been already seen
if (visible && notifications != null && notifications.size() > 0) {
retrieveMissingNotifications(notifications.get(0).getId());
updateNotificationLastId(notifications.get(0).getId());
}
}
public void scrollToTop(){
if( lv_notifications != null)
lv_notifications.setAdapter(notificationsListAdapter);
@ -252,60 +227,13 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
firstLoad = true;
flag_loading = true;
swiped = true;
MainActivity.countNewNotifications = 0;
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, null, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public void refresh(Notification notification){
if( context == null)
return;
if( notification != null){
//Makes sure the notifications is not already displayed
if( !this.notifications.contains(notification)) {
//Update the id of the last notification retrieved
MainActivity.lastNotificationId = notification.getId();
notifications.add(0, notification);
MainActivity.countNewNotifications++;
try {
((MainActivity) context).updateNotifCounter();
}catch (Exception ignored){}
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem > 0)
notificationsListAdapter.notifyItemInserted(0);
else
notificationsListAdapter.notifyDataSetChanged();
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
}
}
@Override
public void onRetrieveMissingNotifications(List<Notification> notifications) {
flag_loading = false;
swipeRefreshLayout.setRefreshing(false);
if( this.notifications != null && this.notifications.size() > 0){
notificationsListAdapter.notifyItemRangeChanged(0,this.notifications.size());
}
if( notifications != null && notifications.size() > 0) {
int inserted = 0;
for (int i = notifications.size()-1 ; i >= 0 ; i--) {
if (this.notifications != null && this.notifications.size() == 0 ||
Long.parseLong(notifications.get(i).getId()) > Long.parseLong(this.notifications.get(0).getId())) {
MainActivity.countNewNotifications++;
this.notifications.add(0, notifications.get(i));
inserted++;
}
}
notificationsListAdapter.notifyItemRangeInserted(0,inserted);
try {
((MainActivity) context).updateNotifCounter();
}catch (Exception ignored){}
}
}
@ -317,7 +245,6 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
if( lastNotif == null || Long.parseLong(notificationId) > Long.parseLong(lastNotif)){
MainActivity.countNewNotifications = 0;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, notificationId);
editor.apply();
@ -339,7 +266,7 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
int previousPosition = notifications.size();
max_id = apiResponse.getMax_id();
List<Notification> notifications = apiResponse.getNotifications();
List<PeertubeNotification> notifications = apiResponse.getPeertubeNotifications();
if( !swiped && firstLoad && (notifications == null || notifications.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
@ -356,11 +283,8 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements O
}
if( notifications != null && notifications.size() > 0) {
for(Notification tmpNotification: notifications){
for(PeertubeNotification tmpNotification: notifications){
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
MainActivity.countNewNotifications++;
}
try {
((MainActivity) context).updateNotifCounter();
}catch (Exception ignored){}

View File

@ -23,34 +23,47 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/peertube_notif_pp"
android:layout_gravity="center_vertical"
android:layout_width="50dp"
android:layout_height="50dp" />
android:paddingTop="10dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:divider="?android:dividerHorizontal"
android:showDividers="end"
android:orientation="vertical">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView
android:id="@+id/peertube_notif_date"
android:layout_gravity="center_vertical"
android:textAlignment="viewEnd"
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/peertube_notif_message"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/peertube_notif_pp"
android:layout_gravity="top"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView
android:id="@+id/peertube_notif_date"
android:layout_gravity="center_vertical"
android:textAlignment="viewEnd"
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/peertube_notif_message"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:autoLink="web"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:visibility="gone"

View File

@ -842,16 +842,16 @@
<string name="settings_category_label_hiddencontent">Hidden content</string>
<string name="settings_category_label_composing">Composing</string>
<string name="contact">Contacts</string>
<string name="peertube_comment_on_video">%1$s commented your video %2$s</string>
<string name="peertube_follow_channel">%1$s is following your channel %2$s</string>
<string name="peertube_follow_account">%1$s is following your account</string>
<string name="peertube_comment_on_video"><![CDATA[<b>%1$s</b> commented your video <b>%2$s</b>]]></string>
<string name="peertube_follow_channel"><![CDATA[<b>%1$s</b> is following your channel <b>%2$s</b>]]></string>
<string name="peertube_follow_account"><![CDATA[<b>%1$s</b> is following your account]]></string>
<string name="peertube_video_published">Your video %1$s has been published</string>
<string name="peertube_video_import_success">Your video import %1$s succeeded</string>
<string name="peertube_video_import_error">Your video import %1$s failed</string>
<string name="peertube_video_from_subscription">%1$s published a new video: %2$s</string>
<string name="peertube_video_blacklist">Your video %1$s has been blacklisted</string>
<string name="peertube_video_unblacklist">Your video %1$s has been unblacklisted</string>
<string name="peertube_video_published"><![CDATA[Your video <b>%1$s</b> has been published]]></string>
<string name="peertube_video_import_success"><![CDATA[Your video import <b>%1$s</b> succeeded]]></string>
<string name="peertube_video_import_error"><![CDATA[Your video import <b>%1$s</b> failed]]></string>
<string name="peertube_video_from_subscription"><![CDATA[<b>%1$s</b> published a new video: <b>%2$s</b>]]></string>
<string name="peertube_video_blacklist"><![CDATA[Your video <b>%1$s</b> has been blacklisted]]></string>
<string name="peertube_video_unblacklist"><![CDATA[Your video <b>%1$s</b> has been unblacklisted]]></string>
<!-- end languages -->