Refactor listview to recylcerview

This commit is contained in:
M M Arif 2022-06-29 13:49:18 +05:00
parent 180cc671d0
commit 00003a1085
11 changed files with 53 additions and 140 deletions

View file

@ -158,15 +158,15 @@ public class CommitsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
commitSha.setText(commitsModel.getSha().substring(0, Math.min(commitsModel.getSha().length(), 10)));
rootView.setOnClickListener(v -> {
if(context instanceof CommitsActivity) {
Intent intent = ((CommitsActivity) context).repository.getIntent(context, CommitDetailActivity.class);
intent.putExtra("sha", commitsModel.getSha());
context.startActivity(intent);
} else {
Intent intent = IssueContext.fromIntent(((DiffActivity) context).getIntent()).getRepository().getIntent(context, CommitDetailActivity.class);
intent.putExtra("sha", commitsModel.getSha());
context.startActivity(intent);
}
Intent intent;
if(context instanceof CommitsActivity) {
intent = ((CommitsActivity) context).repository.getIntent(context, CommitDetailActivity.class);
} else {
intent = IssueContext.fromIntent(((DiffActivity) context).getIntent()).getRepository()
.getIntent(context, CommitDetailActivity.class);
}
intent.putExtra("sha", commitsModel.getSha());
context.startActivity(intent);
});
}

View file

@ -1,90 +0,0 @@
package org.mian.gitnex.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import org.mian.gitnex.R;
import org.mian.gitnex.helpers.FileDiffView;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author opyale
*/
public class DiffFilesAdapter extends BaseAdapter {
private static final Pattern statisticsPattern = Pattern.compile("(\\d+).*?,.*?(\\d+)");
private final Context context;
private final List<FileDiffView> fileDiffViews;
public DiffFilesAdapter(Context context, List<FileDiffView> fileDiffViews) {
this.context = context;
this.fileDiffViews = fileDiffViews;
}
private static class ViewHolder {
private final TextView fileName;
private final TextView fileStatistics;
public ViewHolder(TextView fileName, TextView fileStatistics) {
this.fileName = fileName;
this.fileStatistics = fileStatistics;
}
}
@Override
public int getCount() {
return fileDiffViews.size();
}
@Override
public Object getItem(int position) {
return fileDiffViews.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_diff_files, parent, false);
viewHolder = new ViewHolder(
convertView.findViewById(R.id.fileName),
convertView.findViewById(R.id.fileStatistics)
);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
FileDiffView fileDiffView = fileDiffViews.get(position);
viewHolder.fileName.setText(fileDiffView.getFileName());
Matcher matcher = statisticsPattern.matcher(fileDiffView.getFileInfo());
if(matcher.find() && matcher.groupCount() == 2) {
viewHolder.fileStatistics.setText(context.getString(R.string.diffStatistics, matcher.group(1), matcher.group(2)));
} else {
viewHolder.fileStatistics.setText(fileDiffView.getFileInfo());
}
return convertView;
}
}

View file

@ -18,7 +18,6 @@ import org.mian.gitnex.R;
import org.mian.gitnex.actions.MilestoneActions;
import org.mian.gitnex.activities.RepoDetailActivity;
import org.mian.gitnex.helpers.ClickListener;
import org.mian.gitnex.helpers.Constants;
import org.mian.gitnex.helpers.Markdown;
import org.mian.gitnex.helpers.TimeHelper;
import org.mian.gitnex.helpers.TinyDB;

View file

@ -10,7 +10,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import org.mian.gitnex.databinding.BottomSheetDraftsBinding;
import org.mian.gitnex.helpers.Constants;
import org.mian.gitnex.structs.BottomSheetListener;
/**

View file

@ -12,6 +12,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.text.HtmlCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.vdurmont.emoji.EmojiParser;
import org.gitnex.tea4j.v2.models.Commit;
import org.mian.gitnex.R;
@ -29,7 +30,9 @@ import org.mian.gitnex.helpers.ParseDiff;
import org.mian.gitnex.helpers.RoundedTransformation;
import org.mian.gitnex.helpers.TimeHelper;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.contexts.IssueContext;
import org.mian.gitnex.helpers.contexts.RepositoryContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import retrofit2.Call;
@ -46,7 +49,8 @@ public class CommitDetailFragment extends Fragment {
private String repoOwner;
private String repoName;
private String sha;
private List<FileDiffView> fileDiffViews = new ArrayList<>();
private DiffFilesAdapter adapter;
private int loadingFinished = 0;
public static CommitDetailFragment newInstance() {
@ -63,22 +67,24 @@ public class CommitDetailFragment extends Fragment {
binding = FragmentCommitDetailsBinding.inflate(getLayoutInflater(), container, false);
IssueContext issue = IssueContext.fromIntent(requireActivity().getIntent());
RepositoryContext repository = RepositoryContext.fromIntent(requireActivity().getIntent());
repoOwner = repository.getOwner();
repoName = repository.getName();
sha = requireActivity().getIntent().getStringExtra("sha");
binding.toolbarTitle.setText(sha.substring(0, Math.min(sha.length(), 10)));
adapter = new DiffFilesAdapter(requireContext(), fileDiffViews, issue, "commit");
binding.diffFiles.setHasFixedSize(true);
binding.diffFiles.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.diffFiles.setAdapter(adapter);
getCommit();
getDiff();
binding.close.setOnClickListener((v) -> requireActivity().finish());
binding.diffFiles.setOnItemClickListener((parent, view, position, id) -> requireActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, DiffFragment.newInstance((FileDiffView) parent.getItemAtPosition(position), "commit"))
.commit());
return binding.getRoot();
}
@ -93,15 +99,16 @@ public class CommitDetailFragment extends Fragment {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
checkLoading();
assert response.body() != null;
switch(response.code()) {
case 200:
List<FileDiffView> fileDiffViews;
assert response.body() != null;
fileDiffViews = ParseDiff.getFileDiffViewArray(response.body());
DiffFilesAdapter adapter = new DiffFilesAdapter(requireContext(), fileDiffViews);
requireActivity().runOnUiThread(() -> binding.diffFiles.setAdapter(adapter));
requireActivity().runOnUiThread(() -> {
adapter.updateList(fileDiffViews);
adapter.notifyDataChanged();
});
break;
case 401:
@ -143,7 +150,7 @@ public class CommitDetailFragment extends Fragment {
binding.getRoot().setOnClickListener((v) -> {
// we need a ClickListener here to prevent that the ItemClickListener of the diffFiles ListView handles clicks for the header
});
CommitDetailFragment.this.binding.diffFiles.addHeaderView(binding.getRoot());
//CommitDetailFragment.this.binding.diffFiles.addHeaderView(binding.getRoot());
Commit commitsModel = response.body();
if(commitsModel == null) {
onFailure(call, new Throwable());

View file

@ -7,6 +7,7 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import org.mian.gitnex.R;
import org.mian.gitnex.adapters.DiffFilesAdapter;
import org.mian.gitnex.clients.RetrofitClient;
@ -17,6 +18,7 @@ import org.mian.gitnex.helpers.ParseDiff;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.contexts.IssueContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Response;
@ -29,6 +31,8 @@ public class DiffFilesFragment extends Fragment {
private FragmentDiffFilesBinding binding;
private Context ctx;
private List<FileDiffView> fileDiffViews = new ArrayList<>();
private DiffFilesAdapter adapter;
public DiffFilesFragment() {}
@ -51,15 +55,15 @@ public class DiffFilesFragment extends Fragment {
binding.progressBar.setVisibility(View.VISIBLE);
binding.toolbarTitle.setText(R.string.processingText);
binding.diffFiles.setOnItemClickListener((parent, view, position, id) -> requireActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, DiffFragment.newInstance((FileDiffView) parent.getItemAtPosition(position), issue))
.commit());
adapter = new DiffFilesAdapter(ctx, fileDiffViews, issue, "");
binding.diffFiles.setHasFixedSize(true);
binding.diffFiles.setLayoutManager(new LinearLayoutManager(ctx));
binding.diffFiles.setAdapter(adapter);
getPullDiffFiles(issue.getRepository().getOwner(), issue.getRepository().getName(), String.valueOf(issue.getIssueIndex()));
return binding.getRoot();
}
private void getPullDiffFiles(String owner, String repo, String pullIndex) {
@ -80,7 +84,7 @@ public class DiffFilesFragment extends Fragment {
switch(response.code()) {
case 200:
List<FileDiffView> fileDiffViews = ParseDiff.getFileDiffViewArray(response.body());
fileDiffViews = ParseDiff.getFileDiffViewArray(response.body());
int filesCount = fileDiffViews.size();
@ -88,12 +92,11 @@ public class DiffFilesFragment extends Fragment {
getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount)) :
getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount));
DiffFilesAdapter adapter = new DiffFilesAdapter(ctx, fileDiffViews);
requireActivity().runOnUiThread(() -> {
binding.progressBar.setVisibility(View.GONE);
binding.diffFiles.setAdapter(adapter);
binding.toolbarTitle.setText(toolbarTitleText);
adapter.updateList(fileDiffViews);
adapter.notifyDataChanged();
});
break;
@ -118,7 +121,5 @@ public class DiffFilesFragment extends Fragment {
});
thread.start();
}
}

View file

@ -1,6 +1,5 @@
package org.mian.gitnex.fragments;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -9,13 +8,13 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.google.android.material.tabs.TabLayoutMediator;
import org.jetbrains.annotations.NotNull;
import org.mian.gitnex.R;
import org.mian.gitnex.databinding.FragmentPrChangesBinding;
/**
* @author qwerty287
*/
public class PullRequestChangesFragment extends Fragment {
private FragmentPrChangesBinding binding;
@ -42,9 +41,9 @@ public class PullRequestChangesFragment extends Fragment {
public Fragment createFragment(int position) {
if(position == 0) {
return pullRequestCommitsFragment;
} else {
return diffFilesFragment;
} else {
return pullRequestCommitsFragment;
}
}
@ -54,11 +53,9 @@ public class PullRequestChangesFragment extends Fragment {
return 2;
}
});
String[] tabs = new String[]{getString(R.string.commits), getString(R.string.tabTextFiles)};
String[] tabs = new String[]{getString(R.string.tabTextFiles), getString(R.string.commits)};
new TabLayoutMediator(binding.tabs, binding.container, (tab, position) -> tab.setText(tabs[position])).attach();
return binding.getRoot();
}
}

View file

@ -7,8 +7,8 @@ import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

View file

@ -2,7 +2,6 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
@ -58,15 +57,13 @@
style="@style/Widget.MaterialComponents.LinearProgressIndicator"
app:indicatorColor="?attr/progressIndicatorColor" />
<ListView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/diff_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:nestedScrollingEnabled="false"
android:background="?attr/primaryBackgroundColor"
android:headerDividersEnabled="true"
android:fastScrollEnabled="true"
tools:listitem="@layout/list_diff_files" />
android:background="?attr/primaryBackgroundColor"
android:scrollbars="vertical" />
</LinearLayout>

View file

@ -28,9 +28,11 @@
android:layout_marginBottom="8dp"
android:textSize="15sp" />
<ListView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/diff_files"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:background="?attr/primaryBackgroundColor"
android:scrollbars="vertical" />
</LinearLayout>

View file

@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"