boolean keep = savedInstanceState.getBoolean("keep", true);
if (!keep)
viewModel.reset();
+ else {
+ final int focused = savedInstanceState.getInt("focused", 0);
+ viewModel.setFocusedItem(focused);
+ }
}
}
@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.setEnabled(false);
if (mProfile == null)
throw new AssertionError();
+ View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
+ if (hasFocus) {
+ boolean wasSyncing = syncingData;
+ syncingData = true;
+ try {
+ adapter.updateFocusedItem(getAdapterPosition());
+ }
+ finally {
+ syncingData = wasSyncing;
+ }
+ }
+ };
+
+ tvDescription.setOnFocusChangeListener(focusMonitor);
+ tvAccount.setOnFocusChangeListener(focusMonitor);
+ tvAmount.setOnFocusChangeListener(focusMonitor);
+
MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
(NewTransactionActivity) tvDescription.getContext());
break;
case transactionRow:
- focused = tvAccount.requestFocus();
- tvAccount.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+ // do nothing if a row element already has the focus
+ if (!itemView.hasFocus()) {
+ focused = tvAccount.requestFocus();
+ tvAccount.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+ }
break;
}
if (presentItemCount > 2)
notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
}
+ public void updateFocusedItem(int position) {
+ model.updateFocusedItem(position);
+ }
}
private final Item trailer = new Item(this);
private final ArrayList<Item> items = new ArrayList<>();
private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
- private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(null);
+ private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
public int getAccountCount() {
return items.size();
@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
this.accountCount.removeObserver(observer);
}
+ public int getFocusedItem() { return focusedItem.getValue(); }
public void setFocusedItem(int position) {
focusedItem.setValue(position);
}
public void sendCountNotifications() {
accountCount.setValue(getAccountCount());
}
+ public void sendFocusedNotification() {
+ focusedItem.setValue(focusedItem.getValue());
+ }
+ public void updateFocusedItem(int position) {
+ focusedItem.setValue(position);
+ }
enum ItemType {generalData, transactionRow, bottomFiller}
//==========================================================================================