2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2022-09-14 20:40:44 +02:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.Filter;
|
|
|
|
import android.widget.Filterable;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2020-04-14 21:25:36 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-06-03 23:25:05 +02:00
|
|
|
import com.amulyakhare.textdrawable.TextDrawable;
|
|
|
|
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.activities.RepoDetailActivity;
|
2020-04-01 13:26:32 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2022-09-14 20:40:44 +02:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
|
|
|
import org.mian.gitnex.helpers.ClickListener;
|
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
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
|
|
|
import org.mian.gitnex.helpers.contexts.RepositoryContext;
|
2021-04-13 16:19:42 +02:00
|
|
|
import org.ocpsoft.prettytime.PrettyTime;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
2022-04-04 10:00:54 +02:00
|
|
|
* @author M M Arif
|
2019-06-03 23:25:05 +02:00
|
|
|
*/
|
2022-09-21 07:43:00 +02:00
|
|
|
public class ReposListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
|
|
|
|
implements Filterable {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final Context context;
|
2022-04-18 09:10:54 +02:00
|
|
|
private final List<org.gitnex.tea4j.v2.models.Repository> reposListFull;
|
2022-04-04 10:00:54 +02:00
|
|
|
private final TinyDB tinyDb;
|
2022-07-31 17:47:33 +02:00
|
|
|
public boolean isUserOrg = false;
|
2022-08-15 16:26:02 +02:00
|
|
|
private List<org.gitnex.tea4j.v2.models.Repository> reposList;
|
|
|
|
private OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2022-09-21 07:43:00 +02:00
|
|
|
private final Filter reposFilter =
|
|
|
|
new Filter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
|
|
|
|
|
List<org.gitnex.tea4j.v2.models.Repository> filteredList = new ArrayList<>();
|
|
|
|
|
|
|
|
if (constraint == null || constraint.length() == 0) {
|
|
|
|
filteredList.addAll(reposListFull);
|
|
|
|
} else {
|
|
|
|
String filterPattern = constraint.toString().toLowerCase().trim();
|
|
|
|
|
|
|
|
for (org.gitnex.tea4j.v2.models.Repository item : reposListFull) {
|
|
|
|
if (item.getFullName().toLowerCase().contains(filterPattern)
|
|
|
|
|| item.getDescription()
|
|
|
|
.toLowerCase()
|
|
|
|
.contains(filterPattern)) {
|
|
|
|
filteredList.add(item);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
FilterResults results = new FilterResults();
|
|
|
|
results.values = filteredList;
|
2022-08-15 16:26:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
return results;
|
|
|
|
}
|
2022-08-15 16:26:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
2022-08-15 16:26:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
reposList.clear();
|
|
|
|
reposList.addAll((List) results.values);
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
|
|
|
};
|
2020-04-14 21:25:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
public ReposListAdapter(
|
|
|
|
List<org.gitnex.tea4j.v2.models.Repository> reposListMain, Context ctx) {
|
2022-04-04 10:00:54 +02:00
|
|
|
this.context = ctx;
|
|
|
|
this.reposList = reposListMain;
|
|
|
|
reposListFull = new ArrayList<>(reposList);
|
|
|
|
this.tinyDb = TinyDB.getInstance(context);
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@NonNull @Override
|
2022-04-04 10:00:54 +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 ReposListAdapter.ReposHolder(
|
|
|
|
inflater.inflate(R.layout.list_repositories, parent, false));
|
2022-04-04 10:00:54 +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
|
|
|
|
&& loadMoreListener != null) {
|
2022-04-04 10:00:54 +02:00
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
((ReposListAdapter.ReposHolder) holder).bindData(reposList.get(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return reposList.size();
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!isMoreDataAvailable) {
|
2022-08-15 16:26:02 +02:00
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 21:25:36 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
|
|
|
|
this.loadMoreListener = loadMoreListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateList(List<org.gitnex.tea4j.v2.models.Repository> list) {
|
|
|
|
reposList = list;
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Filter getFilter() {
|
|
|
|
return reposFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnLoadMoreListener {
|
|
|
|
|
|
|
|
void onLoadMore();
|
|
|
|
|
|
|
|
void onLoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReposHolder extends RecyclerView.ViewHolder {
|
2021-04-13 16:19:42 +02:00
|
|
|
|
|
|
|
private final ImageView image;
|
|
|
|
private final TextView repoName;
|
|
|
|
private final TextView orgName;
|
|
|
|
private final TextView repoDescription;
|
|
|
|
private final TextView repoStars;
|
|
|
|
private final TextView repoLastUpdated;
|
2021-10-05 17:16:22 +02:00
|
|
|
private final View spacerView;
|
2022-08-15 16:26:02 +02:00
|
|
|
private org.gitnex.tea4j.v2.models.Repository userRepositories;
|
|
|
|
private CheckBox isRepoAdmin;
|
2020-04-14 21:25:36 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
ReposHolder(View itemView) {
|
2020-04-14 21:25:36 +02:00
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
repoName = itemView.findViewById(R.id.repoName);
|
2021-04-13 16:19:42 +02:00
|
|
|
orgName = itemView.findViewById(R.id.orgName);
|
2020-04-14 21:25:36 +02:00
|
|
|
repoDescription = itemView.findViewById(R.id.repoDescription);
|
|
|
|
isRepoAdmin = itemView.findViewById(R.id.repoIsAdmin);
|
|
|
|
image = itemView.findViewById(R.id.imageAvatar);
|
|
|
|
repoStars = itemView.findViewById(R.id.repoStars);
|
2021-04-13 16:19:42 +02:00
|
|
|
repoLastUpdated = itemView.findViewById(R.id.repoLastUpdated);
|
2021-10-05 17:16:22 +02:00
|
|
|
spacerView = itemView.findViewById(R.id.spacerView);
|
2020-04-14 21:25:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
itemView.setOnClickListener(
|
|
|
|
v -> {
|
|
|
|
Context context = v.getContext();
|
|
|
|
RepositoryContext repo = new RepositoryContext(userRepositories, context);
|
|
|
|
repo.saveToDB(context);
|
|
|
|
Intent intent = repo.getIntent(context, RepoDetailActivity.class);
|
|
|
|
if (isUserOrg) {
|
|
|
|
intent.putExtra("openedFromUserOrg", true);
|
|
|
|
}
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
2020-04-14 21:25:36 +02:00
|
|
|
}
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
@SuppressLint("SetTextI18n")
|
2022-04-18 09:10:54 +02:00
|
|
|
void bindData(org.gitnex.tea4j.v2.models.Repository repositories) {
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
this.userRepositories = repositories;
|
2022-08-07 21:34:02 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 60);
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
Locale locale = context.getResources().getConfiguration().locale;
|
|
|
|
String timeFormat = tinyDb.getString("dateFormat", "pretty");
|
|
|
|
orgName.setText(repositories.getFullName().split("/")[0]);
|
|
|
|
repoName.setText(repositories.getFullName().split("/")[1]);
|
2022-08-07 22:03:17 +02:00
|
|
|
repoStars.setText(AppUtil.numberFormatter(repositories.getStarsCount()));
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
ColorGenerator generator = ColorGenerator.Companion.getMATERIAL();
|
|
|
|
int color = generator.getColor(repositories.getName());
|
|
|
|
String firstCharacter = String.valueOf(repositories.getFullName().charAt(0));
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
TextDrawable drawable =
|
|
|
|
TextDrawable.builder()
|
|
|
|
.beginConfig()
|
|
|
|
.useFont(Typeface.DEFAULT)
|
|
|
|
.fontSize(18)
|
|
|
|
.toUpperCase()
|
|
|
|
.width(28)
|
|
|
|
.height(28)
|
|
|
|
.endConfig()
|
|
|
|
.buildRoundRect(firstCharacter, color, 14);
|
|
|
|
|
|
|
|
if (repositories.getAvatarUrl() != null) {
|
|
|
|
if (!repositories.getAvatarUrl().equals("")) {
|
|
|
|
PicassoService.getInstance(context)
|
|
|
|
.get()
|
|
|
|
.load(repositories.getAvatarUrl())
|
|
|
|
.placeholder(R.drawable.loader_animated)
|
|
|
|
.transform(new RoundedTransformation(imgRadius, 0))
|
|
|
|
.resize(120, 120)
|
|
|
|
.centerCrop()
|
|
|
|
.into(image);
|
|
|
|
} else {
|
2022-04-04 10:00:54 +02:00
|
|
|
image.setImageDrawable(drawable);
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2022-04-04 10:00:54 +02:00
|
|
|
image.setImageDrawable(drawable);
|
|
|
|
}
|
2019-09-14 09:38:41 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (repositories.getUpdatedAt() != null) {
|
|
|
|
|
|
|
|
switch (timeFormat) {
|
|
|
|
case "pretty":
|
|
|
|
{
|
|
|
|
PrettyTime prettyTime = new PrettyTime(locale);
|
|
|
|
String createdTime = prettyTime.format(repositories.getUpdatedAt());
|
|
|
|
repoLastUpdated.setText(
|
|
|
|
context.getString(R.string.lastUpdatedAt, createdTime));
|
|
|
|
repoLastUpdated.setOnClickListener(
|
|
|
|
new ClickListener(
|
|
|
|
TimeHelper.customDateFormatForToastDateFormat(
|
|
|
|
repositories.getUpdatedAt()),
|
|
|
|
context));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal":
|
|
|
|
{
|
|
|
|
DateFormat formatter =
|
|
|
|
new SimpleDateFormat(
|
|
|
|
"yyyy-MM-dd '"
|
|
|
|
+ context.getResources()
|
|
|
|
.getString(R.string.timeAtText)
|
|
|
|
+ "' HH:mm",
|
|
|
|
locale);
|
|
|
|
String createdTime = formatter.format(repositories.getUpdatedAt());
|
|
|
|
repoLastUpdated.setText(
|
|
|
|
context.getString(R.string.lastUpdatedAt, createdTime));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal1":
|
|
|
|
{
|
|
|
|
DateFormat formatter =
|
|
|
|
new SimpleDateFormat(
|
|
|
|
"dd-MM-yyyy '"
|
|
|
|
+ context.getResources()
|
|
|
|
.getString(R.string.timeAtText)
|
|
|
|
+ "' HH:mm",
|
|
|
|
locale);
|
|
|
|
String createdTime = formatter.format(repositories.getUpdatedAt());
|
|
|
|
repoLastUpdated.setText(
|
|
|
|
context.getString(R.string.lastUpdatedAt, createdTime));
|
|
|
|
break;
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2022-04-04 10:00:54 +02:00
|
|
|
repoLastUpdated.setVisibility(View.GONE);
|
2020-04-14 21:25:36 +02:00
|
|
|
}
|
2019-09-13 17:56:30 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!repositories.getDescription().equals("")) {
|
2022-04-04 10:00:54 +02:00
|
|
|
repoDescription.setVisibility(View.VISIBLE);
|
|
|
|
repoDescription.setText(repositories.getDescription());
|
|
|
|
spacerView.setVisibility(View.GONE);
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2022-04-04 10:00:54 +02:00
|
|
|
repoDescription.setVisibility(View.GONE);
|
|
|
|
spacerView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (isRepoAdmin == null) {
|
2022-04-04 10:00:54 +02:00
|
|
|
isRepoAdmin = new CheckBox(context);
|
2021-04-13 16:19:42 +02:00
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
isRepoAdmin.setChecked(repositories.getPermissions().isAdmin());
|
2020-04-14 21:25:36 +02:00
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|