]> git.ktnx.net Git - mobile-ledger.git/commitdiff
observe background task count in the UI fragments
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 08:02:21 +0000 (08:02 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 08:02:21 +0000 (08:02 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java

index d1241601809c09f5699e74924db6363b0f1cef63..99ce8bded745ef230239f09ff4ae358b95e2ab23 100644 (file)
@@ -37,6 +37,7 @@ import android.view.ViewGroup;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.RetrieveAccountsTask;
+import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.RecyclerItemListener;
@@ -46,6 +47,8 @@ import net.ktnx.mobileledger.utils.MLDB;
 import java.lang.ref.WeakReference;
 import java.util.Date;
 import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
 
 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
 
@@ -59,10 +62,38 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     private AccountSummaryAdapter modelAdapter;
     private Menu optMenu;
     private FloatingActionButton fab;
+    private Observer backgroundTaskCountObserver;
+    @Override
+    public void onDestroy() {
+        if(backgroundTaskCountObserver!= null) {
+            Log.d("acc", "destroying background task count observer");
+            Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
+        }
+        super.onDestroy();
+    }
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
+
+        if (backgroundTaskCountObserver == null) {
+            Log.d("acc", "creating background task count observer");
+            Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
+                @Override
+                public void update(Observable o, Object arg) {
+                    if (mActivity == null) return;
+                    if (swiper == null) return;
+                    mActivity.runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            int cnt = Data.backgroundTaskCount.get();
+                            Log.d("acc", String.format("background task count changed to %d", cnt));
+                            swiper.setRefreshing(cnt > 0);
+                        }
+                    });
+                }
+            });
+        }
     }
     public void onAttach(Context context) {
         super.onAttach(context);
@@ -144,13 +175,19 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
             Log.d("ui", "refreshing accounts via swipe");
             update_accounts(true);
         });
-        prepare_db();
-//        update_account_table();
-        update_accounts(false);
 
-    }
-    private void prepare_db() {
-        account_list_last_updated = MLDB.get_option_value(MLDB.OPT_LAST_REFRESH, (long) 0);
+        Data.accounts.addObserver(new Observer() {
+            @Override
+            public void update(Observable o, Object arg) {
+                mActivity.runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        modelAdapter.notifyDataSetChanged();
+                    }
+                });
+            }
+        });
+        update_account_table();
     }
 
     private void update_accounts(boolean force) {
index c84da7921b6ab343b2dbaed5033fd897e00d8466..92b95fb402f5936cb08d0172a7a88b59d9201aa8 100644 (file)
@@ -55,8 +55,17 @@ public class TransactionListFragment extends MobileLedgerListFragment {
     private MenuItem menuTransactionListFilter;
     private View vAccountFilter;
     private AutoCompleteTextView accNameFilter;
+    private Observer backgroundTaskCountObserver;
     private static void update(Observable o, Object arg) {
     }
+    @Override
+    public void onDestroy() {
+        if (backgroundTaskCountObserver != null) {
+            Log.d("rtl", "destroying background task count observer");
+            Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
+        }
+        super.onDestroy();
+    }
     public void setShowOnlyAccountName(String mShowOnlyAccountName) {
         this.mShowOnlyAccountName = mShowOnlyAccountName;
         if (modelAdapter != null) {
@@ -80,6 +89,22 @@ public class TransactionListFragment extends MobileLedgerListFragment {
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
+        if (backgroundTaskCountObserver == null) {
+            Log.d("rtl", "creating background task count observer");
+            Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
+                @Override
+                public void update(Observable o, Object arg) {
+                    mActivity.runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            int cnt = Data.backgroundTaskCount.get();
+                            Log.d("trl", String.format("background task count changed to %d", cnt));
+                            swiper.setRefreshing(cnt > 0);
+                        }
+                    });
+                }
+            });
+        }
     }
     @Override
     public void onAttach(Context context) {
@@ -180,6 +205,7 @@ public class TransactionListFragment extends MobileLedgerListFragment {
                 });
             }
         });
+
     }
     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
index f1c6df01348e000947d75fb5f314ea60014de7c4..3693a1491fef4a27fd00411cd43fa02c69ba447f 100644 (file)
@@ -41,7 +41,6 @@ public class TransactionListViewModel extends ViewModel {
         String accFilter = hasFilter ? String.valueOf(
                 ((AutoCompleteTextView) act.findViewById(R.id.transaction_filter_account_name))
                         .getText()) : null;
-        updating.set(true);
         AsyncTask<String, Void, List<LedgerTransaction>> task = new UTT();
         task.execute(accFilter);
     }
@@ -59,18 +58,7 @@ public class TransactionListViewModel extends ViewModel {
         @Override
         protected void onPostExecute(List<LedgerTransaction> list) {
             super.onPostExecute(list);
-            updating.set(false);
             if (list != null) Data.transactions.set(list);
         }
-        @Override
-        protected void onCancelled(List<LedgerTransaction> ledgerTransactions) {
-            super.onCancelled(ledgerTransactions);
-            updating.set(false);
-        }
-        @Override
-        protected void onCancelled() {
-            super.onCancelled();
-            updating.set(false);
-        }
     }
 }