]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
fix current fragment indicator in the drawer after returning from the new transaction...
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
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;
37
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;
42
43 public class MainActivity extends AppCompatActivity {
44     DrawerLayout drawer;
45     private AccountSummaryFragment accountSummaryFragment;
46     private TransactionListFragment transactionListFragment;
47     private Fragment currentFragment = null;
48     private FragmentManager fragmentManager;
49
50     @Override
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);
56
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);
62         toggle.syncState();
63
64         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
65
66         try {
67             PackageInfo pi =
68                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
69             ver.setText(pi.versionName);
70         }
71         catch (Exception e) {
72             e.printStackTrace();
73         }
74
75         fragmentManager = getSupportFragmentManager();
76
77         onAccountSummaryClicked(null);
78     }
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);
83     }
84
85     public void nav_exit_clicked(View view) {
86         Log.w("app", "exiting");
87         finish();
88     }
89
90     public void nav_settings_clicked(View view) {
91         Intent intent = new Intent(this, SettingsActivity.class);
92         startActivity(intent);
93     }
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()));
98         }
99         else {
100             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
101         }
102
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));
108             }
109         }
110     }
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);
117                 break;
118             case R.id.menu_acc_summary_confirm_selection:
119                 if (accountSummaryFragment != null)
120                     accountSummaryFragment.onConfirmAccSelection(menuItem);
121                 break;
122             case R.id.menu_acc_summary_only_starred:
123                 if (accountSummaryFragment != null)
124                     accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
125                 break;
126             case R.id.menu_transaction_list_filter:
127                 if (transactionListFragment != null)
128                     transactionListFragment.onShowFilterClick(menuItem);
129                 break;
130             default:
131                 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
132         }
133     }
134     public void onViewClicked(View view) {
135         switch (view.getId()) {
136             case R.id.clearAccountNameFilter:
137                 if (transactionListFragment != null)
138                     transactionListFragment.onClearAccountNameClick(view);
139                 break;
140             default:
141                 Log.e("click", String.format("View %d click not handled", view.getId()));
142         }
143     }
144     public void onAccountSummaryClicked(View view) {
145         drawer.closeDrawers();
146
147         resetFragmentBackStack();
148
149         showAccountSummaryFragment();
150     }
151     private void showAccountSummaryFragment() {
152         FragmentTransaction ft = fragmentManager.beginTransaction();
153         accountSummaryFragment = new AccountSummaryFragment();
154         ft.replace(R.id.root_frame, accountSummaryFragment);
155         ft.commit();
156         currentFragment = accountSummaryFragment;
157     }
158     public void onLatestTransactionsClicked(View view) {
159         drawer.closeDrawers();
160
161         resetFragmentBackStack();
162
163         showTransactionsFragment(null);
164     }
165     private void resetFragmentBackStack() {
166 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
167     }
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();
173         }
174         Bundle bundle = new Bundle();
175         if (account != null) {
176             bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
177                     account.getName());
178         }
179         transactionListFragment.setArguments(bundle);
180         ft.replace(R.id.root_frame, transactionListFragment);
181         if (account != null)
182             ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
183         ft.commit();
184
185         currentFragment = transactionListFragment;
186     }
187     public void showAccountTransactions(LedgerAccount account) {
188         showTransactionsFragment(account);
189     }
190     @Override
191     public void onBackPressed() {
192         DrawerLayout drawer = findViewById(R.id.drawer_layout);
193         if (drawer.isDrawerOpen(GravityCompat.START)) {
194             drawer.closeDrawer(GravityCompat.START);
195         }
196         else {
197             Log.d("fragments",
198                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
199
200             super.onBackPressed();
201         }
202     }
203
204 }