2020-07-22 21:32:42 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2020-07-22 21:32:42 +02:00
|
|
|
import android.content.Context;
|
2022-02-22 07:23:46 +01:00
|
|
|
import android.content.res.ColorStateList;
|
2020-07-22 21:32:42 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
2021-01-26 16:10:31 +01:00
|
|
|
import androidx.core.content.res.ResourcesCompat;
|
|
|
|
import androidx.core.text.HtmlCompat;
|
2022-02-22 07:23:46 +01:00
|
|
|
import androidx.core.widget.ImageViewCompat;
|
2020-07-22 21:32:42 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.util.List;
|
2021-12-15 23:53:10 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.NotificationThread;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.mian.gitnex.R;
|
2022-02-22 07:23:46 +01:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2020-07-22 21:32:42 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-28 19:57:47 +02:00
|
|
|
* @author opyale
|
2020-07-22 21:32:42 +02:00
|
|
|
*/
|
2021-09-29 11:29:39 +02:00
|
|
|
public class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final Context context;
|
|
|
|
private final OnMoreClickedListener onMoreClickedListener;
|
|
|
|
private final OnNotificationClickedListener onNotificationClickedListener;
|
2022-08-15 16:26:02 +02:00
|
|
|
private List<NotificationThread> notificationThreads;
|
2021-09-29 11:29:39 +02:00
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
public NotificationsAdapter(
|
|
|
|
Context context,
|
|
|
|
List<NotificationThread> notificationThreads,
|
|
|
|
OnMoreClickedListener onMoreClickedListener,
|
|
|
|
OnNotificationClickedListener onNotificationClickedListener) {
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
|
2020-07-22 21:32:42 +02:00
|
|
|
this.context = context;
|
|
|
|
this.notificationThreads = notificationThreads;
|
|
|
|
this.onMoreClickedListener = onMoreClickedListener;
|
|
|
|
this.onNotificationClickedListener = onNotificationClickedListener;
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@NonNull @Override
|
2021-09-29 11:29:39 +02:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
2022-09-21 07:43:00 +02:00
|
|
|
return new NotificationsAdapter.NotificationsHolder(
|
|
|
|
inflater.inflate(R.layout.list_notifications, parent, false));
|
2021-09-29 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
2022-09-21 07:43:00 +02:00
|
|
|
if (position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading) {
|
2021-09-29 11:29:39 +02:00
|
|
|
isLoading = true;
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
((NotificationsAdapter.NotificationsHolder) holder)
|
|
|
|
.bindData(notificationThreads.get(position));
|
2021-09-29 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
2022-03-28 19:57:47 +02:00
|
|
|
return position;
|
2021-09-29 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return notificationThreads.size();
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateList(List<NotificationThread> list) {
|
|
|
|
notificationThreads = list;
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnNotificationClickedListener {
|
|
|
|
|
|
|
|
void onNotificationClicked(NotificationThread notificationThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnMoreClickedListener {
|
|
|
|
|
|
|
|
void onMoreClicked(NotificationThread notificationThread);
|
|
|
|
}
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
class NotificationsHolder extends RecyclerView.ViewHolder {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final LinearLayout frame;
|
|
|
|
private final TextView subject;
|
|
|
|
private final TextView repository;
|
2022-02-22 07:23:46 +01:00
|
|
|
private final ImageView type;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final ImageView more;
|
2022-08-15 16:26:02 +02:00
|
|
|
private ImageView pinned;
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
NotificationsHolder(View itemView) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
frame = itemView.findViewById(R.id.frame);
|
|
|
|
subject = itemView.findViewById(R.id.subject);
|
|
|
|
repository = itemView.findViewById(R.id.repository);
|
2022-02-22 07:23:46 +01:00
|
|
|
type = itemView.findViewById(R.id.type);
|
2020-07-22 21:32:42 +02:00
|
|
|
pinned = itemView.findViewById(R.id.pinned);
|
|
|
|
more = itemView.findViewById(R.id.more);
|
|
|
|
}
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
void bindData(NotificationThread notificationThread) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
String url = notificationThread.getSubject().getUrl();
|
2021-12-15 23:53:10 +01:00
|
|
|
String subjectId = "";
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (StringUtils.containsAny(
|
|
|
|
notificationThread.getSubject().getType().toLowerCase(), "pull", "issue")) {
|
|
|
|
subjectId =
|
|
|
|
"<font color='"
|
|
|
|
+ ResourcesCompat.getColor(
|
|
|
|
context.getResources(), R.color.lightGray, null)
|
|
|
|
+ "'>"
|
|
|
|
+ context.getResources().getString(R.string.hash)
|
|
|
|
+ url.substring(url.lastIndexOf("/") + 1)
|
|
|
|
+ "</font>";
|
2021-12-15 23:53:10 +01:00
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
subject.setText(
|
|
|
|
HtmlCompat.fromHtml(
|
|
|
|
subjectId + " " + notificationThread.getSubject().getTitle(),
|
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY));
|
|
|
|
if (!notificationThread.getSubject().getType().equalsIgnoreCase("repository")) {
|
2021-12-15 23:53:10 +01:00
|
|
|
repository.setText(notificationThread.getRepository().getFullName());
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2021-12-15 23:53:10 +01:00
|
|
|
repository.setVisibility(View.GONE);
|
|
|
|
pinned.setVisibility(View.GONE);
|
|
|
|
pinned = itemView.findViewById(R.id.pinnedVertical);
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (notificationThread.isPinned()) {
|
2021-09-29 11:29:39 +02:00
|
|
|
pinned.setVisibility(View.VISIBLE);
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2021-09-29 11:29:39 +02:00
|
|
|
pinned.setVisibility(View.GONE);
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
switch (notificationThread.getSubject().getType().toLowerCase()) {
|
2022-02-22 07:23:46 +01:00
|
|
|
case "pull":
|
|
|
|
type.setImageResource(R.drawable.ic_pull_request);
|
|
|
|
break;
|
|
|
|
case "issue":
|
|
|
|
type.setImageResource(R.drawable.ic_issue);
|
2021-09-29 11:29:39 +02:00
|
|
|
break;
|
2022-02-22 07:23:46 +01:00
|
|
|
case "commit":
|
|
|
|
type.setImageResource(R.drawable.ic_commit);
|
2021-09-29 11:29:39 +02:00
|
|
|
break;
|
2022-02-22 07:23:46 +01:00
|
|
|
case "repository":
|
|
|
|
type.setImageResource(R.drawable.ic_repo);
|
2021-12-15 23:53:10 +01:00
|
|
|
break;
|
2022-02-22 07:23:46 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
type.setImageResource(R.drawable.ic_question);
|
2021-12-15 23:53:10 +01:00
|
|
|
break;
|
2022-02-22 07:23:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
switch (notificationThread.getSubject().getState().toLowerCase()) {
|
2022-02-22 07:23:46 +01:00
|
|
|
case "closed":
|
2022-09-21 07:43:00 +02:00
|
|
|
ImageViewCompat.setImageTintList(
|
|
|
|
type,
|
|
|
|
ColorStateList.valueOf(
|
|
|
|
context.getResources()
|
|
|
|
.getColor(R.color.iconIssuePrClosedColor)));
|
2022-02-22 07:23:46 +01:00
|
|
|
break;
|
|
|
|
case "merged":
|
2022-09-21 07:43:00 +02:00
|
|
|
ImageViewCompat.setImageTintList(
|
|
|
|
type,
|
|
|
|
ColorStateList.valueOf(
|
|
|
|
context.getResources().getColor(R.color.iconPrMergedColor)));
|
2022-02-22 07:23:46 +01:00
|
|
|
break;
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
default:
|
2022-02-22 07:23:46 +01:00
|
|
|
case "open":
|
2022-09-21 07:43:00 +02:00
|
|
|
ImageViewCompat.setImageTintList(
|
|
|
|
type,
|
|
|
|
ColorStateList.valueOf(
|
|
|
|
AppUtil.getColorFromAttribute(context, R.attr.iconsColor)));
|
2021-09-29 11:29:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
frame.setOnClickListener(
|
|
|
|
v -> onNotificationClickedListener.onNotificationClicked(notificationThread));
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
more.setOnClickListener(v -> onMoreClickedListener.onMoreClicked(notificationThread));
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|
2021-09-29 11:29:39 +02:00
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|