1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-04-25 07:28:46 +02:00

Fix issue #160 #87 #88 - Notification counter + mark them all as read

This commit is contained in:
Thomas 2021-01-09 17:18:32 +01:00
parent f168f101bc
commit b76a4cfcf5
10 changed files with 144 additions and 13 deletions

View File

@ -19,6 +19,8 @@ import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
@ -52,6 +54,8 @@ import app.fedilab.fedilabtube.helper.SwitchAccountHelper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import static app.fedilab.fedilabtube.MainActivity.badgeCount;
public class AccountActivity extends AppCompatActivity {
@ -112,9 +116,13 @@ public class AccountActivity extends AppCompatActivity {
});
TabLayout.Tab notificationTab = binding.accountTabLayout.newTab();
if (Helper.isLoggedIn(AccountActivity.this)) {
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.title_notifications)));
if (badgeCount > 0) {
binding.accountTabLayout.addTab(notificationTab.setText(getString(R.string.title_notifications) + " (" + badgeCount + ")"));
} else {
binding.accountTabLayout.addTab(notificationTab.setText(getString(R.string.title_notifications)));
}
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.title_muted)));
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.title_channel)));
@ -159,10 +167,30 @@ public class AccountActivity extends AppCompatActivity {
fragment = (Fragment) binding.accountViewpager.getAdapter().instantiateItem(binding.accountViewpager, tab.getPosition());
switch (tab.getPosition()) {
case 0:
if (badgeCount > 0) {
android.app.AlertDialog.Builder builder;
builder = new android.app.AlertDialog.Builder(AccountActivity.this);
builder.setMessage(R.string.mark_all_notifications_as_read_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.mark_all_as_read, (dialog, which) -> {
new Thread(() -> {
new RetrofitPeertubeAPI(AccountActivity.this).markAllAsRead();
Handler mainHandler = new Handler(Looper.getMainLooper());
badgeCount = 0;
Runnable myRunnable = () -> binding.accountTabLayout.getTabAt(0).setText(getString(R.string.title_notifications));
mainHandler.post(myRunnable);
}).start();
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
} else {
if (fragment != null) {
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
displayNotificationsFragment.scrollToTop();
}
}
break;
case 1:
if (fragment != null) {
@ -195,6 +223,14 @@ public class AccountActivity extends AppCompatActivity {
}
}
public void updateCounter() {
if (badgeCount > 0) {
binding.accountTabLayout.getTabAt(0).setText(getString(R.string.title_notifications) + " (" + badgeCount + ")");
} else {
binding.accountTabLayout.getTabAt(0).setText(getString(R.string.title_notifications));
}
}
@Override
protected void onResume() {
super.onResume();

View File

@ -119,7 +119,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment;
private DisplayOverviewFragment overviewFragment;
private ActivityMainBinding binding;
private int badgeCount;
public static int badgeCount;
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
int itemId = item.getItemId();
@ -566,6 +566,12 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
}
}
@Override
public void onResume() {
super.onResume();
invalidateOptionsMenu();
}
private void refreshToken() {
new Thread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);

View File

@ -287,6 +287,13 @@ public interface PeertubeService {
@GET("users/me/notifications?start=0&count=0&unread=true")
Call<NotificationData> countNotifications(@Header("Authorization") String credentials);
@POST("users/me/notifications/read-all")
Call<String> markAllAsRead(@Header("Authorization") String credentials);
@FormUrlEncoded
@POST("users/me/notifications/read")
Call<String> markAsRead(@Header("Authorization") String credentials, @Field("ids[]") List<String> ids);
//Update Notification settings
@PUT("users/me/notification-settings")
Call<String> updateNotifications(@Header("Authorization") String credentials, @Body NotificationSettings notificationSettings);

View File

@ -301,6 +301,32 @@ public class RetrofitPeertubeAPI {
return 0;
}
/**
* Mark all notifications as read
*/
public void markAllAsRead() {
PeertubeService peertubeService = init();
Call<String> notificationsCall = peertubeService.markAllAsRead("Bearer " + token);
try {
Response<String> response = notificationsCall.execute();
} catch (IOException ignored) {
}
}
/**
* Mark a notification as read
*/
public void markAsRead(String id) {
PeertubeService peertubeService = init();
ArrayList<String> ids = new ArrayList<>();
ids.add(id);
Call<String> notificationsCall = peertubeService.markAsRead("Bearer " + token, ids);
try {
Response<String> response = notificationsCall.execute();
} catch (IOException ignored) {
}
}
/**
* Retrieve notifications
*

View File

@ -31,10 +31,12 @@ import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import app.fedilab.fedilabtube.AccountActivity;
import app.fedilab.fedilabtube.PeertubeActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.ShowAccountActivity;
import app.fedilab.fedilabtube.ShowChannelActivity;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.client.data.ChannelData;
import app.fedilab.fedilabtube.client.data.NotificationData.Notification;
@ -43,6 +45,8 @@ import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.HelperInstance;
import static app.fedilab.fedilabtube.MainActivity.badgeCount;
public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@ -72,6 +76,11 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
holder.peertube_notif_pp.setVisibility(View.VISIBLE);
AccountData.Account accountAction = null;
ChannelData.Channel channelAction = null;
if (notification.isRead()) {
holder.unread.setVisibility(View.INVISIBLE);
} else {
holder.unread.setVisibility(View.VISIBLE);
}
if (notification.getActorFollow() != null) {
String profileUrl = notification.getActorFollow().getFollower().getAvatar() != null ? notification.getActorFollow().getFollower().getAvatar().getPath() : null;
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
@ -93,6 +102,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
accountAction.setDisplayName(actor.getDisplayName());
accountAction.setHost(actor.getHost());
accountAction.setUsername(actor.getName());
holder.peertube_notif_message.setOnClickListener(v -> markAsRead(notification, position));
} else if (notification.getComment() != null) { //Comment Notification
String profileUrl = notification.getComment().getAccount().getAvatar() != null ? notification.getComment().getAccount().getAvatar().getPath() : null;
Helper.loadGiF(context, profileUrl, holder.peertube_notif_pp);
@ -112,6 +122,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
b.putString("video_id", notification.getComment().getVideo().getId());
b.putString("video_uuid", notification.getComment().getVideo().getUuid());
intent.putExtras(b);
markAsRead(notification, position);
context.startActivity(intent);
});
} else {
@ -155,6 +166,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
b.putString("video_uuid", notification.getVideo().getUuid());
intent.putExtras(b);
context.startActivity(intent);
markAsRead(notification, position);
});
} else if (notification.getVideoAbuse() != null && notification.getVideoAbuse().getVideo() != null) {
message = context.getString(R.string.peertube_video_abuse, notification.getVideoAbuse().getVideo().getName());
@ -163,6 +175,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
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));
} else if (notification.getAbuse() != null) {
clickableNotification = false;
if (notification.getType() == DisplayNotificationsFragment.MY_VIDEO_REPPORT_SUCCESS) {
@ -172,6 +185,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
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));
}
}
holder.peertube_notif_date.setText(Helper.dateDiff(context, notification.getCreatedAt()));
@ -197,6 +211,17 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
}
}
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();
}
}
@Override
public long getItemId(int position) {
@ -212,7 +237,7 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView peertube_notif_pp;
TextView peertube_notif_message, peertube_notif_date;
TextView peertube_notif_message, peertube_notif_date, unread;
RelativeLayout main_container_trans;
public ViewHolder(View itemView) {
@ -221,6 +246,8 @@ public class PeertubeNotificationsListAdapter extends RecyclerView.Adapter<Recyc
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);
}
public View getView() {

View File

@ -691,4 +691,6 @@ public class Helper {
return String.format(Locale.getDefault(), "%s%s", df.format(rounded), context.getString(R.string.b));
}
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/colorAccent"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4H4C2.89,4 2.01,4.89 2.01,6L2,18c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2V6C22,4.89 21.11,4 20,4zM8.5,15H7.3l-2.55,-3.5V15H3.5V9h1.25l2.5,3.5V9H8.5V15zM13.5,10.26H11v1.12h2.5v1.26H11v1.11h2.5V15h-4V9h4V10.26zM20.5,14c0,0.55 -0.45,1 -1,1h-4c-0.55,0 -1,-0.45 -1,-1V9h1.25v4.51h1.13V9.99h1.25v3.51h1.12V9h1.25V14z" />
</vector>

View File

@ -46,7 +46,7 @@
android:id="@+id/instance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="18sp"

View File

@ -15,6 +15,7 @@
see <http://www.gnu.org/licenses>.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
@ -38,13 +39,27 @@
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/unread"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
app:drawableStartCompat="@drawable/ic_baseline_fiber_new_24" />
<TextView
android:id="@+id/peertube_notif_date"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="end"
android:textAlignment="viewEnd" />
</LinearLayout>
<TextView
android:id="@+id/peertube_notif_message"

View File

@ -471,5 +471,7 @@
<string name="subscription_cancelled">Subscription cancelled!</string>
<string name="cancel_subscription">Cancel subscription</string>
<string name="cancel_subscription_confirm">Are you sure, you want to cancel that subscription?</string>
<string name="mark_all_notifications_as_read_confirm">Are you sure you want to mark all notifications as read?</string>
<string name="mark_all_as_read">Mark all as read</string>
</resources>