2 * Copyright © 2018 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.support.annotation.ColorInt;
25 import android.support.v4.app.Fragment;
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;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
42 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
44 public class MainActivity extends AppCompatActivity {
46 private AccountSummaryFragment accountSummaryFragment;
47 private TransactionListFragment transactionListFragment;
48 private Fragment currentFragment = null;
49 private FragmentManager fragmentManager;
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.activity_main);
55 Toolbar toolbar = findViewById(R.id.toolbar);
56 setSupportActionBar(toolbar);
58 drawer = findViewById(R.id.drawer_layout);
59 ActionBarDrawerToggle toggle =
60 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
61 R.string.navigation_drawer_close);
62 drawer.addDrawerListener(toggle);
65 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
69 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
70 ver.setText(pi.versionName);
76 fragmentManager = getSupportFragmentManager();
78 onAccountSummaryClicked(null);
80 public void fab_new_transaction_clicked(View view) {
81 Intent intent = new Intent(this, NewTransactionActivity.class);
82 startActivity(intent);
83 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
86 public void nav_exit_clicked(View view) {
87 Log.w("app", "exiting");
91 public void nav_settings_clicked(View view) {
92 Intent intent = new Intent(this, SettingsActivity.class);
93 startActivity(intent);
95 public void markDrawerItemCurrent(int id) {
96 View item = drawer.findViewById(id);
97 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
98 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
101 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
104 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
106 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
107 for (int i = 0; i < actions.getChildCount(); i++) {
108 View view = actions.getChildAt(i);
109 if (view.getId() != id) {
110 view.setBackgroundColor(transparent);
114 public void onOptionsMenuClicked(MenuItem menuItem) {
115 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
116 switch (menuItem.getItemId()) {
117 case R.id.menu_acc_summary_cancel_selection:
118 if (accountSummaryFragment != null)
119 accountSummaryFragment.onCancelAccSelection(menuItem);
121 case R.id.menu_acc_summary_confirm_selection:
122 if (accountSummaryFragment != null)
123 accountSummaryFragment.onConfirmAccSelection(menuItem);
125 case R.id.menu_acc_summary_only_starred:
126 if (accountSummaryFragment != null)
127 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
129 case R.id.menu_transaction_list_filter:
130 if (transactionListFragment != null)
131 transactionListFragment.onShowFilterClick(menuItem);
134 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
137 public void onViewClicked(View view) {
138 switch (view.getId()) {
139 case R.id.clearAccountNameFilter:
140 if (transactionListFragment != null)
141 transactionListFragment.onClearAccountNameClick(view);
144 Log.e("click", String.format("View %d click not handled", view.getId()));
147 public void onAccountSummaryClicked(View view) {
148 drawer.closeDrawers();
150 resetFragmentBackStack();
152 showAccountSummaryFragment();
154 private void showAccountSummaryFragment() {
155 FragmentTransaction ft = fragmentManager.beginTransaction();
156 accountSummaryFragment = new AccountSummaryFragment();
157 ft.replace(R.id.root_frame, accountSummaryFragment);
159 currentFragment = accountSummaryFragment;
161 public void onLatestTransactionsClicked(View view) {
162 drawer.closeDrawers();
164 resetFragmentBackStack();
166 showTransactionsFragment(null);
168 private void resetFragmentBackStack() {
169 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
171 private void showTransactionsFragment(LedgerAccount account) {
172 FragmentTransaction ft = fragmentManager.beginTransaction();
173 if (transactionListFragment == null) {
174 Log.d("flow", "MainActivity creating TransactionListFragment");
175 transactionListFragment = new TransactionListFragment();
177 Bundle bundle = new Bundle();
178 if (account != null) {
179 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
182 transactionListFragment.setArguments(bundle);
183 ft.replace(R.id.root_frame, transactionListFragment);
185 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
188 currentFragment = transactionListFragment;
190 public void showAccountTransactions(LedgerAccount account) {
191 showTransactionsFragment(account);
194 public void onBackPressed() {
195 DrawerLayout drawer = findViewById(R.id.drawer_layout);
196 if (drawer.isDrawerOpen(GravityCompat.START)) {
197 drawer.closeDrawer(GravityCompat.START);
201 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
203 super.onBackPressed();