]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
more thread pool async task execution
[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 MoLe.
4  * MoLe 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  * MoLe 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 MoLe. 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.content.res.ColorStateList;
23 import android.graphics.Color;
24 import android.os.AsyncTask;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.animation.Animation;
31 import android.view.animation.AnimationUtils;
32 import android.widget.LinearLayout;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
35 import android.widget.Toast;
36
37 import com.google.android.material.floatingactionbutton.FloatingActionButton;
38
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
41 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.LedgerAccount;
44 import net.ktnx.mobileledger.model.MobileLedgerProfile;
45 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
46 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
47 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
48 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
49 import net.ktnx.mobileledger.utils.Colors;
50 import net.ktnx.mobileledger.utils.MLDB;
51
52 import java.lang.ref.WeakReference;
53 import java.text.DateFormat;
54 import java.util.Date;
55
56 import androidx.appcompat.app.ActionBarDrawerToggle;
57 import androidx.appcompat.widget.Toolbar;
58 import androidx.core.view.GravityCompat;
59 import androidx.drawerlayout.widget.DrawerLayout;
60 import androidx.fragment.app.Fragment;
61 import androidx.fragment.app.FragmentManager;
62 import androidx.fragment.app.FragmentPagerAdapter;
63 import androidx.recyclerview.widget.LinearLayoutManager;
64 import androidx.recyclerview.widget.RecyclerView;
65 import androidx.viewpager.widget.ViewPager;
66
67 public class MainActivity extends CrashReportingActivity {
68     private static final String STATE_CURRENT_PAGE = "current_page";
69     private static final String BUNDLE_SAVED_STATE = "bundle_savedState";
70     DrawerLayout drawer;
71     private LinearLayout profileListContainer;
72     private View profileListHeadArrow;
73     private FragmentManager fragmentManager;
74     private TextView tvLastUpdate;
75     private RetrieveTransactionsTask retrieveTransactionsTask;
76     private View bTransactionListCancelDownload;
77     private ProgressBar progressBar;
78     private LinearLayout progressLayout;
79     private SectionsPagerAdapter mSectionsPagerAdapter;
80     private ViewPager mViewPager;
81     private FloatingActionButton fab;
82     private boolean profileModificationEnabled = false;
83     private boolean profileListExpanded = false;
84     private ProfilesRecyclerViewAdapter mProfileListAdapter;
85
86     @Override
87     protected void onStart() {
88         super.onStart();
89
90         Data.lastUpdateDate.set(null);
91         updateLastUpdateTextFromDB();
92         Date lastUpdate = Data.lastUpdateDate.get();
93
94         long now = new Date().getTime();
95         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
96             if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
97             else Log.d("db",
98                     String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
99                             lastUpdate.getTime() / 1000f, now / 1000f));
100
101             scheduleTransactionListRetrieval();
102         }
103     }
104     @Override
105     protected void onSaveInstanceState(Bundle outState) {
106         super.onSaveInstanceState(outState);
107         outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
108     }
109     @Override
110     protected void onCreate(Bundle savedInstanceState) {
111         super.onCreate(savedInstanceState);
112
113         setContentView(R.layout.activity_main);
114
115         fab = findViewById(R.id.btn_add_transaction);
116         profileListContainer = findViewById(R.id.nav_profile_list_container);
117         profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
118         drawer = findViewById(R.id.drawer_layout);
119         tvLastUpdate = findViewById(R.id.transactions_last_update);
120         bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
121         progressBar = findViewById(R.id.transaction_list_progress_bar);
122         progressLayout = findViewById(R.id.transaction_progress_layout);
123         fragmentManager = getSupportFragmentManager();
124         mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
125         mViewPager = findViewById(R.id.root_frame);
126
127         Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
128         if (extra != null && savedInstanceState == null) savedInstanceState = extra;
129
130
131         Toolbar toolbar = findViewById(R.id.toolbar);
132         setSupportActionBar(toolbar);
133
134         Data.profile.addObserver((o, arg) -> {
135             MobileLedgerProfile profile = Data.profile.get();
136             runOnUiThread(() -> {
137                 if (profile == null) setTitle(R.string.app_name);
138                 else setTitle(profile.getName());
139                 updateLastUpdateTextFromDB();
140                 if (profile.isPostingPermitted()) {
141                     toolbar.setSubtitle(null);
142                     fab.show();
143                 }
144                 else {
145                     toolbar.setSubtitle(R.string.profile_subitlte_read_only);
146                     fab.hide();
147                 }
148
149                 int newProfileTheme = profile.getThemeId();
150                 if (newProfileTheme != Colors.profileThemeId) {
151                     Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
152                             newProfileTheme));
153                     profileThemeChanged();
154                     Colors.profileThemeId = newProfileTheme;
155                 }
156             });
157         });
158
159         ActionBarDrawerToggle toggle =
160                 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
161                         R.string.navigation_drawer_close);
162         drawer.addDrawerListener(toggle);
163         toggle.syncState();
164
165         TextView ver = drawer.findViewById(R.id.drawer_version_text);
166
167         try {
168             PackageInfo pi =
169                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
170             ver.setText(pi.versionName);
171         }
172         catch (Exception e) {
173             e.printStackTrace();
174         }
175
176         if (progressBar == null)
177             throw new RuntimeException("Can't get hold on the transaction value progress bar");
178         if (progressLayout == null) throw new RuntimeException(
179                 "Can't get hold on the transaction value progress bar layout");
180
181         markDrawerItemCurrent(R.id.nav_account_summary);
182
183         mViewPager.setAdapter(mSectionsPagerAdapter);
184         mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
185             @Override
186             public void onPageSelected(int position) {
187                 switch (position) {
188                     case 0:
189                         markDrawerItemCurrent(R.id.nav_account_summary);
190                         break;
191                     case 1:
192                         markDrawerItemCurrent(R.id.nav_latest_transactions);
193                         break;
194                     default:
195                         Log.e("MainActivity", String.format("Unexpected page index %d", position));
196                 }
197
198                 super.onPageSelected(position);
199             }
200         });
201
202         if (savedInstanceState != null) {
203             int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
204             if (currentPage != -1) {
205                 mViewPager.setCurrentItem(currentPage, false);
206             }
207         }
208
209         Data.lastUpdateDate.addObserver((o, arg) -> {
210             Log.d("main", "lastUpdateDate changed");
211             runOnUiThread(() -> {
212                 Date date = Data.lastUpdateDate.get();
213                 if (date == null) {
214                     tvLastUpdate.setText(R.string.transaction_last_update_never);
215                 }
216                 else {
217                     final String text = DateFormat.getDateTimeInstance().format(date);
218                     tvLastUpdate.setText(text);
219                     Log.d("despair", String.format("Date formatted: %s", text));
220                 }
221             });
222         });
223
224         findViewById(R.id.btn_no_profiles_add)
225                 .setOnClickListener(v -> startEditProfileActivity(null));
226
227         findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
228
229         findViewById(R.id.nav_new_profile_button)
230                 .setOnClickListener(v -> startEditProfileActivity(null));
231
232         RecyclerView root = findViewById(R.id.nav_profile_list);
233         if (root == null)
234             throw new RuntimeException("Can't get hold on the transaction value view");
235
236         mProfileListAdapter = new ProfilesRecyclerViewAdapter();
237         root.setAdapter(mProfileListAdapter);
238
239         LinearLayoutManager llm = new LinearLayoutManager(this);
240
241         llm.setOrientation(RecyclerView.VERTICAL);
242         root.setLayoutManager(llm);
243     }
244     private void profileThemeChanged() {
245         setupProfileColors();
246
247         Bundle bundle = new Bundle();
248         onSaveInstanceState(bundle);
249         // restart activity to reflect theme change
250         finish();
251         Intent intent = new Intent(this, this.getClass());
252         intent.putExtra(BUNDLE_SAVED_STATE, bundle);
253         startActivity(intent);
254     }
255     @Override
256     protected void onResume() {
257         super.onResume();
258         setupProfile();
259     }
260     public void startEditProfileActivity(MobileLedgerProfile profile) {
261         Intent intent = new Intent(this, ProfileDetailActivity.class);
262         Bundle args = new Bundle();
263         if (profile != null) {
264             int index = Data.getProfileIndex(profile);
265             if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
266         }
267         intent.putExtras(args);
268         startActivity(intent, args);
269     }
270     private void setupProfile() {
271         String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
272         MobileLedgerProfile profile;
273
274         profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
275
276         if (Data.profiles.getList().isEmpty()) {
277             findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
278             findViewById(R.id.pager_layout).setVisibility(View.GONE);
279             return;
280         }
281
282         findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
283         findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
284
285         if (profile == null) profile = Data.profiles.get(0);
286
287         if (profile == null) throw new AssertionError("profile must have a value");
288
289         Data.setCurrentProfile(profile);
290     }
291     public void fabNewTransactionClicked(View view) {
292         Intent intent = new Intent(this, NewTransactionActivity.class);
293         startActivity(intent);
294         overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
295     }
296     public void navSettingsClicked(View view) {
297         Intent intent = new Intent(this, SettingsActivity.class);
298         startActivity(intent);
299         drawer.closeDrawers();
300     }
301     public void markDrawerItemCurrent(int id) {
302         TextView item = drawer.findViewById(id);
303         item.setBackgroundColor(Colors.tableRowDarkBG);
304
305         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
306         for (int i = 0; i < actions.getChildCount(); i++) {
307             View view = actions.getChildAt(i);
308             if (view.getId() != id) {
309                 view.setBackgroundColor(Color.TRANSPARENT);
310             }
311         }
312     }
313     public void onAccountSummaryClicked(View view) {
314         drawer.closeDrawers();
315
316         showAccountSummaryFragment();
317     }
318     private void showAccountSummaryFragment() {
319         mViewPager.setCurrentItem(0, true);
320         TransactionListFragment.accountFilter.set(null);
321 //        FragmentTransaction ft = fragmentManager.beginTransaction();
322 //        accountSummaryFragment = new AccountSummaryFragment();
323 //        ft.replace(R.id.root_frame, accountSummaryFragment);
324 //        ft.commit();
325 //        currentFragment = accountSummaryFragment;
326     }
327     public void onLatestTransactionsClicked(View view) {
328         drawer.closeDrawers();
329
330         showTransactionsFragment(null);
331     }
332     private void resetFragmentBackStack() {
333 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
334     }
335     private void showTransactionsFragment(LedgerAccount account) {
336         if (account != null) TransactionListFragment.accountFilter.set(account.getName());
337         mViewPager.setCurrentItem(1, true);
338 //        FragmentTransaction ft = fragmentManager.beginTransaction();
339 //        if (transactionListFragment == null) {
340 //            Log.d("flow", "MainActivity creating TransactionListFragment");
341 //            transactionListFragment = new TransactionListFragment();
342 //        }
343 //        Bundle bundle = new Bundle();
344 //        if (account != null) {
345 //            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
346 //                    account.getName());
347 //        }
348 //        transactionListFragment.setArguments(bundle);
349 //        ft.replace(R.id.root_frame, transactionListFragment);
350 //        if (account != null)
351 //            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
352 //        ft.commit();
353 //
354 //        currentFragment = transactionListFragment;
355     }
356     public void showAccountTransactions(LedgerAccount account) {
357         showTransactionsFragment(account);
358     }
359     @Override
360     public void onBackPressed() {
361         DrawerLayout drawer = findViewById(R.id.drawer_layout);
362         if (drawer.isDrawerOpen(GravityCompat.START)) {
363             drawer.closeDrawer(GravityCompat.START);
364         }
365         else {
366             Log.d("fragments",
367                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
368
369             super.onBackPressed();
370         }
371     }
372     public void updateLastUpdateTextFromDB() {
373         {
374             final MobileLedgerProfile profile = Data.profile.get();
375             long last_update =
376                     (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
377
378             Log.d("transactions", String.format("Last update = %d", last_update));
379             if (last_update == 0) {
380                 Data.lastUpdateDate.set(null);
381             }
382             else {
383                 Data.lastUpdateDate.set(new Date(last_update));
384             }
385         }
386     }
387     public void scheduleTransactionListRetrieval() {
388         if (Data.profile.get() == null) return;
389
390         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
391
392         retrieveTransactionsTask.execute();
393     }
394     public void onStopTransactionRefreshClick(View view) {
395         Log.d("interactive", "Cancelling transactions refresh");
396         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
397         bTransactionListCancelDownload.setEnabled(false);
398     }
399     public void onRetrieveDone(String error) {
400         progressLayout.setVisibility(View.GONE);
401
402         if (error == null) {
403             updateLastUpdateTextFromDB();
404
405             new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
406         }
407         else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
408     }
409     public void onRetrieveStart() {
410         bTransactionListCancelDownload.setEnabled(true);
411         progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
412         progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
413         progressBar.setIndeterminate(true);
414         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
415         else progressBar.setProgress(0);
416         progressLayout.setVisibility(View.VISIBLE);
417     }
418     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
419         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
420             (progress.getTotal() == 0))
421         {
422             progressBar.setIndeterminate(true);
423         }
424         else {
425             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
426                 progressBar.setMin(0);
427             }
428             progressBar.setMax(progress.getTotal());
429             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
430                 progressBar.setProgress(progress.getProgress(), true);
431             }
432             else progressBar.setProgress(progress.getProgress());
433             progressBar.setIndeterminate(false);
434         }
435     }
436     public void fabShouldShow() {
437         MobileLedgerProfile profile = Data.profile.get();
438         if ((profile != null) && profile.isPostingPermitted()) fab.show();
439     }
440     public void navProfilesHeadClicked(View view) {
441         if (profileListExpanded) {
442             collapseProfileList();
443         }
444         else {
445             expandProfileList();
446         }
447     }
448     private void expandProfileList() {
449         profileListExpanded = true;
450
451
452         profileListContainer.setVisibility(View.VISIBLE);
453         profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
454         profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
455     }
456     private void collapseProfileList() {
457         profileListExpanded = false;
458
459         final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
460         animation.setAnimationListener(new Animation.AnimationListener() {
461             @Override
462             public void onAnimationStart(Animation animation) {
463
464             }
465             @Override
466             public void onAnimationEnd(Animation animation) {
467                 profileListContainer.setVisibility(View.GONE);
468             }
469             @Override
470             public void onAnimationRepeat(Animation animation) {
471
472             }
473         });
474         profileListContainer.startAnimation(animation);
475         profileListHeadArrow
476                 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
477
478         mProfileListAdapter.stopEditingProfiles();
479     }
480     public void onProfileRowClicked(View v) {
481         Data.setCurrentProfile((MobileLedgerProfile) v.getTag());
482     }
483     public void enableProfileModifications() {
484         profileModificationEnabled = true;
485         ViewGroup profileList = findViewById(R.id.nav_profile_list);
486         for (int i = 0; i < profileList.getChildCount(); i++) {
487             View aRow = profileList.getChildAt(i);
488             aRow.findViewById(R.id.profile_list_edit_button).setVisibility(View.VISIBLE);
489             aRow.findViewById(R.id.profile_list_rearrange_handle).setVisibility(View.VISIBLE);
490         }
491         // FIXME enable rearranging
492
493     }
494     public void disableProfileModifications() {
495         profileModificationEnabled = false;
496         ViewGroup profileList = findViewById(R.id.nav_profile_list);
497         for (int i = 0; i < profileList.getChildCount(); i++) {
498             View aRow = profileList.getChildAt(i);
499             aRow.findViewById(R.id.profile_list_edit_button).setVisibility(View.GONE);
500             aRow.findViewById(R.id.profile_list_rearrange_handle).setVisibility(View.GONE);
501         }
502         // FIXME disable rearranging
503
504     }
505
506     public class SectionsPagerAdapter extends FragmentPagerAdapter {
507
508         public SectionsPagerAdapter(FragmentManager fm) {
509             super(fm);
510         }
511
512         @Override
513         public Fragment getItem(int position) {
514             Log.d("main", String.format("Switching to fragment %d", position));
515             switch (position) {
516                 case 0:
517                     return new AccountSummaryFragment();
518                 case 1:
519                     return new TransactionListFragment();
520                 default:
521                     throw new IllegalStateException(
522                             String.format("Unexpected fragment index: " + "%d", position));
523             }
524         }
525
526         @Override
527         public int getCount() {
528             return 2;
529         }
530     }
531
532 }