2019-09-17 08:17:05 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.PorterDuff;
|
2019-09-17 08:17:05 +02:00
|
|
|
import android.os.Bundle;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2020-03-29 16:24:38 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2019-09-18 11:49:29 +02:00
|
|
|
import android.widget.AdapterView;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.Spinner;
|
2020-04-28 04:08:12 +02:00
|
|
|
import android.widget.TextView;
|
2019-09-18 10:25:49 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import com.google.gson.JsonElement;
|
2019-09-17 08:17:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
2019-09-18 10:25:49 +02:00
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.helpers.AlertDialogs;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2019-09-18 10:25:49 +02:00
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2019-09-18 10:25:49 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import org.mian.gitnex.models.Branches;
|
2020-08-18 16:37:10 +02:00
|
|
|
import org.mian.gitnex.models.DeleteFile;
|
|
|
|
import org.mian.gitnex.models.EditFile;
|
2019-09-18 10:25:49 +02:00
|
|
|
import org.mian.gitnex.models.NewFile;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2019-09-17 08:17:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-03-17 11:32:03 +01:00
|
|
|
public class CreateFileActivity extends BaseActivity {
|
2019-09-17 08:17:05 +02:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
public ImageView closeActivity;
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private Button newFileCreate;
|
|
|
|
|
|
|
|
private EditText newFileName;
|
|
|
|
private EditText newFileContent;
|
|
|
|
private EditText newFileBranchName;
|
|
|
|
private EditText newFileCommitMessage;
|
|
|
|
private Spinner newFileBranchesSpinner;
|
2020-08-18 16:37:10 +02:00
|
|
|
private String filePath;
|
|
|
|
private String fileSha;
|
|
|
|
private int fileAction = 0; // 0 = create, 1 = delete, 2 = edit
|
2019-09-18 10:25:49 +02:00
|
|
|
final Context ctx = this;
|
2020-04-28 15:47:25 +02:00
|
|
|
private Context appCtx;
|
2020-08-18 16:37:10 +02:00
|
|
|
private TinyDB tinyDb;
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
List<Branches> branchesList = new ArrayList<>();
|
|
|
|
|
2019-09-17 08:17:05 +02:00
|
|
|
@Override
|
2020-03-04 20:37:28 +01:00
|
|
|
protected int getLayoutResourceId(){
|
|
|
|
return R.layout.activity_new_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2019-09-17 08:17:05 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-04-28 15:47:25 +02:00
|
|
|
appCtx = getApplicationContext();
|
2020-08-18 16:37:10 +02:00
|
|
|
tinyDb = new TinyDB(appCtx);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
closeActivity = findViewById(R.id.close);
|
|
|
|
newFileName = findViewById(R.id.newFileName);
|
|
|
|
newFileContent = findViewById(R.id.newFileContent);
|
|
|
|
newFileBranchName = findViewById(R.id.newFileBranchName);
|
|
|
|
newFileCommitMessage = findViewById(R.id.newFileCommitMessage);
|
2020-04-28 04:08:12 +02:00
|
|
|
TextView branchNameId = findViewById(R.id.branchNameId);
|
|
|
|
TextView branchNameHintText = findViewById(R.id.branchNameHintText);
|
2020-08-18 16:37:10 +02:00
|
|
|
TextView toolbarTitle = findViewById(R.id.toolbarTitle);
|
|
|
|
TextView fileNameHint = findViewById(R.id.fileNameHint);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
newFileName.requestFocus();
|
|
|
|
assert imm != null;
|
|
|
|
imm.showSoftInput(newFileName, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
newFileCreate = findViewById(R.id.newFileCreate);
|
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
if(getIntent().getStringExtra("filePath") != null && getIntent().getIntExtra("fileAction", 1) == 1) {
|
|
|
|
|
|
|
|
fileNameHint.setVisibility(View.GONE);
|
|
|
|
fileAction = getIntent().getIntExtra("fileAction", 1);
|
|
|
|
|
|
|
|
filePath = getIntent().getStringExtra("filePath");
|
|
|
|
String fileContents = getIntent().getStringExtra("fileContents");
|
|
|
|
fileSha = getIntent().getStringExtra("fileSha");
|
|
|
|
|
|
|
|
toolbarTitle.setText(getString(R.string.deleteFileText, filePath));
|
|
|
|
|
|
|
|
newFileCreate.setText(R.string.deleteFile);
|
|
|
|
newFileName.setText(filePath);
|
|
|
|
newFileName.setEnabled(false);
|
|
|
|
newFileName.setFocusable(false);
|
|
|
|
|
|
|
|
newFileContent.setText(fileContents);
|
|
|
|
newFileContent.setEnabled(false);
|
|
|
|
newFileContent.setFocusable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getIntent().getStringExtra("filePath") != null && getIntent().getIntExtra("fileAction", 2) == 2) {
|
|
|
|
|
|
|
|
fileNameHint.setVisibility(View.GONE);
|
|
|
|
fileAction = getIntent().getIntExtra("fileAction", 2);
|
|
|
|
|
|
|
|
filePath = getIntent().getStringExtra("filePath");
|
|
|
|
String fileContents = getIntent().getStringExtra("fileContents");
|
|
|
|
fileSha = getIntent().getStringExtra("fileSha");
|
|
|
|
|
|
|
|
toolbarTitle.setText(getString(R.string.editFileText, filePath));
|
|
|
|
|
|
|
|
newFileCreate.setText(R.string.editFile);
|
|
|
|
newFileName.setText(filePath);
|
|
|
|
newFileName.setEnabled(false);
|
|
|
|
newFileName.setFocusable(false);
|
|
|
|
|
|
|
|
newFileContent.setText(fileContents);
|
|
|
|
}
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
newFileBranchesSpinner = findViewById(R.id.newFileBranchesSpinner);
|
2020-08-04 22:50:04 +02:00
|
|
|
newFileBranchesSpinner.getBackground().setColorFilter(getResources().getColor(R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
|
2019-09-18 10:25:49 +02:00
|
|
|
getBranches(instanceUrl, instanceToken, repoOwner, repoName, loginUid);
|
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
newFileBranchesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
|
|
|
|
{
|
|
|
|
public void onItemSelected(AdapterView<?> arg0,
|
|
|
|
View arg1, int arg2, long arg3)
|
|
|
|
{
|
|
|
|
Branches bModelValue = (Branches) newFileBranchesSpinner.getSelectedItem();
|
|
|
|
|
|
|
|
if(bModelValue.toString().equals("No branch")) {
|
|
|
|
newFileBranchName.setEnabled(true);
|
2020-04-28 04:08:12 +02:00
|
|
|
newFileBranchName.setVisibility(View.VISIBLE);
|
|
|
|
branchNameId.setVisibility(View.VISIBLE);
|
|
|
|
branchNameHintText.setVisibility(View.VISIBLE);
|
2019-09-18 11:49:29 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
newFileBranchName.setEnabled(false);
|
2020-04-28 04:08:12 +02:00
|
|
|
newFileBranchName.setVisibility(View.GONE);
|
|
|
|
branchNameId.setVisibility(View.GONE);
|
|
|
|
branchNameHintText.setVisibility(View.GONE);
|
2019-09-18 11:49:29 +02:00
|
|
|
newFileBranchName.setText("");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onNothingSelected(AdapterView<?> arg0) {}
|
|
|
|
});
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
disableProcessButton();
|
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
|
|
|
newFileCreate.setEnabled(false);
|
2020-08-06 17:16:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
newFileCreate.setOnClickListener(createFileListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-28 04:08:12 +02:00
|
|
|
private View.OnClickListener createFileListener = v -> processNewFile();
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
private void processNewFile() {
|
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-09-18 10:25:49 +02:00
|
|
|
AppUtil appUtil = new AppUtil();
|
2020-04-28 15:47:25 +02:00
|
|
|
TinyDB tinyDb = new TinyDB(appCtx);
|
2019-09-18 10:25:49 +02:00
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
|
|
|
|
String newFileName_ = newFileName.getText().toString();
|
|
|
|
String newFileContent_ = newFileContent.getText().toString();
|
|
|
|
String newFileBranchName_ = newFileBranchName.getText().toString();
|
|
|
|
String newFileCommitMessage_ = newFileCommitMessage.getText().toString();
|
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
Branches currentBranch = (Branches) newFileBranchesSpinner.getSelectedItem();
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getResources().getString(R.string.checkNetConnection));
|
2019-09-18 10:25:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newFileName_.equals("") || newFileContent_.equals("") || newFileCommitMessage_.equals("")) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.newFileRequiredFields));
|
2019-09-18 10:25:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
if(currentBranch.toString().equals("No branch")) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
if(newFileBranchName_.equals("")) {
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.newFileRequiredFieldNewBranchName));
|
2019-09-18 11:49:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(!appUtil.checkStringsWithDash(newFileBranchName_)) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.newFileInvalidBranchName));
|
2019-09-18 11:49:29 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(appUtil.charactersLength(newFileCommitMessage_) > 255) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.warning(ctx, getString(R.string.newFileCommitMessageError));
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
disableProcessButton();
|
2020-08-18 16:37:10 +02:00
|
|
|
|
|
|
|
if(fileAction == 1) {
|
|
|
|
|
|
|
|
deleteFile(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName, filePath,
|
|
|
|
newFileBranchName_, newFileCommitMessage_, currentBranch.toString(), fileSha);
|
|
|
|
}
|
|
|
|
else if(fileAction == 2) {
|
|
|
|
|
|
|
|
editFile(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName, filePath,
|
|
|
|
appUtil.encodeBase64(newFileContent_), newFileBranchName_, newFileCommitMessage_, currentBranch.toString(), fileSha);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
createNewFile(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName, newFileName_,
|
|
|
|
appUtil.encodeBase64(newFileContent_), newFileBranchName_, newFileCommitMessage_, currentBranch.toString());
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
private void createNewFile(final String instanceUrl, final String token, String repoOwner, String repoName, String fileName, String fileContent, String fileBranchName, String fileCommitMessage, String currentBranch) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2019-09-18 11:49:29 +02:00
|
|
|
NewFile createNewFileJsonStr;
|
|
|
|
if(currentBranch.equals("No branch")) {
|
|
|
|
createNewFileJsonStr = new NewFile("", fileContent, fileCommitMessage, fileBranchName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
createNewFileJsonStr = new NewFile(currentBranch, fileContent, fileCommitMessage, "");
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
2020-04-28 15:47:25 +02:00
|
|
|
.getInstance(instanceUrl, ctx)
|
2019-09-18 10:25:49 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.createNewFile(token, repoOwner, repoName, fileName, createNewFileJsonStr);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
|
|
|
if(response.code() == 201) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.success(ctx, getString(R.string.newFileSuccessMessage));
|
2019-09-18 10:25:49 +02:00
|
|
|
finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 401) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
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() == 404) {
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.warning(ctx, getString(R.string.apiNotFound));
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.orgCreatedError));
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
private void deleteFile(final String instanceUrl, final String token, String repoOwner, String repoName, String fileName, String fileBranchName, String fileCommitMessage, String currentBranch, String fileSha) {
|
|
|
|
|
|
|
|
String branchName;
|
|
|
|
DeleteFile deleteFileJsonStr;
|
|
|
|
if(currentBranch.equals("No branch")) {
|
|
|
|
|
|
|
|
branchName = fileBranchName;
|
|
|
|
deleteFileJsonStr = new DeleteFile("", fileCommitMessage, fileBranchName, fileSha);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
branchName = currentBranch;
|
|
|
|
deleteFileJsonStr = new DeleteFile(currentBranch, fileCommitMessage, "", fileSha);
|
|
|
|
}
|
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
|
|
|
.getInstance(instanceUrl, ctx)
|
|
|
|
.getApiInterface()
|
|
|
|
.deleteFile(token, repoOwner, repoName, fileName, deleteFileJsonStr);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.deleteFileMessage, branchName));
|
|
|
|
getIntent().removeExtra("filePath");
|
|
|
|
getIntent().removeExtra("fileSha");
|
|
|
|
getIntent().removeExtra("fileContents");
|
|
|
|
finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 401) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
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() == 404) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.apiNotFound));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void editFile(final String instanceUrl, final String token, String repoOwner, String repoName, String fileName, String fileContent, String fileBranchName, String fileCommitMessage, String currentBranch, String fileSha) {
|
|
|
|
|
|
|
|
String branchName;
|
|
|
|
EditFile editFileJsonStr;
|
|
|
|
if(currentBranch.equals("No branch")) {
|
|
|
|
|
|
|
|
branchName = fileBranchName;
|
|
|
|
editFileJsonStr = new EditFile("", fileCommitMessage, fileBranchName, fileSha, fileContent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
branchName = currentBranch;
|
|
|
|
editFileJsonStr = new EditFile(currentBranch, fileCommitMessage, "", fileSha, fileContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
|
|
|
.getInstance(instanceUrl, ctx)
|
|
|
|
.getApiInterface()
|
|
|
|
.editFile(token, repoOwner, repoName, fileName, editFileJsonStr);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.editFileMessage, branchName));
|
|
|
|
getIntent().removeExtra("filePath");
|
|
|
|
getIntent().removeExtra("fileSha");
|
|
|
|
getIntent().removeExtra("fileContents");
|
|
|
|
tinyDb.putBoolean("fileModified", true);
|
|
|
|
finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 401) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
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() == 404) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.apiNotFound));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
private void getBranches(String instanceUrl, String instanceToken, String repoOwner, String repoName, String loginUid) {
|
|
|
|
|
|
|
|
Call<List<Branches>> call = RetrofitClient
|
2020-04-28 15:47:25 +02:00
|
|
|
.getInstance(instanceUrl, ctx)
|
2019-09-18 10:25:49 +02:00
|
|
|
.getApiInterface()
|
2020-04-28 15:47:25 +02:00
|
|
|
.getBranches(Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Branches>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Branches>> call, @NonNull retrofit2.Response<List<Branches>> response) {
|
|
|
|
|
|
|
|
if(response.isSuccessful()) {
|
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
List<Branches> branchesList_ = response.body();
|
|
|
|
|
|
|
|
branchesList.add(new Branches("No branch"));
|
|
|
|
assert branchesList_ != null;
|
|
|
|
if(branchesList_.size() > 0) {
|
|
|
|
for (int i = 0; i < branchesList_.size(); i++) {
|
|
|
|
|
|
|
|
Branches data = new Branches(
|
|
|
|
branchesList_.get(i).getName()
|
|
|
|
);
|
|
|
|
branchesList.add(data);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:36:07 +02:00
|
|
|
ArrayAdapter<Branches> adapter = new ArrayAdapter<>(CreateFileActivity.this,
|
2019-09-18 10:25:49 +02:00
|
|
|
R.layout.spinner_item, branchesList);
|
|
|
|
|
|
|
|
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
|
|
|
newFileBranchesSpinner.setAdapter(adapter);
|
|
|
|
enableProcessButton();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Branches>> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
2020-04-28 04:08:12 +02:00
|
|
|
onClickListener = view -> finish();
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void disableProcessButton() {
|
|
|
|
|
|
|
|
newFileCreate.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableProcessButton() {
|
|
|
|
|
|
|
|
newFileCreate.setEnabled(true);
|
2019-09-17 08:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|