Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/login/LoginViewModel.java

154 lines
4.5 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger.ui.login;
2019-05-04 00:27:02 +02:00
import static org.mercury_im.messenger.core.connection.MercuryConnection.TAG;
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
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-08-25 17:54:03 +02:00
import org.mercury_im.messenger.core.centers.ConnectionCenter;
import org.mercury_im.messenger.persistence.entity.AccountModel;
2019-05-19 02:00:33 +02:00
import org.mercury_im.messenger.persistence.repository.AccountRepository;
2019-05-04 00:27:02 +02:00
import javax.inject.Inject;
public class LoginViewModel extends ViewModel {
@Inject
AccountRepository accountRepository;
2019-08-04 04:22:08 +02:00
@Inject
ConnectionCenter connectionCenter;
2019-07-29 22:14:35 +02:00
private String jid;
private String password;
private MutableLiveData<JidError> jidError = new MutableLiveData<>();
private MutableLiveData<PasswordError> passwordError = new MutableLiveData<>();
2019-08-04 04:22:08 +02:00
private MutableLiveData<AccountModel> 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);
init(new AccountModel());
2019-05-04 00:27:02 +02:00
}
2019-08-24 23:06:06 +02:00
public LiveData<Boolean> getSigninSuccessful() {
return signinSuccessful;
}
2019-07-29 22:14:35 +02:00
public enum JidError {
none,
emptyJid,
invalidJid,
unknownJid
}
public enum PasswordError {
none,
emptyPassword,
invalidPassword,
incorrectPassword
}
public void onJidInputChanged(String input) {
this.jid = input;
2019-07-31 17:44:10 +02:00
2019-07-29 22:14:35 +02:00
}
public void onPasswordInputChanged(String input) {
this.password = input;
}
public LiveData<JidError> getJidError() {
return jidError;
}
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
* @return valid jid or null
*/
private EntityBareJid asValidJidOrNull(String input) {
return JidCreate.entityBareFromOrNull(input);
}
private boolean isPasswordValid(String password) {
return !password.isEmpty();
}
2019-08-04 04:22:08 +02:00
public void init(@NonNull AccountModel account) {
2019-05-04 00:27:02 +02:00
this.account.setValue(account);
}
2019-08-04 04:22:08 +02:00
public MutableLiveData<AccountModel> getAccount() {
2019-05-04 00:27:02 +02:00
return account;
}
public void login() {
2019-08-04 04:22:08 +02:00
AccountModel account = getAccount().getValue();
2019-05-04 00:27:02 +02:00
if (account != null && account.getJid() != null && !TextUtils.isEmpty(account.getPassword())) {
accountRepository.upsert(account);
2019-05-04 00:27:02 +02:00
}
}
2019-07-29 22:14:35 +02:00
public void loginDetailsEntered() {
boolean loginIntact = true;
if (jid.isEmpty()) {
jidError.postValue(JidError.emptyJid);
loginIntact = false;
}
EntityBareJid bareJid = asValidJidOrNull(jid);
if (bareJid == null) {
jidError.postValue(JidError.invalidJid);
loginIntact = false;
}
if (!isPasswordValid(password)) {
passwordError.postValue(PasswordError.invalidPassword);
loginIntact = false;
}
if (loginIntact) {
AccountModel accountModel = new AccountModel();
2019-07-29 22:14:35 +02:00
accountModel.setEnabled(true);
accountModel.setJid(bareJid);
accountModel.setPassword(password);
Single<AccountModel> insert = accountRepository.upsert(accountModel);
insert.subscribe(new DisposableSingleObserver<AccountModel>() {
2019-08-04 04:22:08 +02:00
@Override
public void onSuccess(AccountModel inserted) {
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + inserted.getId() + " inserted.");
2019-08-04 04:22:08 +02:00
connectionCenter.createConnection(accountModel);
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) {
Log.e(TAG, "Could not insert new Account data.", e);
}
});
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
}