From 88f8637e232257059418e8ba2242ec757610ac80 Mon Sep 17 00:00:00 2001 From: stom79 Date: Thu, 24 Jan 2019 17:41:53 +0100 Subject: [PATCH] Frontend notifications --- .../etalab/mastodon/client/PeertubeAPI.java | 38 ++++--- .../PeertubeNotificationsListAdapter.java | 48 ++++++--- .../DisplayPeertubeNotificationsFragment.java | 98 +++---------------- .../layout/drawer_peertube_notification.xml | 63 +++++++----- app/src/main/res/values/strings.xml | 18 ++-- 5 files changed, 115 insertions(+), 150 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java index 1e5b1c274..4cb30da5d 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java @@ -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()); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PeertubeNotificationsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PeertubeNotificationsListAdapter.java index acf850b1e..fa24eb651 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PeertubeNotificationsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PeertubeNotificationsListAdapter.java @@ -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 notifications; private LayoutInflater layoutInflater; private PeertubeNotificationsListAdapter peertubeNotificationsListAdapter; - private PeertubeNotificationsListAdapter.ViewHolder holder; + private int style; public PeertubeNotificationsListAdapter(Context context, List 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){ diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayPeertubeNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayPeertubeNotificationsFragment.java index acc34bbff..cde39505f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayPeertubeNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayPeertubeNotificationsFragment.java @@ -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 asyncTask; - private NotificationsListAdapter notificationsListAdapter; + private PeertubeNotificationsListAdapter notificationsListAdapter; private String max_id; - private List notifications; + private List 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 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 notifications = apiResponse.getNotifications(); + List 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){} diff --git a/app/src/main/res/layout/drawer_peertube_notification.xml b/app/src/main/res/layout/drawer_peertube_notification.xml index bfa276bb9..4cf1fe36b 100644 --- a/app/src/main/res/layout/drawer_peertube_notification.xml +++ b/app/src/main/res/layout/drawer_peertube_notification.xml @@ -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"> - + 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"> - - + android:orientation="horizontal" + android:layout_width="match_parent" + android:layout_height="wrap_content"> + + + + + + Hidden content Composing Contacts - %1$s commented your video %2$s - %1$s is following your channel %2$s - %1$s is following your account + %1$s commented your video %2$s]]> + %1$s is following your channel %2$s]]> + %1$s is following your account]]> - Your video %1$s has been published - Your video import %1$s succeeded - Your video import %1$s failed - %1$s published a new video: %2$s - Your video %1$s has been blacklisted - Your video %1$s has been unblacklisted + %1$s has been published]]> + %1$s succeeded]]> + %1$s failed]]> + %1$s published a new video: %2$s]]> + %1$s has been blacklisted]]> + %1$s has been unblacklisted]]>