Notification badge in navigation menu (#702)

Merge branch 'master' into 700-notifications-counter

Notification badge in navigation menu

Co-authored-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: M M Arif <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/702
This commit is contained in:
M M Arif 2020-09-18 06:51:41 +02:00
parent 78782d159a
commit 1f5eeb5632
6 changed files with 92 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -55,6 +56,7 @@ import org.mian.gitnex.helpers.TinyDB;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.Version;
import org.mian.gitnex.models.GiteaVersion;
import org.mian.gitnex.models.NotificationCount;
import org.mian.gitnex.models.UserInfo;
import java.util.ArrayList;
import java.util.List;
@ -82,6 +84,14 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
private Context appCtx;
private Typeface myTypeface;
private String instanceUrl;
private String loginUid;
private String instanceToken;
private View hView;
private MenuItem navNotifications;
private TextView notificationCounter;
@Override
protected int getLayoutResourceId() {
@ -100,9 +110,9 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
Intent mainIntent = getIntent();
String launchFragment = mainIntent.getStringExtra("launchFragment");
final String instanceUrl = tinyDb.getString("instanceUrl");
final String loginUid = tinyDb.getString("loginUid");
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
instanceUrl = tinyDb.getString("instanceUrl");
loginUid = tinyDb.getString("loginUid");
instanceToken = "token " + tinyDb.getString(loginUid + "-token");
if(tinyDb.getString("dateFormat").isEmpty()) {
tinyDb.putString("dateFormat", "pretty");
@ -191,10 +201,15 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
toolbarTitle.setText(getResources().getString(R.string.pageTitleUserAccounts));
}
getNotificationsCount(instanceUrl, instanceToken);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
final View hView = navigationView.getHeaderView(0);
hView = navigationView.getHeaderView(0);
Menu menu = navigationView.getMenu();
navNotifications = menu.findItem(R.id.nav_notifications);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
@ -204,6 +219,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
@Override
public void onDrawerOpened(@NonNull View drawerView) {
getNotificationsCount(instanceUrl, instanceToken);
}
@Override
@ -705,4 +721,31 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
}
private void getNotificationsCount(String instanceUrl, String token) {
Call<NotificationCount> call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().checkUnreadNotifications(token);
call.enqueue(new Callback<NotificationCount>() {
@Override
public void onResponse(@NonNull Call<NotificationCount> call, @NonNull retrofit2.Response<NotificationCount> response) {
NotificationCount notificationCount = response.body();
if(response.code() == 200) {
assert notificationCount != null;
notificationCounter = navNotifications.getActionView().findViewById(R.id.counterBadgeNotification);
notificationCounter.setText(String.valueOf(notificationCount.getCounter()));
}
}
@Override
public void onFailure(@NonNull Call<NotificationCount> call, @NonNull Throwable t) {
Log.e("onFailure-notification", t.toString());
}
});
}
}

View File

@ -36,7 +36,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -79,7 +78,7 @@ public class NotificationsFragment extends Fragment implements NotificationsAdap
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
setHasOptionsMenu(true);
activity = Objects.requireNonNull(getActivity());
activity = requireActivity();
context = getContext();
tinyDB = new TinyDB(context);

View File

@ -19,6 +19,7 @@ import org.mian.gitnex.models.Labels;
import org.mian.gitnex.models.MergePullRequest;
import org.mian.gitnex.models.Milestones;
import org.mian.gitnex.models.NewFile;
import org.mian.gitnex.models.NotificationCount;
import org.mian.gitnex.models.NotificationThread;
import org.mian.gitnex.models.OrgOwner;
import org.mian.gitnex.models.Organization;
@ -93,7 +94,7 @@ public interface ApiInterface {
Call<ResponseBody> markNotificationThreadsAsRead(@Header("Authorization") String token, @Query("last_read_at") String last_read_at, @Query("all") Boolean all, @Query("status-types") String[] statusTypes, @Query("to-status") String toStatus);
@GET("notifications/new") // Check if unread notifications exist
Call<JsonElement> checkUnreadNotifications(@Header("Authorization") String token);
Call<NotificationCount> checkUnreadNotifications(@Header("Authorization") String token);
@GET("notifications/threads/{id}") // Get notification thread by ID
Call<NotificationThread> getNotificationThread(@Header("Authorization") String token, @Path("id") Integer id);

View File

@ -0,0 +1,19 @@
package org.mian.gitnex.models;
import com.google.gson.annotations.SerializedName;
/**
* Author M M Arif
*/
public class NotificationCount {
@SerializedName("new")
private int counter;
public int getCounter() {
return counter;
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:id="@+id/counterBadgeNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLength="4"
android:ellipsize="marquee"
android:background="@drawable/shape_badge_background"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textColor="@color/colorWhite"
android:textSize="12sp" />
</LinearLayout>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group android:checkableBehavior="single"
@ -26,6 +27,7 @@
android:id="@+id/nav_notifications"
android:icon="@drawable/ic_notifications"
android:title="@string/pageTitleNotifications"
app:actionLayout="@layout/badge_notification"
android:visible="false" />
<item android:id="@+id/nav_explore"