add async variant for createAccount

This commit is contained in:
Daniel Gultsch 2023-01-22 12:27:46 +01:00
parent c31fa7ed2b
commit bd343eafa0
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 15 additions and 3 deletions

View file

@ -2,7 +2,6 @@ package im.conversations.android.database.dao;
import androidx.room.Dao; import androidx.room.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query; import androidx.room.Query;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
@ -14,7 +13,7 @@ import java.util.List;
@Dao @Dao
public interface AccountDao { public interface AccountDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert
long insert(final AccountEntity account); long insert(final AccountEntity account);
@Query("SELECT id,address,randomSeed FROM account WHERE enabled = 1") @Query("SELECT id,address,randomSeed FROM account WHERE enabled = 1")

View file

@ -2,9 +2,13 @@ package im.conversations.android.repository;
import android.content.Context; import android.content.Context;
import im.conversations.android.database.ConversationsDatabase; import im.conversations.android.database.ConversationsDatabase;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public abstract class AbstractRepository { public abstract class AbstractRepository {
protected static final Executor IO_EXECUTOR = Executors.newSingleThreadExecutor();
protected final Context context; protected final Context context;
protected final ConversationsDatabase database; protected final ConversationsDatabase database;

View file

@ -3,11 +3,14 @@ package im.conversations.android.repository;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
import im.conversations.android.IDs; import im.conversations.android.IDs;
import im.conversations.android.database.CredentialStore; import im.conversations.android.database.CredentialStore;
import im.conversations.android.database.entity.AccountEntity; import im.conversations.android.database.entity.AccountEntity;
import im.conversations.android.database.model.Account; import im.conversations.android.database.model.Account;
import im.conversations.android.xmpp.ConnectionPool;
public class AccountRepository extends AbstractRepository { public class AccountRepository extends AbstractRepository {
@ -15,7 +18,7 @@ public class AccountRepository extends AbstractRepository {
super(context); super(context);
} }
public Account createAccount(@NonNull final Jid address, final String password) { private Account createAccount(@NonNull final Jid address, final String password) {
Preconditions.checkArgument( Preconditions.checkArgument(
address.isBareJid(), "Account should be specified without resource"); address.isBareJid(), "Account should be specified without resource");
Preconditions.checkArgument(password != null, "Missing password"); Preconditions.checkArgument(password != null, "Missing password");
@ -31,6 +34,12 @@ public class AccountRepository extends AbstractRepository {
} catch (final Exception e) { } catch (final Exception e) {
throw new IllegalStateException("Could not store password", e); throw new IllegalStateException("Could not store password", e);
} }
ConnectionPool.getInstance(context).reconfigure(account);
return account; return account;
} }
public ListenableFuture<Account> createAccountAsync(
final @NonNull Jid address, final String password) {
return Futures.submit(() -> createAccount(address, password), IO_EXECUTOR);
}
} }