]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
detect and report API errors when saving new transactions
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionFragment.java
index 39f53a97eb2f9a0cc074d488184442b0a27d4139..d8271db448c65531f04d830dcbd892cbbe7cb413 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -18,6 +18,7 @@
 package net.ktnx.mobileledger.ui.activity;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.os.Bundle;
 import android.renderscript.RSInvalidStateException;
 import android.view.LayoutInflater;
@@ -30,6 +31,7 @@ import android.widget.ProgressBar;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
 import androidx.lifecycle.ViewModelProvider;
@@ -40,17 +42,17 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
 import com.google.android.material.snackbar.Snackbar;
 
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
+import net.ktnx.mobileledger.utils.SimpleDate;
 
 import org.jetbrains.annotations.NotNull;
 
-import java.util.Date;
-
 /**
  * A simple {@link Fragment} subclass.
  * Activities that contain this fragment must implement the
@@ -64,7 +66,6 @@ import java.util.Date;
 public class NewTransactionFragment extends Fragment {
     private NewTransactionItemsAdapter listAdapter;
     private NewTransactionModel viewModel;
-    private RecyclerView list;
     private FloatingActionButton fab;
     private OnNewTransactionFragmentInteractionListener mListener;
     private MobileLedgerProfile mProfile;
@@ -108,21 +109,23 @@ public class NewTransactionFragment extends Fragment {
     }
 
     @Override
-    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
+    public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
+        super.onViewCreated(view, savedInstanceState);
         FragmentActivity activity = getActivity();
         if (activity == null)
             throw new RSInvalidStateException(
                     "getActivity() returned null within onActivityCreated()");
 
-        list = activity.findViewById(R.id.new_transaction_accounts);
         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
         viewModel.observeDataProfile(this);
-        mProfile = Data.profile.getValue();
+        mProfile = Data.getProfile();
         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
+
+        RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
         list.setAdapter(listAdapter);
         list.setLayoutManager(new LinearLayoutManager(activity));
-        Data.profile.observe(getViewLifecycleOwner(), profile -> {
+
+        Data.observeProfile(getViewLifecycleOwner(), profile -> {
             mProfile = profile;
             listAdapter.setProfile(profile);
         });
@@ -132,7 +135,6 @@ public class NewTransactionFragment extends Fragment {
                      if (isSubmittable) {
                          if (fab != null) {
                              fab.show();
-                             fab.setEnabled(true);
                          }
                      }
                      else {
@@ -153,8 +155,34 @@ public class NewTransactionFragment extends Fragment {
             String error = args.getString("error");
             if (error != null) {
                 Logger.debug("new-trans-f", String.format("Got error: %s", error));
-                Snackbar.make(list, error, Snackbar.LENGTH_LONG)
-                        .show();
+
+                Context context = getContext();
+                if (context != null) {
+                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
+                    final Resources resources = context.getResources();
+                    final StringBuilder message = new StringBuilder();
+                    message.append(resources.getString(R.string.err_json_send_error_head));
+                    message.append("\n\n");
+                    message.append(error);
+                    if (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);
+                        });
+                    }
+                    builder.setMessage(message);
+                    builder.create()
+                           .show();
+                }
+                else {
+                    Snackbar.make(list, error, Snackbar.LENGTH_LONG)
+                            .show();
+                }
                 keep = true;
             }
         }
@@ -180,7 +208,7 @@ public class NewTransactionFragment extends Fragment {
 //                        p.setVisibility(View.VISIBLE);
 //
 //                }, 10);
-                        p.setVisibility(View.VISIBLE);
+                p.setVisibility(View.VISIBLE);
             }
             else
                 p.setVisibility(View.INVISIBLE);
@@ -194,13 +222,14 @@ public class NewTransactionFragment extends Fragment {
         outState.putInt("focused", focusedItem);
     }
     private void onFabPressed() {
-        fab.setEnabled(false);
+        fab.hide();
         Misc.hideSoftKeyboard(this);
         if (mListener != null) {
-            Date date = viewModel.getDate();
+            SimpleDate date = viewModel.getDate();
             LedgerTransaction tr =
                     new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
 
+            tr.setComment(viewModel.getComment());
             LedgerTransactionAccount emptyAmountAccount = null;
             float emptyAmountAccountBalance = 0;
             for (int i = 0; i < viewModel.getAccountCount(); i++) {