]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
ad124667f5b788088a7009e8151c7420fadd94ec
[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
80     @Override
81     protected void onStart() {
82         super.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()));
88             }
89             else {
90                 grp.getChildAt(i)
91                         .setBackgroundColor(getResources().getColor(R.color.drawer_background));
92             }
93         }
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()));
97         }
98         else {
99             drawer.findViewById(R.id.nav_account_summary)
100                     .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
101         }
102     }
103
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);
108     }
109
110     public void nav_exit_clicked(View view) {
111         Log.w("app", "exiting");
112         finish();
113     }
114
115     public void nav_settings_clicked(View view) {
116         Intent intent = new Intent(this, SettingsActivity.class);
117         startActivity(intent);
118     }
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()));
123         }
124         else {
125             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
126         }
127
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));
133             }
134         }
135     }
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);
142                 break;
143             case R.id.menu_acc_summary_confirm_selection:
144                 if (accountSummaryFragment != null)
145                     accountSummaryFragment.onConfirmAccSelection(menuItem);
146                 break;
147             case R.id.menu_acc_summary_only_starred:
148                 if (accountSummaryFragment != null)
149                     accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
150                 break;
151             case R.id.menu_transaction_list_filter:
152                 if (transactionListFragment != null)
153                     transactionListFragment.onShowFilterClick(menuItem);
154                 break;
155             default:
156                 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
157         }
158     }
159     public void onViewClicked(View view) {
160         switch (view.getId()) {
161             case R.id.clearAccountNameFilter:
162                 if (transactionListFragment != null)
163                     transactionListFragment.onClearAccountNameClick(view);
164                 break;
165             default:
166                 Log.e("click", String.format("View %d click not handled", view.getId()));
167         }
168     }
169     public void onAccountSummaryClicked(View view) {
170         drawer.closeDrawers();
171
172         resetFragmentBackStack();
173
174         showAccountSummaryFragment();
175     }
176     private void showAccountSummaryFragment() {
177         FragmentTransaction ft = fragmentManager.beginTransaction();
178         accountSummaryFragment = new AccountSummaryFragment();
179         ft.replace(R.id.root_frame, accountSummaryFragment);
180         ft.commit();
181         currentFragment = accountSummaryFragment;
182     }
183     public void onLatestTransactionsClicked(View view) {
184         drawer.closeDrawers();
185
186         resetFragmentBackStack();
187
188         showTransactionsFragment(null);
189     }
190     private void resetFragmentBackStack() {
191 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
192     }
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();
198         }
199         Bundle bundle = new Bundle();
200         if (account != null) {
201             bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
202                     account.getName());
203         }
204         transactionListFragment.setArguments(bundle);
205         ft.replace(R.id.root_frame, transactionListFragment);
206         if (account != null)
207             ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
208         ft.commit();
209
210         currentFragment = transactionListFragment;
211     }
212     public void showAccountTransactions(LedgerAccount account) {
213         showTransactionsFragment(account);
214     }
215     @Override
216     public void onBackPressed() {
217         DrawerLayout drawer = findViewById(R.id.drawer_layout);
218         if (drawer.isDrawerOpen(GravityCompat.START)) {
219             drawer.closeDrawer(GravityCompat.START);
220         }
221         else {
222             Log.d("fragments",
223                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
224
225             super.onBackPressed();
226         }
227     }
228
229 }