redirect from main to setup on zero accounts

This commit is contained in:
Daniel Gultsch 2023-02-21 12:13:14 +01:00
parent 0727b0aba6
commit d136928322
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
5 changed files with 49 additions and 0 deletions

View file

@ -94,6 +94,7 @@
<activity
android:name="im.conversations.android.ui.activity.MainActivity"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View file

@ -18,6 +18,9 @@ public interface AccountDao {
@Query("SELECT EXISTS (SELECT id FROM account WHERE address=:address)")
boolean hasAccount(BareJid address);
@Query("SELECT NOT EXISTS (SELECT id FROM account)")
LiveData<Boolean> hasNoAccounts();
@Insert
long insert(final AccountEntity account);

View file

@ -114,6 +114,10 @@ public class AccountRepository extends AbstractRepository {
return database.accountDao().getAccounts();
}
public LiveData<Boolean> hasNoAccounts() {
return database.accountDao().hasNoAccounts();
}
public static class AccountAlreadyExistsException extends IllegalStateException {
public AccountAlreadyExistsException(BareJid address) {
super(String.format("The account %s has already been setup", address));

View file

@ -1,11 +1,17 @@
package im.conversations.android.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import im.conversations.android.R;
import im.conversations.android.databinding.ActivityMainBinding;
import im.conversations.android.service.ForegroundService;
import im.conversations.android.ui.Activities;
import im.conversations.android.ui.model.MainViewModel;
import im.conversations.android.ui.model.SetupViewModel;
public class MainActivity extends BaseActivity {
@ -15,6 +21,15 @@ public class MainActivity extends BaseActivity {
ForegroundService.start(this);
final ActivityMainBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_main);
final ViewModelProvider viewModelProvider =
new ViewModelProvider(this, getDefaultViewModelProviderFactory());
final var mainViewModel = viewModelProvider.get(MainViewModel.class);
mainViewModel.hasNoAccounts().observe(this, hasNoAccounts -> {
if (Boolean.TRUE.equals(hasNoAccounts)) {
startActivity(new Intent(this,SetupActivity.class));
finish();
}
});
Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
}
}

View file

@ -0,0 +1,26 @@
package im.conversations.android.ui.model;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Transformations;
import im.conversations.android.repository.AccountRepository;
public class MainViewModel extends AndroidViewModel {
private final AccountRepository accountRepository;
private final LiveData<Boolean> hasNoAccounts;
public MainViewModel(@NonNull Application application) {
super(application);
this.accountRepository = new AccountRepository(application);
this.hasNoAccounts = this.accountRepository.hasNoAccounts();
}
public LiveData<Boolean> hasNoAccounts() {
return Transformations.distinctUntilChanged(this.hasNoAccounts);
}
}