]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
slight optimization
[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.QR;
49 import net.ktnx.mobileledger.utils.Logger;
50
51 import org.jetbrains.annotations.NotNull;
52
53 /**
54  * A simple {@link Fragment} subclass.
55  * Activities that contain this fragment must implement the
56  * {@link OnNewTransactionFragmentInteractionListener} interface
57  * to handle interaction events.
58  */
59
60 // TODO: offer to undo account remove-on-swipe
61
62 public class NewTransactionFragment extends Fragment {
63     private NewTransactionItemsAdapter listAdapter;
64     private NewTransactionModel viewModel;
65     private OnNewTransactionFragmentInteractionListener mListener;
66     private MobileLedgerProfile mProfile;
67     public NewTransactionFragment() {
68         // Required empty public constructor
69         setHasOptionsMenu(true);
70     }
71     @Override
72     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
73         super.onCreateOptionsMenu(menu, inflater);
74         final FragmentActivity activity = getActivity();
75
76         inflater.inflate(R.menu.new_transaction_fragment, menu);
77
78         menu.findItem(R.id.scan_qr)
79             .setOnMenuItemClickListener(this::onScanQrAction);
80
81         menu.findItem(R.id.action_reset_new_transaction_activity)
82             .setOnMenuItemClickListener(item -> {
83                 viewModel.reset();
84                 return true;
85             });
86
87         final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
88         toggleCurrencyItem.setOnMenuItemClickListener(item -> {
89             viewModel.toggleCurrencyVisible();
90             return true;
91         });
92         if (activity != null)
93             viewModel.getShowCurrency()
94                      .observe(activity, toggleCurrencyItem::setChecked);
95
96         final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
97         toggleCommentsItem.setOnMenuItemClickListener(item -> {
98             viewModel.toggleShowComments();
99             return true;
100         });
101         if (activity != null)
102             viewModel.getShowComments()
103                      .observe(activity, toggleCommentsItem::setChecked);
104     }
105     private boolean onScanQrAction(MenuItem item) {
106         try {
107             Context ctx = requireContext();
108             if (ctx instanceof QR.QRScanTrigger)
109                 ((QR.QRScanTrigger) ctx).triggerQRScan();
110         }
111         catch (Exception e) {
112             Logger.debug("qr", "Error launching QR scanner", e);
113         }
114
115         return true;
116     }
117     @Override
118     public View onCreateView(LayoutInflater inflater, ViewGroup container,
119                              Bundle savedInstanceState) {
120         // Inflate the layout for this fragment
121         return inflater.inflate(R.layout.fragment_new_transaction, container, false);
122     }
123
124     @Override
125     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
126         super.onViewCreated(view, savedInstanceState);
127         FragmentActivity activity = getActivity();
128         if (activity == null)
129             throw new RSInvalidStateException(
130                     "getActivity() returned null within onActivityCreated()");
131
132         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
133         viewModel.observeDataProfile(this);
134         mProfile = Data.getProfile();
135         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
136
137         viewModel.getItems()
138                  .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList));
139
140         RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
141         list.setAdapter(listAdapter);
142         list.setLayoutManager(new LinearLayoutManager(activity));
143
144         Data.observeProfile(getViewLifecycleOwner(), profile -> {
145             mProfile = profile;
146             listAdapter.setProfile(profile);
147         });
148         boolean keep = false;
149
150         Bundle args = getArguments();
151         if (args != null) {
152             String error = args.getString("error");
153             if (error != null) {
154                 Logger.debug("new-trans-f", String.format("Got error: %s", error));
155
156                 Context context = getContext();
157                 if (context != null) {
158                     AlertDialog.Builder builder = new AlertDialog.Builder(context);
159                     final Resources resources = context.getResources();
160                     final StringBuilder message = new StringBuilder();
161                     message.append(resources.getString(R.string.err_json_send_error_head));
162                     message.append("\n\n");
163                     message.append(error);
164                     if (mProfile.getApiVersion()
165                                 .equals(API.auto))
166                         message.append(
167                                 resources.getString(R.string.err_json_send_error_unsupported));
168                     else {
169                         message.append(resources.getString(R.string.err_json_send_error_tail));
170                         builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
171                             Logger.debug("error", "will start profile editor");
172                             MobileLedgerProfile.startEditProfileActivity(context, mProfile);
173                         });
174                     }
175                     builder.setMessage(message);
176                     builder.create()
177                            .show();
178                 }
179                 else {
180                     Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
181                             .show();
182                 }
183                 keep = true;
184             }
185         }
186
187         int focused = 0;
188         FocusedElement element = null;
189         if (savedInstanceState != null) {
190             keep |= savedInstanceState.getBoolean("keep", true);
191             focused = savedInstanceState.getInt("focused-item", 0);
192             element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
193         }
194
195         if (!keep)
196             viewModel.reset();
197         else {
198             viewModel.noteFocusChanged(focused, element);
199         }
200
201         ProgressBar p = activity.findViewById(R.id.progressBar);
202         viewModel.getBusyFlag()
203                  .observe(getViewLifecycleOwner(), isBusy -> {
204                      if (isBusy) {
205 //                Handler h = new Handler();
206 //                h.postDelayed(() -> {
207 //                    if (viewModel.getBusyFlag())
208 //                        p.setVisibility(View.VISIBLE);
209 //
210 //                }, 10);
211                          p.setVisibility(View.VISIBLE);
212                      }
213                      else
214                          p.setVisibility(View.INVISIBLE);
215                  });
216     }
217     @Override
218     public void onSaveInstanceState(@NonNull Bundle outState) {
219         super.onSaveInstanceState(outState);
220         outState.putBoolean("keep", true);
221         final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
222                                                                  .getValue();
223         final int focusedItem = focusInfo.position;
224         if (focusedItem >= 0)
225             outState.putInt("focused-item", focusedItem);
226         outState.putString("focused-element", focusInfo.element.toString());
227     }
228
229     @Override
230     public void onAttach(@NotNull Context context) {
231         super.onAttach(context);
232         if (context instanceof OnNewTransactionFragmentInteractionListener) {
233             mListener = (OnNewTransactionFragmentInteractionListener) context;
234         }
235         else {
236             throw new RuntimeException(
237                     context.toString() + " must implement OnFragmentInteractionListener");
238         }
239     }
240
241     @Override
242     public void onDetach() {
243         super.onDetach();
244         mListener = null;
245     }
246
247     /**
248      * This interface must be implemented by activities that contain this
249      * fragment to allow an interaction in this fragment to be communicated
250      * to the activity and potentially other fragments contained in that
251      * activity.
252      * <p>
253      * See the Android Training lesson <a href=
254      * "http://developer.android.com/training/basics/fragments/communicating.html"
255      * >Communicating with Other Fragments</a> for more information.
256      */
257     public interface OnNewTransactionFragmentInteractionListener {
258         void onTransactionSave(LedgerTransaction tr);
259     }
260 }