From 5866974eff14716c2a87cf21ec753033d7605acc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 16 Feb 2023 10:45:53 +0100 Subject: [PATCH] wire up SetupViewModel with account repo --- .../android/ui/model/SetupViewModel.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main/java/im/conversations/android/ui/model/SetupViewModel.java b/src/main/java/im/conversations/android/ui/model/SetupViewModel.java index db1a0d410..4ba6ac9d3 100644 --- a/src/main/java/im/conversations/android/ui/model/SetupViewModel.java +++ b/src/main/java/im/conversations/android/ui/model/SetupViewModel.java @@ -6,11 +6,27 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; 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 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 { + private static final Logger LOGGER = LoggerFactory.getLogger(SetupViewModel.class); + private final MutableLiveData xmppAddress = new MutableLiveData<>(); private final MutableLiveData xmppAddressError = new MutableLiveData<>(); private final MutableLiveData password = new MutableLiveData<>(); @@ -19,8 +35,11 @@ public class SetupViewModel extends AndroidViewModel { private final MutableLiveData> redirection = new MutableLiveData<>(); + private final AccountRepository accountRepository; + public SetupViewModel(@NonNull @NotNull Application application) { super(application); + this.accountRepository = new AccountRepository(application); } public LiveData isLoading() { @@ -49,6 +68,26 @@ public class SetupViewModel extends AndroidViewModel { } 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() { + @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; }