]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
typo in exception name
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionFragment.java
index c54c11798fde11acea4b9fae083ca985792838b4..b11945d2e44a37fad9319461d413d479d4ba1de8 100644 (file)
@@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.new_transaction;
 import android.content.Context;
 import android.content.res.Resources;
 import android.os.Bundle;
-import android.renderscript.RSInvalidStateException;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -41,12 +40,13 @@ import androidx.recyclerview.widget.RecyclerView;
 import com.google.android.material.snackbar.Snackbar;
 
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.FabManager;
 import net.ktnx.mobileledger.ui.QR;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
 import net.ktnx.mobileledger.utils.Logger;
 
 import org.jetbrains.annotations.NotNull;
@@ -64,7 +64,7 @@ public class NewTransactionFragment extends Fragment {
     private NewTransactionItemsAdapter listAdapter;
     private NewTransactionModel viewModel;
     private OnNewTransactionFragmentInteractionListener mListener;
-    private MobileLedgerProfile mProfile;
+    private Profile mProfile;
     public NewTransactionFragment() {
         // Required empty public constructor
         setHasOptionsMenu(true);
@@ -127,7 +127,7 @@ public class NewTransactionFragment extends Fragment {
         super.onViewCreated(view, savedInstanceState);
         FragmentActivity activity = getActivity();
         if (activity == null)
-            throw new RSInvalidStateException(
+            throw new IllegalStateException(
                     "getActivity() returned null within onActivityCreated()");
 
         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
@@ -163,15 +163,15 @@ public class NewTransactionFragment extends Fragment {
                            .append("\n\n")
                            .append(error)
                            .append("\n\n");
-                    if (mProfile.getApiVersion()
-                                .equals(API.auto))
+                    if (API.valueOf(mProfile.getApiVersion())
+                           .equals(API.auto))
                         message.append(
                                 resources.getString(R.string.err_json_send_error_unsupported));
                     else {
                         message.append(resources.getString(R.string.err_json_send_error_tail));
                         builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
                             Logger.debug("error", "will start profile editor");
-                            MobileLedgerProfile.startEditProfileActivity(context, mProfile);
+                            ProfileDetailActivity.start(context, mProfile);
                         });
                     }
                     builder.setMessage(message);
@@ -194,8 +194,13 @@ public class NewTransactionFragment extends Fragment {
             element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
         }
 
-        if (!keep)
-            viewModel.reset();
+        if (!keep) {
+            // we need the DB up and running
+            Data.observeProfile(getViewLifecycleOwner(), p -> {
+                if (p != null)
+                    viewModel.reset();
+            });
+        }
         else {
             viewModel.noteFocusChanged(focused, element);
         }
@@ -225,10 +230,12 @@ public class NewTransactionFragment extends Fragment {
         outState.putBoolean("keep", true);
         final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
                                                                  .getValue();
-        final int focusedItem = focusInfo.position;
-        if (focusedItem >= 0)
-            outState.putInt("focused-item", focusedItem);
-        outState.putString("focused-element", focusInfo.element.toString());
+        if (focusInfo != null) {
+            final int focusedItem = focusInfo.position;
+            if (focusedItem >= 0)
+                outState.putInt("focused-item", focusedItem);
+            outState.putString("focused-element", focusInfo.element.toString());
+        }
     }
 
     @Override