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.LedgerAccount;
44 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
45 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
46 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
47 import net.ktnx.mobileledger.utils.MLDB;
49 import java.lang.ref.WeakReference;
50 import java.time.ZoneId;
51 import java.time.format.DateTimeFormatter;
52 import java.util.Date;
54 public class MainActivity extends AppCompatActivity {
56 private AccountSummaryFragment accountSummaryFragment;
57 private TransactionListFragment transactionListFragment;
58 public MobileLedgerListFragment currentFragment = null;
59 private FragmentManager fragmentManager;
60 private TextView tvLastUpdate;
61 private RetrieveTransactionsTask retrieveTransactionsTask;
62 private View bTransactionListCancelDownload;
63 private ProgressBar progressBar;
64 private LinearLayout progressLayout;
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 setContentView(R.layout.activity_main);
70 Toolbar toolbar = findViewById(R.id.toolbar);
71 setSupportActionBar(toolbar);
73 drawer = findViewById(R.id.drawer_layout);
74 ActionBarDrawerToggle toggle =
75 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
76 R.string.navigation_drawer_close);
77 drawer.addDrawerListener(toggle);
80 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
84 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
85 ver.setText(pi.versionName);
91 tvLastUpdate = findViewById(R.id.transactions_last_update);
92 updateLastUpdateText();
93 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
94 Log.d("transactions", String.format("Last update = %d", last_update));
96 bTransactionListCancelDownload =
97 findViewById(R.id.transaction_list_cancel_download);
98 progressBar = findViewById(R.id.transaction_list_progress_bar);
99 if (progressBar == null)
100 throw new RuntimeException("Can't get hold on the transaction value progress bar");
101 progressLayout = findViewById(R.id.transaction_progress_layout);
102 if (progressLayout == null) throw new RuntimeException(
103 "Can't get hold on the transaction value progress bar layout");
105 fragmentManager = getSupportFragmentManager();
107 onAccountSummaryClicked(null);
109 public void fab_new_transaction_clicked(View view) {
110 Intent intent = new Intent(this, NewTransactionActivity.class);
111 startActivity(intent);
112 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
115 public void nav_exit_clicked(View view) {
116 Log.w("app", "exiting");
120 public void nav_settings_clicked(View view) {
121 Intent intent = new Intent(this, SettingsActivity.class);
122 startActivity(intent);
124 public void markDrawerItemCurrent(int id) {
125 TextView item = drawer.findViewById(id);
126 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
127 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
130 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
133 setTitle(item.getText());
135 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
137 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
138 for (int i = 0; i < actions.getChildCount(); i++) {
139 View view = actions.getChildAt(i);
140 if (view.getId() != id) {
141 view.setBackgroundColor(transparent);
145 public void onOptionsMenuClicked(MenuItem menuItem) {
146 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
147 switch (menuItem.getItemId()) {
148 case R.id.menu_acc_summary_cancel_selection:
149 if (accountSummaryFragment != null)
150 accountSummaryFragment.onCancelAccSelection(menuItem);
152 case R.id.menu_acc_summary_confirm_selection:
153 if (accountSummaryFragment != null)
154 accountSummaryFragment.onConfirmAccSelection(menuItem);
156 case R.id.menu_acc_summary_only_starred:
157 if (accountSummaryFragment != null)
158 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
160 case R.id.menu_transaction_list_filter:
161 if (transactionListFragment != null)
162 transactionListFragment.onShowFilterClick(menuItem);
165 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
168 public void onViewClicked(View view) {
169 switch (view.getId()) {
170 case R.id.clearAccountNameFilter:
171 if (transactionListFragment != null)
172 transactionListFragment.onClearAccountNameClick(view);
175 Log.e("click", String.format("View %d click not handled", view.getId()));
178 public void onAccountSummaryClicked(View view) {
179 drawer.closeDrawers();
181 resetFragmentBackStack();
183 showAccountSummaryFragment();
185 private void showAccountSummaryFragment() {
186 FragmentTransaction ft = fragmentManager.beginTransaction();
187 accountSummaryFragment = new AccountSummaryFragment();
188 ft.replace(R.id.root_frame, accountSummaryFragment);
190 currentFragment = accountSummaryFragment;
192 public void onLatestTransactionsClicked(View view) {
193 drawer.closeDrawers();
195 resetFragmentBackStack();
197 showTransactionsFragment(null);
199 private void resetFragmentBackStack() {
200 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
202 private void showTransactionsFragment(LedgerAccount account) {
203 FragmentTransaction ft = fragmentManager.beginTransaction();
204 if (transactionListFragment == null) {
205 Log.d("flow", "MainActivity creating TransactionListFragment");
206 transactionListFragment = new TransactionListFragment();
208 Bundle bundle = new Bundle();
209 if (account != null) {
210 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
213 transactionListFragment.setArguments(bundle);
214 ft.replace(R.id.root_frame, transactionListFragment);
216 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
219 currentFragment = transactionListFragment;
221 public void showAccountTransactions(LedgerAccount account) {
222 showTransactionsFragment(account);
225 public void onBackPressed() {
226 DrawerLayout drawer = findViewById(R.id.drawer_layout);
227 if (drawer.isDrawerOpen(GravityCompat.START)) {
228 drawer.closeDrawer(GravityCompat.START);
232 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
234 super.onBackPressed();
237 public void updateLastUpdateText() {
239 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
240 Log.d("transactions", String.format("Last update = %d", last_update));
241 if (last_update == 0) {
242 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
245 Date date = new Date(last_update);
246 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
247 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
248 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
251 tvLastUpdate.setText(date.toLocaleString());
256 public void update_transactions() {
257 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
259 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
260 PreferenceManager.getDefaultSharedPreferences(this));
262 retrieveTransactionsTask.execute(params);
263 bTransactionListCancelDownload.setEnabled(true);
265 public void onStopTransactionRefreshClick(View view) {
266 Log.d("interactive", "Cancelling transactions refresh");
267 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
268 bTransactionListCancelDownload.setEnabled(false);
270 public void onRetrieveDone(boolean success) {
271 progressLayout.setVisibility(View.GONE);
272 updateLastUpdateText();
274 public void onRetrieveStart() {
275 progressBar.setIndeterminate(true);
276 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
277 else progressBar.setProgress(0);
278 progressLayout.setVisibility(View.VISIBLE);
280 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
281 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
282 (progress.getTotal() == 0))
284 progressBar.setIndeterminate(true);
287 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
288 progressBar.setMin(0);
290 progressBar.setMax(progress.getTotal());
291 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
292 progressBar.setProgress(progress.getProgress(), true);
294 else progressBar.setProgress(progress.getProgress());
295 progressBar.setIndeterminate(false);