X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FAccountSummary.java;h=3a59003dda2d149801fe139a4b1b9abfa77484f7;hb=8cd58ae381b9c50d07cb20217f1e3823fae3906d;hp=e8cf0398b559c77cc07c36011e4bd5cf561777e1;hpb=60a85a18cc2074b87721173798497260dbc800e5;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
index e8cf0398..3a59003d 100644
--- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
+++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
@@ -1,35 +1,66 @@
+/*
+ * 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;
import android.content.Intent;
+import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
-import android.content.res.Resources;
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;
+import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
-import android.widget.ProgressBar;
-import android.widget.TextView;
+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 android.view.View.GONE;
-import static net.ktnx.mobileledger.MobileLedgerDB.db;
-import static net.ktnx.mobileledger.MobileLedgerDB.set_option_value;
+import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
public class AccountSummary extends AppCompatActivity {
- DrawerLayout drawer;
-
private static long account_list_last_updated;
private static boolean account_list_needs_update = true;
+ DrawerLayout drawer;
+ MenuItem mShowHiddenAccounts;
+ SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
+ private AccountSummaryViewModel model;
+ private AccountSummaryAdapter modelAdapter;
+ private Menu optMenu;
+
public static void preferences_changed() {
account_list_needs_update = true;
}
@@ -37,36 +68,117 @@ public class AccountSummary extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_latest_transactions);
+ setContentView(R.layout.activity_account_summary);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
+ ActionBarDrawerToggle toggle =
+ new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
+ R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
try {
- PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
+ PackageInfo pi =
+ getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
ver.setText(pi.versionName);
- } catch (Exception e) {
+ }
+ catch (Exception e) {
e.printStackTrace();
}
+ model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
+ List accounts = model.getAccounts(getApplicationContext());
+ modelAdapter = new AccountSummaryAdapter(accounts);
+
+ RecyclerView root = findViewById(R.id.account_root);
+ root.setAdapter(modelAdapter);
+
+ LinearLayoutManager llm = new LinearLayoutManager(this);
+ llm.setOrientation(LinearLayoutManager.VERTICAL);
+ root.setLayoutManager(llm);
+
+ root.addOnItemTouchListener(new RecyclerItemListener(this, root,
+ new RecyclerItemListener.RecyclerTouchListener() {
+ @Override
+ public void onClickItem(View v, int position) {
+ Log.d("list", String.format("item %d clicked", position));
+ if (modelAdapter.isSelectionActive()) {
+ modelAdapter.selectItem(position);
+ }
+ }
+
+ @Override
+ public void onLongClickItem(View v, int position) {
+ Log.d("list", String.format("item %d long-clicked", position));
+ modelAdapter.startSelection();
+ if (optMenu != null) {
+ optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
+ .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 = findViewById(R.id.btn_add_transaction);
+ if (fab != null) fab.hide();
+ }
+ }
+ }));
+
+ 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);
+ });
prepare_db();
+// update_account_table();
update_accounts(false);
}
+ @Override
+ protected void onStart() {
+ super.onStart();
+ LinearLayout grp = drawer.findViewById(R.id.nav_actions);
+ for (int i = 0; i < grp.getChildCount(); i++) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ grp.getChildAt(i).setBackgroundColor(
+ getResources().getColor(R.color.drawer_background, getTheme()));
+ }
+ else {
+ grp.getChildAt(i)
+ .setBackgroundColor(getResources().getColor(R.color.drawer_background));
+ }
+ }
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(
+ getResources().getColor(R.color.table_row_even_bg, getTheme()));
+ }
+ else {
+ drawer.findViewById(R.id.nav_account_summary)
+ .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
+ }
+ }
+
public void fab_new_transaction_clicked(View view) {
Intent intent = new Intent(this, NewTransactionActivity.class);
startActivity(intent);
+ overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
}
public void nav_exit_clicked(View view) {
- Log.w("mobileledger", "exiting");
+ Log.w("app", "exiting");
finish();
}
@@ -74,13 +186,17 @@ 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);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
- } else {
+ }
+ else {
super.onBackPressed();
}
}
@@ -88,7 +204,19 @@ public class AccountSummary extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
- //getMenuInflater().inflate(R.menu.latest_transactions, menu);
+ getMenuInflater().inflate(R.menu.account_summary, menu);
+ optMenu = menu;
+
+ mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
+ if (mShowHiddenAccounts == null) throw new AssertionError();
+
+ sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
+ .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
+ pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
+
+ mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+
return true;
}
@@ -97,7 +225,7 @@ public class AccountSummary extends AppCompatActivity {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
+// int id = item.getItemId();
//noinspection SimplifiableIfStatement
//if (id == R.id.action_settings) {
@@ -107,58 +235,77 @@ public class AccountSummary extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
- private void prepare_db() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME);
- }
- else {
- MobileLedgerDB.setDb_filename(MobileLedgerDB.DATABASE_NAME);
- }
- MobileLedgerDB.initDB();
+ public void onRefreshAccountSummaryClicked(MenuItem mi) {
+ update_accounts(true);
+ }
- account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0);
+ public void onShowOnlyStarredClicked(MenuItem mi) {
+ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
+ boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
+ SharedPreferences.Editor editor = pref.edit();
+ 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 = MLDB.get_option_value(this, "last_refresh", (long) 0);
}
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");
+ 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() {
- Resources rm = getResources();
-
- ProgressBar pb = findViewById(R.id.progressBar);
- pb.setVisibility(View.VISIBLE);
- TextView pt = findViewById(R.id.textProgress);
- pt.setVisibility(View.VISIBLE);
- pb.setIndeterminate(true);
-
- RetrieveAccountsTask task = new RetrieveAccountsTask() {
- @Override
- protected void onProgressUpdate(Integer... values) {
- if ( values[0] == 0 )
- pt.setText(R.string.progress_connecting);
- else
- pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
- }
-
- @Override
- protected void onPostExecute(Void result) {
- pb.setVisibility(GONE);
- pt.setVisibility(GONE);
- if (this.error != 0)
- Snackbar.make(drawer, rm.getString(this.error), Snackbar.LENGTH_LONG );
- else
- set_option_value("last_refresh", new Date().getTime() );
- }
- };
+ RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(this));
task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
- task.execute(db);
+ task.execute();
}
+ public void onAccountRefreshDone(int error) {
+ SwipeRefreshLayout srl = findViewById(R.id.account_swiper);
+ srl.setRefreshing(false);
+ if (error != 0) {
+ String err_text = getResources().getString(error);
+ Log.d("visual", String.format("showing snackbar: %s", err_text));
+ Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG).show();
+ }
+ else {
+ MLDB.set_option_value(this, "last_refresh", new Date().getTime());
+ update_account_table();
+ }
+ }
+ private void update_account_table() {
+ model.reloadAccounts(getApplicationContext());
+ modelAdapter.notifyDataSetChanged();
+ }
+ 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_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();
+ }
}