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;
38 import android.widget.TextView;
40 import net.ktnx.mobileledger.R;
41 import net.ktnx.mobileledger.model.LedgerAccount;
42 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
43 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
45 public class MainActivity extends AppCompatActivity {
47 private AccountSummaryFragment accountSummaryFragment;
48 private TransactionListFragment transactionListFragment;
49 private Fragment currentFragment = null;
50 private FragmentManager fragmentManager;
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.activity_main);
56 Toolbar toolbar = findViewById(R.id.toolbar);
57 setSupportActionBar(toolbar);
59 drawer = findViewById(R.id.drawer_layout);
60 ActionBarDrawerToggle toggle =
61 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
62 R.string.navigation_drawer_close);
63 drawer.addDrawerListener(toggle);
66 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
70 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
71 ver.setText(pi.versionName);
77 fragmentManager = getSupportFragmentManager();
79 onAccountSummaryClicked(null);
81 public void fab_new_transaction_clicked(View view) {
82 Intent intent = new Intent(this, NewTransactionActivity.class);
83 startActivity(intent);
84 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
87 public void nav_exit_clicked(View view) {
88 Log.w("app", "exiting");
92 public void nav_settings_clicked(View view) {
93 Intent intent = new Intent(this, SettingsActivity.class);
94 startActivity(intent);
96 public void markDrawerItemCurrent(int id) {
97 TextView item = drawer.findViewById(id);
98 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
99 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
102 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
105 setTitle(item.getText());
107 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
109 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
110 for (int i = 0; i < actions.getChildCount(); i++) {
111 View view = actions.getChildAt(i);
112 if (view.getId() != id) {
113 view.setBackgroundColor(transparent);
117 public void onOptionsMenuClicked(MenuItem menuItem) {
118 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
119 switch (menuItem.getItemId()) {
120 case R.id.menu_acc_summary_cancel_selection:
121 if (accountSummaryFragment != null)
122 accountSummaryFragment.onCancelAccSelection(menuItem);
124 case R.id.menu_acc_summary_confirm_selection:
125 if (accountSummaryFragment != null)
126 accountSummaryFragment.onConfirmAccSelection(menuItem);
128 case R.id.menu_acc_summary_only_starred:
129 if (accountSummaryFragment != null)
130 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
132 case R.id.menu_transaction_list_filter:
133 if (transactionListFragment != null)
134 transactionListFragment.onShowFilterClick(menuItem);
137 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
140 public void onViewClicked(View view) {
141 switch (view.getId()) {
142 case R.id.clearAccountNameFilter:
143 if (transactionListFragment != null)
144 transactionListFragment.onClearAccountNameClick(view);
147 Log.e("click", String.format("View %d click not handled", view.getId()));
150 public void onAccountSummaryClicked(View view) {
151 drawer.closeDrawers();
153 resetFragmentBackStack();
155 showAccountSummaryFragment();
157 private void showAccountSummaryFragment() {
158 FragmentTransaction ft = fragmentManager.beginTransaction();
159 accountSummaryFragment = new AccountSummaryFragment();
160 ft.replace(R.id.root_frame, accountSummaryFragment);
162 currentFragment = accountSummaryFragment;
164 public void onLatestTransactionsClicked(View view) {
165 drawer.closeDrawers();
167 resetFragmentBackStack();
169 showTransactionsFragment(null);
171 private void resetFragmentBackStack() {
172 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
174 private void showTransactionsFragment(LedgerAccount account) {
175 FragmentTransaction ft = fragmentManager.beginTransaction();
176 if (transactionListFragment == null) {
177 Log.d("flow", "MainActivity creating TransactionListFragment");
178 transactionListFragment = new TransactionListFragment();
180 Bundle bundle = new Bundle();
181 if (account != null) {
182 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
185 transactionListFragment.setArguments(bundle);
186 ft.replace(R.id.root_frame, transactionListFragment);
188 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
191 currentFragment = transactionListFragment;
193 public void showAccountTransactions(LedgerAccount account) {
194 showTransactionsFragment(account);
197 public void onBackPressed() {
198 DrawerLayout drawer = findViewById(R.id.drawer_layout);
199 if (drawer.isDrawerOpen(GravityCompat.START)) {
200 drawer.closeDrawer(GravityCompat.START);
204 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
206 super.onBackPressed();