2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.GradientDrawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Patterns;
|
|
|
|
import android.view.View;
|
2020-03-29 16:24:38 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
2020-07-07 19:15:01 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2019-06-03 23:25:05 +02:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import org.mian.gitnex.R;
|
|
|
|
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-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import org.mian.gitnex.models.AddEmail;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-07-07 19:15:01 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-03-04 20:37:28 +01:00
|
|
|
public class ProfileEmailActivity extends BaseActivity {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private EditText userEmail;
|
|
|
|
final Context ctx = this;
|
2020-04-28 15:47:25 +02:00
|
|
|
private Context appCtx;
|
2019-06-03 23:25:05 +02:00
|
|
|
private Button addEmailButton;
|
|
|
|
|
|
|
|
@Override
|
2020-03-04 20:37:28 +01:00
|
|
|
protected int getLayoutResourceId(){
|
|
|
|
return R.layout.activity_profile_email;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2020-04-28 15:47:25 +02:00
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-04-28 15:47:25 +02:00
|
|
|
appCtx = getApplicationContext();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
ImageView closeActivity = findViewById(R.id.close);
|
|
|
|
userEmail = findViewById(R.id.userEmail);
|
|
|
|
addEmailButton = findViewById(R.id.addEmailButton);
|
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
userEmail.requestFocus();
|
|
|
|
assert imm != null;
|
|
|
|
imm.showSoftInput(userEmail, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
|
|
|
disableProcessButton();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
addEmailButton.setOnClickListener(addEmailListener);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private View.OnClickListener addEmailListener = new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
processAddNewEmail();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void processAddNewEmail() {
|
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2020-04-28 15:47:25 +02:00
|
|
|
TinyDB tinyDb = new TinyDB(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
String newUserEmail = userEmail.getText().toString().trim();
|
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getResources().getString(R.string.checkNetConnection));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newUserEmail.equals("")) {
|
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.emailErrorEmpty));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(!Patterns.EMAIL_ADDRESS.matcher(newUserEmail).matches()) {
|
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.emailErrorInvalid));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> newEmailList = new ArrayList<>(Arrays.asList(newUserEmail.split(",")));
|
|
|
|
|
|
|
|
disableProcessButton();
|
2020-04-28 15:47:25 +02:00
|
|
|
addNewEmail(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, instanceToken), newEmailList);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addNewEmail(final String instanceUrl, final String token, List<String> newUserEmail) {
|
|
|
|
|
|
|
|
AddEmail addEmailFunc = new AddEmail(newUserEmail);
|
2020-04-28 15:47:25 +02:00
|
|
|
final TinyDB tinyDb = new TinyDB(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
Call<JsonElement> call;
|
|
|
|
|
|
|
|
call = RetrofitClient
|
2020-04-28 15:47:25 +02:00
|
|
|
.getInstance(instanceUrl, ctx)
|
2019-06-03 23:25:05 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.addNewEmail(token, addEmailFunc);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
|
|
|
if(response.code() == 201) {
|
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.emailAddedText));
|
2019-06-03 23:25:05 +02:00
|
|
|
tinyDb.putBoolean("emailsRefresh", true);
|
|
|
|
enableProcessButton();
|
|
|
|
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() == 403) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, ctx.getString(R.string.authorizeError));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 404) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, ctx.getString(R.string.apiNotFound));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 422) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.info(ctx, ctx.getString(R.string.emailErrorInUse));
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
enableProcessButton();
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.labelGeneralError));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
|
|
|
onClickListener = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disableProcessButton() {
|
|
|
|
|
|
|
|
addEmailButton.setEnabled(false);
|
|
|
|
GradientDrawable shape = new GradientDrawable();
|
|
|
|
shape.setCornerRadius( 8 );
|
|
|
|
shape.setColor(getResources().getColor(R.color.hintColor));
|
|
|
|
addEmailButton.setBackground(shape);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableProcessButton() {
|
|
|
|
|
|
|
|
addEmailButton.setEnabled(true);
|
|
|
|
GradientDrawable shape = new GradientDrawable();
|
|
|
|
shape.setCornerRadius( 8 );
|
|
|
|
shape.setColor(getResources().getColor(R.color.btnBackground));
|
|
|
|
addEmailButton.setBackground(shape);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|