]> git.ktnx.net Git - mobile-ledger.git/commitdiff
NT: row focus survives re-configuration
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 25 Nov 2019 20:57:41 +0000 (22:57 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 25 Nov 2019 20:57:41 +0000 (22:57 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index 5492d81e2f7da9cbf7b74c339614867b8f13029d..13d3ccc8be6b6c5fb8b37c6b47fe10f5c334e594 100644 (file)
@@ -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);
index 6d9ec8c6b0c046986497f87953db23dda79e78ec..507055ff0eda9eac0dcc9e74d824a06c3db2d8cd 100644 (file)
@@ -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;
                 }
index 2466d70c2bc7621cbc35a99cd31bbb73ab579a51..452e348efb308afec5984f7e5bbd7d189aa02d0b 100644 (file)
@@ -201,4 +201,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         if (presentItemCount > 2)
             notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
     }
+    public void updateFocusedItem(int position) {
+        model.updateFocusedItem(position);
+    }
 }
index 4c200fc592af0ea2610c890d2f54b53d67d822fd..e1e1166d7b4cba65e962f0c067e7755f07225830 100644 (file)
@@ -51,7 +51,7 @@ public class NewTransactionModel extends ViewModel {
     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();
@@ -90,6 +90,7 @@ public class NewTransactionModel extends ViewModel {
             @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);
     }
@@ -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}
 
     //==========================================================================================