X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionFragment.java;h=d1f19c46bc51c7018904d874cda141e411c095cc;hb=346b3c8e74a12b1822239481f807479fa81fc706;hp=aa44c72ff8f2d3f9674904cf7125e0566170a0b8;hpb=9fad5003ac30c3e4f9d073e04f4569aeb31779b2;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 aa44c72f..d1f19c46 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 @@ -17,9 +17,7 @@ package net.ktnx.mobileledger.ui.new_transaction; -import android.app.Activity; import android.content.Context; -import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.renderscript.RSInvalidStateException; @@ -31,8 +29,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; -import androidx.activity.result.ActivityResultLauncher; -import androidx.activity.result.contract.ActivityResultContract; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; @@ -42,24 +38,18 @@ 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.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.QR; 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 @@ -72,79 +62,12 @@ import java.util.regex.Pattern; public class NewTransactionFragment extends Fragment { private NewTransactionItemsAdapter listAdapter; private NewTransactionModel viewModel; - final ActivityResultLauncher scanQrLauncher = - registerForActivityResult(new ActivityResultContract() { - @NonNull - @Override - public Intent createIntent(@NonNull Context context, Void input) { - final Intent intent = new Intent("com.google.zxing.client.android.SCAN"); - intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); - return intent; - } - @Override - public String parseResult(int resultCode, @Nullable Intent intent) { - if (resultCode == Activity.RESULT_CANCELED) - return null; - return intent.getStringExtra("SCAN_RESULT"); - } - }, this::onQrScanned); - private FloatingActionButton fab; private OnNewTransactionFragmentInteractionListener mListener; private MobileLedgerProfile mProfile; public NewTransactionFragment() { // Required empty public constructor setHasOptionsMenu(true); } - private 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); @@ -157,7 +80,7 @@ public class NewTransactionFragment extends Fragment { menu.findItem(R.id.action_reset_new_transaction_activity) .setOnMenuItemClickListener(item -> { - listAdapter.reset(); + viewModel.reset(); return true; }); @@ -167,7 +90,8 @@ public class NewTransactionFragment extends Fragment { 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 -> { @@ -175,11 +99,14 @@ public class NewTransactionFragment extends Fragment { 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); @@ -207,6 +134,9 @@ public class NewTransactionFragment extends Fragment { 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)); @@ -215,25 +145,6 @@ public class NewTransactionFragment extends Fragment { 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.fab); - fab.setOnClickListener(v -> onFabPressed()); - boolean keep = false; Bundle args = getArguments(); @@ -274,73 +185,45 @@ public class NewTransactionFragment extends Fragment { } 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); + }); } @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