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;
import net.ktnx.mobileledger.ui.activity.MainActivity;
-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;
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);
- }
+ mActivity.runOnUiThread(() -> {
+ int cnt = Data.backgroundTaskCount.get();
+ Log.d("acc", String.format("background task count changed to %d", cnt));
+ swiper.setRefreshing(cnt > 0);
});
}
});
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
+ Log.d("flow", "AccountSummaryFragment.onCreateView()");
return inflater.inflate(R.layout.account_summary_fragment, container, false);
}
@Override
+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+ Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
super.onActivityCreated(savedInstanceState);
- mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
-
model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
- List<LedgerAccount> accounts = model.getAccounts(this.getContext());
- modelAdapter = new AccountSummaryAdapter(accounts);
-
- RecyclerView root = mActivity.findViewById(R.id.account_root);
- root.setAdapter(modelAdapter);
+ modelAdapter = new AccountSummaryAdapter();
+ root = mActivity.findViewById(R.id.account_root);
LinearLayoutManager llm = new LinearLayoutManager(mActivity);
llm.setOrientation(LinearLayoutManager.VERTICAL);
root.setLayoutManager(llm);
+ root.setAdapter(modelAdapter);
fab = mActivity.findViewById(R.id.btn_add_transaction);
modelAdapter.selectItem(position);
}
else {
- List<LedgerAccount> accounts = model.getAccounts(mActivity);
+ List<LedgerAccount> accounts = Data.accounts.get();
if (accounts != null) {
LedgerAccount account = accounts.get(position);
swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
swiper.setOnRefreshListener(() -> {
Log.d("ui", "refreshing accounts via swipe");
- update_accounts(true);
+ mActivity.scheduleTransactionListRetrieval();
});
Data.accounts.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- modelAdapter.notifyDataSetChanged();
- }
- });
+ mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
+ }
+ });
+ Data.profile.addObserver(new Observer() {
+ @Override
+ public void update(Observable o, Object arg) {
+ mActivity.runOnUiThread(() -> model.scheduleAccountListReload(mActivity));
}
});
update_account_table();
}
-
- private void update_accounts(boolean force) {
- long now = new Date().getTime();
- if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
- Log.d("db",
- "accounts last updated at " + account_list_last_updated + " and now is " + now +
- ". re-fetching");
- update_accounts();
- }
- }
-
- private void update_accounts() {
- RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(mActivity));
-
- task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
- task.execute();
-
- }
private void update_account_table() {
if (this.getContext() == null) return;
- model.reloadAccounts(this.getContext());
- modelAdapter.notifyDataSetChanged();
+ model.scheduleAccountListReload(this.getContext());
}
public void onShowOnlyStarredClicked(MenuItem mi) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
stopSelection();
}
public void onConfirmAccSelection(MenuItem item) {
- model.commitSelections(mActivity);
+ AccountSummaryViewModel.commitSelections(mActivity);
stopSelection();
}
@Override