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(new Runnable() {
115 Date date = Data.lastUpdateDate.get();
117 tvLastUpdate.setText(R.string.transaction_last_update_never);
120 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
121 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
122 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
125 tvLastUpdate.setText(date.toLocaleString());
133 updateLastUpdateTextFromDB();
134 Date lastUpdate = Data.lastUpdateDate.get();
136 long now = new Date().getTime();
137 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
138 if (lastUpdate == null) Log.d("db", "WEB data never fetched. scheduling a fetch");
140 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
141 lastUpdate.getTime() / 1000f, now / 1000f));
143 scheduleTransactionListRetrieval();
146 public void fab_new_transaction_clicked(View view) {
147 Intent intent = new Intent(this, NewTransactionActivity.class);
148 startActivity(intent);
149 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
152 public void nav_exit_clicked(View view) {
153 Log.w("app", "exiting");
157 public void nav_settings_clicked(View view) {
158 Intent intent = new Intent(this, SettingsActivity.class);
159 startActivity(intent);
161 public void markDrawerItemCurrent(int id) {
162 TextView item = drawer.findViewById(id);
163 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
164 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
167 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
170 setTitle(item.getText());
172 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
174 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
175 for (int i = 0; i < actions.getChildCount(); i++) {
176 View view = actions.getChildAt(i);
177 if (view.getId() != id) {
178 view.setBackgroundColor(transparent);
182 public void onOptionsMenuClicked(MenuItem menuItem) {
183 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
184 switch (menuItem.getItemId()) {
185 case R.id.menu_acc_summary_cancel_selection:
186 if (accountSummaryFragment != null)
187 accountSummaryFragment.onCancelAccSelection(menuItem);
189 case R.id.menu_acc_summary_confirm_selection:
190 if (accountSummaryFragment != null)
191 accountSummaryFragment.onConfirmAccSelection(menuItem);
193 case R.id.menu_acc_summary_only_starred:
194 if (accountSummaryFragment != null)
195 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
197 case R.id.menu_transaction_list_filter:
198 if (transactionListFragment != null)
199 transactionListFragment.onShowFilterClick(menuItem);
202 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
205 public void onViewClicked(View view) {
206 switch (view.getId()) {
207 case R.id.clearAccountNameFilter:
208 if (transactionListFragment != null)
209 transactionListFragment.onClearAccountNameClick(view);
212 Log.e("click", String.format("View %d click not handled", view.getId()));
215 public void onAccountSummaryClicked(View view) {
216 drawer.closeDrawers();
218 resetFragmentBackStack();
220 showAccountSummaryFragment();
222 private void showAccountSummaryFragment() {
223 FragmentTransaction ft = fragmentManager.beginTransaction();
224 accountSummaryFragment = new AccountSummaryFragment();
225 ft.replace(R.id.root_frame, accountSummaryFragment);
227 currentFragment = accountSummaryFragment;
229 public void onLatestTransactionsClicked(View view) {
230 drawer.closeDrawers();
232 resetFragmentBackStack();
234 showTransactionsFragment(null);
236 private void resetFragmentBackStack() {
237 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
239 private void showTransactionsFragment(LedgerAccount account) {
240 FragmentTransaction ft = fragmentManager.beginTransaction();
241 if (transactionListFragment == null) {
242 Log.d("flow", "MainActivity creating TransactionListFragment");
243 transactionListFragment = new TransactionListFragment();
245 Bundle bundle = new Bundle();
246 if (account != null) {
247 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
250 transactionListFragment.setArguments(bundle);
251 ft.replace(R.id.root_frame, transactionListFragment);
253 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
256 currentFragment = transactionListFragment;
258 public void showAccountTransactions(LedgerAccount account) {
259 showTransactionsFragment(account);
262 public void onBackPressed() {
263 DrawerLayout drawer = findViewById(R.id.drawer_layout);
264 if (drawer.isDrawerOpen(GravityCompat.START)) {
265 drawer.closeDrawer(GravityCompat.START);
269 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
271 super.onBackPressed();
274 public void updateLastUpdateTextFromDB() {
276 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
278 Log.d("transactions", String.format("Last update = %d", last_update));
279 if (last_update == 0) {
280 Data.lastUpdateDate.set(null);
283 Data.lastUpdateDate.set(new Date(last_update));
287 public void scheduleTransactionListRetrieval() {
288 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
290 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
291 PreferenceManager.getDefaultSharedPreferences(this));
293 retrieveTransactionsTask.execute(params);
294 bTransactionListCancelDownload.setEnabled(true);
296 public void onStopTransactionRefreshClick(View view) {
297 Log.d("interactive", "Cancelling transactions refresh");
298 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
299 bTransactionListCancelDownload.setEnabled(false);
301 public void onRetrieveDone(boolean success) {
302 progressLayout.setVisibility(View.GONE);
303 updateLastUpdateTextFromDB();
305 public void onRetrieveStart() {
306 progressBar.setIndeterminate(true);
307 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
308 else progressBar.setProgress(0);
309 progressLayout.setVisibility(View.VISIBLE);
311 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
312 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
313 (progress.getTotal() == 0))
315 progressBar.setIndeterminate(true);
318 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
319 progressBar.setMin(0);
321 progressBar.setMax(progress.getTotal());
322 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
323 progressBar.setProgress(progress.getProgress(), true);
325 else progressBar.setProgress(progress.getProgress());
326 progressBar.setIndeterminate(false);