X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionFragment.java;h=7741d18ac6656902485662583ba02d924078761c;hb=5df10dc0b58df4d4be4e9ab34f1e0f477ca46766;hp=4932f9de12f2d31e4c9a3305f25ac1ee013db26e;hpb=edad3adadb3ca4e24f020c28296c0850d93931d4;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java index 4932f9de..7741d18a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java @@ -38,25 +38,20 @@ import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.db.Profile; 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.ui.QRScanAbleFragment; +import net.ktnx.mobileledger.ui.FabManager; +import net.ktnx.mobileledger.ui.QR; +import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity; 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.regex.Matcher; -import java.util.regex.Pattern; - /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the @@ -66,66 +61,15 @@ import java.util.regex.Pattern; // TODO: offer to undo account remove-on-swipe -public class NewTransactionFragment extends QRScanAbleFragment { +public class NewTransactionFragment extends Fragment { private NewTransactionItemsAdapter listAdapter; private NewTransactionModel viewModel; - private FloatingActionButton fab; private OnNewTransactionFragmentInteractionListener mListener; - private MobileLedgerProfile mProfile; + private Profile mProfile; public NewTransactionFragment() { // Required empty public constructor setHasOptionsMenu(true); } - protected void onQrScanned(String text) { - Logger.debug("qr", String.format("Got QR scan result [%s]", text)); - Pattern p = - Pattern.compile("^(\\d+)\\*(\\d+)\\*(\\d+)-(\\d+)-(\\d+)\\*([:\\d]+)\\*([\\d.]+)$"); - Matcher m = p.matcher(text); - if (m.matches()) { - float amount = Float.parseFloat(m.group(7)); - viewModel.setDate( - new SimpleDate(Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)), - Integer.parseInt(m.group(5)))); - - if (viewModel.accountsInInitialState()) { - { - NewTransactionModel.Item firstItem = viewModel.getItem(1); - if (firstItem == null) { - viewModel.addAccount(new LedgerTransactionAccount("разход:пазар")); - listAdapter.notifyItemInserted(viewModel.items.size() - 1); - } - else { - firstItem.setAccountName("разход:пазар"); - firstItem.getAccount() - .resetAmount(); - listAdapter.notifyItemChanged(1); - } - } - { - NewTransactionModel.Item secondItem = viewModel.getItem(2); - if (secondItem == null) { - viewModel.addAccount( - new LedgerTransactionAccount("актив:кеш:дам", -amount, null, null)); - listAdapter.notifyItemInserted(viewModel.items.size() - 1); - } - else { - secondItem.setAccountName("актив:кеш:дам"); - secondItem.getAccount() - .setAmount(-amount); - listAdapter.notifyItemChanged(2); - } - } - } - else { - viewModel.addAccount(new LedgerTransactionAccount("разход:пазар")); - viewModel.addAccount( - new LedgerTransactionAccount("актив:кеш:дам", -amount, null, null)); - listAdapter.notifyItemRangeInserted(viewModel.items.size() - 1, 2); - } - - listAdapter.checkTransactionSubmittable(); - } - } @Override public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); @@ -138,7 +82,7 @@ public class NewTransactionFragment extends QRScanAbleFragment { menu.findItem(R.id.action_reset_new_transaction_activity) .setOnMenuItemClickListener(item -> { - listAdapter.reset(); + viewModel.reset(); return true; }); @@ -148,7 +92,8 @@ public class NewTransactionFragment extends QRScanAbleFragment { return true; }); if (activity != null) - viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked); + viewModel.getShowCurrency() + .observe(activity, toggleCurrencyItem::setChecked); final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments); toggleCommentsItem.setOnMenuItemClickListener(item -> { @@ -156,11 +101,14 @@ public class NewTransactionFragment extends QRScanAbleFragment { return true; }); if (activity != null) - viewModel.showComments.observe(activity, toggleCommentsItem::setChecked); + viewModel.getShowComments() + .observe(activity, toggleCommentsItem::setChecked); } private boolean onScanQrAction(MenuItem item) { try { - scanQrLauncher.launch(null); + Context ctx = requireContext(); + if (ctx instanceof QR.QRScanTrigger) + ((QR.QRScanTrigger) ctx).triggerQRScan(); } catch (Exception e) { Logger.debug("qr", "Error launching QR scanner", e); @@ -188,6 +136,9 @@ public class NewTransactionFragment extends QRScanAbleFragment { mProfile = Data.getProfile(); listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile); + viewModel.getItems() + .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList)); + RecyclerView list = activity.findViewById(R.id.new_transaction_accounts); list.setAdapter(listAdapter); list.setLayoutManager(new LinearLayoutManager(activity)); @@ -196,25 +147,6 @@ public class NewTransactionFragment extends QRScanAbleFragment { mProfile = profile; listAdapter.setProfile(profile); }); - listAdapter.notifyDataSetChanged(); - viewModel.isSubmittable() - .observe(getViewLifecycleOwner(), isSubmittable -> { - if (isSubmittable) { - if (fab != null) { - fab.show(); - } - } - else { - if (fab != null) { - fab.hide(); - } - } - }); -// viewModel.checkTransactionSubmittable(listAdapter); - - fab = activity.findViewById(R.id.fabAdd); - fab.setOnClickListener(v -> onFabPressed()); - boolean keep = false; Bundle args = getArguments(); @@ -228,18 +160,19 @@ public class NewTransactionFragment extends QRScanAbleFragment { 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_head)) + .append("\n\n") + .append(error) + .append("\n\n"); + if (API.valueOf(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); + ProfileDetailActivity.start(context, mProfile); }); } builder.setMessage(message); @@ -255,73 +188,48 @@ public class NewTransactionFragment extends QRScanAbleFragment { } int focused = 0; + FocusedElement element = null; if (savedInstanceState != null) { keep |= savedInstanceState.getBoolean("keep", true); - focused = savedInstanceState.getInt("focused", 0); + focused = savedInstanceState.getInt("focused-item", 0); + element = FocusedElement.valueOf(savedInstanceState.getString("focused-element")); } if (!keep) viewModel.reset(); else { - viewModel.setFocusedItem(focused); + viewModel.noteFocusChanged(focused, element); } ProgressBar p = activity.findViewById(R.id.progressBar); - viewModel.observeBusyFlag(getViewLifecycleOwner(), isBusy -> { - if (isBusy) { + viewModel.getBusyFlag() + .observe(getViewLifecycleOwner(), isBusy -> { + if (isBusy) { // Handler h = new Handler(); // h.postDelayed(() -> { // if (viewModel.getBusyFlag()) // p.setVisibility(View.VISIBLE); // // }, 10); - p.setVisibility(View.VISIBLE); - } - else - p.setVisibility(View.INVISIBLE); - }); + p.setVisibility(View.VISIBLE); + } + else + p.setVisibility(View.INVISIBLE); + }); + + if (activity instanceof FabManager.FabHandler) + FabManager.handle((FabManager.FabHandler) activity, list); } @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("keep", true); - final int focusedItem = viewModel.getFocusedItem(); - outState.putInt("focused", focusedItem); - } - private void onFabPressed() { - fab.hide(); - Misc.hideSoftKeyboard(this); - if (mListener != null) { - 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++) { - LedgerTransactionAccount acc = - new LedgerTransactionAccount(viewModel.getAccount(i)); - if (acc.getAccountName() - .trim() - .isEmpty()) - continue; - - if (acc.isAmountSet()) { - emptyAmountAccountBalance += acc.getAmount(); - } - else { - emptyAmountAccount = acc; - } - - tr.addAccount(acc); - } - - if (emptyAmountAccount != null) - emptyAmountAccount.setAmount(-emptyAmountAccountBalance); - - mListener.onTransactionSave(tr); - } + final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo() + .getValue(); + final int focusedItem = focusInfo.position; + if (focusedItem >= 0) + outState.putInt("focused-item", focusedItem); + outState.putString("focused-element", focusInfo.element.toString()); } @Override