]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
switch new transaction UI to FabManager
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionFragment.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.new_transaction;
19
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.renderscript.RSInvalidStateException;
24 import android.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ProgressBar;
31
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.appcompat.app.AlertDialog;
35 import androidx.fragment.app.Fragment;
36 import androidx.fragment.app.FragmentActivity;
37 import androidx.lifecycle.ViewModelProvider;
38 import androidx.recyclerview.widget.LinearLayoutManager;
39 import androidx.recyclerview.widget.RecyclerView;
40
41 import com.google.android.material.snackbar.Snackbar;
42
43 import net.ktnx.mobileledger.R;
44 import net.ktnx.mobileledger.json.API;
45 import net.ktnx.mobileledger.model.Data;
46 import net.ktnx.mobileledger.model.LedgerTransaction;
47 import net.ktnx.mobileledger.model.MobileLedgerProfile;
48 import net.ktnx.mobileledger.ui.FabManager;
49 import net.ktnx.mobileledger.ui.QR;
50 import net.ktnx.mobileledger.utils.Logger;
51
52 import org.jetbrains.annotations.NotNull;
53
54 /**
55  * A simple {@link Fragment} subclass.
56  * Activities that contain this fragment must implement the
57  * {@link OnNewTransactionFragmentInteractionListener} interface
58  * to handle interaction events.
59  */
60
61 // TODO: offer to undo account remove-on-swipe
62
63 public class NewTransactionFragment extends Fragment {
64     private NewTransactionItemsAdapter listAdapter;
65     private NewTransactionModel viewModel;
66     private OnNewTransactionFragmentInteractionListener mListener;
67     private MobileLedgerProfile mProfile;
68     public NewTransactionFragment() {
69         // Required empty public constructor
70         setHasOptionsMenu(true);
71     }
72     @Override
73     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
74         super.onCreateOptionsMenu(menu, inflater);
75         final FragmentActivity activity = getActivity();
76
77         inflater.inflate(R.menu.new_transaction_fragment, menu);
78
79         menu.findItem(R.id.scan_qr)
80             .setOnMenuItemClickListener(this::onScanQrAction);
81
82         menu.findItem(R.id.action_reset_new_transaction_activity)
83             .setOnMenuItemClickListener(item -> {
84                 viewModel.reset();
85                 return true;
86             });
87
88         final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
89         toggleCurrencyItem.setOnMenuItemClickListener(item -> {
90             viewModel.toggleCurrencyVisible();
91             return true;
92         });
93         if (activity != null)
94             viewModel.getShowCurrency()
95                      .observe(activity, toggleCurrencyItem::setChecked);
96
97         final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
98         toggleCommentsItem.setOnMenuItemClickListener(item -> {
99             viewModel.toggleShowComments();
100             return true;
101         });
102         if (activity != null)
103             viewModel.getShowComments()
104                      .observe(activity, toggleCommentsItem::setChecked);
105     }
106     private boolean onScanQrAction(MenuItem item) {
107         try {
108             Context ctx = requireContext();
109             if (ctx instanceof QR.QRScanTrigger)
110                 ((QR.QRScanTrigger) ctx).triggerQRScan();
111         }
112         catch (Exception e) {
113             Logger.debug("qr", "Error launching QR scanner", e);
114         }
115
116         return true;
117     }
118     @Override
119     public View onCreateView(LayoutInflater inflater, ViewGroup container,
120                              Bundle savedInstanceState) {
121         // Inflate the layout for this fragment
122         return inflater.inflate(R.layout.fragment_new_transaction, container, false);
123     }
124
125     @Override
126     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
127         super.onViewCreated(view, savedInstanceState);
128         FragmentActivity activity = getActivity();
129         if (activity == null)
130             throw new RSInvalidStateException(
131                     "getActivity() returned null within onActivityCreated()");
132
133         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
134         viewModel.observeDataProfile(this);
135         mProfile = Data.getProfile();
136         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
137
138         viewModel.getItems()
139                  .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList));
140
141         RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
142         list.setAdapter(listAdapter);
143         list.setLayoutManager(new LinearLayoutManager(activity));
144
145         Data.observeProfile(getViewLifecycleOwner(), profile -> {
146             mProfile = profile;
147             listAdapter.setProfile(profile);
148         });
149         boolean keep = false;
150
151         Bundle args = getArguments();
152         if (args != null) {
153             String error = args.getString("error");
154             if (error != null) {
155                 Logger.debug("new-trans-f", String.format("Got error: %s", error));
156
157                 Context context = getContext();
158                 if (context != null) {
159                     AlertDialog.Builder builder = new AlertDialog.Builder(context);
160                     final Resources resources = context.getResources();
161                     final StringBuilder message = new StringBuilder();
162                     message.append(resources.getString(R.string.err_json_send_error_head));
163                     message.append("\n\n");
164                     message.append(error);
165                     if (mProfile.getApiVersion()
166                                 .equals(API.auto))
167                         message.append(
168                                 resources.getString(R.string.err_json_send_error_unsupported));
169                     else {
170                         message.append(resources.getString(R.string.err_json_send_error_tail));
171                         builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
172                             Logger.debug("error", "will start profile editor");
173                             MobileLedgerProfile.startEditProfileActivity(context, mProfile);
174                         });
175                     }
176                     builder.setMessage(message);
177                     builder.create()
178                            .show();
179                 }
180                 else {
181                     Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
182                             .show();
183                 }
184                 keep = true;
185             }
186         }
187
188         int focused = 0;
189         FocusedElement element = null;
190         if (savedInstanceState != null) {
191             keep |= savedInstanceState.getBoolean("keep", true);
192             focused = savedInstanceState.getInt("focused-item", 0);
193             element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
194         }
195
196         if (!keep)
197             viewModel.reset();
198         else {
199             viewModel.noteFocusChanged(focused, element);
200         }
201
202         ProgressBar p = activity.findViewById(R.id.progressBar);
203         viewModel.getBusyFlag()
204                  .observe(getViewLifecycleOwner(), isBusy -> {
205                      if (isBusy) {
206 //                Handler h = new Handler();
207 //                h.postDelayed(() -> {
208 //                    if (viewModel.getBusyFlag())
209 //                        p.setVisibility(View.VISIBLE);
210 //
211 //                }, 10);
212                          p.setVisibility(View.VISIBLE);
213                      }
214                      else
215                          p.setVisibility(View.INVISIBLE);
216                  });
217
218         if (activity instanceof FabManager.FabHandler)
219             FabManager.handle((FabManager.FabHandler) activity, list);
220     }
221     @Override
222     public void onSaveInstanceState(@NonNull Bundle outState) {
223         super.onSaveInstanceState(outState);
224         outState.putBoolean("keep", true);
225         final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
226                                                                  .getValue();
227         final int focusedItem = focusInfo.position;
228         if (focusedItem >= 0)
229             outState.putInt("focused-item", focusedItem);
230         outState.putString("focused-element", focusInfo.element.toString());
231     }
232
233     @Override
234     public void onAttach(@NotNull Context context) {
235         super.onAttach(context);
236         if (context instanceof OnNewTransactionFragmentInteractionListener) {
237             mListener = (OnNewTransactionFragmentInteractionListener) context;
238         }
239         else {
240             throw new RuntimeException(
241                     context.toString() + " must implement OnFragmentInteractionListener");
242         }
243     }
244
245     @Override
246     public void onDetach() {
247         super.onDetach();
248         mListener = null;
249     }
250
251     /**
252      * This interface must be implemented by activities that contain this
253      * fragment to allow an interaction in this fragment to be communicated
254      * to the activity and potentially other fragments contained in that
255      * activity.
256      * <p>
257      * See the Android Training lesson <a href=
258      * "http://developer.android.com/training/basics/fragments/communicating.html"
259      * >Communicating with Other Fragments</a> for more information.
260      */
261     public interface OnNewTransactionFragmentInteractionListener {
262         void onTransactionSave(LedgerTransaction tr);
263     }
264 }