mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-15 16:14:52 +02:00
153 lines
4.4 KiB
Java
153 lines
4.4 KiB
Java
package org.mercury_im.messenger.ui.login;
|
|
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
import io.reactivex.Single;
|
|
import io.reactivex.observers.DisposableSingleObserver;
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
|
import org.mercury_im.messenger.data.repository.AccountRepository;
|
|
import org.mercury_im.messenger.entity.Account;
|
|
import org.mercury_im.messenger.entity.IAccount;
|
|
import org.mercury_im.messenger.entity.PasswordAuthentication;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
public class LoginViewModel extends ViewModel {
|
|
|
|
@Inject
|
|
AccountRepository accountRepository;
|
|
|
|
// @Inject
|
|
// ConnectionCenter connectionCenter;
|
|
|
|
private String jid;
|
|
private String password;
|
|
|
|
private MutableLiveData<JidError> jidError = new MutableLiveData<>();
|
|
private MutableLiveData<PasswordError> passwordError = new MutableLiveData<>();
|
|
|
|
private MutableLiveData<Account> account = new MutableLiveData<>();
|
|
|
|
private MutableLiveData<Boolean> signinSuccessful = new MutableLiveData<>();
|
|
|
|
public LoginViewModel() {
|
|
super();
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
init(new IAccount());
|
|
}
|
|
|
|
public LiveData<Boolean> getSigninSuccessful() {
|
|
return signinSuccessful;
|
|
}
|
|
|
|
public enum JidError {
|
|
none,
|
|
emptyJid,
|
|
invalidJid,
|
|
unknownJid
|
|
}
|
|
|
|
public enum PasswordError {
|
|
none,
|
|
emptyPassword,
|
|
invalidPassword,
|
|
incorrectPassword
|
|
}
|
|
|
|
public void onJidInputChanged(String input) {
|
|
this.jid = input;
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public void init(@NonNull Account account) {
|
|
this.account.setValue(account);
|
|
}
|
|
|
|
public MutableLiveData<Account> getAccount() {
|
|
return account;
|
|
}
|
|
|
|
public void login() {
|
|
Account account = getAccount().getValue();
|
|
if (account != null && account.getAddress() != null && !TextUtils.isEmpty(account.getAuthentication().getPassword())) {
|
|
accountRepository.upsertAccount(account);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
Account account = new IAccount();
|
|
account.setEnabled(true);
|
|
account.setAddress(bareJid.toString());
|
|
account.setAuthentication(new PasswordAuthentication(password));
|
|
Single<Account> insert = accountRepository.upsertAccount(account);
|
|
insert.subscribe(new DisposableSingleObserver<Account>() {
|
|
@Override
|
|
public void onSuccess(Account inserted) {
|
|
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + inserted.getId() + " inserted.");
|
|
// connectionCenter.createConnection(account);
|
|
signinSuccessful.setValue(true);
|
|
}
|
|
|
|
@Override
|
|
public void onError(Throwable e) {
|
|
Log.e("Mercury", "Could not insert new Account data.", e);
|
|
}
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|