]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
8d1b2f8b1a0936026a6eabf4dc89b1411430fde8
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
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;
40
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;
48
49 import java.lang.ref.WeakReference;
50 import java.time.ZoneId;
51 import java.time.format.DateTimeFormatter;
52 import java.util.Date;
53
54 public class MainActivity extends AppCompatActivity {
55     DrawerLayout drawer;
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;
65
66     @Override
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);
72
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);
78         toggle.syncState();
79
80         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
81
82         try {
83             PackageInfo pi =
84                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
85             ver.setText(pi.versionName);
86         }
87         catch (Exception e) {
88             e.printStackTrace();
89         }
90
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));
95
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");
104
105         fragmentManager = getSupportFragmentManager();
106
107         onAccountSummaryClicked(null);
108     }
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);
113     }
114
115     public void nav_exit_clicked(View view) {
116         Log.w("app", "exiting");
117         finish();
118     }
119
120     public void nav_settings_clicked(View view) {
121         Intent intent = new Intent(this, SettingsActivity.class);
122         startActivity(intent);
123     }
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()));
128         }
129         else {
130             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
131         }
132
133         setTitle(item.getText());
134
135         @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
136
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);
142             }
143         }
144     }
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);
151                 break;
152             case R.id.menu_acc_summary_confirm_selection:
153                 if (accountSummaryFragment != null)
154                     accountSummaryFragment.onConfirmAccSelection(menuItem);
155                 break;
156             case R.id.menu_acc_summary_only_starred:
157                 if (accountSummaryFragment != null)
158                     accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
159                 break;
160             case R.id.menu_transaction_list_filter:
161                 if (transactionListFragment != null)
162                     transactionListFragment.onShowFilterClick(menuItem);
163                 break;
164             default:
165                 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
166         }
167     }
168     public void onViewClicked(View view) {
169         switch (view.getId()) {
170             case R.id.clearAccountNameFilter:
171                 if (transactionListFragment != null)
172                     transactionListFragment.onClearAccountNameClick(view);
173                 break;
174             default:
175                 Log.e("click", String.format("View %d click not handled", view.getId()));
176         }
177     }
178     public void onAccountSummaryClicked(View view) {
179         drawer.closeDrawers();
180
181         resetFragmentBackStack();
182
183         showAccountSummaryFragment();
184     }
185     private void showAccountSummaryFragment() {
186         FragmentTransaction ft = fragmentManager.beginTransaction();
187         accountSummaryFragment = new AccountSummaryFragment();
188         ft.replace(R.id.root_frame, accountSummaryFragment);
189         ft.commit();
190         currentFragment = accountSummaryFragment;
191     }
192     public void onLatestTransactionsClicked(View view) {
193         drawer.closeDrawers();
194
195         resetFragmentBackStack();
196
197         showTransactionsFragment(null);
198     }
199     private void resetFragmentBackStack() {
200 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
201     }
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();
207         }
208         Bundle bundle = new Bundle();
209         if (account != null) {
210             bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
211                     account.getName());
212         }
213         transactionListFragment.setArguments(bundle);
214         ft.replace(R.id.root_frame, transactionListFragment);
215         if (account != null)
216             ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
217         ft.commit();
218
219         currentFragment = transactionListFragment;
220     }
221     public void showAccountTransactions(LedgerAccount account) {
222         showTransactionsFragment(account);
223     }
224     @Override
225     public void onBackPressed() {
226         DrawerLayout drawer = findViewById(R.id.drawer_layout);
227         if (drawer.isDrawerOpen(GravityCompat.START)) {
228             drawer.closeDrawer(GravityCompat.START);
229         }
230         else {
231             Log.d("fragments",
232                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
233
234             super.onBackPressed();
235         }
236     }
237     public void updateLastUpdateText() {
238         {
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));
243             }
244             else {
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));
249                 }
250                 else {
251                     tvLastUpdate.setText(date.toLocaleString());
252                 }
253             }
254         }
255     }
256     public void update_transactions() {
257         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
258
259         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
260                 PreferenceManager.getDefaultSharedPreferences(this));
261
262         retrieveTransactionsTask.execute(params);
263         bTransactionListCancelDownload.setEnabled(true);
264     }
265     public void onStopTransactionRefreshClick(View view) {
266         Log.d("interactive", "Cancelling transactions refresh");
267         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
268         bTransactionListCancelDownload.setEnabled(false);
269     }
270     public void onRetrieveDone(boolean success) {
271         progressLayout.setVisibility(View.GONE);
272         updateLastUpdateText();
273     }
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);
279     }
280     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
281         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
282             (progress.getTotal() == 0))
283         {
284             progressBar.setIndeterminate(true);
285         }
286         else {
287             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
288                 progressBar.setMin(0);
289             }
290             progressBar.setMax(progress.getTotal());
291             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
292                 progressBar.setProgress(progress.getProgress(), true);
293             }
294             else progressBar.setProgress(progress.getProgress());
295             progressBar.setIndeterminate(false);
296         }
297     }
298
299 }