Show notifications toast when switch accounts (#1124)

As title

Co-authored-by: M M Arif <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1124
Reviewed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
M M Arif 2022-05-05 16:57:22 +02:00
parent 7d271a236e
commit b2196fdf87
6 changed files with 48 additions and 10 deletions

View File

@ -762,6 +762,7 @@ public class IssueDetailActivity extends BaseActivity implements LabelsListAdapt
if(issue.getIssue().getDueDate() != null) { if(issue.getIssue().getDueDate() != null) {
viewBinding.dueDateFrame.setVisibility(View.VISIBLE);
if(timeFormat.equals("normal") || timeFormat.equals("pretty")) { if(timeFormat.equals("normal") || timeFormat.equals("pretty")) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", locale); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", locale);

View File

@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -14,9 +15,11 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.content.res.AppCompatResources;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.gitnex.tea4j.v2.models.NotificationCount;
import org.mian.gitnex.R; import org.mian.gitnex.R;
import org.mian.gitnex.activities.AddNewAccountActivity; import org.mian.gitnex.activities.AddNewAccountActivity;
import org.mian.gitnex.clients.PicassoService; import org.mian.gitnex.clients.PicassoService;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.database.api.BaseApi; import org.mian.gitnex.database.api.BaseApi;
import org.mian.gitnex.database.api.UserAccountsApi; import org.mian.gitnex.database.api.UserAccountsApi;
import org.mian.gitnex.database.models.UserAccount; import org.mian.gitnex.database.models.UserAccount;
@ -27,6 +30,8 @@ import org.mian.gitnex.helpers.Toasty;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import io.mikael.urlbuilder.UrlBuilder; import io.mikael.urlbuilder.UrlBuilder;
import retrofit2.Call;
import retrofit2.Callback;
/** /**
* @author M M Arif * @author M M Arif
@ -118,6 +123,7 @@ public class UserAccountsAdapter extends RecyclerView.Adapter<UserAccountsAdapte
.toString(); .toString();
Toasty.success(context, context.getResources().getString(R.string.switchAccountSuccess, userAccount.getUserName(), url)); Toasty.success(context, context.getResources().getString(R.string.switchAccountSuccess, userAccount.getUserName(), url));
getNotificationsCount();
((Activity) context).recreate(); ((Activity) context).recreate();
dialog.dismiss(); dialog.dismiss();
@ -142,6 +148,33 @@ public class UserAccountsAdapter extends RecyclerView.Adapter<UserAccountsAdapte
Toasty.success(context, context.getResources().getString(R.string.accountDeletedMessage)); Toasty.success(context, context.getResources().getString(R.string.accountDeletedMessage));
} }
private void getNotificationsCount() {
Call<NotificationCount> call = RetrofitClient.getApiInterface(context).notifyNewAvailable();
call.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<NotificationCount> call, @NonNull retrofit2.Response<NotificationCount> response) {
NotificationCount notificationCount = response.body();
if(response.code() == 200) {
assert notificationCount != null;
if(notificationCount.getNew() > 0) {
String toastMsg = context.getResources().getQuantityString(R.plurals.youHaveNewNotifications, Math.toIntExact(notificationCount.getNew()), Math.toIntExact(notificationCount.getNew()));
new Handler().postDelayed(() -> Toasty.info(context, toastMsg), 5000);
}
}
}
@Override
public void onFailure(@NonNull Call<NotificationCount> call, @NonNull Throwable t) {
}
});
}
@NonNull @NonNull
@Override @Override
public UserAccountsAdapter.UserAccountsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public UserAccountsAdapter.UserAccountsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View File

@ -25,12 +25,12 @@ import java.util.List;
import io.mikael.urlbuilder.UrlBuilder; import io.mikael.urlbuilder.UrlBuilder;
/** /**
* Author M M Arif * @author M M Arif
*/ */
public class UserAccountsNavAdapter extends RecyclerView.Adapter<UserAccountsNavAdapter.UserAccountsViewHolder> { public class UserAccountsNavAdapter extends RecyclerView.Adapter<UserAccountsNavAdapter.UserAccountsViewHolder> {
private static DrawerLayout drawer; private final DrawerLayout drawer;
private final List<UserAccount> userAccountsList; private final List<UserAccount> userAccountsList;
private final Context context; private final Context context;
@ -38,7 +38,7 @@ public class UserAccountsNavAdapter extends RecyclerView.Adapter<UserAccountsNav
this.context = ctx; this.context = ctx;
this.userAccountsList = userAccountsListMain; this.userAccountsList = userAccountsListMain;
drawer = drawerLayout; this.drawer = drawerLayout;
} }
class UserAccountsViewHolder extends RecyclerView.ViewHolder { class UserAccountsViewHolder extends RecyclerView.ViewHolder {
@ -55,9 +55,7 @@ public class UserAccountsNavAdapter extends RecyclerView.Adapter<UserAccountsNav
customDialogUserAccountsList(); customDialogUserAccountsList();
drawer.closeDrawers(); drawer.closeDrawers();
}); });
} }
} }
@NonNull @NonNull

View File

@ -7,7 +7,7 @@ import com.google.android.material.snackbar.Snackbar;
import org.mian.gitnex.R; import org.mian.gitnex.R;
/** /**
* Author M M Arif * @author M M Arif
*/ */
public class SnackBar { public class SnackBar {
@ -16,7 +16,7 @@ public class SnackBar {
Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG); Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView(); View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text); TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.colorWhite)); textView.setTextColor(context.getColor(R.color.colorWhite));
snackBar.show(); snackBar.show();
} }
@ -24,7 +24,7 @@ public class SnackBar {
Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG); Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView(); View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text); TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.colorLightGreen)); textView.setTextColor(context.getColor(R.color.colorLightGreen));
snackBar.show(); snackBar.show();
} }
@ -32,7 +32,7 @@ public class SnackBar {
Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG); Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView(); View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text); TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.lightYellow)); textView.setTextColor(context.getColor(R.color.lightYellow));
snackBar.show(); snackBar.show();
} }
@ -40,7 +40,7 @@ public class SnackBar {
Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG); Snackbar snackBar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackBar.getView(); View sbView = snackBar.getView();
TextView textView = sbView.findViewById(R.id.snackbar_text); TextView textView = sbView.findViewById(R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.darkRed)); textView.setTextColor(context.getColor(R.color.darkRed));
snackBar.show(); snackBar.show();
} }
} }

View File

@ -127,6 +127,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="end|center_vertical" android:gravity="end|center_vertical"
android:orientation="horizontal" android:orientation="horizontal"
android:visibility="gone"
tools:ignore="UseCompoundDrawables"> tools:ignore="UseCompoundDrawables">
<ImageView <ImageView
@ -152,6 +153,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="end|center_vertical" android:gravity="end|center_vertical"
android:orientation="horizontal" android:orientation="horizontal"
android:visibility="gone"
tools:ignore="UseCompoundDrawables"> tools:ignore="UseCompoundDrawables">
<ImageView <ImageView

View File

@ -653,6 +653,10 @@
<string name="mainNotificationChannelName">Notifications</string> <string name="mainNotificationChannelName">Notifications</string>
<string name="mainNotificationChannelDescription">This is the main notification channel of GitNex.</string> <string name="mainNotificationChannelDescription">This is the main notification channel of GitNex.</string>
<string name="notificationExtraInfo" translatable="false">- %s (%s)</string> <string name="notificationExtraInfo" translatable="false">- %s (%s)</string>
<plurals name="youHaveNewNotifications">
<item quantity="one">You have %s new notification.</item>
<item quantity="other">You have %s new notifications.</item>
</plurals>
<string name="isRead">Read</string> <string name="isRead">Read</string>
<string name="isUnread">Unread</string> <string name="isUnread">Unread</string>