GitNex/app/src/main/java/org/mian/gitnex/activities/FileDiffActivity.java

160 lines
4.7 KiB
Java
Raw Normal View History

2019-11-24 13:42:57 +01:00
package org.mian.gitnex.activities;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
Increasing usability and design of files diff. (#413) Applying sizes. Additional changes. First changes. Merge branch 'master' into diff-cleaner Fixing formatting. Merge branch 'master' into diff-cleaner Final changes for working with custom themes. Merge remote-tracking branch 'remotes/main/master' into diff-cleaner First changes for working with custom themes. Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Adding custom COLOR_FONT. Even smaller cleanups. Merge remote-tracking branch 'remotes/main/master' into diff-cleaner Small cleanups. Fixing bug and adding maximum line limit. Adding option to set cursor to end and small cleanup. First aid. Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Merge branch 'master' into diff-cleaner Few improvements. Performance improvements and cleanups. Minor improvements (code in order) and many bug fixes Bug fix. Combining cited code. Adding code commenting option. Renaming list_files_diffs_new to list_files_diffs Moving ProcessBar into center Increasing performance. Applying size to all icons globally. Removing another unused file. Merge remote-tracking branch 'remotes/main/master' into diff-cleaner Removing unused files. Changing size of 'close'-button. Major changes concerning design and bug fixes. Temporary save point. 2 1 Co-authored-by: opyale <example@example.com> Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/413 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: M M Arif <mmarif@swatian.com>
2020-04-28 14:39:41 +02:00
import android.widget.ListView;
2019-11-24 13:42:57 +01:00
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import org.gitnex.tea4j.models.FileDiffView;
2019-11-24 13:42:57 +01:00
import org.mian.gitnex.R;
import org.mian.gitnex.adapters.FilesDiffAdapter;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.databinding.ActivityFileDiffBinding;
2019-11-24 13:42:57 +01:00
import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.ParseDiff;
import org.mian.gitnex.helpers.TinyDB;
2019-11-24 13:42:57 +01:00
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.Version;
2019-11-24 13:42:57 +01:00
import java.io.IOException;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
/**
* Author M M Arif
*/
2020-03-04 20:37:28 +01:00
public class FileDiffActivity extends BaseActivity {
2019-11-24 13:42:57 +01:00
private View.OnClickListener onClickListener;
private TextView toolbarTitle;
private ListView mListView;
private ProgressBar mProgressBar;
2019-11-24 13:42:57 +01:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-11-24 13:42:57 +01:00
ActivityFileDiffBinding activityFileDiffBinding = ActivityFileDiffBinding.inflate(getLayoutInflater());
setContentView(activityFileDiffBinding.getRoot());
Toolbar toolbar = activityFileDiffBinding.toolbar;
setSupportActionBar(toolbar);
2019-11-24 13:42:57 +01:00
final TinyDB tinyDb = TinyDB.getInstance(appCtx);
String repoFullName = tinyDb.getString("repoFullName");
String[] parts = repoFullName.split("/");
final String repoOwner = parts[0];
final String repoName = parts[1];
final String loginUid = tinyDb.getString("loginUid");
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
2019-11-24 13:42:57 +01:00
ImageView closeActivity = activityFileDiffBinding.close;
toolbarTitle = activityFileDiffBinding.toolbarTitle;
mListView = activityFileDiffBinding.listView;
mProgressBar = activityFileDiffBinding.progressBar;
2019-11-24 13:42:57 +01:00
mListView.setDivider(null);
2019-11-24 13:42:57 +01:00
toolbarTitle.setText(R.string.processingText);
initCloseListener();
closeActivity.setOnClickListener(onClickListener);
2019-11-24 13:42:57 +01:00
mProgressBar.setVisibility(View.VISIBLE);
2019-11-24 13:42:57 +01:00
String pullIndex = tinyDb.getString("issueNumber");
2019-11-24 13:42:57 +01:00
boolean apiCall = !new Version(tinyDb.getString("giteaVersion")).less("1.13.0");
getPullDiffContent(repoOwner, repoName, pullIndex, instanceToken, apiCall);
2019-11-24 13:42:57 +01:00
}
2019-11-24 13:42:57 +01:00
private void getPullDiffContent(String owner, String repo, String pullIndex, String token, boolean apiCall) {
2019-11-24 13:42:57 +01:00
Call<ResponseBody> call;
if(apiCall) {
call = RetrofitClient.getApiInterface(ctx).getPullDiffContent(token, owner, repo, pullIndex);
}
else {
call = RetrofitClient.getWebInterface(ctx).getPullDiffContent(owner, repo, pullIndex);
}
2019-11-24 13:42:57 +01:00
call.enqueue(new Callback<ResponseBody>() {
2019-11-24 13:42:57 +01:00
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) {
2019-11-24 13:42:57 +01:00
if(response.code() == 200) {
2019-11-24 13:42:57 +01:00
try {
assert response.body() != null;
2019-11-24 13:42:57 +01:00
List<FileDiffView> fileContentsArray = ParseDiff.getFileDiffViewArray(response.body().string());
2019-11-24 13:42:57 +01:00
int filesCount = fileContentsArray.size();
if(filesCount > 1) {
toolbarTitle.setText(getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount)));
}
else {
toolbarTitle.setText(getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount)));
}
2019-11-24 13:42:57 +01:00
FilesDiffAdapter adapter = new FilesDiffAdapter(ctx, getSupportFragmentManager(), fileContentsArray);
mListView.setAdapter(adapter);
2019-11-24 13:42:57 +01:00
mProgressBar.setVisibility(View.GONE);
}
catch(IOException e) {
e.printStackTrace();
}
}
else if(response.code() == 401) {
2019-11-24 13:42:57 +01:00
AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), getResources().getString(R.string.alertDialogTokenRevokedMessage), getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
}
else if(response.code() == 403) {
2019-11-24 13:42:57 +01:00
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
}
else if(response.code() == 404) {
2019-11-24 13:42:57 +01:00
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
}
else {
2019-11-24 13:42:57 +01:00
Toasty.error(ctx, getString(R.string.labelGeneralError));
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
}
});
}
private void initCloseListener() {
onClickListener = view -> {
getIntent().removeExtra("singleFileName");
finish();
};
}
2019-11-24 13:42:57 +01:00
}