]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
more pronounced day/month delimiters in the transaction list
[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.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ProgressBar;
30
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.appcompat.app.AlertDialog;
34 import androidx.fragment.app.Fragment;
35 import androidx.fragment.app.FragmentActivity;
36 import androidx.lifecycle.ViewModelProvider;
37 import androidx.recyclerview.widget.LinearLayoutManager;
38 import androidx.recyclerview.widget.RecyclerView;
39
40 import com.google.android.material.snackbar.Snackbar;
41
42 import net.ktnx.mobileledger.R;
43 import net.ktnx.mobileledger.db.Profile;
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.ui.FabManager;
48 import net.ktnx.mobileledger.ui.QR;
49 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
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 Profile 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 IllegalStateException(
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                            .append("\n\n")
164                            .append(error)
165                            .append("\n\n");
166                     if (API.valueOf(mProfile.getApiVersion())
167                            .equals(API.auto))
168                         message.append(
169                                 resources.getString(R.string.err_json_send_error_unsupported));
170                     else {
171                         message.append(resources.getString(R.string.err_json_send_error_tail));
172                         builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
173                             Logger.debug("error", "will start profile editor");
174                             ProfileDetailActivity.start(context, mProfile);
175                         });
176                     }
177                     builder.setMessage(message);
178                     builder.create()
179                            .show();
180                 }
181                 else {
182                     Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
183                             .show();
184                 }
185                 keep = true;
186             }
187         }
188
189         int focused = 0;
190         FocusedElement element = null;
191         if (savedInstanceState != null) {
192             keep |= savedInstanceState.getBoolean("keep", true);
193             focused = savedInstanceState.getInt("focused-item", 0);
194             final String focusedElementString = savedInstanceState.getString("focused-element");
195             if (focusedElementString != null)
196                 element = FocusedElement.valueOf(focusedElementString);
197         }
198
199         if (!keep) {
200             // we need the DB up and running
201             Data.observeProfile(getViewLifecycleOwner(), p -> {
202                 if (p != null)
203                     viewModel.reset();
204             });
205         }
206         else {
207             viewModel.noteFocusChanged(focused, element);
208         }
209
210         ProgressBar p = activity.findViewById(R.id.progressBar);
211         viewModel.getBusyFlag()
212                  .observe(getViewLifecycleOwner(), isBusy -> {
213                      if (isBusy) {
214 //                Handler h = new Handler();
215 //                h.postDelayed(() -> {
216 //                    if (viewModel.getBusyFlag())
217 //                        p.setVisibility(View.VISIBLE);
218 //
219 //                }, 10);
220                          p.setVisibility(View.VISIBLE);
221                      }
222                      else
223                          p.setVisibility(View.INVISIBLE);
224                  });
225
226         if (activity instanceof FabManager.FabHandler)
227             FabManager.handle((FabManager.FabHandler) activity, list);
228     }
229     @Override
230     public void onSaveInstanceState(@NonNull Bundle outState) {
231         super.onSaveInstanceState(outState);
232         outState.putBoolean("keep", true);
233         final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
234                                                                  .getValue();
235         if (focusInfo != null) {
236             final int focusedItem = focusInfo.position;
237             if (focusedItem >= 0)
238                 outState.putInt("focused-item", focusedItem);
239             if (focusInfo.element != null)
240                 outState.putString("focused-element", focusInfo.element.toString());
241         }
242     }
243
244     @Override
245     public void onAttach(@NotNull Context context) {
246         super.onAttach(context);
247         if (context instanceof OnNewTransactionFragmentInteractionListener) {
248             mListener = (OnNewTransactionFragmentInteractionListener) context;
249         }
250         else {
251             throw new RuntimeException(
252                     context.toString() + " must implement OnFragmentInteractionListener");
253         }
254     }
255
256     @Override
257     public void onDetach() {
258         super.onDetach();
259         mListener = null;
260     }
261
262     /**
263      * This interface must be implemented by activities that contain this
264      * fragment to allow an interaction in this fragment to be communicated
265      * to the activity and potentially other fragments contained in that
266      * activity.
267      * <p>
268      * See the Android Training lesson <a href=
269      * "http://developer.android.com/training/basics/fragments/communicating.html"
270      * >Communicating with Other Fragments</a> for more information.
271      */
272     public interface OnNewTransactionFragmentInteractionListener {
273         void onTransactionSave(LedgerTransaction tr);
274     }
275 }