From 9ab140e69f987d4a207037e9ef1d3ac9888f3fd4 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 5 Jan 2019 08:02:21 +0000 Subject: [PATCH] observe background task count in the UI fragments --- .../AccountSummaryFragment.java | 49 ++++++++++++++++--- .../TransactionListFragment.java | 26 ++++++++++ .../TransactionListViewModel.java | 12 ----- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index d1241601..99ce8bde 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -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) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index c84da792..92b95fb4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -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) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index f1c6df01..3693a149 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -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> task = new UTT(); task.execute(accFilter); } @@ -59,18 +58,7 @@ public class TransactionListViewModel extends ViewModel { @Override protected void onPostExecute(List list) { super.onPostExecute(list); - updating.set(false); if (list != null) Data.transactions.set(list); } - @Override - protected void onCancelled(List ledgerTransactions) { - super.onCancelled(ledgerTransactions); - updating.set(false); - } - @Override - protected void onCancelled() { - super.onCancelled(); - updating.set(false); - } } } -- 2.39.5