2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
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 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.UserInfo;
|
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 CreateNewUserActivity extends BaseActivity {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private EditText fullName;
|
|
|
|
private EditText userUserName;
|
|
|
|
private EditText userEmail;
|
|
|
|
private EditText userPassword;
|
|
|
|
private Button createUserButton;
|
|
|
|
final Context ctx = this;
|
2020-04-28 15:47:25 +02:00
|
|
|
private Context appCtx;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
@Override
|
2020-03-04 20:37:28 +01:00
|
|
|
protected int getLayoutResourceId(){
|
|
|
|
return R.layout.activity_create_new_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
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);
|
|
|
|
createUserButton = findViewById(R.id.createUserButton);
|
|
|
|
fullName = findViewById(R.id.fullName);
|
|
|
|
userUserName = findViewById(R.id.userUserName);
|
|
|
|
userEmail = findViewById(R.id.userEmail);
|
|
|
|
userPassword = findViewById(R.id.userPassword);
|
|
|
|
|
2020-03-29 16:24:38 +02:00
|
|
|
fullName.requestFocus();
|
|
|
|
assert imm != null;
|
|
|
|
imm.showSoftInput(fullName, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
|
|
|
disableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
createUserButton.setOnClickListener(createNewUserListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processCreateNewUser() {
|
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
AppUtil appUtil = new AppUtil();
|
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 newFullName = fullName.getText().toString().trim();
|
|
|
|
String newUserName = userUserName.getText().toString().trim();
|
|
|
|
String newUserEmail = userEmail.getText().toString().trim();
|
|
|
|
String newUserPassword = userPassword.getText().toString();
|
|
|
|
|
|
|
|
if(!connToInternet) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getResources().getString(R.string.checkNetConnection));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newFullName.equals("") || newUserName.equals("") | newUserEmail.equals("") || newUserPassword.equals("")) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.emptyFields));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!appUtil.checkStrings(newFullName)) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.userInvalidFullName));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!appUtil.checkStringsWithAlphaNumeric(newUserName)) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.userInvalidUserName));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Patterns.EMAIL_ADDRESS.matcher(newUserEmail).matches()) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.userInvalidEmail));
|
2019-06-03 23:25:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableProcessButton();
|
2020-04-28 15:47:25 +02:00
|
|
|
createNewUser(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, instanceToken), newFullName, newUserName, newUserEmail, newUserPassword);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void createNewUser(final String instanceUrl, final String instanceToken, String newFullName, String newUserName, String newUserEmail, String newUserPassword) {
|
|
|
|
|
|
|
|
UserInfo createUser = new UserInfo(newUserEmail, newFullName, newUserName, newUserPassword, newUserName, 0, true);
|
|
|
|
|
|
|
|
Call<UserInfo> call;
|
|
|
|
|
|
|
|
call = RetrofitClient
|
2020-04-28 15:47:25 +02:00
|
|
|
.getInstance(instanceUrl, ctx)
|
2019-06-03 23:25:05 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.createNewUser(instanceToken, createUser);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<UserInfo>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<UserInfo> call, @NonNull retrofit2.Response<UserInfo> response) {
|
|
|
|
|
|
|
|
if(response.code() == 201) {
|
|
|
|
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.success(ctx, getString(R.string.userCreatedText));
|
2019-06-03 23:25:05 +02:00
|
|
|
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();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
else if(response.code() == 404) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
else if(response.code() == 422) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.warning(ctx, ctx.getString(R.string.userExistsError));
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
enableProcessButton();
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.genericError));
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
|
2020-08-06 17:16:51 +02:00
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
private final View.OnClickListener createNewUserListener = v -> processCreateNewUser();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
private void initCloseListener() {
|
2020-08-06 17:16:51 +02:00
|
|
|
|
|
|
|
onClickListener = view -> finish();
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void disableProcessButton() {
|
|
|
|
|
|
|
|
createUserButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableProcessButton() {
|
|
|
|
|
|
|
|
createUserButton.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|