From 7cb9091315c4adf9bb278995469c0bb401c33939 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 25 Nov 2019 22:57:41 +0200 Subject: [PATCH] NT: row focus survives re-configuration --- .../ui/activity/NewTransactionFragment.java | 6 ++++ .../ui/activity/NewTransactionItemHolder.java | 28 ++++++++++++++++--- .../activity/NewTransactionItemsAdapter.java | 3 ++ .../ui/activity/NewTransactionModel.java | 9 +++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java index 5492d81e..13d3ccc8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java @@ -173,12 +173,18 @@ public class NewTransactionFragment extends Fragment { 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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java index 6d9ec8c6..507055ff 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java @@ -92,6 +92,23 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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, @@ -215,10 +232,13 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder (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; } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java index 2466d70c..452e348e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java @@ -201,4 +201,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 2) notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone } + public void updateFocusedItem(int position) { + model.updateFocusedItem(position); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index 4c200fc5..e1e1166d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -51,7 +51,7 @@ public class NewTransactionModel extends ViewModel { private final Item trailer = new Item(this); private final ArrayList items = new ArrayList<>(); private final MutableLiveData isSubmittable = new MutableLiveData<>(false); - private final MutableLiveData focusedItem = new MutableLiveData<>(null); + private final MutableLiveData focusedItem = new MutableLiveData<>(0); private final MutableLiveData accountCount = new MutableLiveData<>(0); public int getAccountCount() { return items.size(); @@ -90,6 +90,7 @@ public class NewTransactionModel extends ViewModel { @NonNull androidx.lifecycle.Observer observer) { this.accountCount.removeObserver(observer); } + public int getFocusedItem() { return focusedItem.getValue(); } public void setFocusedItem(int position) { focusedItem.setValue(position); } @@ -270,6 +271,12 @@ public class NewTransactionModel extends ViewModel { 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} //========================================================================================== -- 2.39.5