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
// 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);
menu.findItem(R.id.action_reset_new_transaction_activity)
.setOnMenuItemClickListener(item -> {
- listAdapter.reset();
+ viewModel.reset();
return true;
});
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 -> {
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);
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));
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();
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);
}
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