2019-09-17 08:17:05 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
2020-10-30 08:00:52 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.content.Context;
|
2019-09-17 08:17:05 +02:00
|
|
|
import android.os.Bundle;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.util.Log;
|
2020-10-30 08:00:52 +01:00
|
|
|
import android.view.MotionEvent;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.view.View;
|
2020-03-29 16:24:38 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.widget.ArrayAdapter;
|
2020-10-20 20:09:07 +02:00
|
|
|
import android.widget.AutoCompleteTextView;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
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;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.Branches;
|
|
|
|
import org.gitnex.tea4j.models.DeleteFile;
|
|
|
|
import org.gitnex.tea4j.models.EditFile;
|
|
|
|
import org.gitnex.tea4j.models.NewFile;
|
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;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.ActivityCreateFileBinding;
|
2019-09-18 10:25:49 +02:00
|
|
|
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;
|
2021-02-01 19:13:48 +01:00
|
|
|
import org.mian.gitnex.helpers.NetworkStatusObserver;
|
2019-09-18 10:25:49 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
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 newFileCommitMessage;
|
2021-02-01 19:13:48 +01:00
|
|
|
private AutoCompleteTextView newFileBranches;
|
2020-08-18 16:37:10 +02:00
|
|
|
private String filePath;
|
|
|
|
private String fileSha;
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
public static final int FILE_ACTION_CREATE = 0;
|
|
|
|
public static final int FILE_ACTION_DELETE = 1;
|
|
|
|
public static final int FILE_ACTION_EDIT = 2;
|
|
|
|
|
|
|
|
private int fileAction = FILE_ACTION_CREATE;
|
|
|
|
|
|
|
|
private final List<String> branches = new ArrayList<>();
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-10-20 20:09:07 +02:00
|
|
|
private String repoOwner;
|
|
|
|
private String repoName;
|
|
|
|
|
2020-10-30 08:00:52 +01:00
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
2020-03-04 20:37:28 +01:00
|
|
|
@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);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
ActivityCreateFileBinding activityCreateFileBinding = ActivityCreateFileBinding.inflate(getLayoutInflater());
|
2021-01-26 18:43:25 +01:00
|
|
|
setContentView(activityCreateFileBinding.getRoot());
|
2021-01-26 16:10:31 +01:00
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
String repoFullName = tinyDB.getString("repoFullName");
|
2019-09-18 10:25:49 +02:00
|
|
|
String[] parts = repoFullName.split("/");
|
2020-10-20 20:09:07 +02:00
|
|
|
repoOwner = parts[0];
|
|
|
|
repoName = parts[1];
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
closeActivity = activityCreateFileBinding.close;
|
|
|
|
newFileName = activityCreateFileBinding.newFileName;
|
|
|
|
newFileContent = activityCreateFileBinding.newFileContent;
|
|
|
|
newFileCommitMessage = activityCreateFileBinding.newFileCommitMessage;
|
|
|
|
TextView toolbarTitle = activityCreateFileBinding.toolbarTitle;
|
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();
|
2021-02-01 19:13:48 +01:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
newFileCreate = activityCreateFileBinding.newFileCreate;
|
2020-10-30 08:00:52 +01:00
|
|
|
newFileContent.setOnTouchListener((touchView, motionEvent) -> {
|
|
|
|
|
|
|
|
touchView.getParent().requestDisallowInterceptTouchEvent(true);
|
|
|
|
|
|
|
|
if ((motionEvent.getAction() & MotionEvent.ACTION_UP) != 0 && (motionEvent.getActionMasked() & MotionEvent.ACTION_UP) != 0) {
|
|
|
|
|
|
|
|
touchView.getParent().requestDisallowInterceptTouchEvent(false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
if(getIntent().getStringExtra("filePath") != null && getIntent().getIntExtra("fileAction", FILE_ACTION_DELETE) == FILE_ACTION_DELETE) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
fileAction = getIntent().getIntExtra("fileAction", FILE_ACTION_DELETE);
|
2020-08-18 16:37:10 +02:00
|
|
|
|
|
|
|
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);
|
2021-02-01 19:13:48 +01:00
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
if(getIntent().getStringExtra("filePath") != null && getIntent().getIntExtra("fileAction", FILE_ACTION_EDIT) == FILE_ACTION_EDIT) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
fileAction = getIntent().getIntExtra("fileAction", FILE_ACTION_EDIT);
|
2020-08-18 16:37:10 +02:00
|
|
|
|
|
|
|
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);
|
2021-02-01 19:13:48 +01:00
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
newFileBranches = activityCreateFileBinding.newFileBranches;
|
|
|
|
getBranches(repoOwner, repoName);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
disableProcessButton();
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
NetworkStatusObserver networkStatusObserver = NetworkStatusObserver.get(ctx);
|
|
|
|
networkStatusObserver.registerNetworkStatusListener(hasNetworkConnection -> newFileCreate.setEnabled(hasNetworkConnection));
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
newFileCreate.setOnClickListener(createFileListener);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
private final 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();
|
|
|
|
|
|
|
|
String newFileName_ = newFileName.getText().toString();
|
|
|
|
String newFileContent_ = newFileContent.getText().toString();
|
2021-02-01 19:13:48 +01:00
|
|
|
String newFileBranchName_ = newFileBranches.getText().toString();
|
2019-09-18 10:25:49 +02:00
|
|
|
String newFileCommitMessage_ = newFileCommitMessage.getText().toString();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
if(!appUtil.checkStringsWithDash(newFileBranchName_)) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
Toasty.error(ctx, getString(R.string.newFileInvalidBranchName));
|
|
|
|
return;
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
if(newFileCommitMessage_.length() > 255) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
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
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
switch(fileAction) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
case FILE_ACTION_CREATE:
|
|
|
|
createNewFile(Authorization.get(ctx), repoOwner, repoName, newFileName_, appUtil.encodeBase64(newFileContent_), newFileCommitMessage_, newFileBranchName_);
|
|
|
|
break;
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
case FILE_ACTION_DELETE:
|
|
|
|
deleteFile(Authorization.get(ctx), repoOwner, repoName, filePath, newFileCommitMessage_, newFileBranchName_, fileSha);
|
|
|
|
break;
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
case FILE_ACTION_EDIT:
|
|
|
|
editFile(Authorization.get(ctx), repoOwner, repoName, filePath,
|
|
|
|
appUtil.encodeBase64(newFileContent_), newFileCommitMessage_, newFileBranchName_, fileSha);
|
|
|
|
break;
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
private void createNewFile(final String token, String repoOwner, String repoName, String fileName, String fileContent, String fileCommitMessage, String branchName) {
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
NewFile createNewFileJsonStr = branches.contains(branchName) ?
|
|
|
|
new NewFile(branchName, fileContent, fileCommitMessage, "") :
|
|
|
|
new NewFile("", fileContent, fileCommitMessage, branchName);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2019-09-18 10:25:49 +02:00
|
|
|
.createNewFile(token, repoOwner, repoName, fileName, createNewFileJsonStr);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
switch(response.code()) {
|
|
|
|
|
|
|
|
case 201:
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.success(ctx, getString(R.string.newFileSuccessMessage));
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 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));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 404:
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.warning(ctx, getString(R.string.apiNotFound));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.orgCreatedError));
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
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();
|
2021-02-01 19:13:48 +01:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
private void deleteFile(final String token, String repoOwner, String repoName, String fileName, String fileCommitMessage, String branchName, String fileSha) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
DeleteFile deleteFileJsonStr = branches.contains(branchName) ?
|
|
|
|
new DeleteFile(branchName, fileCommitMessage, "", fileSha) :
|
|
|
|
new DeleteFile("", fileCommitMessage, branchName, fileSha);
|
2020-08-18 16:37:10 +02:00
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2020-08-18 16:37:10 +02:00
|
|
|
.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();
|
2021-02-01 19:13:48 +01:00
|
|
|
Toasty.info(ctx, getString(R.string.deleteFileMessage, tinyDB.getString("repoBranch")));
|
2020-08-18 16:37:10 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
private void editFile(final String token, String repoOwner, String repoName, String fileName, String fileContent, String fileCommitMessage, String branchName, String fileSha) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
EditFile editFileJsonStr = branches.contains(branchName) ?
|
|
|
|
new EditFile(branchName, fileCommitMessage, "", fileSha, fileContent) :
|
|
|
|
new EditFile("", fileCommitMessage, branchName, fileSha, fileContent);
|
2020-08-18 16:37:10 +02:00
|
|
|
|
|
|
|
Call<JsonElement> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2020-08-18 16:37:10 +02:00
|
|
|
.editFile(token, repoOwner, repoName, fileName, editFileJsonStr);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
switch(response.code()) {
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
case 200:
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.editFileMessage, branchName));
|
|
|
|
getIntent().removeExtra("filePath");
|
|
|
|
getIntent().removeExtra("fileSha");
|
|
|
|
getIntent().removeExtra("fileContents");
|
|
|
|
tinyDB.putBoolean("fileModified", true);
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 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));
|
|
|
|
break;
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
case 404:
|
2020-08-18 16:37:10 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.apiNotFound));
|
2021-02-01 19:13:48 +01:00
|
|
|
break;
|
2020-08-18 16:37:10 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
default:
|
2020-08-18 16:37:10 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, getString(R.string.genericError));
|
2021-02-01 19:13:48 +01:00
|
|
|
break;
|
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
2021-02-01 19:13:48 +01:00
|
|
|
|
2020-08-18 16:37:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
private void getBranches(String repoOwner, String repoName) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
|
|
|
Call<List<Branches>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
|
|
|
.getBranches(Authorization.get(ctx), 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) {
|
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
if(response.code() == 200) {
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
assert response.body() != null;
|
|
|
|
for(Branches branch : response.body()) branches.add(branch.getName());
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(CreateFileActivity.this, R.layout.list_spinner_items, branches);
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
newFileBranches.setAdapter(adapter);
|
|
|
|
newFileBranches.setText(tinyDB.getString("repoBranch"), false);
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2021-02-01 19:13:48 +01:00
|
|
|
enableProcessButton();
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Branches>> call, @NonNull Throwable t) {
|
2020-10-20 20:09:07 +02:00
|
|
|
|
2019-09-18 10:25:49 +02:00
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
2020-10-20 20:09:07 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|