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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.activity;
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;
37 import com.google.android.material.floatingactionbutton.FloatingActionButton;
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;
52 import java.lang.ref.WeakReference;
53 import java.text.DateFormat;
54 import java.util.Date;
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;
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";
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;
87 protected void onStart() {
90 Data.lastUpdateDate.set(null);
91 updateLastUpdateTextFromDB();
92 Date lastUpdate = Data.lastUpdateDate.get();
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");
98 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
99 lastUpdate.getTime() / 1000f, now / 1000f));
101 scheduleTransactionListRetrieval();
105 protected void onSaveInstanceState(Bundle outState) {
106 super.onSaveInstanceState(outState);
107 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
110 protected void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
113 setContentView(R.layout.activity_main);
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);
127 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
128 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
131 Toolbar toolbar = findViewById(R.id.toolbar);
132 setSupportActionBar(toolbar);
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);
145 toolbar.setSubtitle(R.string.profile_subitlte_read_only);
149 int newProfileTheme = profile.getThemeId();
150 if (newProfileTheme != Colors.profileThemeId) {
151 Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
153 profileThemeChanged();
154 Colors.profileThemeId = newProfileTheme;
159 ActionBarDrawerToggle toggle =
160 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
161 R.string.navigation_drawer_close);
162 drawer.addDrawerListener(toggle);
165 TextView ver = drawer.findViewById(R.id.drawer_version_text);
169 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
170 ver.setText(pi.versionName);
172 catch (Exception e) {
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");
181 markDrawerItemCurrent(R.id.nav_account_summary);
183 mViewPager.setAdapter(mSectionsPagerAdapter);
184 mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
186 public void onPageSelected(int position) {
189 markDrawerItemCurrent(R.id.nav_account_summary);
192 markDrawerItemCurrent(R.id.nav_latest_transactions);
195 Log.e("MainActivity", String.format("Unexpected page index %d", position));
198 super.onPageSelected(position);
202 if (savedInstanceState != null) {
203 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
204 if (currentPage != -1) {
205 mViewPager.setCurrentItem(currentPage, false);
209 Data.lastUpdateDate.addObserver((o, arg) -> {
210 Log.d("main", "lastUpdateDate changed");
211 runOnUiThread(() -> {
212 Date date = Data.lastUpdateDate.get();
214 tvLastUpdate.setText(R.string.transaction_last_update_never);
217 final String text = DateFormat.getDateTimeInstance().format(date);
218 tvLastUpdate.setText(text);
219 Log.d("despair", String.format("Date formatted: %s", text));
224 findViewById(R.id.btn_no_profiles_add)
225 .setOnClickListener(v -> startEditProfileActivity(null));
227 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
229 findViewById(R.id.nav_new_profile_button)
230 .setOnClickListener(v -> startEditProfileActivity(null));
232 RecyclerView root = findViewById(R.id.nav_profile_list);
234 throw new RuntimeException("Can't get hold on the transaction value view");
236 mProfileListAdapter = new ProfilesRecyclerViewAdapter();
237 root.setAdapter(mProfileListAdapter);
239 LinearLayoutManager llm = new LinearLayoutManager(this);
241 llm.setOrientation(RecyclerView.VERTICAL);
242 root.setLayoutManager(llm);
244 private void profileThemeChanged() {
245 setupProfileColors();
247 Bundle bundle = new Bundle();
248 onSaveInstanceState(bundle);
249 // restart activity to reflect theme change
251 Intent intent = new Intent(this, this.getClass());
252 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
253 startActivity(intent);
256 protected void onResume() {
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);
267 intent.putExtras(args);
268 startActivity(intent, args);
270 private void setupProfile() {
271 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
272 MobileLedgerProfile profile;
274 profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
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);
282 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
283 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
285 if (profile == null) profile = Data.profiles.get(0);
287 if (profile == null) throw new AssertionError("profile must have a value");
289 Data.setCurrentProfile(profile);
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);
296 public void navSettingsClicked(View view) {
297 Intent intent = new Intent(this, SettingsActivity.class);
298 startActivity(intent);
299 drawer.closeDrawers();
301 public void markDrawerItemCurrent(int id) {
302 TextView item = drawer.findViewById(id);
303 item.setBackgroundColor(Colors.tableRowDarkBG);
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);
313 public void onAccountSummaryClicked(View view) {
314 drawer.closeDrawers();
316 showAccountSummaryFragment();
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);
325 // currentFragment = accountSummaryFragment;
327 public void onLatestTransactionsClicked(View view) {
328 drawer.closeDrawers();
330 showTransactionsFragment(null);
332 private void resetFragmentBackStack() {
333 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
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();
343 // Bundle bundle = new Bundle();
344 // if (account != null) {
345 // bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
346 // account.getName());
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));
354 // currentFragment = transactionListFragment;
356 public void showAccountTransactions(LedgerAccount account) {
357 showTransactionsFragment(account);
360 public void onBackPressed() {
361 DrawerLayout drawer = findViewById(R.id.drawer_layout);
362 if (drawer.isDrawerOpen(GravityCompat.START)) {
363 drawer.closeDrawer(GravityCompat.START);
367 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
369 super.onBackPressed();
372 public void updateLastUpdateTextFromDB() {
374 final MobileLedgerProfile profile = Data.profile.get();
376 (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
378 Log.d("transactions", String.format("Last update = %d", last_update));
379 if (last_update == 0) {
380 Data.lastUpdateDate.set(null);
383 Data.lastUpdateDate.set(new Date(last_update));
387 public void scheduleTransactionListRetrieval() {
388 if (Data.profile.get() == null) return;
390 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
392 retrieveTransactionsTask.execute();
394 public void onStopTransactionRefreshClick(View view) {
395 Log.d("interactive", "Cancelling transactions refresh");
396 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
397 bTransactionListCancelDownload.setEnabled(false);
399 public void onRetrieveDone(String error) {
400 progressLayout.setVisibility(View.GONE);
403 updateLastUpdateTextFromDB();
405 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
407 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
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);
418 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
419 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
420 (progress.getTotal() == 0))
422 progressBar.setIndeterminate(true);
425 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
426 progressBar.setMin(0);
428 progressBar.setMax(progress.getTotal());
429 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
430 progressBar.setProgress(progress.getProgress(), true);
432 else progressBar.setProgress(progress.getProgress());
433 progressBar.setIndeterminate(false);
436 public void fabShouldShow() {
437 MobileLedgerProfile profile = Data.profile.get();
438 if ((profile != null) && profile.isPostingPermitted()) fab.show();
440 public void navProfilesHeadClicked(View view) {
441 if (profileListExpanded) {
442 collapseProfileList();
448 private void expandProfileList() {
449 profileListExpanded = true;
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));
456 private void collapseProfileList() {
457 profileListExpanded = false;
459 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
460 animation.setAnimationListener(new Animation.AnimationListener() {
462 public void onAnimationStart(Animation animation) {
466 public void onAnimationEnd(Animation animation) {
467 profileListContainer.setVisibility(View.GONE);
470 public void onAnimationRepeat(Animation animation) {
474 profileListContainer.startAnimation(animation);
476 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
478 mProfileListAdapter.stopEditingProfiles();
480 public void onProfileRowClicked(View v) {
481 Data.setCurrentProfile((MobileLedgerProfile) v.getTag());
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);
491 // FIXME enable rearranging
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);
502 // FIXME disable rearranging
506 public class SectionsPagerAdapter extends FragmentPagerAdapter {
508 public SectionsPagerAdapter(FragmentManager fm) {
513 public Fragment getItem(int position) {
514 Log.d("main", String.format("Switching to fragment %d", position));
517 return new AccountSummaryFragment();
519 return new TransactionListFragment();
521 throw new IllegalStateException(
522 String.format("Unexpected fragment index: " + "%d", position));
527 public int getCount() {