2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.content.Context;
|
2021-08-02 20:04:33 +02:00
|
|
|
import android.content.Intent;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2020-06-11 01:45:11 +02:00
|
|
|
import android.widget.LinearLayout;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.widget.TextView;
|
2020-06-11 01:45:11 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Release;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
2021-08-02 20:04:33 +02:00
|
|
|
import org.mian.gitnex.activities.ProfileActivity;
|
2020-06-11 01:45:11 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2021-04-13 16:19:42 +02:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2020-06-11 01:45:11 +02:00
|
|
|
import org.mian.gitnex.helpers.ClickListener;
|
2020-11-02 16:17:00 +01:00
|
|
|
import org.mian.gitnex.helpers.Markdown;
|
2020-06-11 01:45:11 +02:00
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2022-04-24 17:15:35 +02:00
|
|
|
import org.mian.gitnex.structs.FragmentRefreshListener;
|
2019-06-03 23:25:05 +02:00
|
|
|
import java.util.List;
|
2020-06-11 01:45:11 +02:00
|
|
|
import java.util.Locale;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
2022-04-24 17:15:35 +02:00
|
|
|
* @author M M Arif
|
2019-06-03 23:25:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class ReleasesAdapter extends RecyclerView.Adapter<ReleasesAdapter.ReleasesViewHolder> {
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
private List<Release> releasesList;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final Context context;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-02-12 14:41:11 +01:00
|
|
|
private OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2022-04-24 17:15:35 +02:00
|
|
|
private final FragmentRefreshListener startDownload;
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2020-06-11 01:45:11 +02:00
|
|
|
static class ReleasesViewHolder extends RecyclerView.ViewHolder {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
private Release releases;
|
2021-08-02 20:04:33 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final TextView releaseType;
|
|
|
|
private final TextView releaseName;
|
|
|
|
private final ImageView authorAvatar;
|
|
|
|
private final TextView authorName;
|
|
|
|
private final TextView releaseTag;
|
|
|
|
private final TextView releaseDate;
|
|
|
|
private final TextView releaseBodyContent;
|
2022-04-24 17:15:35 +02:00
|
|
|
private final LinearLayout downloadCopyFrame;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final LinearLayout downloads;
|
2022-04-24 17:15:35 +02:00
|
|
|
private final LinearLayout releaseZipDownloadFrame;
|
|
|
|
private final LinearLayout releaseTarDownloadFrame;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final ImageView downloadDropdownIcon;
|
|
|
|
private final RecyclerView downloadList;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
private ReleasesViewHolder(View itemView) {
|
2020-06-11 01:45:11 +02:00
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
super(itemView);
|
|
|
|
|
2020-06-11 01:45:11 +02:00
|
|
|
releaseType = itemView.findViewById(R.id.releaseType);
|
|
|
|
releaseName = itemView.findViewById(R.id.releaseName);
|
|
|
|
authorAvatar = itemView.findViewById(R.id.authorAvatar);
|
|
|
|
authorName = itemView.findViewById(R.id.authorName);
|
|
|
|
releaseTag = itemView.findViewById(R.id.releaseTag);
|
2021-04-13 16:19:42 +02:00
|
|
|
TextView releaseCommitSha = itemView.findViewById(R.id.releaseCommitSha);
|
2020-06-11 01:45:11 +02:00
|
|
|
releaseDate = itemView.findViewById(R.id.releaseDate);
|
|
|
|
releaseBodyContent = itemView.findViewById(R.id.releaseBodyContent);
|
2022-04-24 17:15:35 +02:00
|
|
|
downloadCopyFrame = itemView.findViewById(R.id.downloadCopyFrame);
|
2020-06-11 01:45:11 +02:00
|
|
|
downloads = itemView.findViewById(R.id.downloads);
|
2022-04-24 17:15:35 +02:00
|
|
|
releaseZipDownloadFrame = itemView.findViewById(R.id.releaseZipDownloadFrame);
|
|
|
|
releaseTarDownloadFrame = itemView.findViewById(R.id.releaseTarDownloadFrame);
|
2020-06-11 01:45:11 +02:00
|
|
|
downloadDropdownIcon = itemView.findViewById(R.id.downloadDropdownIcon);
|
|
|
|
downloadList = itemView.findViewById(R.id.downloadList);
|
|
|
|
|
|
|
|
downloadList.setHasFixedSize(true);
|
|
|
|
downloadList.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
|
2021-08-02 20:04:33 +02:00
|
|
|
|
|
|
|
authorAvatar.setOnClickListener(loginId -> {
|
|
|
|
Context context = loginId.getContext();
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, ProfileActivity.class);
|
|
|
|
intent.putExtra("username", releases.getAuthor().getLogin());
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
public ReleasesAdapter(Context ctx, List<Release> releasesMain, FragmentRefreshListener startDownload) {
|
2021-04-13 16:19:42 +02:00
|
|
|
this.context = ctx;
|
2019-06-03 23:25:05 +02:00
|
|
|
this.releasesList = releasesMain;
|
2022-04-24 17:15:35 +02:00
|
|
|
this.startDownload = startDownload;
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public ReleasesAdapter.ReleasesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2020-03-28 11:52:09 +01:00
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_releases, parent, false);
|
2019-06-03 23:25:05 +02:00
|
|
|
return new ReleasesAdapter.ReleasesViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ReleasesAdapter.ReleasesViewHolder holder, int position) {
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
final TinyDB tinyDb = TinyDB.getInstance(context);
|
2021-05-30 19:28:14 +02:00
|
|
|
final Locale locale = context.getResources().getConfiguration().locale;
|
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
|
|
|
final String timeFormat = tinyDb.getString("dateFormat", "pretty");
|
2021-04-13 16:19:42 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
|
2019-09-29 18:44:42 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Release currentItem = releasesList.get(position);
|
2021-08-02 20:04:33 +02:00
|
|
|
holder.releases = currentItem;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 01:45:11 +02:00
|
|
|
holder.releaseName.setText(currentItem.getName());
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 01:45:11 +02:00
|
|
|
if(currentItem.isPrerelease()) {
|
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_pre_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseTypePre);
|
|
|
|
}
|
|
|
|
else if(currentItem.isDraft()) {
|
2022-03-28 19:57:47 +02:00
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_draft_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseDraftText);
|
2020-06-11 01:45:11 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_stable_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseTypeStable);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
if(currentItem.getAuthor().getAvatarUrl() != null) {
|
|
|
|
PicassoService.getInstance(context).get().load(currentItem.getAuthor().getAvatarUrl()).placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(imgRadius, 0)).resize(120, 120).centerCrop().into(holder.authorAvatar);
|
2020-06-11 01:45:11 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
holder.authorName.setText(context.getResources().getString(R.string.releasePublishedBy, currentItem.getAuthor().getLogin()));
|
2020-06-11 01:45:11 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
if(currentItem.getTagName() != null) {
|
|
|
|
holder.releaseTag.setText(currentItem.getTagName());
|
2020-06-11 01:45:11 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
if(currentItem.getPublishedAt() != null) {
|
|
|
|
holder.releaseDate.setText(TimeHelper.formatTime(currentItem.getPublishedAt(), locale, timeFormat, context));
|
2020-06-11 01:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(timeFormat.equals("pretty")) {
|
2022-04-18 09:10:54 +02:00
|
|
|
holder.releaseDate.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(currentItem.getPublishedAt()), context));
|
2020-06-11 01:45:11 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
if(!currentItem.getBody().equals("")) {
|
2021-04-13 16:19:42 +02:00
|
|
|
Markdown.render(context, currentItem.getBody(), holder.releaseBodyContent);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-06-28 16:45:39 +02:00
|
|
|
holder.releaseBodyContent.setText(R.string.noReleaseBodyContent);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
2019-09-16 17:28:34 +02:00
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
holder.downloadCopyFrame.setOnClickListener(v -> {
|
2020-06-11 01:45:11 +02:00
|
|
|
|
|
|
|
if(holder.downloads.getVisibility() == View.GONE) {
|
|
|
|
|
2020-07-12 19:18:50 +02:00
|
|
|
holder.downloadDropdownIcon.setImageResource(R.drawable.ic_chevron_down);
|
2020-06-11 01:45:11 +02:00
|
|
|
holder.downloads.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2020-07-12 19:18:50 +02:00
|
|
|
holder.downloadDropdownIcon.setImageResource(R.drawable.ic_chevron_right);
|
2020-06-11 01:45:11 +02:00
|
|
|
holder.downloads.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
holder.releaseZipDownloadFrame.setOnClickListener(v -> startDownload.onRefresh(currentItem.getZipballUrl()));
|
|
|
|
holder.releaseTarDownloadFrame.setOnClickListener(v -> startDownload.onRefresh(currentItem.getTarballUrl()));
|
2019-09-16 17:28:34 +02:00
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
ReleasesDownloadsAdapter adapter = new ReleasesDownloadsAdapter(currentItem.getAssets(), startDownload);
|
2020-06-11 01:45:11 +02:00
|
|
|
holder.downloadList.setAdapter(adapter);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
|
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return releasesList.size();
|
|
|
|
}
|
|
|
|
|
2022-02-12 14:41:11 +01:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
if(!isMoreDataAvailable) {
|
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 17:15:35 +02:00
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
2022-02-12 14:41:11 +01:00
|
|
|
public void notifyDataChanged() {
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnLoadMoreListener {
|
|
|
|
void onLoadMore();
|
|
|
|
|
|
|
|
void onLoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
|
|
|
|
this.loadMoreListener = loadMoreListener;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public void updateList(List<Release> list) {
|
2022-02-12 14:41:11 +01:00
|
|
|
releasesList = list;
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|