]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
hide new transaction fab on scroll
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummary.java
1 package net.ktnx.mobileledger;
2
3 import android.arch.lifecycle.ViewModelProviders;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.content.pm.PackageInfo;
7 import android.os.Build;
8 import android.os.Bundle;
9 import android.preference.PreferenceManager;
10 import android.support.annotation.NonNull;
11 import android.support.design.widget.FloatingActionButton;
12 import android.support.design.widget.Snackbar;
13 import android.support.v4.view.GravityCompat;
14 import android.support.v4.widget.DrawerLayout;
15 import android.support.v4.widget.SwipeRefreshLayout;
16 import android.support.v7.app.ActionBarDrawerToggle;
17 import android.support.v7.app.AppCompatActivity;
18 import android.support.v7.widget.LinearLayoutManager;
19 import android.support.v7.widget.RecyclerView;
20 import android.support.v7.widget.Toolbar;
21 import android.util.Log;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
25 import android.widget.LinearLayout;
26
27 import java.lang.ref.WeakReference;
28 import java.util.Date;
29 import java.util.List;
30
31 public class AccountSummary extends AppCompatActivity {
32     DrawerLayout drawer;
33
34     private static long account_list_last_updated;
35     private static boolean account_list_needs_update = true;
36     MenuItem mShowHiddenAccounts;
37     SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
38     private MobileLedgerDatabase dbh;
39     private AccountSummaryViewModel model;
40     private AccountSummaryAdapter modelAdapter;
41     private Menu optMenu;
42
43     public static void preferences_changed() {
44         account_list_needs_update = true;
45     }
46
47     @Override
48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.activity_account_summary);
51         Toolbar toolbar = findViewById(R.id.toolbar);
52         setSupportActionBar(toolbar);
53
54         dbh = new MobileLedgerDatabase(this);
55
56         drawer = findViewById(R.id.drawer_layout);
57         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
58                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
59         drawer.addDrawerListener(toggle);
60         toggle.syncState();
61
62         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
63
64         try {
65             PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
66             ver.setText(pi.versionName);
67         } catch (Exception e) {
68             e.printStackTrace();
69         }
70
71         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
72         List<LedgerAccount> accounts = model.getAccounts();
73         modelAdapter = new AccountSummaryAdapter(accounts);
74
75         RecyclerView root = findViewById(R.id.account_root);
76         root.setAdapter(modelAdapter);
77
78         LinearLayoutManager llm = new LinearLayoutManager(this);
79         llm.setOrientation(LinearLayoutManager.VERTICAL);
80         root.setLayoutManager(llm);
81
82         root.addOnItemTouchListener(new RecyclerItemListener(this, root, new RecyclerItemListener.RecyclerTouchListener() {
83             @Override
84             public void onClickItem(View v, int position) {
85                 Log.d("list", String.format("item %d clicked", position));
86                 if (modelAdapter.isSelectionActive()) {
87                     modelAdapter.selectItem(position);
88                 }
89             }
90
91             @Override
92             public void onLongClickItem(View v, int position) {
93                 Log.d("list", String.format("item %d long-clicked", position));
94                 modelAdapter.startSelection();
95                 if (optMenu != null) {
96                     optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(true);
97                     optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(true);
98                 }
99             }
100         }));
101
102         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
103             @Override
104             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
105                 if (dy < 0) ((FloatingActionButton) findViewById(R.id.btn_add_transaction)).show();
106                 if (dy > 0) ((FloatingActionButton) findViewById(R.id.btn_add_transaction)).hide();
107             }
108         });
109         ((SwipeRefreshLayout) findViewById(R.id.account_swiper)).setOnRefreshListener(() -> {
110             Log.d("ui", "refreshing accounts via swipe");
111             update_accounts(true);
112         });
113         prepare_db();
114 //        update_account_table();
115         update_accounts(false);
116     }
117
118     @Override
119     protected void onStart() {
120         super.onStart();
121         LinearLayout grp = drawer.findViewById(R.id.nav_actions);
122         for (int i = 0; i < grp.getChildCount(); i++) {
123             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
124                 grp.getChildAt(i).setBackgroundColor(
125                         getResources().getColor(R.color.drawer_background, getTheme()));
126             }
127             else {
128                 grp.getChildAt(i)
129                         .setBackgroundColor(getResources().getColor(R.color.drawer_background));
130             }
131         }
132         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
133             drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(
134                     getResources().getColor(R.color.table_row_even_bg, getTheme()));
135         }
136         else {
137             drawer.findViewById(R.id.nav_account_summary)
138                     .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
139         }
140     }
141
142     public void fab_new_transaction_clicked(View view) {
143         Intent intent = new Intent(this, NewTransactionActivity.class);
144         startActivity(intent);
145         overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
146     }
147
148     public void nav_exit_clicked(View view) {
149         Log.w("app", "exiting");
150         finish();
151     }
152
153     public void nav_settings_clicked(View view) {
154         Intent intent = new Intent(this, SettingsActivity.class);
155         startActivity(intent);
156     }
157
158     @Override
159     public void onBackPressed() {
160         DrawerLayout drawer = findViewById(R.id.drawer_layout);
161         if (drawer.isDrawerOpen(GravityCompat.START)) {
162             drawer.closeDrawer(GravityCompat.START);
163         } else {
164             super.onBackPressed();
165         }
166     }
167
168     @Override
169     public boolean onCreateOptionsMenu(Menu menu) {
170         // Inflate the menu; this adds items to the action bar if it is present.
171         getMenuInflater().inflate(R.menu.account_summary, menu);
172         optMenu = menu;
173
174         mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
175         if (mShowHiddenAccounts == null) throw new AssertionError();
176
177         sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
178                 .setChecked(preference.getBoolean("show_hidden_accounts", false));
179         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
180         pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
181
182         mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false));
183
184         return true;
185     }
186
187     @Override
188     public boolean onOptionsItemSelected(MenuItem item) {
189         // Handle action bar item clicks here. The action bar will
190         // automatically handle clicks on the Home/Up button, so long
191         // as you specify a parent activity in AndroidManifest.xml.
192 //        int id = item.getItemId();
193
194         //noinspection SimplifiableIfStatement
195         //if (id == R.id.action_settings) {
196         //    return true;
197         // }
198
199         return super.onOptionsItemSelected(item);
200     }
201
202     public void onRefreshAccountSummaryClicked(MenuItem mi) {
203         update_accounts(true);
204     }
205
206     public
207     void onShowOnlyStarredClicked(MenuItem mi) {
208         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
209         boolean flag = pref.getBoolean("show_hidden_accounts", false);
210
211         SharedPreferences.Editor editor = pref.edit();
212         editor.putBoolean("show_hidden_accounts", !flag);
213         Log.d("pref", "Setting show_hidden_accounts to " + (flag ? "false" : "true"));
214         editor.apply();
215
216         update_account_table();
217     }
218
219     private void prepare_db() {
220         account_list_last_updated = dbh.get_option_value("last_refresh", (long) 0);
221     }
222
223     private void update_accounts(boolean force) {
224         long now = new Date().getTime();
225         if ((now > (account_list_last_updated + (24 * 3600*1000))) || force) {
226             Log.d("db", "accounts last updated at " + account_list_last_updated+" and now is " + now+". re-fetching");
227             update_accounts();
228         }
229     }
230
231     private void update_accounts() {
232         RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(this));
233
234         task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
235         task.execute();
236
237     }
238     void onAccountRefreshDone(int error) {
239         SwipeRefreshLayout srl = findViewById(R.id.account_swiper);
240         srl.setRefreshing(false);
241         if (error != 0) {
242             String err_text = getResources().getString(error);
243             Log.d("visual", String.format("showing snackbar: %s", err_text));
244             Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG ).show();
245         }
246         else {
247             dbh.set_option_value("last_refresh", new Date().getTime() );
248             update_account_table();
249         }
250     }
251     private void update_account_table() {
252         model.reloadAccounts();
253         modelAdapter.notifyDataSetChanged();
254     }
255     public void onCancelAccSelection(MenuItem item) {
256         modelAdapter.stopSelection();
257         if (optMenu != null) {
258             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
259             optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(false);
260         }
261     }
262 }