2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Intent;
21 import android.content.pm.PackageInfo;
22 import android.os.Build;
23 import android.os.Bundle;
24 import android.preference.PreferenceManager;
25 import android.support.annotation.ColorInt;
26 import android.support.v4.app.FragmentManager;
27 import android.support.v4.app.FragmentTransaction;
28 import android.support.v4.view.GravityCompat;
29 import android.support.v4.widget.DrawerLayout;
30 import android.support.v7.app.ActionBarDrawerToggle;
31 import android.support.v7.app.AppCompatActivity;
32 import android.support.v7.widget.Toolbar;
33 import android.util.Log;
34 import android.view.ContextMenu;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.widget.LinearLayout;
38 import android.widget.ProgressBar;
39 import android.widget.TextView;
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
43 import net.ktnx.mobileledger.model.Data;
44 import net.ktnx.mobileledger.model.LedgerAccount;
45 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
46 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
47 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
48 import net.ktnx.mobileledger.utils.MLDB;
50 import java.lang.ref.WeakReference;
51 import java.time.ZoneId;
52 import java.time.format.DateTimeFormatter;
53 import java.util.Date;
54 import java.util.Observable;
55 import java.util.Observer;
57 public class MainActivity extends AppCompatActivity {
58 public MobileLedgerListFragment currentFragment = null;
60 private AccountSummaryFragment accountSummaryFragment;
61 private TransactionListFragment transactionListFragment;
62 private FragmentManager fragmentManager;
63 private TextView tvLastUpdate;
64 private RetrieveTransactionsTask retrieveTransactionsTask;
65 private View bTransactionListCancelDownload;
66 private ProgressBar progressBar;
67 private LinearLayout progressLayout;
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.activity_main);
73 Toolbar toolbar = findViewById(R.id.toolbar);
74 setSupportActionBar(toolbar);
76 drawer = findViewById(R.id.drawer_layout);
77 ActionBarDrawerToggle toggle =
78 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
79 R.string.navigation_drawer_close);
80 drawer.addDrawerListener(toggle);
83 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
87 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
88 ver.setText(pi.versionName);
94 tvLastUpdate = findViewById(R.id.transactions_last_update);
96 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
97 progressBar = findViewById(R.id.transaction_list_progress_bar);
98 if (progressBar == null)
99 throw new RuntimeException("Can't get hold on the transaction value progress bar");
100 progressLayout = findViewById(R.id.transaction_progress_layout);
101 if (progressLayout == null) throw new RuntimeException(
102 "Can't get hold on the transaction value progress bar layout");
104 fragmentManager = getSupportFragmentManager();
106 onAccountSummaryClicked(null);
108 Data.lastUpdateDate.addObserver(new Observer() {
110 public void update(Observable o, Object arg) {
111 Log.d("main", "lastUpdateDate changed");
112 runOnUiThread(() -> {
113 Date date = Data.lastUpdateDate.get();
115 tvLastUpdate.setText(R.string.transaction_last_update_never);
118 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
119 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
120 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
123 tvLastUpdate.setText(date.toLocaleString());
130 updateLastUpdateTextFromDB();
131 Date lastUpdate = Data.lastUpdateDate.get();
133 long now = new Date().getTime();
134 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
135 if (lastUpdate == null) Log.d("db", "WEB data never fetched. scheduling a fetch");
137 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
138 lastUpdate.getTime() / 1000f, now / 1000f));
140 scheduleTransactionListRetrieval();
143 Data.ledgerTitle.addObserver(new Observer() {
145 public void update(Observable o, Object arg) {
146 runOnUiThread(() -> {
147 String title = Data.ledgerTitle.get();
149 toolbar.setSubtitle("");
151 toolbar.setSubtitle(title);
156 public void fab_new_transaction_clicked(View view) {
157 Intent intent = new Intent(this, NewTransactionActivity.class);
158 startActivity(intent);
159 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
162 public void nav_exit_clicked(View view) {
163 Log.w("app", "exiting");
167 public void nav_settings_clicked(View view) {
168 Intent intent = new Intent(this, SettingsActivity.class);
169 startActivity(intent);
171 public void markDrawerItemCurrent(int id) {
172 TextView item = drawer.findViewById(id);
173 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
174 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
177 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
180 setTitle(item.getText());
182 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
184 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
185 for (int i = 0; i < actions.getChildCount(); i++) {
186 View view = actions.getChildAt(i);
187 if (view.getId() != id) {
188 view.setBackgroundColor(transparent);
192 public void onOptionsMenuClicked(MenuItem menuItem) {
193 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
194 switch (menuItem.getItemId()) {
195 case R.id.menu_acc_summary_cancel_selection:
196 if (accountSummaryFragment != null)
197 accountSummaryFragment.onCancelAccSelection(menuItem);
199 case R.id.menu_acc_summary_confirm_selection:
200 if (accountSummaryFragment != null)
201 accountSummaryFragment.onConfirmAccSelection(menuItem);
203 case R.id.menu_acc_summary_only_starred:
204 if (accountSummaryFragment != null)
205 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
207 case R.id.menu_transaction_list_filter:
208 if (transactionListFragment != null)
209 transactionListFragment.onShowFilterClick(menuItem);
212 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
215 public void onViewClicked(View view) {
216 switch (view.getId()) {
217 case R.id.clearAccountNameFilter:
218 if (transactionListFragment != null)
219 transactionListFragment.onClearAccountNameClick(view);
222 Log.e("click", String.format("View %d click not handled", view.getId()));
225 public void onAccountSummaryClicked(View view) {
226 drawer.closeDrawers();
228 resetFragmentBackStack();
230 showAccountSummaryFragment();
232 private void showAccountSummaryFragment() {
233 FragmentTransaction ft = fragmentManager.beginTransaction();
234 accountSummaryFragment = new AccountSummaryFragment();
235 ft.replace(R.id.root_frame, accountSummaryFragment);
237 currentFragment = accountSummaryFragment;
239 public void onLatestTransactionsClicked(View view) {
240 drawer.closeDrawers();
242 resetFragmentBackStack();
244 showTransactionsFragment(null);
246 private void resetFragmentBackStack() {
247 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
249 private void showTransactionsFragment(LedgerAccount account) {
250 FragmentTransaction ft = fragmentManager.beginTransaction();
251 if (transactionListFragment == null) {
252 Log.d("flow", "MainActivity creating TransactionListFragment");
253 transactionListFragment = new TransactionListFragment();
255 Bundle bundle = new Bundle();
256 if (account != null) {
257 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
260 transactionListFragment.setArguments(bundle);
261 ft.replace(R.id.root_frame, transactionListFragment);
263 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
266 currentFragment = transactionListFragment;
268 public void showAccountTransactions(LedgerAccount account) {
269 showTransactionsFragment(account);
272 public void onBackPressed() {
273 DrawerLayout drawer = findViewById(R.id.drawer_layout);
274 if (drawer.isDrawerOpen(GravityCompat.START)) {
275 drawer.closeDrawer(GravityCompat.START);
279 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
281 super.onBackPressed();
284 public void updateLastUpdateTextFromDB() {
286 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
288 Log.d("transactions", String.format("Last update = %d", last_update));
289 if (last_update == 0) {
290 Data.lastUpdateDate.set(null);
293 Data.lastUpdateDate.set(new Date(last_update));
297 public void scheduleTransactionListRetrieval() {
298 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
300 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
301 PreferenceManager.getDefaultSharedPreferences(this));
303 retrieveTransactionsTask.execute(params);
304 bTransactionListCancelDownload.setEnabled(true);
306 public void onStopTransactionRefreshClick(View view) {
307 Log.d("interactive", "Cancelling transactions refresh");
308 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
309 bTransactionListCancelDownload.setEnabled(false);
311 public void onRetrieveDone(boolean success) {
312 progressLayout.setVisibility(View.GONE);
313 updateLastUpdateTextFromDB();
315 public void onRetrieveStart() {
316 progressBar.setIndeterminate(true);
317 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
318 else progressBar.setProgress(0);
319 progressLayout.setVisibility(View.VISIBLE);
321 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
322 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
323 (progress.getTotal() == 0))
325 progressBar.setIndeterminate(true);
328 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
329 progressBar.setMin(0);
331 progressBar.setMax(progress.getTotal());
332 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
333 progressBar.setProgress(progress.getProgress(), true);
335 else progressBar.setProgress(progress.getProgress());
336 progressBar.setIndeterminate(false);