GitNex-Android-App/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter....

241 lines
7.6 KiB
Java
Raw Normal View History

package org.mian.gitnex.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import org.mian.gitnex.R;
2019-10-10 17:49:12 +02:00
import org.mian.gitnex.activities.RepoDetailActivity;
import org.mian.gitnex.clients.PicassoService;
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;
import org.ocpsoft.prettytime.PrettyTime;
/**
* @author M M Arif
*/
public class ExploreRepositoriesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private final TinyDB tinyDb;
private List<org.gitnex.tea4j.v2.models.Repository> reposList;
private Runnable loadMoreListener;
private boolean isLoading = false, isMoreDataAvailable = true;
public ExploreRepositoriesAdapter(
List<org.gitnex.tea4j.v2.models.Repository> dataList, Context ctx) {
this.context = ctx;
this.reposList = dataList;
this.tinyDb = TinyDB.getInstance(context);
}
@NonNull @Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
return new ExploreRepositoriesAdapter.RepositoriesHolder(
inflater.inflate(R.layout.list_repositories, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (position >= getItemCount() - 1
&& isMoreDataAvailable
&& !isLoading
&& loadMoreListener != null) {
isLoading = true;
loadMoreListener.run();
}
((ExploreRepositoriesAdapter.RepositoriesHolder) holder).bindData(reposList.get(position));
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return reposList.size();
}
public void setMoreDataAvailable(boolean moreDataAvailable) {
isMoreDataAvailable = moreDataAvailable;
}
@SuppressLint("NotifyDataSetChanged")
public void notifyDataChanged() {
notifyDataSetChanged();
isLoading = false;
}
public void setLoadMoreListener(Runnable loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
public void updateList(List<org.gitnex.tea4j.v2.models.Repository> list) {
reposList = list;
notifyDataChanged();
}
class RepositoriesHolder extends RecyclerView.ViewHolder {
private final ImageView image;
private final TextView repoName;
private final TextView orgName;
private final TextView repoDescription;
private final TextView repoStars;
private final TextView repoLastUpdated;
private final View spacerView;
private org.gitnex.tea4j.v2.models.Repository userRepositories;
private CheckBox isRepoAdmin;
RepositoriesHolder(View itemView) {
super(itemView);
repoName = itemView.findViewById(R.id.repoName);
orgName = itemView.findViewById(R.id.orgName);
repoDescription = itemView.findViewById(R.id.repoDescription);
isRepoAdmin = itemView.findViewById(R.id.repoIsAdmin);
image = itemView.findViewById(R.id.imageAvatar);
repoStars = itemView.findViewById(R.id.repoStars);
repoLastUpdated = itemView.findViewById(R.id.repoLastUpdated);
spacerView = itemView.findViewById(R.id.spacerView);
itemView.setOnClickListener(
v -> {
Context context = v.getContext();
RepositoryContext repo = new RepositoryContext(userRepositories, context);
repo.saveToDB(context);
Intent intent = repo.getIntent(context, RepoDetailActivity.class);
Implement drafts, introduce Room persistence library for db (#139) Fix no draft message translation updates format improvements typo update some renaming refactors Use better naming convention remove duplicate source arrange draft titles enhance click listener area Launch drafts from reply screen and clean up Add message draft saved update repositories tasks Update user accounts repository with thread, remove async tasks remove async task in drafts update layout, change async to thread in drafts Merge branch 'master' into pull_139 # Conflicts: # app/build.gradle # app/src/main/java/org/mian/gitnex/activities/LoginActivity.java Merge branch 'master' into pull_139 # Conflicts: # app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java Merge branch 'pull_139' of codeberg.org:gitnex/GitNex into pull_139 # Conflicts: # app/src/main/java/org/mian/gitnex/adapters/MyReposListAdapter.java Merge branch 'master' into pull_139 # Conflicts: # app/src/main/java/org/mian/gitnex/activities/LoginActivity.java # app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java # app/src/main/java/org/mian/gitnex/adapters/MyReposListAdapter.java Merge branch 'master' into pull_139 Merge branch 'master' into pull_139 Merge branch 'master' into pull_139 and fix conflicts # Conflicts: # app/build.gradle # app/src/main/java/org/mian/gitnex/activities/LoginActivity.java # app/src/main/java/org/mian/gitnex/activities/MainActivity.java # app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java # app/src/main/java/org/mian/gitnex/adapters/MyReposListAdapter.java # app/src/main/java/org/mian/gitnex/helpers/StaticGlobalVariables.java # app/src/main/res/values/strings.xml Code Format Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft # Conflicts: # app/src/main/java/org/mian/gitnex/activities/MainActivity.java # app/src/main/res/values/strings.xml Go to draft, save on type and other fixes delete all drafts, added messages where needed delete draft Force logout Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft # Conflicts: # app/src/main/java/org/mian/gitnex/activities/MainActivity.java # app/src/main/java/org/mian/gitnex/helpers/StaticGlobalVariables.java check if account data is null, we need to log the user out for the 1st time. Merge branch 'master' into 15-comments-draft fix repo owner, name sequence Add comments for test, show drafts list Add repos to db Add account to db and other refactors to the code Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft Merge branch 'master' into 15-comments-draft # Conflicts: # app/build.gradle # app/src/main/AndroidManifest.xml Merge branch 'master' into 15-comments-draft merge more queries, added dao repositories, layout update Added queries in dao some refactor. added models, dao, entities (accounts, repositories, drafts) WIP on implementing drafts, introduced Room persistence library for db. Co-authored-by: M M Arif <mmarif@swatian.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/139 Reviewed-by: opyale <opyale@noreply.codeberg.org>
2020-07-04 22:51:55 +02:00
context.startActivity(intent);
});
}
@SuppressLint("SetTextI18n")
void bindData(org.gitnex.tea4j.v2.models.Repository userRepositories) {
this.userRepositories = userRepositories;
int imgRadius = AppUtil.getPixelsFromDensity(context, 60);
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
String timeFormat = tinyDb.getString("dateFormat", "pretty");
orgName.setText(userRepositories.getFullName().split("/")[0]);
repoName.setText(userRepositories.getFullName().split("/")[1]);
repoStars.setText(AppUtil.numberFormatter(userRepositories.getStarsCount()));
ColorGenerator generator = ColorGenerator.Companion.getMATERIAL();
int color = generator.getColor(userRepositories.getName());
String firstCharacter = String.valueOf(userRepositories.getFullName().charAt(0));
TextDrawable drawable =
TextDrawable.builder()
.beginConfig()
.useFont(Typeface.DEFAULT)
.fontSize(18)
.toUpperCase()
.width(28)
.height(28)
.endConfig()
.buildRoundRect(firstCharacter, color, 14);
if (userRepositories.getAvatarUrl() != null) {
if (!userRepositories.getAvatarUrl().equals("")) {
PicassoService.getInstance(context)
.get()
.load(userRepositories.getAvatarUrl())
.placeholder(R.drawable.loader_animated)
.transform(new RoundedTransformation(imgRadius, 0))
.resize(120, 120)
.centerCrop()
.into(image);
} else {
image.setImageDrawable(drawable);
}
} else {
image.setImageDrawable(drawable);
}
if (userRepositories.getUpdatedAt() != null) {
switch (timeFormat) {
case "pretty":
{
PrettyTime prettyTime = new PrettyTime(locale);
String createdTime = prettyTime.format(userRepositories.getUpdatedAt());
repoLastUpdated.setText(
context.getString(R.string.lastUpdatedAt, createdTime));
repoLastUpdated.setOnClickListener(
new ClickListener(
TimeHelper.customDateFormatForToastDateFormat(
userRepositories.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(userRepositories.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(userRepositories.getUpdatedAt());
repoLastUpdated.setText(
context.getString(R.string.lastUpdatedAt, createdTime));
break;
}
}
} else {
repoLastUpdated.setVisibility(View.GONE);
}
if (!userRepositories.getDescription().equals("")) {
repoDescription.setVisibility(View.VISIBLE);
repoDescription.setText(userRepositories.getDescription());
spacerView.setVisibility(View.GONE);
} else {
repoDescription.setVisibility(View.GONE);
spacerView.setVisibility(View.VISIBLE);
}
if (isRepoAdmin == null) {
isRepoAdmin = new CheckBox(context);
}
isRepoAdmin.setChecked(userRepositories.getPermissions().isAdmin());
}
}
}