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;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.CreateEmailOption;
|
|
|
|
import org.gitnex.tea4j.v2.models.Email;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.ActivityProfileEmailBinding;
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
import org.mian.gitnex.fragments.MyProfileEmailsFragment;
|
2019-06-03 23:25:05 +02:00
|
|
|
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.Toasty;
|
2020-07-07 19:15:01 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
2022-07-29 14:08:44 +02:00
|
|
|
* @author M M Arif
|
2019-06-03 23:25:05 +02:00
|
|
|
*/
|
2021-08-02 20:04:33 +02:00
|
|
|
public class MyProfileEmailActivity extends BaseActivity {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private EditText userEmail;
|
|
|
|
private Button addEmailButton;
|
|
|
|
private final View.OnClickListener addEmailListener = v -> processAddNewEmail();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2020-04-28 15:47:25 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
ActivityProfileEmailBinding activityProfileEmailBinding =
|
|
|
|
ActivityProfileEmailBinding.inflate(getLayoutInflater());
|
2022-08-15 16:26:02 +02:00
|
|
|
setContentView(activityProfileEmailBinding.getRoot());
|
2021-01-26 16:10:31 +01:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
InputMethodManager imm =
|
|
|
|
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
2020-03-29 16:24:38 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
ImageView closeActivity = activityProfileEmailBinding.close;
|
|
|
|
userEmail = activityProfileEmailBinding.userEmail;
|
|
|
|
addEmailButton = activityProfileEmailBinding.addEmailButton;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
userEmail.requestFocus();
|
|
|
|
assert imm != null;
|
|
|
|
imm.showSoftInput(userEmail, InputMethodManager.SHOW_IMPLICIT);
|
2020-03-29 16:24:38 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!connToInternet) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
disableProcessButton();
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
addEmailButton.setOnClickListener(addEmailListener);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private void processAddNewEmail() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
String newUserEmail = userEmail.getText().toString().trim();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!connToInternet) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
Toasty.error(ctx, getResources().getString(R.string.checkNetConnection));
|
|
|
|
return;
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (newUserEmail.equals("")) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
Toasty.error(ctx, getString(R.string.emailErrorEmpty));
|
|
|
|
return;
|
2022-09-21 07:43:00 +02:00
|
|
|
} else if (!Patterns.EMAIL_ADDRESS.matcher(newUserEmail).matches()) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
Toasty.warning(ctx, getString(R.string.emailErrorInvalid));
|
|
|
|
return;
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
List<String> newEmailList = new ArrayList<>(Arrays.asList(newUserEmail.split(",")));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
disableProcessButton();
|
|
|
|
addNewEmail(newEmailList);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private void addNewEmail(List<String> newUserEmail) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
CreateEmailOption addEmailFunc = new CreateEmailOption();
|
2022-04-18 09:10:54 +02:00
|
|
|
addEmailFunc.setEmails(newUserEmail);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
Call<List<Email>> call = RetrofitClient.getApiInterface(ctx).userAddEmail(addEmailFunc);
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Email>> call,
|
|
|
|
@NonNull retrofit2.Response<List<Email>> response) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.code() == 201) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.success(ctx, getString(R.string.emailAddedText));
|
|
|
|
MyProfileEmailsFragment.refreshEmails = true;
|
|
|
|
enableProcessButton();
|
|
|
|
finish();
|
|
|
|
} else if (response.code() == 401) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
enableProcessButton();
|
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(ctx);
|
|
|
|
} else if (response.code() == 403) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
|
|
|
|
} else if (response.code() == 404) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
|
|
|
|
} else if (response.code() == 422) {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.warning(ctx, ctx.getString(R.string.emailErrorInUse));
|
|
|
|
} else {
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 09:10:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Email>> call, @NonNull Throwable t) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private void initCloseListener() {
|
2020-08-06 17:16:51 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
onClickListener = view -> finish();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private void disableProcessButton() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
addEmailButton.setEnabled(false);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private void enableProcessButton() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
addEmailButton.setEnabled(true);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|