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;
|
2022-08-01 09:24:23 +02:00
|
|
|
import android.os.Handler;
|
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-05-01 12:00:02 +02:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialog;
|
2022-08-07 21:34:02 +02:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
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;
|
2022-05-01 12:00:02 +02:00
|
|
|
import org.mian.gitnex.activities.MainActivity;
|
2021-08-02 20:04:33 +02:00
|
|
|
import org.mian.gitnex.activities.ProfileActivity;
|
2022-05-01 12:00:02 +02:00
|
|
|
import org.mian.gitnex.activities.RepoDetailActivity;
|
2020-06-11 01:45:11 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2022-05-01 12:00:02 +02:00
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.databinding.FragmentReleasesBinding;
|
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.Markdown;
|
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
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;
|
2022-05-01 12:00:02 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
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-08-15 16:26:02 +02:00
|
|
|
private final Context context;
|
2022-05-01 12:00:02 +02:00
|
|
|
private final String repoOwner;
|
|
|
|
private final String repoName;
|
2022-04-24 17:15:35 +02:00
|
|
|
private final FragmentRefreshListener startDownload;
|
2022-05-01 12:00:02 +02:00
|
|
|
private final FragmentReleasesBinding fragmentReleasesBinding;
|
2022-08-15 16:26:02 +02:00
|
|
|
private List<Release> releasesList;
|
|
|
|
private OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public ReleasesAdapter(Context ctx, List<Release> releasesMain, FragmentRefreshListener startDownload, String repoOwner, String repoName, FragmentReleasesBinding fragmentReleasesBinding) {
|
|
|
|
this.context = ctx;
|
|
|
|
this.releasesList = releasesMain;
|
|
|
|
this.startDownload = startDownload;
|
|
|
|
this.repoOwner = repoOwner;
|
|
|
|
this.repoName = repoName;
|
|
|
|
this.fragmentReleasesBinding = fragmentReleasesBinding;
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public ReleasesAdapter.ReleasesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_releases, parent, false);
|
|
|
|
return new ReleasesAdapter.ReleasesViewHolder(v);
|
|
|
|
}
|
2021-08-02 20:04:33 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ReleasesAdapter.ReleasesViewHolder holder, int position) {
|
|
|
|
|
|
|
|
final TinyDB tinyDb = TinyDB.getInstance(context);
|
|
|
|
final Locale locale = context.getResources().getConfiguration().locale;
|
|
|
|
final String timeFormat = tinyDb.getString("dateFormat", "pretty");
|
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
|
|
|
|
|
|
|
|
Release currentItem = releasesList.get(position);
|
|
|
|
holder.releases = currentItem;
|
|
|
|
|
|
|
|
holder.releaseName.setText(currentItem.getName());
|
|
|
|
|
|
|
|
if(currentItem.isPrerelease()) {
|
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_pre_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseTypePre);
|
|
|
|
}
|
|
|
|
else if(currentItem.isDraft()) {
|
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_draft_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseDraftText);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
holder.releaseType.setBackgroundResource(R.drawable.shape_stable_release);
|
|
|
|
holder.releaseType.setText(R.string.releaseTypeStable);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
holder.authorName.setText(context.getResources().getString(R.string.releasePublishedBy, currentItem.getAuthor().getLogin()));
|
|
|
|
|
|
|
|
if(currentItem.getTagName() != null) {
|
|
|
|
holder.releaseTag.setText(currentItem.getTagName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(currentItem.getPublishedAt() != null) {
|
|
|
|
holder.releaseDate.setText(TimeHelper.formatTime(currentItem.getPublishedAt(), locale, timeFormat, context));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(timeFormat.equals("pretty")) {
|
|
|
|
holder.releaseDate.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(currentItem.getPublishedAt()), context));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!currentItem.getBody().equals("")) {
|
|
|
|
Markdown.render(context, currentItem.getBody(), holder.releaseBodyContent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
holder.releaseBodyContent.setText(R.string.noReleaseBodyContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
holder.downloadCopyFrame.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
if(holder.downloads.getVisibility() == View.GONE) {
|
|
|
|
|
|
|
|
holder.downloadDropdownIcon.setImageResource(R.drawable.ic_chevron_down);
|
|
|
|
holder.downloads.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
holder.downloadDropdownIcon.setImageResource(R.drawable.ic_chevron_right);
|
|
|
|
holder.downloads.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
holder.releaseZipDownloadFrame.setOnClickListener(v -> startDownload.onRefresh(currentItem.getZipballUrl()));
|
|
|
|
holder.releaseTarDownloadFrame.setOnClickListener(v -> startDownload.onRefresh(currentItem.getTarballUrl()));
|
|
|
|
|
|
|
|
ReleasesDownloadsAdapter adapter = new ReleasesDownloadsAdapter(currentItem.getAssets(), startDownload);
|
|
|
|
holder.downloadList.setAdapter(adapter);
|
|
|
|
|
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
|
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!((RepoDetailActivity) context).repository.getPermissions().isPush()) {
|
|
|
|
holder.optionsMenu.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return releasesList.size();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
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 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();
|
|
|
|
}
|
2022-05-01 12:00:02 +02:00
|
|
|
|
|
|
|
private void updateAdapter(int position) {
|
|
|
|
releasesList.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
notifyItemRangeChanged(position, releasesList.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteRelease(final Context context, final String releaseName, final Long releaseId, final String owner, final String repo, int position) {
|
|
|
|
|
2022-08-07 21:34:02 +02:00
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_Material3_Dialog_Alert);
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
materialAlertDialogBuilder.setTitle(String.format(context.getString(R.string.deleteGenericTitle), releaseName)).setMessage(R.string.deleteReleaseConfirmation)
|
|
|
|
.setPositiveButton(R.string.menuDeleteText, (dialog, whichButton) -> RetrofitClient.getApiInterface(context).repoDeleteRelease(owner, repo, releaseId).enqueue(new Callback<>() {
|
2022-05-01 12:00:02 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
|
2022-05-01 12:00:02 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
if(response.isSuccessful()) {
|
|
|
|
updateAdapter(position);
|
|
|
|
Toasty.success(context, context.getString(R.string.releaseDeleted));
|
|
|
|
MainActivity.reloadRepos = true;
|
|
|
|
if(getItemCount() == 0) {
|
|
|
|
fragmentReleasesBinding.noDataReleases.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(response.code() == 403) {
|
|
|
|
Toasty.error(context, context.getString(R.string.authorizeError));
|
|
|
|
}
|
|
|
|
else {
|
2022-05-01 12:00:02 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
})).setNeutralButton(R.string.cancelButton, null).show();
|
2022-05-01 12:00:02 +02:00
|
|
|
}
|
2022-08-15 16:26:02 +02:00
|
|
|
|
|
|
|
public interface OnLoadMoreListener {
|
|
|
|
|
|
|
|
void onLoadMore();
|
|
|
|
|
|
|
|
void onLoadFinished();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class ReleasesViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
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;
|
|
|
|
private final LinearLayout downloadCopyFrame;
|
|
|
|
private final LinearLayout downloads;
|
|
|
|
private final LinearLayout releaseZipDownloadFrame;
|
|
|
|
private final LinearLayout releaseTarDownloadFrame;
|
|
|
|
private final ImageView downloadDropdownIcon;
|
|
|
|
private final RecyclerView downloadList;
|
|
|
|
private final ImageView optionsMenu;
|
|
|
|
private Release releases;
|
|
|
|
|
|
|
|
private ReleasesViewHolder(View itemView) {
|
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
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);
|
|
|
|
TextView releaseCommitSha = itemView.findViewById(R.id.releaseCommitSha);
|
|
|
|
releaseDate = itemView.findViewById(R.id.releaseDate);
|
|
|
|
releaseBodyContent = itemView.findViewById(R.id.releaseBodyContent);
|
|
|
|
downloadCopyFrame = itemView.findViewById(R.id.downloadCopyFrame);
|
|
|
|
downloads = itemView.findViewById(R.id.downloads);
|
|
|
|
releaseZipDownloadFrame = itemView.findViewById(R.id.releaseZipDownloadFrame);
|
|
|
|
releaseTarDownloadFrame = itemView.findViewById(R.id.releaseTarDownloadFrame);
|
|
|
|
downloadDropdownIcon = itemView.findViewById(R.id.downloadDropdownIcon);
|
|
|
|
downloadList = itemView.findViewById(R.id.downloadList);
|
|
|
|
optionsMenu = itemView.findViewById(R.id.releasesOptionsMenu);
|
|
|
|
|
|
|
|
downloadList.setHasFixedSize(true);
|
|
|
|
downloadList.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
|
|
|
|
|
|
|
|
new Handler().postDelayed(() -> {
|
|
|
|
if(!AppUtil.checkGhostUsers(releases.getAuthor().getLogin())) {
|
|
|
|
|
|
|
|
authorAvatar.setOnClickListener(loginId -> {
|
|
|
|
Context context = loginId.getContext();
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, ProfileActivity.class);
|
|
|
|
intent.putExtra("username", releases.getAuthor().getLogin());
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
optionsMenu.setOnClickListener(v -> {
|
|
|
|
final Context context = v.getContext();
|
|
|
|
|
|
|
|
View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_release_in_list, itemView.findViewById(android.R.id.content), false);
|
|
|
|
|
|
|
|
TextView deleteRelease = view.findViewById(R.id.deleteRelease);
|
|
|
|
|
|
|
|
BottomSheetDialog dialog = new BottomSheetDialog(context);
|
|
|
|
dialog.setContentView(view);
|
|
|
|
dialog.show();
|
|
|
|
|
|
|
|
deleteRelease.setOnClickListener(v1 -> {
|
|
|
|
deleteRelease(context, releases.getName(), releases.getId(), repoOwner, repoName, getBindingAdapterPosition());
|
|
|
|
dialog.dismiss();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|