From 8d1b47de8e7bcb9bfdd82b04fef3bea3e2514aa5 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Wed, 18 Sep 2019 13:25:49 +0500 Subject: [PATCH] wip on creating new file. still has to do branches section for old and new. --- .../gitnex/activities/NewFileActivity.java | 284 +++++++++++++++++- .../mian/gitnex/interfaces/ApiInterface.java | 4 + .../java/org/mian/gitnex/models/NewFile.java | 98 ++++++ .../java/org/mian/gitnex/util/AppUtil.java | 30 ++ app/src/main/res/layout/activity_new_file.xml | 20 ++ app/src/main/res/values-de/strings.xml | 14 + app/src/main/res/values-fr/strings.xml | 14 + app/src/main/res/values-ru/strings.xml | 14 + app/src/main/res/values/strings.xml | 9 +- 9 files changed, 482 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/models/NewFile.java diff --git a/app/src/main/java/org/mian/gitnex/activities/NewFileActivity.java b/app/src/main/java/org/mian/gitnex/activities/NewFileActivity.java index f1881b5e..8f1e2aa4 100644 --- a/app/src/main/java/org/mian/gitnex/activities/NewFileActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/NewFileActivity.java @@ -1,9 +1,32 @@ package org.mian.gitnex.activities; +import android.content.Context; +import android.graphics.PorterDuff; +import android.graphics.drawable.GradientDrawable; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Spinner; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; +import com.google.gson.JsonElement; import org.mian.gitnex.R; +import org.mian.gitnex.clients.RetrofitClient; +import org.mian.gitnex.helpers.AlertDialogs; +import org.mian.gitnex.helpers.Authorization; +import org.mian.gitnex.helpers.Toasty; +import org.mian.gitnex.models.Branches; +import org.mian.gitnex.models.NewFile; +import org.mian.gitnex.util.AppUtil; +import org.mian.gitnex.util.TinyDB; +import java.util.ArrayList; +import java.util.List; +import retrofit2.Call; +import retrofit2.Callback; /** * Author M M Arif @@ -11,12 +34,267 @@ import org.mian.gitnex.R; public class NewFileActivity extends AppCompatActivity { + 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; + final Context ctx = this; + + List branchesList = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_file); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + + boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext()); + + TinyDB tinyDb = new TinyDB(getApplicationContext()); + 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); + + initCloseListener(); + closeActivity.setOnClickListener(onClickListener); + + newFileCreate = findViewById(R.id.newFileCreate); + + initCloseListener(); + closeActivity.setOnClickListener(onClickListener); + + newFileBranchesSpinner = findViewById(R.id.newFileBranchesSpinner); + newFileBranchesSpinner.getBackground().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); + getBranches(instanceUrl, instanceToken, repoOwner, repoName, loginUid); + + disableProcessButton(); + + if(!connToInternet) { + + newFileCreate.setEnabled(false); + GradientDrawable shape = new GradientDrawable(); + shape.setCornerRadius( 8 ); + shape.setColor(getResources().getColor(R.color.hintColor)); + newFileCreate.setBackground(shape); + + } else { + + newFileCreate.setOnClickListener(createFileListener); + + } + + } + + private View.OnClickListener createFileListener = new View.OnClickListener() { + public void onClick(View v) { + processNewFile(); + } + }; + + private void processNewFile() { + + boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext()); + AppUtil appUtil = new AppUtil(); + TinyDB tinyDb = new TinyDB(getApplicationContext()); + 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(); + + Branches bModel = (Branches) newFileBranchesSpinner.getSelectedItem(); + + Log.i("bModel", bModel.toString()); + + if(!connToInternet) { + + Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection)); + return; + + } + + if(newFileName_.equals("") || newFileContent_.equals("") || newFileCommitMessage_.equals("")) { + + Toasty.info(getApplicationContext(), getString(R.string.newFileRequiredFields)); + return; + + } + + if(!appUtil.checkStringsWithDash(newFileBranchName_)) { + + Toasty.info(getApplicationContext(), getString(R.string.newFileInvalidBranchName)); + return; + + } + + if(appUtil.charactersLength(newFileCommitMessage_) > 255) { + + Toasty.info(getApplicationContext(), getString(R.string.newFileCommitMessageError)); + + } + else { + + disableProcessButton(); + //Log.i("base64", appUtil.encodeBase64(newFileContent_)); + createNewFile(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName, newFileName_, appUtil.encodeBase64(newFileContent_), newFileBranchName_, newFileCommitMessage_); + + } + + } + + private void createNewFile(final String instanceUrl, final String token, String repoOwner, String repoName, String fileName, String fileContent, String fileBranchName, String fileCommitMessage) { + + NewFile createNewFileJsonStr = new NewFile(fileContent, fileCommitMessage, fileBranchName); + + Call call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .createNewFile(token, repoOwner, repoName, fileName, createNewFileJsonStr); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + + if(response.code() == 201) { + + enableProcessButton(); + Toasty.info(getApplicationContext(), getString(R.string.newFileSuccessMessage)); + 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(getApplicationContext(), getString(R.string.apiNotFound)); + } + else { + enableProcessButton(); + Toasty.info(getApplicationContext(), getString(R.string.orgCreatedError)); + } + + } + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.e("onFailure", t.toString()); + enableProcessButton(); + } + }); + + } + + private void getBranches(String instanceUrl, String instanceToken, String repoOwner, String repoName, String loginUid) { + + Call> call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .getBranches(Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName); + + call.enqueue(new Callback>() { + + @Override + public void onResponse(@NonNull Call> call, @NonNull retrofit2.Response> response) { + + if(response.isSuccessful()) { + if(response.code() == 200) { + + List 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); + + } + } + + ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(), + R.layout.spinner_item, branchesList); + + adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); + newFileBranchesSpinner.setAdapter(adapter); + enableProcessButton(); + + } + } + + } + + @Override + public void onFailure(@NonNull Call> call, @NonNull Throwable t) { + Log.e("onFailure", t.toString()); + } + }); + + } + + private void initCloseListener() { + onClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }; + } + + private void disableProcessButton() { + + newFileCreate.setEnabled(false); + GradientDrawable shape = new GradientDrawable(); + shape.setCornerRadius( 8 ); + shape.setColor(getResources().getColor(R.color.hintColor)); + newFileCreate.setBackground(shape); + + } + + private void enableProcessButton() { + + newFileCreate.setEnabled(true); + GradientDrawable shape = new GradientDrawable(); + shape.setCornerRadius( 8 ); + shape.setColor(getResources().getColor(R.color.btnBackground)); + newFileCreate.setBackground(shape); + } } diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index 9fbbf13e..c78b196b 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -3,6 +3,7 @@ package org.mian.gitnex.interfaces; import com.google.gson.JsonElement; import org.mian.gitnex.models.AddEmail; import org.mian.gitnex.models.Branches; +import org.mian.gitnex.models.NewFile; import org.mian.gitnex.models.UpdateIssueAssignee; import org.mian.gitnex.models.UpdateIssueState; import org.mian.gitnex.models.Collaborators; @@ -210,4 +211,7 @@ public interface ApiInterface { @GET("repos/{owner}/{repo}/subscribers") // get all repo watchers Call> getRepoWatchers(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); + + @POST("repos/{owner}/{repo}/contents/{file}") // create new file + Call createNewFile(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileName, @Body NewFile jsonStr); } \ No newline at end of file diff --git a/app/src/main/java/org/mian/gitnex/models/NewFile.java b/app/src/main/java/org/mian/gitnex/models/NewFile.java new file mode 100644 index 00000000..87c107ea --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/models/NewFile.java @@ -0,0 +1,98 @@ +package org.mian.gitnex.models; + +/** + * Author M M Arif + */ + +public class NewFile { + + private String branch; + private String content; + private String message; + private String new_branch; + + private authorObject author; + private committerObject committer; + + public String getBranch() { + return branch; + } + + public void setBranch(String branch) { + this.branch = branch; + } + + public String getContents() { + return content; + } + + public void setContents(String contents) { + this.content = contents; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getNew_branch() { + return new_branch; + } + + public void setNew_branch(String new_branch) { + this.new_branch = new_branch; + } + + public class authorObject { + + private String email; + private String name; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + public class committerObject { + + private String email; + private String name; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + public NewFile(String content, String message, String new_branch) { + this.content = content; + this.message = message; + this.new_branch = new_branch; + } +} \ No newline at end of file diff --git a/app/src/main/java/org/mian/gitnex/util/AppUtil.java b/app/src/main/java/org/mian/gitnex/util/AppUtil.java index 9681077e..e9f86255 100644 --- a/app/src/main/java/org/mian/gitnex/util/AppUtil.java +++ b/app/src/main/java/org/mian/gitnex/util/AppUtil.java @@ -7,9 +7,11 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.util.Base64; import android.util.DisplayMetrics; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.util.Calendar; import java.util.Locale; @@ -77,6 +79,10 @@ public class AppUtil { return str.matches("^[\\w.-]+$"); } + public Boolean checkStringsWithDash(String str) { // [a-zA-Z0-9-_. ] + return str.matches("^[\\w-]+$"); + } + public Boolean checkIntegers(String str) { return str.matches("\\d+"); } @@ -180,4 +186,28 @@ public class AppUtil { } + public String encodeBase64(String str) { + + String base64Str = str; + if(!str.equals("")) { + byte[] data = str.getBytes(StandardCharsets.UTF_8); + base64Str = Base64.encodeToString(data, Base64.DEFAULT); + } + + return base64Str; + + } + + public String decodeBase64(String str) { + + String base64Str = str; + if(!str.equals("")) { + byte[] data = Base64.decode(base64Str, Base64.DEFAULT); + base64Str = new String(data, StandardCharsets.UTF_8); + } + + return base64Str; + + } + } diff --git a/app/src/main/res/layout/activity_new_file.xml b/app/src/main/res/layout/activity_new_file.xml index 5eeb0f1c..800f48ef 100644 --- a/app/src/main/res/layout/activity_new_file.xml +++ b/app/src/main/res/layout/activity_new_file.xml @@ -115,6 +115,25 @@ android:inputType="textCapSentences|textMultiLine" android:textColorHighlight="@color/colorWhite"/> + + + + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index b66a6f24..4836e0b2 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -408,6 +408,19 @@ Zip Archive Tar Archive + + File Name + Branch Name + File Content + Create New File + with folder: app/test.md + Commit Message + Invalid branch name, may only contain –, a–z, 0–9 + Commit message is too long + New file created + Current Branches + Fields like filename, contents and commit message are required. + OK Fertig @@ -427,6 +440,7 @@ Beschreibung Website Standort + Max 255 characters diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index fcf0afb9..72995d48 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -408,6 +408,19 @@ Zip Archive Tar Archive + + File Name + Branch Name + File Content + Create New File + with folder: app/test.md + Commit Message + Invalid branch name, may only contain –, a–z, 0–9 + Commit message is too long + New file created + Current Branches + Fields like filename, contents and commit message are required. + OK Done @@ -428,6 +441,7 @@ Website Location Ouvrir avec le Browser + Max 255 characters \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 892c9c92..9216aebc 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -408,6 +408,19 @@ Zip Archive Tar Archive + + File Name + Branch Name + File Content + Create New File + with folder: app/test.md + Commit Message + Invalid branch name, may only contain –, a–z, 0–9 + Commit message is too long + New file created + Current Branches + Fields like filename, contents and commit message are required. + OK Готово @@ -428,6 +441,7 @@ Веб-сайт Место расположения Открыть с помощью браузера + Max 255 characters \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 29539ec6..8d2e89c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -99,7 +99,6 @@ Password is required Cannot access network, please check your Internet connection - All fields are required Repository name is empty Repository name is not valid. [a–z A–Z 0–9 – _] @@ -449,11 +448,16 @@ File Name - Branch Name + New Branch Name File Content Create New File with folder: app/test.md Commit Message + Invalid branch name, may only contain –, a–z, 0–9 + Commit message is too long + New file created + Current Branches + Fields like filename, contents and commit message are required. OK @@ -478,6 +482,7 @@ Website Location Max 255 characters + All fields are required