2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.login;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-08-04 04:22:08 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-07-29 22:14:35 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-09-13 09:17:59 +02:00
|
|
|
import io.reactivex.Single;
|
|
|
|
import io.reactivex.observers.DisposableSingleObserver;
|
|
|
|
|
2019-07-29 22:14:35 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-12-09 15:49:59 +01:00
|
|
|
import org.mercury_im.messenger.account.error.PasswordError;
|
|
|
|
import org.mercury_im.messenger.account.error.UsernameError;
|
2019-11-22 23:35:14 +01:00
|
|
|
import org.mercury_im.messenger.data.repository.AccountRepository;
|
2019-12-06 15:52:50 +01:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
|
|
|
import org.mercury_im.messenger.entity.IAccount;
|
|
|
|
import org.mercury_im.messenger.entity.PasswordAuthentication;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class LoginViewModel extends ViewModel {
|
|
|
|
|
|
|
|
@Inject
|
2019-11-22 23:35:14 +01:00
|
|
|
AccountRepository accountRepository;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
// @Inject
|
|
|
|
// ConnectionCenter connectionCenter;
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-12-09 15:49:59 +01:00
|
|
|
private String username;
|
2019-07-29 22:14:35 +02:00
|
|
|
private String password;
|
|
|
|
|
2019-12-09 15:49:59 +01:00
|
|
|
private MutableLiveData<UsernameError> usernameError = new MutableLiveData<>(UsernameError.none);
|
|
|
|
private MutableLiveData<PasswordError> passwordError = new MutableLiveData<>(PasswordError.none);
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
private MutableLiveData<Account> account = new MutableLiveData<>();
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-08-24 23:06:06 +02:00
|
|
|
private MutableLiveData<Boolean> signinSuccessful = new MutableLiveData<>();
|
|
|
|
|
2019-05-04 00:27:02 +02:00
|
|
|
public LoginViewModel() {
|
|
|
|
super();
|
2019-07-31 17:44:10 +02:00
|
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
2019-12-06 15:52:50 +01:00
|
|
|
init(new IAccount());
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 23:06:06 +02:00
|
|
|
public LiveData<Boolean> getSigninSuccessful() {
|
|
|
|
return signinSuccessful;
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:49:59 +01:00
|
|
|
public void onUsernameInputChanged(String input) {
|
|
|
|
this.username = input;
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onPasswordInputChanged(String input) {
|
|
|
|
this.password = input;
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:49:59 +01:00
|
|
|
public LiveData<UsernameError> getUsernameError() {
|
|
|
|
return usernameError;
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<PasswordError> getPasswordError() {
|
|
|
|
return passwordError;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to parse the input string into a {@link EntityBareJid} and return it.
|
|
|
|
* Return null on failure.
|
|
|
|
* @param input input string
|
2019-12-09 15:49:59 +01:00
|
|
|
* @return valid username or null
|
2019-07-29 22:14:35 +02:00
|
|
|
*/
|
2019-12-09 15:49:59 +01:00
|
|
|
private EntityBareJid asValidUsernameOrNull(String input) {
|
2019-07-29 22:14:35 +02:00
|
|
|
return JidCreate.entityBareFromOrNull(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
|
return !password.isEmpty();
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
public void init(@NonNull Account account) {
|
2019-05-04 00:27:02 +02:00
|
|
|
this.account.setValue(account);
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
public MutableLiveData<Account> getAccount() {
|
2019-05-04 00:27:02 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void login() {
|
2019-12-06 15:52:50 +01:00
|
|
|
Account account = getAccount().getValue();
|
2019-12-09 15:49:59 +01:00
|
|
|
if (account != null && account.getAddress() != null
|
|
|
|
&& !TextUtils.isEmpty(account.getAuthentication().getPassword())) {
|
2019-12-06 15:52:50 +01:00
|
|
|
accountRepository.upsertAccount(account);
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 22:14:35 +02:00
|
|
|
|
|
|
|
public void loginDetailsEntered() {
|
|
|
|
boolean loginIntact = true;
|
2019-12-09 15:49:59 +01:00
|
|
|
if (username.isEmpty()) {
|
|
|
|
usernameError.postValue(UsernameError.emptyUsername);
|
2019-07-29 22:14:35 +02:00
|
|
|
loginIntact = false;
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:49:59 +01:00
|
|
|
EntityBareJid bareJid = asValidUsernameOrNull(username);
|
2019-07-29 22:14:35 +02:00
|
|
|
if (bareJid == null) {
|
2019-12-09 15:49:59 +01:00
|
|
|
usernameError.postValue(UsernameError.invalidUsername);
|
2019-07-29 22:14:35 +02:00
|
|
|
loginIntact = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPasswordValid(password)) {
|
|
|
|
passwordError.postValue(PasswordError.invalidPassword);
|
|
|
|
loginIntact = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loginIntact) {
|
2019-12-06 15:52:50 +01:00
|
|
|
Account account = new IAccount();
|
|
|
|
account.setEnabled(true);
|
|
|
|
account.setAddress(bareJid.toString());
|
|
|
|
account.setAuthentication(new PasswordAuthentication(password));
|
|
|
|
Single<Account> insert = accountRepository.upsertAccount(account);
|
2019-12-09 15:49:59 +01:00
|
|
|
insert.subscribe(
|
|
|
|
new DisposableSingleObserver<Account>() {
|
2019-08-04 04:22:08 +02:00
|
|
|
@Override
|
2019-12-06 15:52:50 +01:00
|
|
|
public void onSuccess(Account inserted) {
|
|
|
|
// connectionCenter.createConnection(account);
|
2019-08-24 23:06:06 +02:00
|
|
|
signinSuccessful.setValue(true);
|
2019-08-04 04:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(Throwable e) {
|
2019-12-06 15:52:50 +01:00
|
|
|
Log.e("Mercury", "Could not insert new Account data.", e);
|
2019-08-04 04:22:08 +02:00
|
|
|
}
|
|
|
|
});
|
2019-07-29 22:14:35 +02:00
|
|
|
|
2019-08-04 04:22:08 +02:00
|
|
|
}
|
2019-07-29 22:14:35 +02:00
|
|
|
}
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|