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