2019-12-21 03:46:17 +01:00
|
|
|
package org.mercury_im.messenger.ui.account;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
import android.app.Application;
|
|
|
|
import android.widget.Toast;
|
2019-08-04 04:22:08 +02:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-12-21 00:27:48 +01:00
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2019-07-29 22:14:35 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2019-09-13 09:17:59 +02:00
|
|
|
|
2019-12-21 03:01:35 +01:00
|
|
|
import org.jivesoftware.smack.sasl.SASLErrorException;
|
2019-07-29 22:14:35 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
2019-07-29 22:14:35 +02:00
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.mercury_im.messenger.Messenger;
|
|
|
|
import org.mercury_im.messenger.R;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public class LoginViewModel extends AndroidViewModel {
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
private MutableLiveData<Error> usernameError = new MutableLiveData<>(new Error());
|
|
|
|
private MutableLiveData<Error> passwordError = new MutableLiveData<>(new Error());
|
|
|
|
private MutableLiveData<Boolean> loginEnabled = new MutableLiveData<>(false);
|
|
|
|
private MutableLiveData<Boolean> loginSuccessful = new MutableLiveData<>(false);
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
private EntityBareJid username;
|
2019-07-29 22:14:35 +02:00
|
|
|
private String password;
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
@Inject
|
|
|
|
Messenger messenger;
|
2019-08-24 23:06:06 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public LoginViewModel(@NonNull Application application) {
|
|
|
|
super(application);
|
|
|
|
((MercuryImApplication) application).getAppComponent().inject(this);
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public void setUsername(String username) {
|
|
|
|
if (username == null || username.isEmpty()) {
|
|
|
|
usernameError.setValue(new Error(getApplication().getResources().getString(R.string.error_field_required)));
|
|
|
|
this.username = null;
|
|
|
|
updateLoginEnabled();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.username = JidCreate.entityBareFrom(username);
|
|
|
|
updateLoginEnabled();
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
usernameError.setValue(new Error(getApplication().getResources().getString(R.string.error_invalid_username)));
|
|
|
|
this.username = null;
|
|
|
|
updateLoginEnabled();
|
|
|
|
}
|
2019-08-24 23:06:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public void setPassword(String password) {
|
|
|
|
this.password = password;
|
|
|
|
updateLoginEnabled();
|
|
|
|
if (password == null || password.isEmpty()) {
|
|
|
|
passwordError.setValue(new Error(getApplication().getResources().getString(R.string.error_field_required)));
|
|
|
|
}
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
private void updateLoginEnabled() {
|
|
|
|
loginEnabled.setValue(username != null && !(password == null || password.isEmpty()));
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 01:45:30 +01:00
|
|
|
public synchronized void login() {
|
|
|
|
if (!loginEnabled.getValue()) {
|
|
|
|
// Prevent race condition where account would be logged in twice
|
|
|
|
return;
|
|
|
|
}
|
2019-12-21 00:27:48 +01:00
|
|
|
loginEnabled.setValue(false);
|
2019-12-21 01:45:30 +01:00
|
|
|
messenger.addAccount()
|
|
|
|
.setAddress(username.asEntityBareJidString())
|
|
|
|
.setPassword(password)
|
2019-12-21 03:01:35 +01:00
|
|
|
.loginAndStoreAccountIfSuccessful()
|
2019-12-21 00:27:48 +01:00
|
|
|
.subscribeOn(Schedulers.newThread())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2019-12-21 03:01:35 +01:00
|
|
|
.doOnComplete(() -> loginSuccessful.setValue(true))
|
|
|
|
.doOnError(this::handleConnectionError)
|
2019-12-21 00:27:48 +01:00
|
|
|
.subscribe();
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 03:01:35 +01:00
|
|
|
private void handleConnectionError(Throwable error) {
|
|
|
|
if (error instanceof SASLErrorException) {
|
|
|
|
passwordError.setValue(new Error(getApplication().getResources().getString(R.string.error_incorrect_password)));
|
|
|
|
loginEnabled.setValue(true);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getApplication(), "A connection error occurred", Toast.LENGTH_LONG).show();
|
|
|
|
loginEnabled.setValue(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public LiveData<Error> getPasswordError() {
|
2019-07-29 22:14:35 +02:00
|
|
|
return passwordError;
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public LiveData<Error> getUsernameError() {
|
|
|
|
return usernameError;
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public LiveData<Boolean> isLoginSuccessful() {
|
|
|
|
return loginSuccessful;
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public LiveData<Boolean> isLoginEnabled() {
|
|
|
|
return loginEnabled;
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public static class Error {
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
private String message;
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public Error() {
|
2019-07-29 22:14:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public Error(String errorMessage) {
|
|
|
|
this.message = errorMessage;
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public boolean isError() {
|
|
|
|
return message != null;
|
|
|
|
}
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public String getErrorMessage() {
|
|
|
|
return message;
|
2019-08-04 04:22:08 +02:00
|
|
|
}
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|