X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FAccountSummary.java;h=2c9589e37cffeb6b25dad537fecd02b9ba2c7f27;hp=c1f834b183f427bbb49e4ee2c947681b0525b1a4;hb=6c58c1072c9af6ea0d0f52baeae7f95ba7f7ee2f;hpb=f6c803b3c43e2601b3c5ed7f6b6a0e630a71cf6b diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index c1f834b1..2c9589e3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -1,3 +1,20 @@ +/* + * Copyright © 2018 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + package net.ktnx.mobileledger; import android.arch.lifecycle.ViewModelProviders; @@ -7,6 +24,8 @@ import android.content.pm.PackageInfo; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; +import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; @@ -22,10 +41,16 @@ import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; +import net.ktnx.mobileledger.async.RetrieveAccountsTask; +import net.ktnx.mobileledger.model.LedgerAccount; +import net.ktnx.mobileledger.utils.MLDB; + import java.lang.ref.WeakReference; import java.util.Date; import java.util.List; +import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; + public class AccountSummary extends AppCompatActivity { DrawerLayout drawer; @@ -33,7 +58,6 @@ public class AccountSummary extends AppCompatActivity { private static boolean account_list_needs_update = true; MenuItem mShowHiddenAccounts; SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener; - private MobileLedgerDatabase dbh; private AccountSummaryViewModel model; private AccountSummaryAdapter modelAdapter; private Menu optMenu; @@ -49,8 +73,6 @@ public class AccountSummary extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - dbh = new MobileLedgerDatabase(this); - drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); @@ -67,7 +89,7 @@ public class AccountSummary extends AppCompatActivity { } model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class); - List accounts = model.getAccounts(); + List accounts = model.getAccounts(getApplicationContext()); modelAdapter = new AccountSummaryAdapter(accounts); RecyclerView root = findViewById(R.id.account_root); @@ -92,12 +114,26 @@ public class AccountSummary extends AppCompatActivity { modelAdapter.startSelection(); if (optMenu != null) { optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(true); - optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(true); + optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(true); + optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false); + } + { + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_transaction); + if (fab != null) fab.hide(); } } })); - ((SwipeRefreshLayout) findViewById(R.id.account_swiper)).setOnRefreshListener(() -> { + root.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + if (dy < 0) ((FloatingActionButton) findViewById(R.id.btn_add_transaction)).show(); + if (dy > 0) ((FloatingActionButton) findViewById(R.id.btn_add_transaction)).hide(); + } + }); + SwipeRefreshLayout swiper = findViewById(R.id.account_swiper); + swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); + swiper.setOnRefreshListener(() -> { Log.d("ui", "refreshing accounts via swipe"); update_accounts(true); }); @@ -145,7 +181,10 @@ public class AccountSummary extends AppCompatActivity { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); } - + public void onLatestTransactionsClicked(View view) { + Intent intent = new Intent(this, TransactionListActivity.class); + startActivity(intent); + } @Override public void onBackPressed() { DrawerLayout drawer = findViewById(R.id.drawer_layout); @@ -162,15 +201,15 @@ public class AccountSummary extends AppCompatActivity { getMenuInflater().inflate(R.menu.account_summary, menu); optMenu = menu; - mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_show_hidden); + mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred); if (mShowHiddenAccounts == null) throw new AssertionError(); sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts - .setChecked(preference.getBoolean("show_hidden_accounts", false)); + .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener); - mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false)); + mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); return true; } @@ -195,20 +234,20 @@ public class AccountSummary extends AppCompatActivity { } public - void onShowHiddenAccountsClicked(MenuItem mi) { + void onShowOnlyStarredClicked(MenuItem mi) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); - boolean flag = pref.getBoolean("show_hidden_accounts", false); + boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); SharedPreferences.Editor editor = pref.edit(); - editor.putBoolean("show_hidden_accounts", !flag); - Log.d("pref", "Setting show_hidden_accounts to " + (flag ? "false" : "true")); + editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag); + Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true")); editor.apply(); update_account_table(); } private void prepare_db() { - account_list_last_updated = dbh.get_option_value("last_refresh", (long) 0); + account_list_last_updated = MLDB.get_option_value(this, "last_refresh", (long) 0); } private void update_accounts(boolean force) { @@ -226,7 +265,7 @@ public class AccountSummary extends AppCompatActivity { task.execute(); } - void onAccountRefreshDone(int error) { + public void onAccountRefreshDone(int error) { SwipeRefreshLayout srl = findViewById(R.id.account_swiper); srl.setRefreshing(false); if (error != 0) { @@ -235,19 +274,31 @@ public class AccountSummary extends AppCompatActivity { Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG ).show(); } else { - dbh.set_option_value("last_refresh", new Date().getTime() ); + MLDB.set_option_value(this, "last_refresh", new Date().getTime()); update_account_table(); } } private void update_account_table() { - model.reloadAccounts(); + model.reloadAccounts(getApplicationContext()); modelAdapter.notifyDataSetChanged(); } - public void onCancelAccSelection(MenuItem item) { + void stopSelection() { modelAdapter.stopSelection(); if (optMenu != null) { optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false); - optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(false); + optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false); + optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true); } + { + FloatingActionButton fab = findViewById(R.id.btn_add_transaction); + if (fab != null) fab.show(); + } + } + public void onCancelAccSelection(MenuItem item) { + stopSelection(); + } + public void onConfirmAccSelection(MenuItem item) { + model.commitSelections(getApplicationContext()); + stopSelection(); } }