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.ui.account_summary.AccountSummaryFragment;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
42 public class MainActivity extends AppCompatActivity {
44 private AccountSummaryFragment accountSummaryFragment;
45 private TransactionListFragment transactionListFragment;
46 private Fragment currentFragment = null;
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.activity_main);
52 Toolbar toolbar = findViewById(R.id.toolbar);
53 setSupportActionBar(toolbar);
55 drawer = findViewById(R.id.drawer_layout);
56 ActionBarDrawerToggle toggle =
57 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
58 R.string.navigation_drawer_close);
59 drawer.addDrawerListener(toggle);
62 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
66 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
67 ver.setText(pi.versionName);
73 onAccountSummaryClicked(null);
77 protected void onStart() {
79 LinearLayout grp = drawer.findViewById(R.id.nav_actions);
80 for (int i = 0; i < grp.getChildCount(); i++) {
81 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
82 grp.getChildAt(i).setBackgroundColor(
83 getResources().getColor(R.color.drawer_background, getTheme()));
87 .setBackgroundColor(getResources().getColor(R.color.drawer_background));
90 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
91 drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(
92 getResources().getColor(R.color.table_row_even_bg, getTheme()));
95 drawer.findViewById(R.id.nav_account_summary)
96 .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
100 public void fab_new_transaction_clicked(View view) {
101 Intent intent = new Intent(this, NewTransactionActivity.class);
102 startActivity(intent);
103 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
106 public void nav_exit_clicked(View view) {
107 Log.w("app", "exiting");
111 public void nav_settings_clicked(View view) {
112 Intent intent = new Intent(this, SettingsActivity.class);
113 startActivity(intent);
115 private void markDrawerItemCurrent(int id) {
116 View item = drawer.findViewById(id);
117 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
118 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
121 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
124 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
125 for (int i = 0; i < actions.getChildCount(); i++) {
126 View view = actions.getChildAt(i);
127 if (view.getId() != id) {
128 view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
132 public void onOptionsMenuClicked(MenuItem menuItem) {
133 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
134 switch (menuItem.getItemId()) {
135 case R.id.menu_acc_summary_cancel_selection:
136 if (accountSummaryFragment != null)
137 accountSummaryFragment.onCancelAccSelection(menuItem);
139 case R.id.menu_acc_summary_confirm_selection:
140 if (accountSummaryFragment != null)
141 accountSummaryFragment.onConfirmAccSelection(menuItem);
143 case R.id.menu_acc_summary_only_starred:
144 if (accountSummaryFragment != null)
145 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
147 case R.id.menu_transaction_list_filter:
148 if (transactionListFragment != null)
149 transactionListFragment.onShowFilterClick(menuItem);
152 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
155 public void onViewClicked(View view) {
156 switch (view.getId()) {
157 case R.id.clearAccountNameFilter:
158 if (transactionListFragment != null)
159 transactionListFragment.onClearAccountNameClick(view);
162 Log.e("click", String.format("View %d click not handled", view.getId()));
165 public void onAccountSummaryClicked(View view) {
166 markDrawerItemCurrent(R.id.nav_account_summary);
167 drawer.closeDrawers();
169 FragmentManager fm = getSupportFragmentManager();
170 FragmentTransaction ft = fm.beginTransaction();
171 currentFragment = accountSummaryFragment = new AccountSummaryFragment();
172 ft.replace(R.id.root_frame, accountSummaryFragment);
175 public void onLatestTransactionsClicked(View view) {
176 markDrawerItemCurrent(R.id.nav_latest_transactions);
177 drawer.closeDrawers();
179 FragmentManager fm = getSupportFragmentManager();
180 FragmentTransaction ft = fm.beginTransaction();
181 currentFragment = transactionListFragment = new TransactionListFragment();
182 ft.replace(R.id.root_frame, transactionListFragment);
186 public void onBackPressed() {
187 DrawerLayout drawer = findViewById(R.id.drawer_layout);
188 if (drawer.isDrawerOpen(GravityCompat.START)) {
189 drawer.closeDrawer(GravityCompat.START);
192 super.onBackPressed();