]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java
move async DB stuff away of AsyncTask
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionActivity.java
index 791bbcefa5a5cc44300caac28c04b133af08ece0..d4355a9b012df33398474e2658eaba5dd660058c 100644 (file)
@@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.new_transaction;
 import android.content.Context;
 import android.content.Intent;
 import android.database.AbstractCursor;
-import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.ParcelFormatException;
 import android.util.TypedValue;
@@ -39,10 +38,11 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.async.AsyncCrasher;
 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
+import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
 import net.ktnx.mobileledger.async.SendTransactionTask;
 import net.ktnx.mobileledger.async.TaskCallback;
+import net.ktnx.mobileledger.dao.BaseDAO;
 import net.ktnx.mobileledger.dao.TransactionDAO;
 import net.ktnx.mobileledger.databinding.ActivityNewTransactionBinding;
 import net.ktnx.mobileledger.db.DB;
@@ -54,6 +54,7 @@ import net.ktnx.mobileledger.model.MatchedTemplate;
 import net.ktnx.mobileledger.ui.FabManager;
 import net.ktnx.mobileledger.ui.QR;
 import net.ktnx.mobileledger.ui.activity.ProfileThemedActivity;
+import net.ktnx.mobileledger.ui.activity.SplashActivity;
 import net.ktnx.mobileledger.ui.templates.TemplatesActivity;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
@@ -83,8 +84,17 @@ public class NewTransactionActivity extends ProfileThemedActivity
         b = ActivityNewTransactionBinding.inflate(getLayoutInflater(), null, false);
         setContentView(b.getRoot());
         setSupportActionBar(b.toolbar);
-        Data.observeProfile(this,
-                mobileLedgerProfile -> b.toolbar.setSubtitle(mobileLedgerProfile.getName()));
+        Data.observeProfile(this, profile -> {
+            if (profile == null) {
+                Logger.debug("new-t-act", "no active profile. Redirecting to SplashActivity");
+                Intent intent = new Intent(this, SplashActivity.class);
+                intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME | Intent.FLAG_ACTIVITY_NEW_TASK);
+                startActivity(intent);
+                finish();
+            }
+            else
+                b.toolbar.setSubtitle(profile.getName());
+        });
 
         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
                 getSupportFragmentManager().findFragmentById(R.id.new_transaction_nav));
@@ -154,7 +164,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
             saver.execute(tr);
         }
         catch (Exception e) {
-            debug("new-transaction", "Unknown error", e);
+            debug("new-transaction", "Unknown error: " + e);
 
             Bundle b = new Bundle();
             b.putString("error", "unknown error");
@@ -163,7 +173,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
     }
     public boolean onSimulateCrashMenuItemClicked(MenuItem item) {
         debug("crash", "Will crash intentionally");
-        new AsyncCrasher().execute();
+        GeneralBackgroundTasks.run(() -> { throw new RuntimeException("Simulated crash");});
         return true;
     }
     public boolean onCreateOptionsMenu(Menu menu) {
@@ -198,14 +208,23 @@ public class NewTransactionActivity extends ProfileThemedActivity
                 getResources().getDisplayMetrics()));
     }
     @Override
-    public void done(String error) {
+    public void onTransactionSaveDone(String error, Object arg) {
         Bundle b = new Bundle();
         if (error != null) {
             b.putString("error", error);
             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
         }
-        else
+        else {
             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
+
+            BaseDAO.runAsync(() -> commitToDb((LedgerTransaction) arg));
+        }
+    }
+    public void commitToDb(LedgerTransaction tr) {
+        TransactionWithAccounts dbTransaction = tr.toDBO();
+        DB.get()
+          .getTransactionDAO()
+          .appendSync(dbTransaction);
     }
     public boolean onToggleSimulateSaveMenuItemClicked(MenuItem item) {
         model.toggleSimulateSave();
@@ -342,28 +361,24 @@ public class NewTransactionActivity extends ProfileThemedActivity
                .create()
                .show();
     }
-    public void descriptionSelected(String description) {
+    public void onDescriptionSelected(String description) {
         debug("description selected", description);
         if (!model.accountListIsEmpty())
             return;
 
-        AsyncTask.execute(() -> {
+        BaseDAO.runAsync(() -> {
             String accFilter = mProfile.getPreferredAccountsFilter();
 
             TransactionDAO trDao = DB.get()
                                      .getTransactionDAO();
 
-            TransactionWithAccounts tr;
+            TransactionWithAccounts tr = null;
 
-            if (Misc.emptyIsNull(accFilter) != null) {
+            if (Misc.emptyIsNull(accFilter) != null)
                 tr = trDao.getFirstByDescriptionHavingAccountSync(description, accFilter);
-                if (tr != null) {
-                    model.loadTransactionIntoModel(tr);
-                    return;
-                }
-            }
+            if (tr == null)
+                tr = trDao.getFirstByDescriptionSync(description);
 
-            tr = trDao.getFirstByDescriptionSync(description);
             if (tr != null)
                 model.loadTransactionIntoModel(tr);
         });