]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
set title when fragment is changed
[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.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;
39
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;
44
45 public class MainActivity extends AppCompatActivity {
46     DrawerLayout drawer;
47     private AccountSummaryFragment accountSummaryFragment;
48     private TransactionListFragment transactionListFragment;
49     private Fragment currentFragment = null;
50     private FragmentManager fragmentManager;
51
52     @Override
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);
58
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);
64         toggle.syncState();
65
66         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
67
68         try {
69             PackageInfo pi =
70                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
71             ver.setText(pi.versionName);
72         }
73         catch (Exception e) {
74             e.printStackTrace();
75         }
76
77         fragmentManager = getSupportFragmentManager();
78
79         onAccountSummaryClicked(null);
80     }
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);
85     }
86
87     public void nav_exit_clicked(View view) {
88         Log.w("app", "exiting");
89         finish();
90     }
91
92     public void nav_settings_clicked(View view) {
93         Intent intent = new Intent(this, SettingsActivity.class);
94         startActivity(intent);
95     }
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()));
100         }
101         else {
102             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
103         }
104
105         setTitle(item.getText());
106
107         @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
108
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);
114             }
115         }
116     }
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);
123                 break;
124             case R.id.menu_acc_summary_confirm_selection:
125                 if (accountSummaryFragment != null)
126                     accountSummaryFragment.onConfirmAccSelection(menuItem);
127                 break;
128             case R.id.menu_acc_summary_only_starred:
129                 if (accountSummaryFragment != null)
130                     accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
131                 break;
132             case R.id.menu_transaction_list_filter:
133                 if (transactionListFragment != null)
134                     transactionListFragment.onShowFilterClick(menuItem);
135                 break;
136             default:
137                 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
138         }
139     }
140     public void onViewClicked(View view) {
141         switch (view.getId()) {
142             case R.id.clearAccountNameFilter:
143                 if (transactionListFragment != null)
144                     transactionListFragment.onClearAccountNameClick(view);
145                 break;
146             default:
147                 Log.e("click", String.format("View %d click not handled", view.getId()));
148         }
149     }
150     public void onAccountSummaryClicked(View view) {
151         drawer.closeDrawers();
152
153         resetFragmentBackStack();
154
155         showAccountSummaryFragment();
156     }
157     private void showAccountSummaryFragment() {
158         FragmentTransaction ft = fragmentManager.beginTransaction();
159         accountSummaryFragment = new AccountSummaryFragment();
160         ft.replace(R.id.root_frame, accountSummaryFragment);
161         ft.commit();
162         currentFragment = accountSummaryFragment;
163     }
164     public void onLatestTransactionsClicked(View view) {
165         drawer.closeDrawers();
166
167         resetFragmentBackStack();
168
169         showTransactionsFragment(null);
170     }
171     private void resetFragmentBackStack() {
172 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
173     }
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();
179         }
180         Bundle bundle = new Bundle();
181         if (account != null) {
182             bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
183                     account.getName());
184         }
185         transactionListFragment.setArguments(bundle);
186         ft.replace(R.id.root_frame, transactionListFragment);
187         if (account != null)
188             ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
189         ft.commit();
190
191         currentFragment = transactionListFragment;
192     }
193     public void showAccountTransactions(LedgerAccount account) {
194         showTransactionsFragment(account);
195     }
196     @Override
197     public void onBackPressed() {
198         DrawerLayout drawer = findViewById(R.id.drawer_layout);
199         if (drawer.isDrawerOpen(GravityCompat.START)) {
200             drawer.closeDrawer(GravityCompat.START);
201         }
202         else {
203             Log.d("fragments",
204                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
205
206             super.onBackPressed();
207         }
208     }
209
210 }