wire up SetupViewModel with account repo

This commit is contained in:
Daniel Gultsch 2023-02-16 10:45:53 +01:00
parent 3c42066a7c
commit 5866974eff
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -6,11 +6,27 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations; import androidx.lifecycle.Transformations;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import im.conversations.android.database.model.Account;
import im.conversations.android.repository.AccountRepository;
import im.conversations.android.ui.Event; import im.conversations.android.ui.Event;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Future;
public class SetupViewModel extends AndroidViewModel { public class SetupViewModel extends AndroidViewModel {
private static final Logger LOGGER = LoggerFactory.getLogger(SetupViewModel.class);
private final MutableLiveData<String> xmppAddress = new MutableLiveData<>(); private final MutableLiveData<String> xmppAddress = new MutableLiveData<>();
private final MutableLiveData<String> xmppAddressError = new MutableLiveData<>(); private final MutableLiveData<String> xmppAddressError = new MutableLiveData<>();
private final MutableLiveData<String> password = new MutableLiveData<>(); private final MutableLiveData<String> password = new MutableLiveData<>();
@ -19,8 +35,11 @@ public class SetupViewModel extends AndroidViewModel {
private final MutableLiveData<Event<Target>> redirection = new MutableLiveData<>(); private final MutableLiveData<Event<Target>> redirection = new MutableLiveData<>();
private final AccountRepository accountRepository;
public SetupViewModel(@NonNull @NotNull Application application) { public SetupViewModel(@NonNull @NotNull Application application) {
super(application); super(application);
this.accountRepository = new AccountRepository(application);
} }
public LiveData<Boolean> isLoading() { public LiveData<Boolean> isLoading() {
@ -49,6 +68,26 @@ public class SetupViewModel extends AndroidViewModel {
} }
public boolean submitPassword() { public boolean submitPassword() {
final BareJid address;
try {
address =JidCreate.bareFrom(this.xmppAddress.getValue());
} catch (final XmppStringprepException e) {
xmppAddressError.postValue("Not a valid jid");
return true;
}
final String password = this.password.getValue();
final var creationFuture = this.accountRepository.createAccountAsync(address,password, true);
Futures.addCallback(creationFuture, new FutureCallback<Account>() {
@Override
public void onSuccess(final Account account) {
LOGGER.info("Successfully created account {}",account.address);
}
@Override
public void onFailure(@NonNull final Throwable t) {
LOGGER.warn("Could not create account", t);
}
}, MoreExecutors.directExecutor());
return true; return true;
} }