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.v4.app.Fragment;
25 import android.support.v4.app.FragmentManager;
26 import android.support.v4.app.FragmentTransaction;
27 import android.support.v4.view.GravityCompat;
28 import android.support.v4.widget.DrawerLayout;
29 import android.support.v7.app.ActionBarDrawerToggle;
30 import android.support.v7.app.AppCompatActivity;
31 import android.support.v7.widget.Toolbar;
32 import android.util.Log;
33 import android.view.ContextMenu;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.widget.LinearLayout;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
41 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
43 public class MainActivity extends AppCompatActivity {
45 private AccountSummaryFragment accountSummaryFragment;
46 private TransactionListFragment transactionListFragment;
47 private Fragment currentFragment = null;
48 private FragmentManager fragmentManager;
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 setContentView(R.layout.activity_main);
54 Toolbar toolbar = findViewById(R.id.toolbar);
55 setSupportActionBar(toolbar);
57 drawer = findViewById(R.id.drawer_layout);
58 ActionBarDrawerToggle toggle =
59 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
60 R.string.navigation_drawer_close);
61 drawer.addDrawerListener(toggle);
64 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
68 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
69 ver.setText(pi.versionName);
75 fragmentManager = getSupportFragmentManager();
77 onAccountSummaryClicked(null);
79 public void fab_new_transaction_clicked(View view) {
80 Intent intent = new Intent(this, NewTransactionActivity.class);
81 startActivity(intent);
82 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
85 public void nav_exit_clicked(View view) {
86 Log.w("app", "exiting");
90 public void nav_settings_clicked(View view) {
91 Intent intent = new Intent(this, SettingsActivity.class);
92 startActivity(intent);
94 public void markDrawerItemCurrent(int id) {
95 View item = drawer.findViewById(id);
96 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
97 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
100 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
103 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
104 for (int i = 0; i < actions.getChildCount(); i++) {
105 View view = actions.getChildAt(i);
106 if (view.getId() != id) {
107 view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
111 public void onOptionsMenuClicked(MenuItem menuItem) {
112 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
113 switch (menuItem.getItemId()) {
114 case R.id.menu_acc_summary_cancel_selection:
115 if (accountSummaryFragment != null)
116 accountSummaryFragment.onCancelAccSelection(menuItem);
118 case R.id.menu_acc_summary_confirm_selection:
119 if (accountSummaryFragment != null)
120 accountSummaryFragment.onConfirmAccSelection(menuItem);
122 case R.id.menu_acc_summary_only_starred:
123 if (accountSummaryFragment != null)
124 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
126 case R.id.menu_transaction_list_filter:
127 if (transactionListFragment != null)
128 transactionListFragment.onShowFilterClick(menuItem);
131 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
134 public void onViewClicked(View view) {
135 switch (view.getId()) {
136 case R.id.clearAccountNameFilter:
137 if (transactionListFragment != null)
138 transactionListFragment.onClearAccountNameClick(view);
141 Log.e("click", String.format("View %d click not handled", view.getId()));
144 public void onAccountSummaryClicked(View view) {
145 drawer.closeDrawers();
147 resetFragmentBackStack();
149 showAccountSummaryFragment();
151 private void showAccountSummaryFragment() {
152 FragmentTransaction ft = fragmentManager.beginTransaction();
153 accountSummaryFragment = new AccountSummaryFragment();
154 ft.replace(R.id.root_frame, accountSummaryFragment);
156 currentFragment = accountSummaryFragment;
158 public void onLatestTransactionsClicked(View view) {
159 drawer.closeDrawers();
161 resetFragmentBackStack();
163 showTransactionsFragment(null);
165 private void resetFragmentBackStack() {
166 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
168 private void showTransactionsFragment(LedgerAccount account) {
169 FragmentTransaction ft = fragmentManager.beginTransaction();
170 if (transactionListFragment == null) {
171 Log.d("flow", "MainActivity creating TransactionListFragment");
172 transactionListFragment = new TransactionListFragment();
174 Bundle bundle = new Bundle();
175 if (account != null) {
176 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
179 transactionListFragment.setArguments(bundle);
180 ft.replace(R.id.root_frame, transactionListFragment);
182 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
185 currentFragment = transactionListFragment;
187 public void showAccountTransactions(LedgerAccount account) {
188 showTransactionsFragment(account);
191 public void onBackPressed() {
192 DrawerLayout drawer = findViewById(R.id.drawer_layout);
193 if (drawer.isDrawerOpen(GravityCompat.START)) {
194 drawer.closeDrawer(GravityCompat.START);
198 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
200 super.onBackPressed();