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