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;
|
2021-12-15 23:53:10 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.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
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
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;
|
2021-09-29 11:29:39 +02:00
|
|
|
private List<NotificationThread> notificationThreads;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final OnMoreClickedListener onMoreClickedListener;
|
|
|
|
private final OnNotificationClickedListener onNotificationClickedListener;
|
2021-09-29 11:29:39 +02:00
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2020-07-22 21:32:42 +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;
|
|
|
|
}
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
2022-03-28 19:57:47 +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-03-18 09:56:49 +01:00
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading) {
|
2021-09-29 11:29:39 +02:00
|
|
|
isLoading = true;
|
|
|
|
}
|
2022-03-28 19:57:47 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-15 23:53:10 +01:00
|
|
|
private ImageView pinned;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final ImageView more;
|
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 = "";
|
|
|
|
|
|
|
|
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>";
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
subject.setText(HtmlCompat.fromHtml(subjectId + " " + notificationThread.getSubject().getTitle(), HtmlCompat.FROM_HTML_MODE_LEGACY));
|
2021-12-15 23:53:10 +01:00
|
|
|
if(!notificationThread.getSubject().getType().equalsIgnoreCase("repository")) {
|
|
|
|
repository.setText(notificationThread.getRepository().getFullName());
|
|
|
|
} else {
|
|
|
|
repository.setVisibility(View.GONE);
|
|
|
|
pinned.setVisibility(View.GONE);
|
|
|
|
pinned = itemView.findViewById(R.id.pinnedVertical);
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
if(notificationThread.isPinned()) {
|
|
|
|
pinned.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pinned.setVisibility(View.GONE);
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
switch(notificationThread.getSubject().getType().toLowerCase()) {
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(notificationThread.getSubject().getState().toLowerCase()) {
|
|
|
|
|
|
|
|
case "closed":
|
|
|
|
ImageViewCompat.setImageTintList(type, ColorStateList.valueOf(context.getResources().getColor(R.color.iconIssuePrClosedColor)));
|
|
|
|
break;
|
|
|
|
case "merged":
|
|
|
|
ImageViewCompat.setImageTintList(type, ColorStateList.valueOf(context.getResources().getColor(R.color.iconPrMergedColor)));
|
|
|
|
break;
|
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
default:
|
2022-02-22 07:23:46 +01:00
|
|
|
case "open":
|
|
|
|
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
|
|
|
|
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
|
|
|
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
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
}
|
2020-07-29 20:04:40 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
}
|
2020-07-29 20:04:40 +02:00
|
|
|
|
2021-09-29 11:29:39 +02:00
|
|
|
public void updateList(List<NotificationThread> list) {
|
|
|
|
notificationThreads = list;
|
|
|
|
notifyDataChanged();
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnNotificationClickedListener {
|
|
|
|
void onNotificationClicked(NotificationThread notificationThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnMoreClickedListener {
|
|
|
|
void onMoreClicked(NotificationThread notificationThread);
|
|
|
|
}
|
|
|
|
}
|