From d136928322d08081c91b6036941f470ad2b9b407 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 21 Feb 2023 12:13:14 +0100 Subject: [PATCH] redirect from main to setup on zero accounts --- app/src/main/AndroidManifest.xml | 1 + .../android/database/dao/AccountDao.java | 3 +++ .../android/repository/AccountRepository.java | 4 +++ .../android/ui/activity/MainActivity.java | 15 +++++++++++ .../android/ui/model/MainViewModel.java | 26 +++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 app/src/main/java/im/conversations/android/ui/model/MainViewModel.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f4b2fd334..dc4cbfa33 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -94,6 +94,7 @@ diff --git a/app/src/main/java/im/conversations/android/database/dao/AccountDao.java b/app/src/main/java/im/conversations/android/database/dao/AccountDao.java index 01c4bfade..c75d3c9cc 100644 --- a/app/src/main/java/im/conversations/android/database/dao/AccountDao.java +++ b/app/src/main/java/im/conversations/android/database/dao/AccountDao.java @@ -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 hasNoAccounts(); + @Insert long insert(final AccountEntity account); diff --git a/app/src/main/java/im/conversations/android/repository/AccountRepository.java b/app/src/main/java/im/conversations/android/repository/AccountRepository.java index fd83611dd..04b1e6afe 100644 --- a/app/src/main/java/im/conversations/android/repository/AccountRepository.java +++ b/app/src/main/java/im/conversations/android/repository/AccountRepository.java @@ -114,6 +114,10 @@ public class AccountRepository extends AbstractRepository { return database.accountDao().getAccounts(); } + public LiveData 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)); diff --git a/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java b/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java index 5608bde51..db8436210 100644 --- a/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java +++ b/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java @@ -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()); } } diff --git a/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java b/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java new file mode 100644 index 000000000..0f4e55d3c --- /dev/null +++ b/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java @@ -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 hasNoAccounts; + + public MainViewModel(@NonNull Application application) { + super(application); + this.accountRepository = new AccountRepository(application); + this.hasNoAccounts = this.accountRepository.hasNoAccounts(); + } + + public LiveData hasNoAccounts() { + return Transformations.distinctUntilChanged(this.hasNoAccounts); + } +}