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);
81 protected void onStart() {
83 LinearLayout grp = drawer.findViewById(R.id.nav_actions);
84 for (int i = 0; i < grp.getChildCount(); i++) {
85 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
86 grp.getChildAt(i).setBackgroundColor(
87 getResources().getColor(R.color.drawer_background, getTheme()));
91 .setBackgroundColor(getResources().getColor(R.color.drawer_background));
94 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
95 drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(
96 getResources().getColor(R.color.table_row_even_bg, getTheme()));
99 drawer.findViewById(R.id.nav_account_summary)
100 .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
104 public void fab_new_transaction_clicked(View view) {
105 Intent intent = new Intent(this, NewTransactionActivity.class);
106 startActivity(intent);
107 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
110 public void nav_exit_clicked(View view) {
111 Log.w("app", "exiting");
115 public void nav_settings_clicked(View view) {
116 Intent intent = new Intent(this, SettingsActivity.class);
117 startActivity(intent);
119 public void markDrawerItemCurrent(int id) {
120 View item = drawer.findViewById(id);
121 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
122 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
125 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
128 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
129 for (int i = 0; i < actions.getChildCount(); i++) {
130 View view = actions.getChildAt(i);
131 if (view.getId() != id) {
132 view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
136 public void onOptionsMenuClicked(MenuItem menuItem) {
137 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
138 switch (menuItem.getItemId()) {
139 case R.id.menu_acc_summary_cancel_selection:
140 if (accountSummaryFragment != null)
141 accountSummaryFragment.onCancelAccSelection(menuItem);
143 case R.id.menu_acc_summary_confirm_selection:
144 if (accountSummaryFragment != null)
145 accountSummaryFragment.onConfirmAccSelection(menuItem);
147 case R.id.menu_acc_summary_only_starred:
148 if (accountSummaryFragment != null)
149 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
151 case R.id.menu_transaction_list_filter:
152 if (transactionListFragment != null)
153 transactionListFragment.onShowFilterClick(menuItem);
156 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
159 public void onViewClicked(View view) {
160 switch (view.getId()) {
161 case R.id.clearAccountNameFilter:
162 if (transactionListFragment != null)
163 transactionListFragment.onClearAccountNameClick(view);
166 Log.e("click", String.format("View %d click not handled", view.getId()));
169 public void onAccountSummaryClicked(View view) {
170 drawer.closeDrawers();
172 resetFragmentBackStack();
174 showAccountSummaryFragment();
176 private void showAccountSummaryFragment() {
177 FragmentTransaction ft = fragmentManager.beginTransaction();
178 accountSummaryFragment = new AccountSummaryFragment();
179 ft.replace(R.id.root_frame, accountSummaryFragment);
181 currentFragment = accountSummaryFragment;
183 public void onLatestTransactionsClicked(View view) {
184 drawer.closeDrawers();
186 resetFragmentBackStack();
188 showTransactionsFragment(null);
190 private void resetFragmentBackStack() {
191 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
193 private void showTransactionsFragment(LedgerAccount account) {
194 FragmentTransaction ft = fragmentManager.beginTransaction();
195 if (transactionListFragment == null) {
196 Log.d("flow", "MainActivity creating TransactionListFragment");
197 transactionListFragment = new TransactionListFragment();
199 Bundle bundle = new Bundle();
200 if (account != null) {
201 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
204 transactionListFragment.setArguments(bundle);
205 ft.replace(R.id.root_frame, transactionListFragment);
207 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
210 currentFragment = transactionListFragment;
212 public void showAccountTransactions(LedgerAccount account) {
213 showTransactionsFragment(account);
216 public void onBackPressed() {
217 DrawerLayout drawer = findViewById(R.id.drawer_layout);
218 if (drawer.isDrawerOpen(GravityCompat.START)) {
219 drawer.closeDrawer(GravityCompat.START);
223 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
225 super.onBackPressed();