]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
several fixes when there are no profiles after full room adoption
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
index 04aed0424d9d68f4b7745b72d2c23ca809812bce..cb7cb46c520ea173da9bbd15c71a4bcf9e88d5e0 100644 (file)
@@ -41,6 +41,8 @@ import androidx.core.view.GravityCompat;
 import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
+import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
@@ -51,11 +53,14 @@ import com.google.android.material.snackbar.Snackbar;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
+import net.ktnx.mobileledger.async.TransactionAccumulator;
 import net.ktnx.mobileledger.databinding.ActivityMainBinding;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Option;
 import net.ktnx.mobileledger.db.Profile;
+import net.ktnx.mobileledger.db.TransactionWithAccounts;
 import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.ui.FabManager;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
@@ -66,6 +71,7 @@ import net.ktnx.mobileledger.ui.templates.TemplatesActivity;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
 import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NotNull;
 
@@ -86,6 +92,7 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
     public static final String STATE_ACC_FILTER = "account_filter";
     private static final boolean FAB_HIDDEN = false;
     private static final boolean FAB_SHOWN = true;
+    private ConverterThread converterThread = null;
     private SectionsPagerAdapter mSectionsPagerAdapter;
     private ProfilesRecyclerViewAdapter mProfileListAdapter;
     private int mCurrentPage;
@@ -386,7 +393,16 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
         createShortcuts(newList);
 
         Profile currentProfile = Data.getProfile();
-        if (currentProfile == null || !newList.contains(currentProfile)) {
+        boolean currentProfilePresent = false;
+        if (currentProfile != null) {
+            for (Profile p : newList) {
+                if (p.getId() == currentProfile.getId()) {
+                    currentProfilePresent = true;
+                    break;
+                }
+            }
+        }
+        if (!currentProfilePresent) {
             Logger.debug(TAG, "Switching profile because the current is no longer available");
             Data.setCurrentProfile(newList.get(0));
         }
@@ -395,11 +411,7 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
      * called when the current profile has changed
      */
     private void onProfileChanged(@Nullable Profile newProfile) {
-        if (this.profile == null) {
-            if (newProfile == null)
-                return;
-        }
-        else {
+        if (this.profile != null) {
             if (this.profile.equals(newProfile))
                 return;
         }
@@ -411,7 +423,7 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
         else
             setTitle(R.string.app_name);
 
-        int newProfileTheme = haveProfile ? newProfile.getTheme() : -1;
+        int newProfileTheme = haveProfile ? newProfile.getTheme() : Colors.DEFAULT_HUE_DEG;
         if (newProfileTheme != Colors.profileThemeId) {
             Logger.debug("profiles",
                     String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
@@ -456,14 +468,40 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
             return;
         }
 
-        mainModel.stopTransactionsRetrieval();
+        mainModel.getAccountFilter()
+                 .observe(this, this::onAccountFilterChanged);
 
+        mainModel.stopTransactionsRetrieval();
         mainModel.clearTransactions();
-
-        if (haveProfile) {
-            Logger.debug("transactions", "requesting list reload");
-            mainModel.scheduleTransactionListReload();
+    }
+    private void onAccountFilterChanged(String accFilter) {
+        Logger.debug(TAG, "account filter changed, reloading transactions");
+//                     mainModel.scheduleTransactionListReload();
+        LiveData<List<TransactionWithAccounts>> transactions =
+                new MutableLiveData<>(new ArrayList<>());
+        if (profile != null) {
+            if (accFilter == null || accFilter.isEmpty()) {
+                transactions = DB.get()
+                                 .getTransactionDAO()
+                                 .getAllWithAccounts(profile.getId());
+            }
+            else {
+                transactions = DB.get()
+                                 .getTransactionDAO()
+                                 .getAllWithAccountsFiltered(profile.getId(), accFilter);
+            }
         }
+
+        transactions.observe(this, list -> {
+            Logger.debug(TAG,
+                    String.format(Locale.ROOT, "got transaction list from DB (%d transactions)",
+                            list.size()));
+
+            if (converterThread != null)
+                converterThread.interrupt();
+            converterThread = new ConverterThread(mainModel, list, accFilter);
+            converterThread.start();
+        });
     }
     private void profileThemeChanged() {
         // un-hook all observed LiveData
@@ -718,4 +756,37 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
             return 2;
         }
     }
+
+    static private class ConverterThread extends Thread {
+        private final List<TransactionWithAccounts> list;
+        private final MainModel model;
+        private final String accFilter;
+        public ConverterThread(@NonNull MainModel model,
+                               @NonNull List<TransactionWithAccounts> list, String accFilter) {
+            this.model = model;
+            this.list = list;
+            this.accFilter = accFilter;
+        }
+        @Override
+        public void run() {
+            TransactionAccumulator accumulator = new TransactionAccumulator(accFilter);
+
+            for (TransactionWithAccounts tr : list) {
+                if (isInterrupted()) {
+                    Logger.debug(TAG, "ConverterThread bailing out on interrupt");
+                    return;
+                }
+                accumulator.put(new LedgerTransaction(tr));
+            }
+
+            if (isInterrupted()) {
+                Logger.debug(TAG, "ConverterThread bailing out on interrupt");
+                return;
+            }
+
+            Logger.debug(TAG, "ConverterThread publishing results");
+
+            Misc.onMainThread(() -> accumulator.publishResults(model));
+        }
+    }
 }